Skip to content

Commit 3316f39

Browse files
committed
Fix maintenance job to take into account shorter intervals (1h) in partitions
1 parent 4f27f18 commit 3316f39

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

dbutils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
db_pool = None
2525
DB_PREFIX = 'netflow_'
2626
S_PER_PARTITION = 3600
27+
LEAVE_N_PAST_PARTITIONS = 24 * 5 # 5 days
2728
register_adapter(dict, Json)
2829

2930

netflowbot.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import requests
1515

1616
from grafoleancollector import Collector, send_results_to_grafolean
17-
from dbutils import get_db_cursor, DB_PREFIX
17+
from dbutils import get_db_cursor, DB_PREFIX, S_PER_PARTITION, LEAVE_N_PAST_PARTITIONS
1818
from lookup import PROTOCOLS, DIRECTION_INGRESS, DIRECTION_EGRESS
1919

2020
logging.basicConfig(format='%(asctime)s.%(msecs)03d | %(levelname)s | %(message)s',
@@ -77,10 +77,9 @@ def _save_current_max_ts(job_id, max_ts):
7777

7878

7979
def job_maint_remove_old_partitions(*args, **kwargs):
80-
LEAVE_N_PAST_DAYS = 5
8180
with get_db_cursor() as c:
8281
log.info("MAINT: Maintenance started - removing old partitions")
83-
today_seq = int(time.time() // (24 * 3600))
82+
today_seq = int(time.time() // S_PER_PARTITION)
8483
c.execute(f"SELECT tablename FROM pg_tables WHERE schemaname = 'public' AND tablename LIKE '{DB_PREFIX}flows_%';")
8584
for tablename, in c.fetchall():
8685
m = re.match(f'^{DB_PREFIX}flows_([0-9]+)$', tablename)
@@ -91,7 +90,7 @@ def job_maint_remove_old_partitions(*args, **kwargs):
9190
if day_seq > today_seq:
9291
log.warning(f"MAINT: CAREFUL! Table {tablename} marks a future day (today is {today_seq}); this should never happen! Skipping.")
9392
continue
94-
if day_seq < today_seq - LEAVE_N_PAST_DAYS:
93+
if day_seq < today_seq - LEAVE_N_PAST_PARTITIONS:
9594
log.info(f"MAINT: Removing old data: {tablename} (today is {today_seq})")
9695
c.execute(f"DROP TABLE {tablename};")
9796
else:

0 commit comments

Comments
 (0)