-
Notifications
You must be signed in to change notification settings - Fork 0
/
rulesBuilder.js
343 lines (328 loc) · 11.2 KB
/
rulesBuilder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
class RuleBuilder {
constructor(client, plugins) {
this.client = client
this.plugins = plugins
}
static async initialize(client) {
let plugins = await client.sensors.list()
return new RuleBuilder(client, plugins)
}
getPlugin(name) {
return this.plugins.find(x=> x.name === name)
}
guid(name) {
function _p8(s) {
var p = (Math.random().toString(16)+"000000000").substr(2,8);
return s ? "-" + p.substr(0,4) + "-" + p.substr(4,4) : p ;
}
return name + _p8(true) + _p8(true) + _p8();
}
/*
stream template that has a threshold crossing lower and upper boundaries
if you want to make it only the higher or lower threshold rule, you make one of these
two values very high. E.g. if the lowerLimit is negative and INFINIT BIG value, and upperLimit 20,
then it is safe to assumet that the rule will trigger only when incoming value is above 20.
TRUE state means that the condition is out of boundaries
*/
prepareStream(trigger, iter = 0) {
const { resource, metric = 'temperature', lowerLimit=0, upperLimit=10, targetNode = 'problem' } = trigger
const suffix = iter === 0 ? '' : '' + iter
const value = '${streamdata.' + metric + '}'
const inRangePlug = {...this.getPlugin('inRange'), label: 'inRange' + suffix}
//this node will hold a result, and it will be TRUE id the issue is found
const conditionPlug = {...this.getPlugin('condition'), label: targetNode}
const x_offset = 0//iter * 100
const y_offset = iter * 400
const network = {
sensors: [
{
label: inRangePlug.label,
name: inRangePlug.name,
version: inRangePlug.version,
dataTrigger: true,
tickTrigger: false,
resource,
evictionTime: 1000,
properties: {
value: value,
lowerLimit: lowerLimit,
upperLimit: upperLimit
},
position: [ 150 + x_offset, 150 + y_offset]
},
{
label: conditionPlug.label,
name: conditionPlug.name,
version: conditionPlug.version,
dataTrigger: false,
tickTrigger: false,
properties: {
condition: '$${nodes.' + inRangePlug.label + '.state} !== "In Range" '
},
position: [ 350 + x_offset, 150 + y_offset]
}
],
triggers: [
{
sourceLabel: inRangePlug.label,
destinationLabel: conditionPlug.label
}
]
}
return network
}
/*
polling template with the same boundary conditions, but it also has polling window - duration,
and statistical inputs, such as the aggregation type (mean, nax etc..).
TRUE state means that the conditio is out of boudnaries
*/
preparePolling(trigger, iter = 0) {
const { resource, metric = 'temperature', lowerLimit=0, upperLimit=10, targetNode = 'problem', polling_window, aggregate = mean} = trigger
const suffix = iter === 0 ? '' : '' + iter
const pollingInterval = moment.duration(polling_window).asMilliseconds() / 2
const evictionTime = 2 * pollingInterval
const getMetricValuePlug = {...this.getPlugin('getMetricValue'), label: 'getMetricValue' + suffix}
const conditionPlug = {...this.getPlugin('condition'), label: targetNode}
const x_offset = 0//iter * 100
const y_offset = iter * 400
const network = {
sensors: [
{
label: getMetricValuePlug.label,
name: getMetricValuePlug.name,
version: getMetricValuePlug.version,
dataTrigger: false,
tickTrigger: true,
pollingPeriod: pollingInterval,
evictionTime: evictionTime,
resource,
properties: {
resource, metric, aggregate,
duration : polling_window,
},
position: [ 150 + x_offset, 150 + y_offset]
},
{
label: conditionPlug.label,
name: conditionPlug.name,
version: conditionPlug.version,
dataTrigger: false,
tickTrigger: false,
properties: {
condition: '${nodes.' + getMetricValuePlug.label + '.rawData.result} > ' + upperLimit + ' || ${nodes.' + getMetricValuePlug.label + '.rawData.result} < ' + lowerLimit
},
position: [ 350 + x_offset, 250 + y_offset]
}
],
triggers: [
{
sourceLabel: getMetricValuePlug.label,
destinationLabel: conditionPlug.label,
statesTrigger: [ 'Collected' ]
}
]
}
return network
}
/*
can be used to process events, such as camera events etc, where in the JSON part expression
you need to specity the condition under which the target node will be in the TRUE state
*/
prepareEventStream(trigger, iter = 0) {
const { resource, targetNode = 'problem', path } = trigger
const suffix = iter === 0 ? '' : '' + iter
const conditionPlug = {...this.getPlugin('condition'), label: targetNode}
const streamPlug = {...this.getPlugin('stream'), label: 'stream' + suffix}
const x_offset = 0//iter * 100
const y_offset = iter * 400
const network = {
sensors: [
{
label: streamPlug.label,
name: streamPlug.name,
version: streamPlug.version,
dataTrigger: true,
tickTrigger: false,
resource,
evictionTime: 1000,
position: [ 150 + x_offset, 150 + y_offset]
},
{
label: conditionPlug.label,
name: conditionPlug.name,
version: conditionPlug.version,
dataTrigger: false,
tickTrigger: false,
properties: {
condition: '(() => {\n let data = $${?nodes.'+ streamPlug.label + '.rawData.stream.' + path +'}\n return data !== "" \n})()'
},
position: [ 350 + x_offset, 250 + y_offset]
}
],
triggers: [
{
sourceLabel: streamPlug.label,
destinationLabel: conditionPlug.label,
statesTrigger: [ 'Data' ]
}
]
}
return network
}
/*
merging all templates via the target node, and it will fire an alarm any time one
of the networks is in the TRUE state.
*/
createTaskResultGate(nodes, relation = 'OR', state = 'True', resource = '${task.TASK_ID}') {
const createAlarmPlug = {...this.getPlugin('createAlarm'), label: 'createResultAlarm'}
const clearAlarmPlug = {...this.getPlugin('clearAlarm'), label: 'clearResultAlarm'}
const relations = [{
label: 'PROBLEM',
type: relation,
parentLabels: nodes,
combinations: [nodes.map( () => state)],
position: [ 800 , 150]
}]
const sensors = [{
label: createAlarmPlug.label,
name: createAlarmPlug.name,
version: createAlarmPlug.version,
properties: {
text: 'Result',
severity: 'CRITICAL',
type: 'Taks result',
resource
},
position: [ 1000, 100 ]
},
{
label: clearAlarmPlug.label,
name: clearAlarmPlug.name,
version: clearAlarmPlug.version,
properties: {
type: 'Taks result',
resource
},
position: [ 1000, 300 ]
}]
const triggers = [
{
sourceLabel: 'PROBLEM',
destinationLabel: createAlarmPlug.label,
statesTrigger: [ 'TRUE']
},
{
sourceLabel: 'PROBLEM',
destinationLabel: clearAlarmPlug.label,
statesTrigger: [ 'FALSE']
}]
return {relations, sensors, triggers}
}
async startTaskForTriggers(name='Task builder', triggers, resource, tags, alarmOnTask = true, createTemplateFirst = true) {
let alarmId = '${task.TASK_ID}'
var task = {
sensors: [],
triggers: [],
task: {
type: 'reactive',
start: true,
name, tags
}
}
if(resource !== undefined && resource !== '') {
if(!alarmOnTask)
alarmId = resource
}
var i = 0
var nodes = []
triggers.forEach(trigger => {
var network
if(trigger.type === 'reactive')
network = this.prepareStream(trigger, i++)
else if(trigger.type === 'periodic')
network = this.preparePolling(trigger, i++)
else if(trigger.type === 'event')
network = this.prepareEventStream(trigger, i++)
task.sensors = task.sensors.concat(network.sensors)
task.triggers = task.triggers.concat(network.triggers)
nodes.push(trigger.targetNode)
})
const resultNetwork = this.createTaskResultGate(nodes, 'OR', 'True', alarmId)
task.relations = resultNetwork.relations
task.sensors = task.sensors.concat(resultNetwork.sensors)
task.triggers = task.triggers.concat(resultNetwork.triggers)
if(createTemplateFirst){
const templateName = this.guid(name)
let template = {...task, ...{taskDefaults: {type: 'reactive', tags}}, ...{name: templateName}}
const templateResult = await this.client.templates.create(template)
return await this.client.tasks.create({name, template: templateName})
} else {
return await this.client.tasks.create(task)
}
}
/*
check the status of the running task. Since the gate genererate ALARM for a given
task id, we can simply check if that alarm is present. Other option, if the alarm
service is not present is to check if the GATE (PROBLEM) is in the state TRUE.
*/
async checkStatus(id) {
const task = await this.client.tasks.get(id)
var node = task.nodes.find(x => x.name === 'PROBLEM')
const problemGATE = (node !== undefined && node.mostLikelyState.state === 'TRUE' && node.mostLikelyState.probability === 1)
const alarms = await this.client.alarms.search({source: id})
const problem = alarms.alarms.length > 0 || problemGATE
const nodes = task.tags.targetNodes || []
const targetNodes = nodes.map(node => {
let n = task.nodes.find(x => x.name === node)
return {
name: n.name,
problem: n.mostLikelyState.state === 'TRUE' && n.mostLikelyState.probability === 1
}
})
return { problem, targetNodes }
}
async startNotificationTask(resource, name = 'notification task', plugin = 'mandrillMail', states = ["Created", "Occurred again"], tags = {}) {
const alarmEventSensorPlug = this.getPlugin('AlarmEventSensor')
const notificationPlug = this.getPlugin(plugin)
const task = {
sensors: [
{
label: alarmEventSensorPlug.name,
name: alarmEventSensorPlug.name,
version: alarmEventSensorPlug.version,
resource: resource,
dataTrigger: false,
tickTrigger: false,
properties: {
status: 'ACTIVE'
},
position: [ 150, 150 ]
},
{
label: notificationPlug.name,
name: notificationPlug.name,
version: notificationPlug.version,
properties: {
...config[plugin],
message: 'Out of range',
subject: 'Out of range resource ' + resource,
},
position: [ 800, 250 ]
}
],
triggers: [
{
sourceLabel: alarmEventSensorPlug.name,
destinationLabel: notificationPlug.name,
statesTrigger: states
}
],
task: {
resource, name, tags,
type: 'reactive',
start: true
}
}
return await this.client.tasks.create(task, {})
}
}