core/source/logic/repositories/result.py

72 lines
1.9 KiB
Python
Raw Normal View History

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