This commit is contained in:
Christian Fraß 2023-06-19 18:40:13 +02:00
parent 2b6e350e3b
commit 0f6116ddcd
7 changed files with 227 additions and 106 deletions

143
source/logic/base.ts Normal file
View file

@ -0,0 +1,143 @@
namespace _heimdall
{
/**
*/
export enum enum_condition {
unknown = 0,
ok = 1,
concerning = 2,
critical = 3,
};
/**
*/
export type type_result = {
condition : enum_condition;
info : any;
};
/**
*/
export type type_interval = (
null
|
int
|
(
"minute"
|
"hour"
|
"day"
|
"week"
)
);
/**
*/
export type type_schedule = {
regular_interval : type_interval;
attentive_interval : type_interval;
reminding_interval : type_interval;
};
/**
*/
export type type_notification = {
kind : string;
parameters : any;
};
/**
*/
export type type_check_common = {
active ?: boolean;
threshold ?: int;
annoy ?: boolean;
schedule ?: type_schedule;
notifications ?: Array<type_notification>;
};
/**
*/
export type type_check = (
type_check_common
&
{
name : string;
title : string;
kind : string;
parameters : any;
custom : any;
}
);
/**
*/
export type type_order = {
defaults : type_check_common;
includes : Array<string>;
checks : Array<type_check>;
};
/**
*/
export type type_item_state = {
timestamp : int;
condition : _heimdall.enum_condition,
count : (null | int);
last_notification_timestamp : (null | int);
};
/**
* converts a condition to a human readable string
*/
export function condition_show(
condition : enum_condition
) : string
{
return lib_plankton.translate.get(
(
(condition) => {
switch (condition) {
case enum_condition.unknown: return "conditions.unknown";
case enum_condition.ok: return "conditions.ok";
case enum_condition.concerning: return "conditions.concerning";
case enum_condition.critical: return "conditions.critical";
}
}
) (condition)
)
}
/*
def condition_encode(condition):
return {
enum_condition.unknown: "unknown",
enum_condition.ok: "ok",
enum_condition.concerning: "concerning",
enum_condition.critical: "critical",
}[condition]
def condition_decode(condition_encoded):
return {
"unknown": enum_condition.unknown,
"ok": enum_condition.ok,
"warning": enum_condition.concerning, # deprecated
"concerning": enum_condition.concerning,
"critical": enum_condition.critical,
}[condition_encoded]
*/
}

View file

@ -1,21 +0,0 @@
namespace _heimdall
{
/**
*/
export enum enum_condition {
unknown = 0,
ok = 1,
concerning = 2,
critical = 3,
};
/**
*/
export type type_result = {
condition : enum_condition;
info : any;
};
}

View file

@ -1,14 +1,6 @@
async function main(
) : Promise<void>
{
// types
type type_item_state = {
timestamp : int;
condition : _heimdall.enum_condition,
count : (null | int);
last_notification_timestamp : (null | int);
};
// consts
const version : string = "0.8";
@ -214,6 +206,7 @@ async function main(
}
else {
const notification_kind_implementations : Record<string, _heimdall.notification_kinds.type_notification_kind> = {
"console": _heimdall.notification_kinds.console.notification_kind_implementation(),
};
const check_kind_implementations : Record<string, _heimdall.check_kinds.type_check_kind> = {
"http_request": _heimdall.check_kinds.http_request.check_kind_implementation(),
@ -327,7 +320,7 @@ async function main(
);
// TODO: type
let old_item_state : (null | type_item_state);
let old_item_state : (null | _heimdall.type_item_state);
let last_notification_timestamp : (null | int);
@ -469,7 +462,7 @@ async function main(
args["send_ok_notifications"]
)
)
const new_item_state : type_item_state = {
const new_item_state : _heimdall.type_item_state = {
"timestamp": timestamp,
"condition": result.condition,
"count": count,

View file

@ -6,7 +6,13 @@ namespace _heimdall.notification_kinds
export type type_notification_kind = {
parameters_schema : (() => _heimdall.helpers.json_schema.type_schema);
normalize_order_node : ((node : any) => any);
notify : (parameters, name, data, state, info) => Promise<void>;
notify : (
parameters : any,
name : string,
data : type_check,
state : type_item_state,
info : any
) => Promise<void>;
};
}

View file

@ -0,0 +1,70 @@
namespace _heimdall.notification_kinds.console
{
/**
*/
function parameters_schema(
) : _heimdall.helpers.json_schema.type_schema
{
return {
"type": "object",
"additionalProperties": false,
"properties": {
},
"required": [
]
};
}
/**
*/
function normalize_order_node(
node : any
) : any
{
return Object.assign(
{
},
node
);
}
/**
*/
function notify(
parameters : any,
name : string,
check : type_check,
state : type_item_state,
info : any
) : Promise<void>
{
process.stdout.write(
lib_plankton.string.coin(
"[{{title}}] <{{condition}}> {{info}}\n",
{
"title": check.title,
"condition": _heimdall.condition_show(state.condition),
"info": lib_plankton.json.encode(info, true),
}
)
);
return Promise.resolve<void>(undefined);
}
/**
*/
export function notification_kind_implementation(
) : type_notification_kind
{
return {
"parameters_schema": parameters_schema,
"normalize_order_node": normalize_order_node,
"notify": notify,
};
}
}

View file

@ -1,77 +1,6 @@
namespace _heimdall.order
{
/**
*/
type type_interval = (
null
|
int
|
(
"minute"
|
"hour"
|
"day"
|
"week"
)
);
/**
*/
type type_schedule = {
regular_interval : type_interval;
attentive_interval : type_interval;
reminding_interval : type_interval;
};
/**
*/
export type type_notification = {
kind : string;
parameters : any;
};
/**
*/
export type type_check_common = {
active ?: boolean;
threshold ?: int;
annoy ?: boolean;
schedule ?: type_schedule;
notifications ?: Array<type_notification>;
};
/**
*/
export type type_check = (
type_check_common
&
{
name : string;
title : string;
kind : string;
parameters : any;
custom : any;
}
);
/**
*/
export type type_order = {
defaults : type_check_common;
includes : Array<string>;
checks : Array<type_check>;
};
/**
*/
function schema_active(
@ -302,7 +231,7 @@ namespace _heimdall.order
*/
function normalize_interval(
interval_raw
) : (null | type_interval)
) : (null | _heimdall.type_interval)
{
if (interval_raw === null) {
return null;
@ -336,7 +265,7 @@ namespace _heimdall.order
*/
function normalize_schedule(
node
) : type_schedule
) : _heimdall.type_schedule
{
const node_ = Object.assign(
{

View file

@ -19,10 +19,11 @@
"parameters": {
"inputs": [
"lib/plankton/plankton.d.ts",
"source/logic/condition.ts",
"source/logic/base.ts",
"source/logic/helpers/json_schema.ts",
"source/logic/helpers/sqlite.ts",
"source/logic/notification_kinds/_abstract.ts",
"source/logic/notification_kinds/console.ts",
"source/logic/check_kinds/_abstract.ts",
"source/logic/check_kinds/http_request.ts",
"source/logic/state_repository.ts",