From 0f6116ddcde1657a0f437622496f9f0be40935cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Mon, 19 Jun 2023 18:40:13 +0200 Subject: [PATCH] [int] --- source/logic/base.ts | 143 +++++++++++++++++++ source/logic/condition.ts | 21 --- source/logic/main.ts | 13 +- source/logic/notification_kinds/_abstract.ts | 8 +- source/logic/notification_kinds/console.ts | 70 +++++++++ source/logic/order.ts | 75 +--------- tools/heimdall.prj.json | 3 +- 7 files changed, 227 insertions(+), 106 deletions(-) create mode 100644 source/logic/base.ts delete mode 100644 source/logic/condition.ts create mode 100644 source/logic/notification_kinds/console.ts diff --git a/source/logic/base.ts b/source/logic/base.ts new file mode 100644 index 0000000..d3d2573 --- /dev/null +++ b/source/logic/base.ts @@ -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; + }; + + + /** + */ + 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; + checks : Array; + }; + + + /** + */ + 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] + */ +} diff --git a/source/logic/condition.ts b/source/logic/condition.ts deleted file mode 100644 index 0c5f087..0000000 --- a/source/logic/condition.ts +++ /dev/null @@ -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; - }; - -} diff --git a/source/logic/main.ts b/source/logic/main.ts index b191d0b..ed8ca71 100644 --- a/source/logic/main.ts +++ b/source/logic/main.ts @@ -1,14 +1,6 @@ async function main( ) : Promise { - // 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 = { + "console": _heimdall.notification_kinds.console.notification_kind_implementation(), }; const check_kind_implementations : Record = { "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, diff --git a/source/logic/notification_kinds/_abstract.ts b/source/logic/notification_kinds/_abstract.ts index 03859b3..3c4fae8 100644 --- a/source/logic/notification_kinds/_abstract.ts +++ b/source/logic/notification_kinds/_abstract.ts @@ -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; + notify : ( + parameters : any, + name : string, + data : type_check, + state : type_item_state, + info : any + ) => Promise; }; } diff --git a/source/logic/notification_kinds/console.ts b/source/logic/notification_kinds/console.ts new file mode 100644 index 0000000..adad472 --- /dev/null +++ b/source/logic/notification_kinds/console.ts @@ -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 + { + 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(undefined); + } + + + /** + */ + export function notification_kind_implementation( + ) : type_notification_kind + { + return { + "parameters_schema": parameters_schema, + "normalize_order_node": normalize_order_node, + "notify": notify, + }; + } + +} diff --git a/source/logic/order.ts b/source/logic/order.ts index 51938fa..302a8eb 100644 --- a/source/logic/order.ts +++ b/source/logic/order.ts @@ -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; - }; - - - /** - */ - 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; - checks : Array; - }; - - /** */ 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( { diff --git a/tools/heimdall.prj.json b/tools/heimdall.prj.json index 7db5443..60e4eb3 100644 --- a/tools/heimdall.prj.json +++ b/tools/heimdall.prj.json @@ -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",