-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventAnalytics.js
416 lines (385 loc) · 16.3 KB
/
eventAnalytics.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
const EventOperationsClient = require('eventanalytics-sdk').EventOperationsClient;
const PatternOperationsClient = require('eventanalytics-sdk').PatternOperationsClient;
const tokenUtil = require('./tokenUtil');
let eventOperationsClient;
let patternOperationsClient;
var logger = require('cf-nodejs-logging-support');
logger.setLoggingLevel("info");
/**
* For complete API specification of eventanalytics service refer :
* https://developer.mindsphere.io/apis/analytics-eventanalytics/api-eventanalytics-api.html
*/
async function topEvents(req, res) {
/**
* @route /eventanalytics/topevents
* @return The most frequent events, which are sorted by the number of appearances in a dataset in a descending order.
* @description This method - topEvents internally calls method topEvents of EventOperationsClient class.
* This class is available as dependency in eventanalytics-sdk-<version-here>.tgz
*
* @apiEndpoint : POST /api/eventanalytics/v3/findTopEvents of eventanalytics
* service.
* @apiNote Finds the most frequent events, which are sorted by the number of appearances in a dataset in a descending order.
* @throws Error if an error occurs while attempting to invoke the sdk call.
*/
// log("/eventanalytics/topevents invoked.");
logger.info("/eventanalytics/topevents invoked.");
eventOperationsClient = new EventOperationsClient(tokenUtil.getConfig(req.hostname), tokenUtil.getCredential(req));
let data = {
"eventsMetadata": {
"eventTextPropertyName": "text"
},
"events": [
{
"_time": "2017-10-01T12:00:00.001Z",
"text": "INTRODUCING FUEL",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:01.001Z",
"text": "Status@Flame On",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:02.001Z",
"text": "Status@Flame On",
"text_qc": 0
}
],
"numberOfTopPositionsRequired": 1
}
try {
let response = await eventOperationsClient.topEvents({ data: data });
logger.info(`Getting Response successfully for topevents : ${JSON.stringify(response)}`);
res.send(response);
} catch (err) {
logger.info("Getting error"+err);
res.send(err);
};
}
async function countEvents(req, res) {
/**
* @route /eventanalytics/countevents
* @return Output the given time interval (startTime, endTime) and the resulted number of event occurrences.
* @description This method - countEvents internally calls method countEvents of EventOperationsClient class.
* This class is available as dependency in eventanalytics-sdk-<version-here>.tgz.
* The request object is formed and passed dynamically.
* This method takes data as part of request body.
* `data` is a Data structure with two parts eventsMetadata, events.
* eventsMetadata Metadata -> for the events list specifying the property name of the item in the events list
* that contains the text of the event (eventTextPropertyName) and time window length in miliseconds of the
* period in which time interval will be split (splitInterval).
* events List --> with the events that will be processed.
*
* @apiEndpoint : POST /api/eventanalytics/v3/countEvents of eventanalytics
* service.
* @apiNote Determines the number of events for a required time resolution.
* @throws Error if an error occurs while attempting to invoke the sdk call.
*/
logger.info("/eventanalytics/countevents invoked.");
eventOperationsClient = new EventOperationsClient(tokenUtil.getConfig(req.hostname), tokenUtil.getCredential(req));
let data = {
"eventsMetadata": {
"eventTextPropertyName": "text",
"splitInterval": 5000
},
"events": [
{
"_time": "2017-10-01T12:00:00.001Z",
"text": "Downloading the module database causes module 11 restart",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:01.001Z",
"text": "The direction for forwarding the time of day is recognized automatically by the module",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:02.001Z",
"text": "Status@Flame On",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:02.001Z",
"text": "Status@Flame On",
"text_qc": 0
}
]
}
try {
let response = await eventOperationsClient.countEvents({ data: data });
logger.info(`Getting Response successfully for countEvents : ${JSON.stringify(response)}`);
res.send(response);
} catch (err) {
logger.info("Getting error"+err)
res.send(err);
};
}
async function removeduplicateevent(req, res) {
/**
* @route /eventanalytics/removeduplicateevents
* @return List of events after removal of duplicate events.
* @description This method - removeDuplicateEvents internally calls method removeDuplicateEvents of EventOperationsClient class.
* This class is available as dependency in eventanalytics-sdk-<version-here>.tgz.
* The request object is formed and passed dynamically.
* This method takes data as part of request body.
* `data` is a Data structure with two parts eventsMetadata, events.
* eventsMetadata Metadata -> for the events list specifying the property name of the item in the events list that contains
* the text of the event (eventTextPropertyName) and time window length in miliseconds of the period in which time interval
* will be split (splitInterval).
* events List --> with the events that will be processed.
*
* @apiEndpoint : POST /api/eventanalytics/v3/removeDuplicateEvents of eventanalytics
* service.
* @apiNote Removes the duplicate events. Determine pre-existing relationships between events for a requested temporal
* resolution (example 500ms) and reduce the data set by aggregating events with duplicate value.
* @throws Error if an error occurs while attempting to invoke the sdk call.
*/
logger.info("/eventanalytics/removeduplicateevents invoked.");
eventOperationsClient = new EventOperationsClient(tokenUtil.getConfig(req.hostname), tokenUtil.getCredential(req));
let data = {
"eventsMetadata": {
"eventTextPropertyName": "text",
"splitInterval": 5000
},
"events": [
{
"_time": "2017-10-01T12:00:00.001Z",
"text": "INTRODUCING FUEL",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:01.001Z",
"text": "Status@Flame On",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:02.001Z",
"text": "Status@Flame Off",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:03.001Z",
"text": "Error code: 340",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:04.001Z",
"text": "Error code: 340",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:06.001Z",
"text": "INTRODUCING FUEL",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:08.001Z",
"text": "Status@Flame On",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:09.001Z",
"text": "Status@Flame Off",
"text_qc": 0
}
]
}
try {
let response = await eventOperationsClient.removeDuplicateEvents({ data: data });
logger.info(`Getting Response successfully for countEvents : ${JSON.stringify(response)}`);
res.send(response);
} catch (err) {
logger.info("Getting error "+err);
res.send(err);
};
}
async function filterEvents(req, res) {
/**
* @route /eventanalytics/filterevents
* @return List of events after filtering.
* @description This method - filterEvents internally calls method filterEvents of EventOperationsClient class.
* This class is available as dependency in eventanalytics-sdk-<version-here>.tgz.
* The request object is formed and passed dynamically.
* This method takes data as part of request body.
* `data` is a Data structure with two parts eventsMetadata, events.
* eventsMetadata Metadata -> for the events list specifying the property name of the item in the events list
* that contains the text of the event (eventTextPropertyName) and time window length in miliseconds of the
* period in which time interval will be split (splitInterval).
* events List --> with the events that will be processed.
*
* @apiEndpoint : POST /api/eventanalytics/v3/filterEvents of eventanalytics
* service.
* @apiNote Simplifies the dataset to the most meaningful data. Filtering the dataset based on the text of the event.
* @throws Error if an error occurs while attempting to invoke the sdk call.
*/
logger.info("/eventanalytics/filterevents invoked.");
eventOperationsClient = new EventOperationsClient(tokenUtil.getConfig(req.hostname), tokenUtil.getCredential(req));
let data = {
"eventsMetadata": {
"eventTextPropertyName": "text"
},
"events": [
{
"_time": "2017-10-01T12:00:00.001Z",
"text": "INTRODUCING FUEL",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:01.001Z",
"text": "Status@Flame On",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:02.001Z",
"text": "Status@Flame Off",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:03.001Z",
"text": "Error code: 340",
"text_qc": 0
}
],
"filterList": [
"INTRODUCING FUEL",
"MEANINGLESS ALARM",
"Error code: 340"
]
}
try {
let response = await eventOperationsClient.filterEvents({ data: data });
logger.info(`Getting Response successfully for filterEvents : ${JSON.stringify(response)}`);
res.send(response);
} catch (err) {
logger.info("Getting error "+err);
res.send(err);
};
}
async function matchEventPatteren(req, res) {
/**
* @route /eventanalytics/matcheventpatterns
* @return List of events matching pattern.
* @description This method - matchEventPatterns internally calls method matchPatternsOverEvents of PatternOperationsClient
class.
* This class is available as dependency in eventanalytics-sdk-<version-here>.tgz.
* The request object is formed and passed dynamically.
* This method takes data as part of request body.
* Data structure with four parts - maxPatternInterval, patternsList, nonEvents and eventsInput.
*
* maxPatternInterval ---> The maximum time length (in milliseconds) of the sliding window where the
* pattern occurs (Maximum difference allowed between the first event of the pattern and the last one).
*
* patternsList ---> The patterns to be found in events. The eventText can contain regular expressions.
* The acceptable syntax for the regular expressions is the java syntax. minRepetitions and maxRepetitions
* represent the minimum and maximum number of events of the specified type that are allowed to occur in order
* for the pattern to be matched on the events.
*
* nonEvents ---> A list of events that is not allowed to be part of a pattern. Any pattern which contains a
* non-event is excluded from the final report.
*
* eventsInput ---> Metadata for the events list specifying the property name of the item in the events list
* that contains the text of the event and the list with the events that will be processed.
*
* @apiEndpoint : POST /api/eventanalytics/v3/matchEventPatterns of eventanalytics
* service.
* @apiNote Applies the patterns specified in body on a list of events. Finds all instances of the specified pattern(s)
* in a collection of events.
* @throws Error if an error occurs while attempting to invoke the sdk call.
*/
logger.info("/eventanalytics/matcheventpatterns invoked.");
patternOperationsClient = new PatternOperationsClient(tokenUtil.getConfig(req.hostname), tokenUtil.getCredential(req));
let data = {
"maxPatternInterval": 200000,
"patternsList": [
{
"pattern": [
{
"eventText": "INTRODUCING FUEL",
"minRepetitions": 1,
"maxRepetitions": 2
},
{
"eventText": "Status@Flame On",
"minRepetitions": 0,
"maxRepetitions": 1
},
{
"eventText": "Module STOP due to parameter assignment",
"minRepetitions": 1,
"maxRepetitions": 1
}
]
},
{
"pattern": [
{
"eventText": "Downloading the module database causes module .. restart",
"minRepetitions": 1,
"maxRepetitions": 1
},
{
"eventText": "The SIMATIC mode was selected for time-of-day synchronization of the module with Id: ..",
"minRepetitions": 1,
"maxRepetitions": 1
}
]
}
],
"nonEvents": [
"Error 2.. occurred",
"STOPPING ENGINE"
],
"eventsInput": {
"eventsMetadata": {
"eventTextPropertyName": "text"
},
"events": [
{
"_time": "2017-10-01T12:00:00.001Z",
"text": "Downloading the module database causes module 11 restart",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:01.001Z",
"text": "The direction for forwarding the time of day is recognized automatically by the module",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:02.001Z",
"text": "Status@Flame On",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:03.001Z",
"text": "The SIMATIC mode was selected for time-of-day synchronization of the module with Id: 33",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:06.001Z",
"text": "INTRODUCING FUEL",
"text_qc": 0
},
{
"_time": "2017-10-01T12:00:09.001Z",
"text": "Module STOP due to parameter assignment",
"text_qc": 0
}
]
}
}
try {
let response = await patternOperationsClient.matchPatternsOverEvents({ data: data });
logger.info(`Getting Response successfully for matchPatternsOverEvents : ${JSON.stringify(response)}`);
res.send(response);
} catch (err) {
logger.info("Getting error "+err);
res.send(err);
};
}
module.exports = {
topEvents,
countEvents,
removeduplicateevent,
matchEventPatteren,
filterEvents
}