From 92d5779157bd595b0852a3f92bcde9ce65aa612d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Fri, 4 Aug 2023 09:08:01 +0200 Subject: [PATCH] [int] --- source/logic/master.ts | 33 ++++-- source/test/test.mocha.ts | 225 +++++++++++++++++++++++++++----------- todo.md | 1 + 3 files changed, 183 insertions(+), 76 deletions(-) diff --git a/source/logic/master.ts b/source/logic/master.ts index 6218928..8c72919 100644 --- a/source/logic/master.ts +++ b/source/logic/master.ts @@ -6,21 +6,34 @@ namespace _heimdall.master */ export function determine_due( check : _heimdall.type_check, - timestamp : int, old_item_state : (null | _heimdall.type_item_state), + options : { + timestamp ?: int; + } = {} ) : boolean { + options = Object.assign( + { + "timestamp": _heimdall.get_current_timestamp(), + }, + options + ); + return ( - (old_item_state === null) - || - (old_item_state.condition !== _heimdall.enum_condition.ok) - || - ((timestamp - old_item_state.timestamp) >= check.schedule.regular_interval) - || + check.active + && ( - (! (old_item_state.count === null)) - && - ((timestamp - old_item_state.timestamp) >= check.schedule.attentive_interval) + (old_item_state === null) + || + (old_item_state.condition !== _heimdall.enum_condition.ok) + || + ((options.timestamp - old_item_state.timestamp) >= check.schedule.regular_interval) + || + ( + (! (old_item_state.count === null)) + && + ((options.timestamp - old_item_state.timestamp) >= check.schedule.attentive_interval) + ) ) ); } diff --git a/source/test/test.mocha.ts b/source/test/test.mocha.ts index 188b261..d75c9b7 100644 --- a/source/test/test.mocha.ts +++ b/source/test/test.mocha.ts @@ -10,83 +10,175 @@ describe( function datetimestring_to_timestamp(datetimestring) { return Math.floor((new Date(datetimestring)).getTime() / 1000); }; + function resolve_condition(condition_raw) { + return { + "unknown": _heimdall.enum_condition.unknown, + "ok": _heimdall.enum_condition.ok, + "concerning": _heimdall.enum_condition.concerning, + "critical": _heimdall.enum_condition.critical, + }[condition_raw]; + }; describe( "master.determine_due", () => { - const cases = [ - { - "name": "regular interval hit", - "input": { - "check": { - "active": true, - "threshold": 1, - "annoy": false, - "schedule": { - "regular_interval": 3600, - "attentive_interval": 120, - "reminding_interval": 86400, - }, - "notifications": [], - "name": "test", - "title": "Test", - "kind": "BOGUS", - "parameters": {}, - "custom": null, - }, - "timestamp": "2023-01-15T12:30:00", - "old_item_state": { - "timestamp": "2023-01-15T11:00:00", - "condition": _heimdall.enum_condition.ok, - "count": 0, - "last_notification_timestamp": "2023-01-15T10:00:00" - } - }, - "output": true, - }, - { - "name": "regular interval not hit", - "input": { - "check": { - "active": true, - "threshold": 1, - "annoy": false, - "schedule": { - "regular_interval": 3600, - "attentive_interval": 120, - "reminding_interval": 86400, - }, - "notifications": [], - "name": "test", - "title": "Test", - "kind": "BOGUS", - "parameters": {}, - "custom": null, - }, - "timestamp": "2023-01-15T11:30:00", - "old_item_state": { - "timestamp": "2023-01-15T11:00:00", - "condition": _heimdall.enum_condition.ok, - "count": 0, - "last_notification_timestamp": "2023-01-15T10:00:00" - } - }, - "output": false, - }, + const dimensions = [ + {"name": "annoy", "elements": [false, true]}, + {"name": "condition", "elements": ["unknown", "ok", "concerning", "critical"]}, + {"name": "treshold_reached", "elements": [false, true]}, + {"name": "passed", "elements": ["none", "attentive", "regular", "reminding"]}, ]; - cases.forEach( + const parameters = { + "check": { + "active": true, + "threshold": 1, + "schedule": { + "attentive_interval": 120, + "regular_interval": 3600, + "reminding_interval": 86400, + }, + "notifications": [], + "name": "test", + "title": "Test", + "kind": "BOGUS", + "parameters": {}, + "custom": null, + }, + "old_item_state": { + "timestamp": "2023-01-15T11:00:00", + "last_notification_timestamp": "2023-01-15T10:00:00", + }, + }; + const cases = ( + dimensions + .reduce( + (product, dimension) => ( + product + .map( + tuple => ( + dimension.elements + .map( + element => Object.fromEntries( + Object.entries(tuple) + .concat([[dimension.name, element]]) + ) + ) + ) + ) + .reduce( + ((x, y) => x.concat(y)), + [] + ) + ), + [{}] + ) + .map( + input => ({ + "name": JSON.stringify(input), + "input": input, + // "output": false, + }) + ) + ); + const data = { + "parameters": parameters, + "cases": ( + cases + .map( + case_ => { + // setup + const check : _heimdall.type_check = Object.assign( + parameters["check"], + { + "annoy": case_.input["annoy"], + } + ); + const old_item_state : _heimdall.type_item_state = { + "timestamp": datetimestring_to_timestamp(parameters["old_item_state"]["timestamp"]), + "condition": resolve_condition(case_.input["condition"]), + "count": (case_.input["treshold_reached"] ? null : 0), + "last_notification_timestamp": datetimestring_to_timestamp(parameters["old_item_state"]["last_notification_timestamp"]), + }; + const timestamp : int = ( + datetimestring_to_timestamp( + parameters["old_item_state"]["timestamp"] + ) + + + ( + { + "none": 0, + "attentive": parameters["check"]["schedule"]["attentive_interval"], + "regular": parameters["check"]["schedule"]["regular_interval"], + "reminding": parameters["check"]["schedule"]["reminding_interval"] + }[case_.input["passed"]] + ) + + + 60 + ); +console.info({check,old_item_state,timestamp}); + + // execution + const result : boolean = _heimdall.master.determine_due( + check, + old_item_state, + { + "timestamp": timestamp, + } + ); + + return Object.assign( + case_, + { + "output": result, + } + ); + } + ) + ) + }; +process.stdout.write(JSON.stringify(data, undefined, "\t") + "\n"); + /* + data.cases.forEach( case_ => { it( case_.name, () => { + // setup + const check : _heimdall.type_check = Object.assign( + data.parameters["check"], + { + "annoy": case_.input["annoy"], + } + ); + const old_item_state : _heimdall.type_item_state = { + "timestamp": datetimestring_to_timestamp(data.parameters["old_item_state"]["timestamp"]), + "condition": resolve_condition(case_.input["condition"]), + "count": (case_.input["treshold_reached"] ? null : 0), + "last_notification_timestamp": datetimestring_to_timestamp(data.parameters["old_item_state"]["last_notification_timestamp"]), + }; + const timestamp : int = ( + datetimestring_to_timestamp( + data.parameters["old_item_state"]["timestamp"] + ) + + + ( + { + "none": 0, + "attentive": data.parameters["check"]["schedule"]["attentive_interval"], + "regular": data.parameters["check"]["schedule"]["regular_interval"], + "reminding": data.parameters["check"]["schedule"]["reminding_interval"] + }[case_.input["passed"]] + ) + + + 60 + ); +console.info({check,old_item_state,timestamp}); + // execution const result : boolean = _heimdall.master.determine_due( - case_.input["check"], - datetimestring_to_timestamp(case_.input["timestamp"]), + check, + old_item_state, { - "timestamp": datetimestring_to_timestamp(case_.input["old_item_state"]["timestamp"]), - "condition": case_.input["old_item_state"]["condition"], - "count": case_.input["old_item_state"]["count"], - "last_notification_timestamp": datetimestring_to_timestamp(case_.input["old_item_state"]["last_notification_timestamp"]), + "timestamp": timestamp, } ); @@ -96,6 +188,7 @@ describe( ); } ); + */ } ); } diff --git a/todo.md b/todo.md index 9641e76..962ffe5 100644 --- a/todo.md +++ b/todo.md @@ -7,3 +7,4 @@ - Versionierung - Test-Routinen - ein paar der Kommandozeilen-Argumente in Konfiguration auslagern +- Zustand 'ok' in 'good' umbenennen