Skip to content

Commit

Permalink
undo storing timestamps as strings in the db
Browse files Browse the repository at this point in the history
  • Loading branch information
AlyaGomaa committed May 9, 2024
1 parent bc06b48 commit 25b6c0f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
21 changes: 10 additions & 11 deletions database/sqlite_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def init_tables(self):
# decimals. check here
# https://www.b4x.com/android/forum/threads/prevent-automatic-rounding-in-sqlite.129246/
self.tables.TIMEWINDOW_DETAILS: "timewindow INTEGER PRIMARY KEY, "
"start_time TEXT, "
"end_time TEXT ",
"start_time REAL, "
"end_time REAL ",

# this table will be used to store all the tools' labels per IP
# per timewindow, not flow by flow
Expand Down Expand Up @@ -409,7 +409,6 @@ def register_tw(self, tw: int, tw_start_ts: float, tw_end_ts: float) -> \
if self.is_registered_timewindow(tw):
return False

tw_start_ts, tw_end_ts = str(tw_start_ts), str(tw_end_ts)
query = f'INSERT INTO {self.tables.TIMEWINDOW_DETAILS} ' \
f'(timewindow, start_time, end_time) VALUES (?, ?, ?);'
params = (tw, tw_start_ts, tw_end_ts)
Expand All @@ -427,15 +426,17 @@ def get_last_row(self, table: str):
self.execute(query)
return self.fetchone()

def get_timewindows_limit(self) -> Tuple[str, str]:
def get_timewindows_limit(self) -> Tuple[float, float]:
"""
returns the period of time that the ground truth knows about and has
flows and labels for
flow and tws for
:return: (the start timestamp of the first timewindow,
the end timestamp of the last timewindow)
"""
start_time: dict = self.get_first_row(self.tables.TIMEWINDOW_DETAILS)[1]
end_time: str = self.get_last_row(self.tables.TIMEWINDOW_DETAILS)[2]
start_time: float = self.get_first_row(
self.tables.TIMEWINDOW_DETAILS)[1]
end_time: float = self.get_last_row(
self.tables.TIMEWINDOW_DETAILS)[2]

return start_time, end_time

Expand All @@ -462,14 +463,12 @@ def get_timewindow_of_ts(self, ts: float) -> int:

# timewindow was not seen by the gt
# calc it manually
starttime_of_first_timewindow: str = self.select(
starttime_of_first_timewindow: float = self.select(
self.tables.TIMEWINDOW_DETAILS,
'start_time',
condition=f"timewindow = 1",
fetch='one')[0]
starttime_of_first_timewindow: float = float(
starttime_of_first_timewindow)


if ts == starttime_of_first_timewindow:
tw = 1
else:
Expand Down
2 changes: 1 addition & 1 deletion parsers/slips.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def parse_alerts_table(self):
# │ tw 1 tw 2 │

for ts in (alert['tw_start'], alert['tw_end']):
self.mark_tw_as_malicious(ts , alert['ip_alerted'])
self.mark_tw_as_malicious(ts, alert['ip_alerted'])


def parse_flow_by_flow_labels(self):
Expand Down
4 changes: 3 additions & 1 deletion utils/timewindow_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from math import ceil
from typing import Tuple


class TimewindowHandler:
"""
Expand Down Expand Up @@ -28,7 +30,7 @@ class TimewindowHandler:
def __init__(self, ts_of_first_flow):
self.ts_of_first_flow = float(ts_of_first_flow)

def get_start_and_end_ts(self, tw: int):
def get_start_and_end_ts(self, tw: int) -> Tuple[float, float]:
"""
this function is responsible for getting the tw limits ,
later we'll store em in the db
Expand Down

0 comments on commit 25b6c0f

Please sign in to comment.