Skip to content

Commit 5f0797c

Browse files
committed
Single table for flows
1 parent f9cb8ec commit 5f0797c

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

dbutils.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,23 @@ def migration_step_1():
130130

131131
def migration_step_2():
132132
with get_db_cursor() as c:
133-
# UNLOGGED: Disabling WAL avoids high I/O load. Since NetFlow data is of temporary nature, this still
134-
# allows us to perform queries, but if the database crashes it is acceptable to lose all of the records.
135-
c.execute(f'CREATE UNLOGGED TABLE {DB_PREFIX}records (seq BIGSERIAL NOT NULL PRIMARY KEY, ts NUMERIC(16,6) NOT NULL, client_ip TEXT);')
136-
133+
# UNLOGGED: Disabling WAL avoids high I/O load. Since NetFlow data is of temporary nature, this is acceptable.
134+
# It still allows us to perform queries, but if the database crashes we lose the raw records.
137135
c.execute(f"""
138136
CREATE UNLOGGED TABLE {DB_PREFIX}flows (
139-
record INTEGER NOT NULL REFERENCES {DB_PREFIX}records(seq) ON DELETE CASCADE,
140-
IN_BYTES INTEGER,
141-
PROTOCOL SMALLINT,
142-
DIRECTION SMALLINT,
143-
L4_DST_PORT INTEGER,
144-
L4_SRC_PORT INTEGER,
145-
INPUT_SNMP SMALLINT,
146-
OUTPUT_SNMP SMALLINT,
147-
IPV4_DST_ADDR TEXT,
148-
IPV4_SRC_ADDR TEXT
137+
ts NUMERIC(16,6) NOT NULL,
138+
entity_ip INET NOT NULL,
139+
in_bytes INTEGER NOT NULL,
140+
protocol SMALLINT NOT NULL,
141+
direction SMALLINT NOT NULL,
142+
l4_dst_port INTEGER NOT NULL,
143+
l4_src_port INTEGER NOT NULL,
144+
input_snmp SMALLINT NOT NULL,
145+
output_snmp SMALLINT NOT NULL,
146+
ipv4_dst_addr INET NOT NULL,
147+
ipv4_src_addr INET NOT NULL
149148
);
150149
""")
151-
c.execute(f'CREATE INDEX netflow_flows_record on netflow_flows (record);')
150+
c.execute(f'CREATE INDEX {DB_PREFIX}flows_ts on {DB_PREFIX}flows (ts);')
152151

153-
c.execute(f'CREATE TABLE {DB_PREFIX}bot_jobs (job_id TEXT NOT NULL PRIMARY KEY, last_used_seq BIGSERIAL);')
152+
c.execute(f'CREATE TABLE {DB_PREFIX}bot_jobs (job_id TEXT NOT NULL PRIMARY KEY, last_used_ts NUMERIC(16,6) NOT NULL);')

0 commit comments

Comments
 (0)