core/source/logic/state_repository.ts

199 lines
4.3 KiB
TypeScript
Raw Permalink Normal View History

2024-07-02 15:02:35 +02:00
/*
Copyright 2016-2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
<info@greenscale.de>
»heimdall« is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»heimdall« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with »heimdall«. If not, see <http://www.gnu.org/licenses/>.
*/
2023-06-19 13:05:17 +02:00
namespace _heimdall.state_repository
{
2023-08-03 08:34:33 +02:00
/**
*/
var _connection : (null | lib_plankton.sqlite.type_connection) = null;
2023-06-19 15:04:15 +02:00
/**
*/
function condition_decode(
condition_encoded : int
) : _heimdall.enum_condition
{
return {
0: enum_condition.unknown,
1: enum_condition.ok,
2: enum_condition.concerning,
3: enum_condition.critical,
}[condition_encoded];
}
/**
*/
function condition_encode(
condition : _heimdall.enum_condition
) : int
{
switch (condition) {
case enum_condition.unknown: return 0;
case enum_condition.ok: return 1;
case enum_condition.concerning: return 2;
case enum_condition.critical: return 3;
}
}
2023-06-19 13:05:17 +02:00
/**
*/
2023-08-03 08:34:33 +02:00
export async function init(
2023-06-19 15:04:15 +02:00
database_path : string
2023-06-19 13:05:17 +02:00
) : Promise<void>
{
2023-08-03 08:34:33 +02:00
_connection = await lib_plankton.sqlite.get_connection(database_path);
await setup();
}
/**
*/
export async function kill(
) : Promise<void>
{
await lib_plankton.sqlite.drop_connection(_connection);
_connection = null;
}
/**
*/
export async function setup(
) : Promise<void>
{
const result = await lib_plankton.sqlite.query_set(
_connection,
2023-06-19 15:04:15 +02:00
{
"template": "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);",
"arguments": {
},
},
2023-06-19 13:05:17 +02:00
);
}
/**
*/
export async function clean(
time_to_live : int,
2023-06-19 15:04:15 +02:00
erase_state : boolean
2023-06-19 13:05:17 +02:00
) : Promise<int>
{
2023-08-03 08:34:33 +02:00
const result = await lib_plankton.sqlite.query_put(
_connection,
2023-06-19 13:05:17 +02:00
{
2023-06-19 15:04:15 +02:00
"template": "DELETE FROM results WHERE ((timestamp < :timestamp_min) OR :erase_state);",
"arguments": {
2023-06-19 18:05:19 +02:00
"timestamp_min": Math.floor((Date.now() / 1000) - time_to_live),
2023-06-19 15:04:15 +02:00
"erase_state": erase_state,
},
}
);
return result.affected;
}
/**
*/
export async function probe(
check_name : string,
threshold : int
) : Promise<
Array<
{
timestamp : int;
condition : _heimdall.enum_condition;
notification_sent : boolean;
}
>
>
{
2023-08-03 08:34:33 +02:00
const rows : Array<Record<string, any>> = await lib_plankton.sqlite.query_get(
_connection,
2023-06-19 15:04:15 +02:00
{
"template": "SELECT timestamp, condition, notification_sent FROM results WHERE (check_name = :check_name) ORDER BY timestamp DESC LIMIT :limit;",
"arguments": {
"check_name": check_name,
"limit": threshold,
},
}
);
return (
rows
.map(
row => ({
2023-08-03 08:34:33 +02:00
"timestamp": row["timestamp"],
"condition": condition_decode(row["condition"]),
"notification_sent": row["notification_sent"],
2023-06-19 15:04:15 +02:00
})
)
);
}
/**
*/
export async function get_last_notification_timestamp(
check_name : string
) : Promise<(null | int)>
{
2023-08-03 08:34:33 +02:00
const rows : Array<Record<string, any>> = await lib_plankton.sqlite.query_get(
_connection,
{
2023-08-03 08:34:33 +02:00
"template": "SELECT MAX(timestamp) AS max_timestamp FROM results WHERE (check_name = :check_name) AND (notification_sent = TRUE);",
"arguments": {
"check_name": check_name,
},
}
);
2023-08-03 08:34:33 +02:00
return rows[0]["max_timestamp"];
}
2023-06-19 15:04:15 +02:00
/**
*/
export async function feed(
check_name : string,
timestamp : int,
condition : _heimdall.enum_condition,
shall_send_notification : boolean,
info : any
) : Promise<void>
{
2023-08-03 08:34:33 +02:00
await lib_plankton.sqlite.query_put(
_connection,
2023-06-19 15:04:15 +02:00
{
"template": "INSERT INTO results(check_name, timestamp, condition, notification_sent, info) VALUES (:check_name, :timestamp, :condition, :notification_sent, :info);",
"arguments": {
"check_name": check_name,
"timestamp": timestamp,
"condition": condition_encode(condition),
"notification_sent": shall_send_notification,
"info": lib_plankton.json.encode(info),
}
2023-06-19 13:05:17 +02:00
}
);
}
}