From 69afdcf0bc2c6c10c0702c0843a6b1f6236aebb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Sat, 20 May 2023 10:13:41 +0200 Subject: [PATCH] [mod] outsourced database operations to dedicated repository modules --- source/logic/main.py | 59 ++++-------------------- source/logic/repositories/result.py | 71 +++++++++++++++++++++++++++++ tools/build | 1 + 3 files changed, 81 insertions(+), 50 deletions(-) create mode 100644 source/logic/repositories/result.py diff --git a/source/logic/main.py b/source/logic/main.py index c024e25..c82aa93 100644 --- a/source/logic/main.py +++ b/source/logic/main.py @@ -167,21 +167,10 @@ def main(): _sys.exit(2) else: ### setup database - sqlite_query_set( - database_path, - "CREATE TABLE IF NOT EXISTS results(check_name TEXT NOT NULL, timestamp INTEGER NOT NULL, condition TEXT NOT NULL, notification_sent BOOLEAN NOT NULL, info TEXT NOT NULL);", - {} - ) + repository_result_setup(database_path) ### clean database - result = sqlite_query_put( - database_path, - "DELETE FROM results WHERE ((timestamp < :timestamp_min) OR :erase_state);", - { - "timestamp_min": (get_current_timestamp() - args.time_to_live), - "erase_state": args.erase_state, - } - ) + count = repository_result_clean(database_path, args.time_to_live, args.erase_state) _sys.stderr.write( string_coin( "[info] {{text}}\n", @@ -189,7 +178,7 @@ def main(): "text": translation_get( "misc.cleanup_info", { - "count": ("%u" % result.rowcount), + "count": ("%u" % count), } ), } @@ -204,40 +193,11 @@ def main(): pass else: ### get old state and examine whether the check shall be executed - rows = sqlite_query_get( + old_item_state = repository_result_scan( database_path, - "SELECT timestamp, condition, notification_sent FROM results WHERE (check_name = :check_name) ORDER BY timestamp DESC LIMIT :limit;", - { - "check_name": check_data["name"], - "limit": (check_data["threshold"] + 1), - } + check_data["name"], + check_data["threshold"] ) - if (len(rows) <= 0): - old_item_state = None - else: - last_notification_timestamp = None - count = 1 - for row in rows[1:]: - if (row[1] == rows[0][1]): - count += 1 - else: - break - if (count > check_data["threshold"]): - count = None - else: - pass - for row in rows: - if (row[2]): - last_notification_timestamp = row[0] - break - else: - pass - old_item_state = { - "timestamp": rows[0][0], - "condition": condition_decode(rows[0][1]), - "count": count, - "last_notification_timestamp": last_notification_timestamp, - } timestamp = get_current_timestamp() due = ( @@ -346,15 +306,14 @@ def main(): ) ), } - sqlite_query_put( + repository_result_add( database_path, - "INSERT INTO results(check_name, timestamp, condition, notification_sent, info) VALUES (:check_name, :timestamp, :condition, :notification_sent, :info);", { "check_name": check_data["name"], "timestamp": timestamp, - "condition": condition_encode(result["condition"]), + "condition": result["condition"], "notification_sent": shall_send_notification, - "info": _json.dumps(result["info"]), + "info": result["info"], } ) diff --git a/source/logic/repositories/result.py b/source/logic/repositories/result.py new file mode 100644 index 0000000..7644fcf --- /dev/null +++ b/source/logic/repositories/result.py @@ -0,0 +1,71 @@ +def repository_result_setup(database_path): + sqlite_query_set( + database_path, + "CREATE TABLE IF NOT EXISTS results(check_name TEXT NOT NULL, timestamp INTEGER NOT NULL, condition TEXT NOT NULL, notification_sent BOOLEAN NOT NULL, info TEXT NOT NULL);", + {} + ) + + +def repository_result_clean(database_path, time_to_live, erase_state): + result = sqlite_query_put( + database_path, + "DELETE FROM results WHERE ((timestamp < :timestamp_min) OR :erase_state);", + { + "timestamp_min": (get_current_timestamp() - time_to_live), + "erase_state": erase_state, + } + ) + return result.rowcount + + +def repository_result_scan(database_path, check_name, threshold): + rows = sqlite_query_get( + database_path, + "SELECT timestamp, condition, notification_sent FROM results WHERE (check_name = :check_name) ORDER BY timestamp DESC LIMIT :limit;", + { + "check_name": check_name, + "limit": (threshold + 1), + } + ) + # return rows + if (len(rows) <= 0): + return None + else: + last_notification_timestamp = None + count = 1 + for row in rows[1:]: + if (row[1] == rows[0][1]): + count += 1 + else: + break + if (count > threshold): + count = None + else: + pass + for row in rows: + if (row[2]): + last_notification_timestamp = row[0] + break + else: + pass + return { + "timestamp": rows[0][0], + "condition": condition_decode(rows[0][1]), + "count": count, + "last_notification_timestamp": last_notification_timestamp, + } + + +def repository_result_add(database_path, data): + sqlite_query_put( + database_path, + "INSERT INTO results(check_name, timestamp, condition, notification_sent, info) VALUES (:check_name, :timestamp, :condition, :notification_sent, :info);", + { + "check_name": data["check_name"], + "timestamp": data["timestamp"], + "condition": condition_encode(data["condition"]), + "notification_sent": data["notification_sent"], + "info": _json.dumps(data["info"]), + } + ) + diff --git a/tools/build b/tools/build index 6ac894d..0625510 100755 --- a/tools/build +++ b/tools/build @@ -51,6 +51,7 @@ def main(): _os.path.join(dir_source, "logic", "channels", "console.py"), _os.path.join(dir_source, "logic", "channels", "email.py"), _os.path.join(dir_source, "logic", "channels", "libnotify.py"), + _os.path.join(dir_source, "logic", "repositories", "result.py"), ] targets = { "app": {