[mod] outsourced database operations to dedicated repository modules

This commit is contained in:
Christian Fraß 2023-05-20 10:13:41 +02:00
parent 415fe597b2
commit 69afdcf0bc
3 changed files with 81 additions and 50 deletions

View file

@ -167,21 +167,10 @@ def main():
_sys.exit(2) _sys.exit(2)
else: else:
### setup database ### setup database
sqlite_query_set( repository_result_setup(database_path)
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);",
{}
)
### clean database ### clean database
result = sqlite_query_put( count = repository_result_clean(database_path, args.time_to_live, args.erase_state)
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,
}
)
_sys.stderr.write( _sys.stderr.write(
string_coin( string_coin(
"[info] {{text}}\n", "[info] {{text}}\n",
@ -189,7 +178,7 @@ def main():
"text": translation_get( "text": translation_get(
"misc.cleanup_info", "misc.cleanup_info",
{ {
"count": ("%u" % result.rowcount), "count": ("%u" % count),
} }
), ),
} }
@ -204,40 +193,11 @@ def main():
pass pass
else: else:
### get old state and examine whether the check shall be executed ### get old state and examine whether the check shall be executed
rows = sqlite_query_get( old_item_state = repository_result_scan(
database_path, database_path,
"SELECT timestamp, condition, notification_sent FROM results WHERE (check_name = :check_name) ORDER BY timestamp DESC LIMIT :limit;", check_data["name"],
{ check_data["threshold"]
"check_name": check_data["name"],
"limit": (check_data["threshold"] + 1),
}
) )
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() timestamp = get_current_timestamp()
due = ( due = (
@ -346,15 +306,14 @@ def main():
) )
), ),
} }
sqlite_query_put( repository_result_add(
database_path, 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"], "check_name": check_data["name"],
"timestamp": timestamp, "timestamp": timestamp,
"condition": condition_encode(result["condition"]), "condition": result["condition"],
"notification_sent": shall_send_notification, "notification_sent": shall_send_notification,
"info": _json.dumps(result["info"]), "info": result["info"],
} }
) )

View file

@ -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"]),
}
)

View file

@ -51,6 +51,7 @@ def main():
_os.path.join(dir_source, "logic", "channels", "console.py"), _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", "email.py"),
_os.path.join(dir_source, "logic", "channels", "libnotify.py"), _os.path.join(dir_source, "logic", "channels", "libnotify.py"),
_os.path.join(dir_source, "logic", "repositories", "result.py"),
] ]
targets = { targets = {
"app": { "app": {