[int]
This commit is contained in:
parent
2606aaab53
commit
92d5779157
|
|
@ -6,21 +6,34 @@ namespace _heimdall.master
|
||||||
*/
|
*/
|
||||||
export function determine_due(
|
export function determine_due(
|
||||||
check : _heimdall.type_check,
|
check : _heimdall.type_check,
|
||||||
timestamp : int,
|
|
||||||
old_item_state : (null | _heimdall.type_item_state),
|
old_item_state : (null | _heimdall.type_item_state),
|
||||||
|
options : {
|
||||||
|
timestamp ?: int;
|
||||||
|
} = {}
|
||||||
) : boolean
|
) : boolean
|
||||||
{
|
{
|
||||||
|
options = Object.assign(
|
||||||
|
{
|
||||||
|
"timestamp": _heimdall.get_current_timestamp(),
|
||||||
|
},
|
||||||
|
options
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
check.active
|
||||||
|
&&
|
||||||
|
(
|
||||||
(old_item_state === null)
|
(old_item_state === null)
|
||||||
||
|
||
|
||||||
(old_item_state.condition !== _heimdall.enum_condition.ok)
|
(old_item_state.condition !== _heimdall.enum_condition.ok)
|
||||||
||
|
||
|
||||||
((timestamp - old_item_state.timestamp) >= check.schedule.regular_interval)
|
((options.timestamp - old_item_state.timestamp) >= check.schedule.regular_interval)
|
||||||
||
|
||
|
||||||
(
|
(
|
||||||
(! (old_item_state.count === null))
|
(! (old_item_state.count === null))
|
||||||
&&
|
&&
|
||||||
((timestamp - old_item_state.timestamp) >= check.schedule.attentive_interval)
|
((options.timestamp - old_item_state.timestamp) >= check.schedule.attentive_interval)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,83 +10,175 @@ describe(
|
||||||
function datetimestring_to_timestamp(datetimestring) {
|
function datetimestring_to_timestamp(datetimestring) {
|
||||||
return Math.floor((new Date(datetimestring)).getTime() / 1000);
|
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(
|
describe(
|
||||||
"master.determine_due",
|
"master.determine_due",
|
||||||
() => {
|
() => {
|
||||||
const cases = [
|
const dimensions = [
|
||||||
{
|
{"name": "annoy", "elements": [false, true]},
|
||||||
"name": "regular interval hit",
|
{"name": "condition", "elements": ["unknown", "ok", "concerning", "critical"]},
|
||||||
"input": {
|
{"name": "treshold_reached", "elements": [false, true]},
|
||||||
"check": {
|
{"name": "passed", "elements": ["none", "attentive", "regular", "reminding"]},
|
||||||
"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,
|
|
||||||
},
|
|
||||||
];
|
];
|
||||||
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<any>(
|
||||||
|
(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_ => {
|
case_ => {
|
||||||
it(
|
it(
|
||||||
case_.name,
|
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
|
// execution
|
||||||
const result : boolean = _heimdall.master.determine_due(
|
const result : boolean = _heimdall.master.determine_due(
|
||||||
case_.input["check"],
|
check,
|
||||||
datetimestring_to_timestamp(case_.input["timestamp"]),
|
old_item_state,
|
||||||
{
|
{
|
||||||
"timestamp": datetimestring_to_timestamp(case_.input["old_item_state"]["timestamp"]),
|
"timestamp": 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"]),
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -96,6 +188,7 @@ describe(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue