2023-06-19 13:05:17 +02:00
namespace _heimdall . state_repository
{
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
/ * *
* /
export async function setup (
2023-06-19 15:04:15 +02:00
database_path : string
2023-06-19 13:05:17 +02:00
) : Promise < void >
{
const result = await _heimdall . helpers . sqlite . query_set (
database_path ,
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 (
database_path : string ,
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 >
{
const result = await _heimdall . helpers . sqlite . query_put (
database_path ,
{
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 (
database_path : string ,
check_name : string ,
threshold : int
) : Promise <
Array <
{
timestamp : int ;
condition : _heimdall.enum_condition ;
notification_sent : boolean ;
}
>
>
{
const rows : Array < Array < any > > = await _heimdall . helpers . sqlite . query_get (
database_path ,
{
"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 = > ( {
"timestamp" : row [ 0 ] ,
"condition" : condition_decode ( row [ 1 ] ) ,
"notification_sent" : row [ 2 ] ,
} )
)
) ;
}
2023-06-23 13:00:39 +02:00
/ * *
* /
2023-06-23 13:01:24 +02:00
export async function get_last_notification_timestamp (
database_path : string ,
2023-06-23 13:00:39 +02:00
check_name : string
) : Promise < ( null | int ) >
{
const rows : Array < Array < any > > = await _heimdall . helpers . sqlite . query_get (
database_path ,
{
2023-06-23 13:52:55 +02:00
"template" : "SELECT MAX(timestamp) FROM results WHERE (check_name = :check_name) AND (notification_sent = TRUE);" ,
2023-06-23 13:00:39 +02:00
"arguments" : {
"check_name" : check_name ,
} ,
}
) ;
return rows [ 0 ] [ 0 ] ;
}
2023-06-19 15:04:15 +02:00
/ * *
* /
export async function feed (
database_path : string ,
check_name : string ,
timestamp : int ,
condition : _heimdall.enum_condition ,
shall_send_notification : boolean ,
info : any
) : Promise < void >
{
await _heimdall . helpers . sqlite . query_put (
database_path ,
{
"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
}
) ;
}
}