@jambonz/time-series 0.1.10 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +272 -62
- package/package.json +2 -2
- package/test/unit-tests.js +38 -2
package/index.js
CHANGED
|
@@ -33,6 +33,7 @@ const schemas = {
|
|
|
33
33
|
trace_id: Influx.FieldType.STRING
|
|
34
34
|
},
|
|
35
35
|
tags: [
|
|
36
|
+
'service_provider_sid',
|
|
36
37
|
'account_sid',
|
|
37
38
|
'host',
|
|
38
39
|
'trunk',
|
|
@@ -47,6 +48,7 @@ const schemas = {
|
|
|
47
48
|
detail: Influx.FieldType.STRING,
|
|
48
49
|
},
|
|
49
50
|
tags: [
|
|
51
|
+
'service_provider_sid',
|
|
50
52
|
'account_sid',
|
|
51
53
|
'alert_type'
|
|
52
54
|
]
|
|
@@ -57,6 +59,7 @@ const schemas = {
|
|
|
57
59
|
calls_in_progress: Influx.FieldType.INTEGER,
|
|
58
60
|
},
|
|
59
61
|
tags: [
|
|
62
|
+
'service_provider_sid',
|
|
60
63
|
'account_sid'
|
|
61
64
|
]
|
|
62
65
|
}
|
|
@@ -80,87 +83,165 @@ const writeData = async(client) => {
|
|
|
80
83
|
}
|
|
81
84
|
};
|
|
82
85
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
+
/* for Service Provider */
|
|
87
|
+
const createCallCountsQuerySP = ({page, page_size, days, start, end}) => {
|
|
88
|
+
let sql = 'SELECT * from call_counts WHERE service_provider_sid = $service_provider_sid ';
|
|
89
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
86
90
|
else {
|
|
87
|
-
if (start) sql +=
|
|
88
|
-
if (end) sql +=
|
|
91
|
+
if (start) sql += 'AND time >= $start ';
|
|
92
|
+
if (end) sql += 'AND time <= $end ';
|
|
89
93
|
}
|
|
90
94
|
sql += ' ORDER BY time DESC';
|
|
91
|
-
if (page_size) sql +=
|
|
92
|
-
if (page) sql +=
|
|
93
|
-
//console.log(sql);
|
|
95
|
+
if (page_size) sql += ' LIMIT $page_size';
|
|
96
|
+
if (page) sql += ' OFFSET $offset';
|
|
94
97
|
return sql;
|
|
95
98
|
};
|
|
96
99
|
|
|
97
|
-
const
|
|
98
|
-
let sql =
|
|
99
|
-
if (days) sql +=
|
|
100
|
+
const createCallCountsCountQuerySP = ({days, start, end}) => {
|
|
101
|
+
let sql = 'SELECT COUNT(calls_in_progress) from call_counts WHERE service_provider_sid = $service_provider_sid ';
|
|
102
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
100
103
|
else {
|
|
101
|
-
if (start) sql +=
|
|
102
|
-
if (end) sql +=
|
|
104
|
+
if (start) sql += 'AND time >= $start ';
|
|
105
|
+
if (end) sql += 'AND time <= $end ';
|
|
103
106
|
}
|
|
104
|
-
//console.log(sql);
|
|
105
107
|
return sql;
|
|
106
108
|
};
|
|
107
109
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (
|
|
112
|
-
if (['true', 'false'].includes(answered)) sql += `AND answered = '${answered}' `;
|
|
113
|
-
if (days) sql += `AND time > now() - ${days}d `;
|
|
110
|
+
/* for Account */
|
|
111
|
+
const createCallCountsQuery = ({page, page_size, days, start, end}) => {
|
|
112
|
+
let sql = 'SELECT * from call_counts WHERE account_sid = $account_sid ';
|
|
113
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
114
114
|
else {
|
|
115
|
-
if (start) sql +=
|
|
116
|
-
if (end) sql +=
|
|
115
|
+
if (start) sql += 'AND time >= $start ';
|
|
116
|
+
if (end) sql += 'AND time <= $end ';
|
|
117
117
|
}
|
|
118
118
|
sql += ' ORDER BY time DESC';
|
|
119
|
-
if (page_size) sql +=
|
|
120
|
-
if (page) sql +=
|
|
121
|
-
//console.log(sql);
|
|
119
|
+
if (page_size) sql += ' LIMIT $page_size';
|
|
120
|
+
if (page) sql += ' OFFSET $offset';
|
|
122
121
|
return sql;
|
|
123
122
|
};
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (
|
|
128
|
-
|
|
129
|
-
|
|
123
|
+
|
|
124
|
+
const createCallCountsCountQuery = ({days, start, end}) => {
|
|
125
|
+
let sql = 'SELECT COUNT(calls_in_progress) from call_counts WHERE account_sid = $account_sid ';
|
|
126
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
127
|
+
else {
|
|
128
|
+
if (start) sql += 'AND time >= $start ';
|
|
129
|
+
if (end) sql += 'AND time <= $end ';
|
|
130
|
+
}
|
|
131
|
+
return sql;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/* for Service Provider */
|
|
135
|
+
const createCdrQuerySP = ({page, page_size, trunk, direction, answered, days, start, end}) => {
|
|
136
|
+
let sql = 'SELECT * from cdrs WHERE service_provider_sid = $service_provider_sid ';
|
|
137
|
+
if (trunk) sql += 'AND trunk = $trunk ';
|
|
138
|
+
if (direction) sql += 'AND direction = $direction ';
|
|
139
|
+
if (['true', 'false'].includes(answered)) sql += 'AND answered = $answered ';
|
|
140
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
141
|
+
else {
|
|
142
|
+
if (start) sql += 'AND time >= $start ';
|
|
143
|
+
if (end) sql += 'AND time <= $end ';
|
|
144
|
+
}
|
|
145
|
+
sql += ' ORDER BY time DESC';
|
|
146
|
+
if (page_size) sql += ' LIMIT $page_size';
|
|
147
|
+
if (page) sql += ' OFFSET $offset';
|
|
148
|
+
return sql;
|
|
149
|
+
};
|
|
150
|
+
const createCdrCountQuerySP = ({trunk, direction, answered, days, start, end}) => {
|
|
151
|
+
let sql = 'SELECT COUNT(sip_callid) from cdrs WHERE service_provider_sid = $service_provider_sid ';
|
|
152
|
+
if (trunk) sql += 'AND trunk = $trunk ';
|
|
153
|
+
if (direction) sql += 'AND direction = $direction ';
|
|
154
|
+
if (['true', 'false'].includes(answered)) sql += 'AND answered = $answered ';
|
|
155
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
156
|
+
else {
|
|
157
|
+
if (start) sql += 'AND time >= $start ';
|
|
158
|
+
if (end) sql += 'AND time <= $end ';
|
|
159
|
+
}
|
|
160
|
+
return sql;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/* for Account */
|
|
164
|
+
const createCdrQuery = ({page, page_size, trunk, direction, answered, days, start, end}) => {
|
|
165
|
+
let sql = 'SELECT * from cdrs WHERE account_sid = $account_sid ';
|
|
166
|
+
if (trunk) sql += 'AND trunk = $trunk ';
|
|
167
|
+
if (direction) sql += 'AND direction = $direction ';
|
|
168
|
+
if (['true', 'false'].includes(answered)) sql += 'AND answered = $answered ';
|
|
169
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
170
|
+
else {
|
|
171
|
+
if (start) sql += 'AND time >= $start ';
|
|
172
|
+
if (end) sql += 'AND time <= $end ';
|
|
173
|
+
}
|
|
174
|
+
sql += ' ORDER BY time DESC';
|
|
175
|
+
if (page_size) sql += ' LIMIT $page_size';
|
|
176
|
+
if (page) sql += ' OFFSET $offset';
|
|
177
|
+
return sql;
|
|
178
|
+
};
|
|
179
|
+
const createCdrCountQuery = ({trunk, direction, answered, days, start, end}) => {
|
|
180
|
+
let sql = 'SELECT COUNT(sip_callid) from cdrs WHERE account_sid = $account_sid ';
|
|
181
|
+
if (trunk) sql += 'AND trunk = $trunk ';
|
|
182
|
+
if (direction) sql += 'AND direction = $direction ';
|
|
183
|
+
if (['true', 'false'].includes(answered)) sql += 'AND answered = $answered ';
|
|
184
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
130
185
|
else {
|
|
131
|
-
if (start) sql +=
|
|
132
|
-
if (end) sql +=
|
|
186
|
+
if (start) sql += 'AND time >= $start ';
|
|
187
|
+
if (end) sql += 'AND time <= $end ';
|
|
133
188
|
}
|
|
134
|
-
//console.log(sql);
|
|
135
189
|
return sql;
|
|
136
190
|
};
|
|
137
191
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (
|
|
142
|
-
if (
|
|
192
|
+
/* for Service Provider */
|
|
193
|
+
const createAlertsQuerySP = ({target_sid, alert_type, page, page_size, days, start, end}) => {
|
|
194
|
+
let sql = 'SELECT * FROM alerts WHERE service_provider_sid = $service_provider_sid ';
|
|
195
|
+
if (target_sid) sql += 'AND target_sid = $target_sid ';
|
|
196
|
+
if (alert_type) sql += 'AND alert_type = $alert_type ';
|
|
197
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
143
198
|
else {
|
|
144
|
-
if (start) sql +=
|
|
145
|
-
if (end) sql +=
|
|
199
|
+
if (start) sql += 'AND time >= $start ';
|
|
200
|
+
if (end) sql += 'AND time <= $end ';
|
|
146
201
|
}
|
|
147
202
|
sql += ' ORDER BY time DESC';
|
|
148
|
-
if (page_size) sql +=
|
|
149
|
-
if (page) sql +=
|
|
150
|
-
//console.log(sql);
|
|
203
|
+
if (page_size) sql += ' LIMIT $page_size';
|
|
204
|
+
if (page) sql += ' OFFSET $offset';
|
|
151
205
|
return sql;
|
|
152
206
|
};
|
|
153
207
|
|
|
154
|
-
const
|
|
155
|
-
let sql =
|
|
156
|
-
if (target_sid) sql +=
|
|
157
|
-
if (alert_type) sql +=
|
|
158
|
-
if (days) sql +=
|
|
208
|
+
const createAlertsCountQuerySP = ({target_sid, alert_type, days, start, end}) => {
|
|
209
|
+
let sql = 'SELECT COUNT(message) FROM alerts WHERE service_provider_sid = $service_provider_sid ';
|
|
210
|
+
if (target_sid) sql += 'AND target_sid = $target_sid ';
|
|
211
|
+
if (alert_type) sql += 'AND alert_type = $alert_type ';
|
|
212
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
159
213
|
else {
|
|
160
|
-
if (start) sql +=
|
|
161
|
-
if (end) sql +=
|
|
214
|
+
if (start) sql += 'AND time >= $start ';
|
|
215
|
+
if (end) sql += 'AND time <= $end ';
|
|
216
|
+
}
|
|
217
|
+
return sql;
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/* for Account */
|
|
221
|
+
const createAlertsQuery = ({target_sid, alert_type, page, page_size, days, start, end}) => {
|
|
222
|
+
let sql = 'SELECT * FROM alerts WHERE account_sid = $account_sid ';
|
|
223
|
+
if (target_sid) sql += 'AND target_sid = $target_sid ';
|
|
224
|
+
if (alert_type) sql += 'AND alert_type = $alert_type ';
|
|
225
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
226
|
+
else {
|
|
227
|
+
if (start) sql += 'AND time >= $start ';
|
|
228
|
+
if (end) sql += 'AND time <= $end ';
|
|
229
|
+
}
|
|
230
|
+
sql += ' ORDER BY time DESC';
|
|
231
|
+
if (page_size) sql += ' LIMIT $page_size';
|
|
232
|
+
if (page) sql += ' OFFSET $offset';
|
|
233
|
+
return sql;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const createAlertsCountQuery = ({target_sid, alert_type, days, start, end}) => {
|
|
237
|
+
let sql = 'SELECT COUNT(message) FROM alerts WHERE account_sid = $account_sid ';
|
|
238
|
+
if (target_sid) sql += 'AND target_sid = $target_sid ';
|
|
239
|
+
if (alert_type) sql += 'AND alert_type = $alert_type ';
|
|
240
|
+
if (days) sql += 'AND time > $timestamp ';
|
|
241
|
+
else {
|
|
242
|
+
if (start) sql += 'AND time >= $start ';
|
|
243
|
+
if (end) sql += 'AND time <= $end ';
|
|
162
244
|
}
|
|
163
|
-
//console.log(sql);
|
|
164
245
|
return sql;
|
|
165
246
|
};
|
|
166
247
|
|
|
@@ -174,11 +255,12 @@ const initDatabase = async(client, dbName) => {
|
|
|
174
255
|
|
|
175
256
|
const writeCallCount = async(client, count) => {
|
|
176
257
|
if (!client.locals.initialized) await initDatabase(client, 'call_counts');
|
|
177
|
-
const {account_sid, ...fields} = count;
|
|
258
|
+
const {service_provider_sid, account_sid, ...fields} = count;
|
|
178
259
|
const data = {
|
|
179
260
|
measurement: 'call_counts',
|
|
180
261
|
fields,
|
|
181
262
|
tags: {
|
|
263
|
+
service_provider_sid,
|
|
182
264
|
account_sid
|
|
183
265
|
}
|
|
184
266
|
};
|
|
@@ -189,6 +271,41 @@ const writeCallCount = async(client, count) => {
|
|
|
189
271
|
return;
|
|
190
272
|
};
|
|
191
273
|
|
|
274
|
+
const queryCallCountsSP = async(client, opts) => {
|
|
275
|
+
if (!client.locals.initialized) await initDatabase(client, 'call_counts');
|
|
276
|
+
const response = {
|
|
277
|
+
total: 0,
|
|
278
|
+
page_size: opts.page_size,
|
|
279
|
+
page: opts.page,
|
|
280
|
+
data: []
|
|
281
|
+
};
|
|
282
|
+
const params = generateBindParameters(opts);
|
|
283
|
+
const sqlTotal = createCallCountsCountQuerySP(opts);
|
|
284
|
+
const obj = await client.queryRaw(sqlTotal, { placeholders: params});
|
|
285
|
+
//console.log(`sqlTotal: ${sqlTotal}, results: ${JSON.stringify(obj)}`);
|
|
286
|
+
if (!obj.results || !obj.results[0].series) return response;
|
|
287
|
+
response.total = obj.results[0].series[0].values[0][1];
|
|
288
|
+
|
|
289
|
+
const sql = createCallCountsQuerySP(opts);
|
|
290
|
+
const res = await client.queryRaw(sql, { placeholders: params});
|
|
291
|
+
//console.log(`sql: ${sqlTotal}, results: ${JSON.stringify(res)}`);
|
|
292
|
+
if (res.results[0].series && res.results[0].series.length) {
|
|
293
|
+
const {columns, values} = res.results[0].series[0];
|
|
294
|
+
const data = values.map((v) => {
|
|
295
|
+
const obj = {};
|
|
296
|
+
v.forEach((val, idx) => {
|
|
297
|
+
v.forEach((val, idx) => {
|
|
298
|
+
const key = columns[idx];
|
|
299
|
+
obj[key] = val;
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
return obj;
|
|
303
|
+
});
|
|
304
|
+
response.data = data;
|
|
305
|
+
}
|
|
306
|
+
return response;
|
|
307
|
+
};
|
|
308
|
+
|
|
192
309
|
const queryCallCounts = async(client, opts) => {
|
|
193
310
|
if (!client.locals.initialized) await initDatabase(client, 'call_counts');
|
|
194
311
|
const response = {
|
|
@@ -197,14 +314,15 @@ const queryCallCounts = async(client, opts) => {
|
|
|
197
314
|
page: opts.page,
|
|
198
315
|
data: []
|
|
199
316
|
};
|
|
317
|
+
const params = generateBindParameters(opts);
|
|
200
318
|
const sqlTotal = createCallCountsCountQuery(opts);
|
|
201
|
-
const obj = await client.queryRaw(sqlTotal);
|
|
319
|
+
const obj = await client.queryRaw(sqlTotal, { placeholders: params});
|
|
202
320
|
//console.log(`sqlTotal: ${sqlTotal}, results: ${JSON.stringify(obj)}`);
|
|
203
321
|
if (!obj.results || !obj.results[0].series) return response;
|
|
204
322
|
response.total = obj.results[0].series[0].values[0][1];
|
|
205
323
|
|
|
206
324
|
const sql = createCallCountsQuery(opts);
|
|
207
|
-
const res = await client.queryRaw(sql);
|
|
325
|
+
const res = await client.queryRaw(sql, { placeholders: params});
|
|
208
326
|
//console.log(`sql: ${sqlTotal}, results: ${JSON.stringify(res)}`);
|
|
209
327
|
if (res.results[0].series && res.results[0].series.length) {
|
|
210
328
|
const {columns, values} = res.results[0].series[0];
|
|
@@ -227,7 +345,7 @@ const writeCdrs = async(client, cdrs) => {
|
|
|
227
345
|
if (!client.locals.initialized) await initDatabase(client, 'cdrs');
|
|
228
346
|
cdrs = (Array.isArray(cdrs) ? cdrs : [cdrs])
|
|
229
347
|
.map((cdr) => {
|
|
230
|
-
const {direction, host, trunk, account_sid, answered, attempted_at, ...fields} = cdr;
|
|
348
|
+
const {direction, host, trunk, service_provider_sid, account_sid, answered, attempted_at, ...fields} = cdr;
|
|
231
349
|
return {
|
|
232
350
|
measurement: 'cdrs',
|
|
233
351
|
timestamp: new Date(attempted_at),
|
|
@@ -236,6 +354,7 @@ const writeCdrs = async(client, cdrs) => {
|
|
|
236
354
|
direction,
|
|
237
355
|
host,
|
|
238
356
|
trunk,
|
|
357
|
+
service_provider_sid,
|
|
239
358
|
account_sid,
|
|
240
359
|
answered
|
|
241
360
|
}
|
|
@@ -249,6 +368,41 @@ const writeCdrs = async(client, cdrs) => {
|
|
|
249
368
|
return;
|
|
250
369
|
};
|
|
251
370
|
|
|
371
|
+
const queryCdrsSP = async(client, opts) => {
|
|
372
|
+
if (!client.locals.initialized) await initDatabase(client, 'cdrs');
|
|
373
|
+
const response = {
|
|
374
|
+
total: 0,
|
|
375
|
+
page_size: opts.page_size,
|
|
376
|
+
page: opts.page,
|
|
377
|
+
data: []
|
|
378
|
+
};
|
|
379
|
+
const params = generateBindParameters(opts);
|
|
380
|
+
const sqlTotal = createCdrCountQuerySP(opts);
|
|
381
|
+
const obj = await client.queryRaw(sqlTotal, { placeholders: params});
|
|
382
|
+
//console.log(`sql: ${sqlTotal}, results: ${JSON.stringify(obj)}`);
|
|
383
|
+
if (!obj.results || !obj.results[0].series) return response;
|
|
384
|
+
response.total = obj.results[0].series[0].values[0][1];
|
|
385
|
+
|
|
386
|
+
const sql = createCdrQuerySP(opts);
|
|
387
|
+
const res = await client.queryRaw(sql, { placeholders: params});
|
|
388
|
+
if (res.results[0].series && res.results[0].series.length) {
|
|
389
|
+
const {columns, values} = res.results[0].series[0];
|
|
390
|
+
const data = values.map((v) => {
|
|
391
|
+
const obj = {};
|
|
392
|
+
v.forEach((val, idx) => {
|
|
393
|
+
const key = 'time' === columns[idx] ? 'attempted_at' : columns[idx];
|
|
394
|
+
let retvalue = val;
|
|
395
|
+
if (['answered_at', 'terminated_at'].includes(key)) retvalue = new Date(val);
|
|
396
|
+
if (key === 'answered') retvalue = 'true' === val ? true : false;
|
|
397
|
+
obj[key] = retvalue;
|
|
398
|
+
});
|
|
399
|
+
return obj;
|
|
400
|
+
});
|
|
401
|
+
response.data = data;
|
|
402
|
+
}
|
|
403
|
+
return response;
|
|
404
|
+
};
|
|
405
|
+
|
|
252
406
|
const queryCdrs = async(client, opts) => {
|
|
253
407
|
if (!client.locals.initialized) await initDatabase(client, 'cdrs');
|
|
254
408
|
const response = {
|
|
@@ -257,14 +411,15 @@ const queryCdrs = async(client, opts) => {
|
|
|
257
411
|
page: opts.page,
|
|
258
412
|
data: []
|
|
259
413
|
};
|
|
414
|
+
const params = generateBindParameters(opts);
|
|
260
415
|
const sqlTotal = createCdrCountQuery(opts);
|
|
261
|
-
const obj = await client.queryRaw(sqlTotal);
|
|
416
|
+
const obj = await client.queryRaw(sqlTotal, { placeholders: params});
|
|
262
417
|
//console.log(`sql: ${sqlTotal}, results: ${JSON.stringify(obj)}`);
|
|
263
418
|
if (!obj.results || !obj.results[0].series) return response;
|
|
264
419
|
response.total = obj.results[0].series[0].values[0][1];
|
|
265
420
|
|
|
266
421
|
const sql = createCdrQuery(opts);
|
|
267
|
-
const res = await client.queryRaw(sql);
|
|
422
|
+
const res = await client.queryRaw(sql, { placeholders: params});
|
|
268
423
|
if (res.results[0].series && res.results[0].series.length) {
|
|
269
424
|
const {columns, values} = res.results[0].series[0];
|
|
270
425
|
const data = values.map((v) => {
|
|
@@ -287,7 +442,18 @@ const writeAlerts = async(client, alerts) => {
|
|
|
287
442
|
if (!client.locals.initialized) await initDatabase(client, 'alerts');
|
|
288
443
|
alerts = (Array.isArray(alerts) ? alerts : [alerts])
|
|
289
444
|
.map((alert) => {
|
|
290
|
-
const {
|
|
445
|
+
const {
|
|
446
|
+
alert_type,
|
|
447
|
+
service_provider_sid,
|
|
448
|
+
account_sid,
|
|
449
|
+
target_sid,
|
|
450
|
+
url,
|
|
451
|
+
status,
|
|
452
|
+
vendor,
|
|
453
|
+
count,
|
|
454
|
+
detail,
|
|
455
|
+
timestamp
|
|
456
|
+
} = alert;
|
|
291
457
|
let message = alert.message;
|
|
292
458
|
if (!message) {
|
|
293
459
|
switch (alert_type) {
|
|
@@ -337,7 +503,7 @@ const writeAlerts = async(client, alerts) => {
|
|
|
337
503
|
}
|
|
338
504
|
let fields = { message };
|
|
339
505
|
if (target_sid) fields = Object.assign(fields, {target_sid});
|
|
340
|
-
const obj = {measurement: 'alerts', fields: fields, tags: { alert_type, account_sid }};
|
|
506
|
+
const obj = {measurement: 'alerts', fields: fields, tags: { alert_type, service_provider_sid, account_sid }};
|
|
341
507
|
if (timestamp) obj.timestamp = timestamp;
|
|
342
508
|
if (detail) obj.fields.detail = detail;
|
|
343
509
|
return obj;
|
|
@@ -350,6 +516,46 @@ const writeAlerts = async(client, alerts) => {
|
|
|
350
516
|
return;
|
|
351
517
|
};
|
|
352
518
|
|
|
519
|
+
const generateBindParameters = (opts) => {
|
|
520
|
+
const params = {...opts};
|
|
521
|
+
if (opts.days) params.timestamp = Date.now() * 1000000 - opts.days * 24 * 60 * 60 * 1000000000;
|
|
522
|
+
if (opts.page) params.offset = opts.page - 1 >= 0 && opts.page_size >= 0 ? (opts.page - 1) * opts.page_size : 0;
|
|
523
|
+
if (opts.page_size) params.page_size = parseInt(opts.page_size);
|
|
524
|
+
return params;
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
const queryAlertsSP = async(client, opts) => {
|
|
528
|
+
if (!client.locals.initialized) await initDatabase(client, 'alerts');
|
|
529
|
+
const response = {
|
|
530
|
+
total: 0,
|
|
531
|
+
page_size: opts.page_size,
|
|
532
|
+
page: opts.page,
|
|
533
|
+
data: []
|
|
534
|
+
};
|
|
535
|
+
const params = generateBindParameters(opts);
|
|
536
|
+
const sqlTotal = createAlertsCountQuerySP(opts);
|
|
537
|
+
const obj = await client.queryRaw(sqlTotal, { placeholders: params});
|
|
538
|
+
//console.log(`query total alerts: ${sqlTotal}: ${JSON.stringify(obj)}`);
|
|
539
|
+
if (!obj.results || !obj.results[0].series) return response;
|
|
540
|
+
response.total = obj.results[0].series[0].values[0][1];
|
|
541
|
+
|
|
542
|
+
const sql = createAlertsQuerySP(opts);
|
|
543
|
+
const res = await client.queryRaw(sql, { placeholders: params});
|
|
544
|
+
if (res.results[0].series && res.results[0].series.length) {
|
|
545
|
+
const {columns, values} = res.results[0].series[0];
|
|
546
|
+
const data = values.map((v) => {
|
|
547
|
+
const obj = {};
|
|
548
|
+
v.forEach((val, idx) => {
|
|
549
|
+
const key = columns[idx];
|
|
550
|
+
obj[key] = val;
|
|
551
|
+
});
|
|
552
|
+
return obj;
|
|
553
|
+
});
|
|
554
|
+
response.data = data;
|
|
555
|
+
}
|
|
556
|
+
return response;
|
|
557
|
+
};
|
|
558
|
+
|
|
353
559
|
const queryAlerts = async(client, opts) => {
|
|
354
560
|
if (!client.locals.initialized) await initDatabase(client, 'alerts');
|
|
355
561
|
const response = {
|
|
@@ -358,14 +564,15 @@ const queryAlerts = async(client, opts) => {
|
|
|
358
564
|
page: opts.page,
|
|
359
565
|
data: []
|
|
360
566
|
};
|
|
567
|
+
const params = generateBindParameters(opts);
|
|
361
568
|
const sqlTotal = createAlertsCountQuery(opts);
|
|
362
|
-
const obj = await client.queryRaw(sqlTotal);
|
|
569
|
+
const obj = await client.queryRaw(sqlTotal, { placeholders: params});
|
|
363
570
|
//console.log(`query total alerts: ${sqlTotal}: ${JSON.stringify(obj)}`);
|
|
364
571
|
if (!obj.results || !obj.results[0].series) return response;
|
|
365
572
|
response.total = obj.results[0].series[0].values[0][1];
|
|
366
573
|
|
|
367
574
|
const sql = createAlertsQuery(opts);
|
|
368
|
-
const res = await client.queryRaw(sql);
|
|
575
|
+
const res = await client.queryRaw(sql, { placeholders: params});
|
|
369
576
|
if (res.results[0].series && res.results[0].series.length) {
|
|
370
577
|
const {columns, values} = res.results[0].series[0];
|
|
371
578
|
const data = values.map((v) => {
|
|
@@ -423,10 +630,13 @@ module.exports = (logger, opts) => {
|
|
|
423
630
|
return {
|
|
424
631
|
writeCallCount: writeCallCount.bind(null, callCountClient),
|
|
425
632
|
queryCallCounts: queryCallCounts.bind(null, callCountClient),
|
|
633
|
+
queryCallCountsSP: queryCallCountsSP.bind(null, callCountClient),
|
|
426
634
|
writeCdrs: writeCdrs.bind(null, cdrClient),
|
|
635
|
+
queryCdrsSP: queryCdrsSP.bind(null, cdrClient),
|
|
427
636
|
queryCdrs: queryCdrs.bind(null, cdrClient),
|
|
428
637
|
writeAlerts: writeAlerts.bind(null, alertClient),
|
|
429
638
|
queryAlerts: queryAlerts.bind(null, alertClient),
|
|
639
|
+
queryAlertsSP: queryAlertsSP.bind(null, alertClient),
|
|
430
640
|
AlertType: { ...AlertType }
|
|
431
641
|
};
|
|
432
642
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jambonz/time-series",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "write and query data to time series daetabase",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"debug": "^4.3.1",
|
|
15
|
-
"influx": "^5.
|
|
15
|
+
"influx": "^5.9.3"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"eslint": "^7.23.0",
|
package/test/unit-tests.js
CHANGED
|
@@ -5,10 +5,13 @@ const consoleLogger = {error: console.error, info: console.log, debug: console.l
|
|
|
5
5
|
const {
|
|
6
6
|
writeCallCount,
|
|
7
7
|
queryCallCounts,
|
|
8
|
+
queryCallCountsSP,
|
|
8
9
|
writeCdrs,
|
|
9
10
|
queryCdrs,
|
|
11
|
+
queryCdrsSP,
|
|
10
12
|
writeAlerts,
|
|
11
13
|
queryAlerts,
|
|
14
|
+
queryAlertsSP,
|
|
12
15
|
AlertType
|
|
13
16
|
} = require('..')(consoleLogger, '127.0.0.1', {commitSize: 1});
|
|
14
17
|
|
|
@@ -27,6 +30,7 @@ test('write timeseries data', async(t) => {
|
|
|
27
30
|
host: '10.10.100.1',
|
|
28
31
|
remote_host: '10.10.100.8',
|
|
29
32
|
trunk: 'device',
|
|
33
|
+
service_provider_sid: 'zzzzz',
|
|
30
34
|
account_sid: 'xxxx',
|
|
31
35
|
call_sid: 'foo'
|
|
32
36
|
},
|
|
@@ -44,6 +48,7 @@ test('write timeseries data', async(t) => {
|
|
|
44
48
|
host: '10.10.100.1',
|
|
45
49
|
remote_host: '10.10.100.8',
|
|
46
50
|
trunk: 'twilio',
|
|
51
|
+
service_provider_sid: 'zzzzz',
|
|
47
52
|
account_sid: 'yyyy',
|
|
48
53
|
call_sid: 'bar'
|
|
49
54
|
}]);
|
|
@@ -51,70 +56,85 @@ test('write timeseries data', async(t) => {
|
|
|
51
56
|
|
|
52
57
|
result = await queryCdrs({account_sid: 'xxxx', page: 1, page_size:25});
|
|
53
58
|
//npm tesconsole.log(JSON.stringify(result));
|
|
54
|
-
t.ok(result.data.length === 1, 'queried cdrs')
|
|
59
|
+
t.ok(result.data.length === 1, 'queried cdrs by account sid')
|
|
55
60
|
|
|
56
61
|
result = await queryCdrs({account_sid: 'yyyy', trunk: 'twilio', page: 1, page_size:25});
|
|
57
62
|
t.ok(result.data.length === 1, 'queried cdrs by trunk')
|
|
58
63
|
|
|
64
|
+
result = await queryCdrsSP({service_provider_sid: 'zzzzz', page: 1, page_size:25});
|
|
65
|
+
t.ok(result.data.length === 2, 'queried cdrs by service provider sid')
|
|
66
|
+
|
|
59
67
|
result = await writeAlerts([
|
|
60
68
|
{
|
|
61
69
|
alert_type: AlertType.WEBHOOK_STATUS_FAILURE,
|
|
70
|
+
service_provider_sid: 'zzzzz',
|
|
62
71
|
account_sid: 'yyyy',
|
|
63
72
|
url: 'http://foo.bar',
|
|
64
73
|
status: 404
|
|
65
74
|
},
|
|
66
75
|
{
|
|
67
76
|
alert_type: AlertType.WEBHOOK_CONNECTION_FAILURE,
|
|
77
|
+
service_provider_sid: 'zzzzz',
|
|
68
78
|
account_sid: 'yyyy',
|
|
69
79
|
url: 'http://foo.bar'
|
|
70
80
|
},
|
|
71
81
|
{
|
|
72
82
|
alert_type: AlertType.WEBHOOK_AUTH_FAILURE,
|
|
83
|
+
service_provider_sid: 'zzzzz',
|
|
73
84
|
account_sid: 'yyyy',
|
|
74
85
|
url: 'http://foo.bar'
|
|
75
86
|
},
|
|
76
87
|
{
|
|
77
88
|
alert_type: AlertType.INVALID_APP_PAYLOAD,
|
|
89
|
+
service_provider_sid: 'zzzzz',
|
|
78
90
|
account_sid: 'yyyy',
|
|
79
91
|
target_sid: 'zzzz',
|
|
80
92
|
message: 'invalid app payload'
|
|
81
93
|
},
|
|
82
94
|
{
|
|
83
95
|
alert_type: AlertType.TTS_NOT_PROVISIONED,
|
|
96
|
+
service_provider_sid: 'zzzzz',
|
|
84
97
|
account_sid: 'yyyy',
|
|
85
98
|
vendor: 'google'
|
|
86
99
|
},
|
|
87
100
|
{
|
|
88
101
|
alert_type: AlertType.STT_NOT_PROVISIONED,
|
|
102
|
+
service_provider_sid: 'zzzzz',
|
|
89
103
|
account_sid: 'yyyy',
|
|
90
104
|
vendor: 'google'
|
|
91
105
|
},
|
|
92
106
|
{
|
|
93
107
|
alert_type: AlertType.TTS_FAILURE,
|
|
108
|
+
service_provider_sid: 'zzzzz',
|
|
94
109
|
account_sid: 'yyyy',
|
|
95
110
|
vendor: 'google'
|
|
96
111
|
},
|
|
97
112
|
{
|
|
98
113
|
alert_type: AlertType.STT_FAILURE,
|
|
114
|
+
service_provider_sid: 'zzzzz',
|
|
99
115
|
account_sid: 'yyyy',
|
|
100
116
|
vendor: 'google'
|
|
101
117
|
},
|
|
102
118
|
{
|
|
103
119
|
alert_type: AlertType.CARRIER_NOT_PROVISIONED,
|
|
120
|
+
service_provider_sid: 'zzzzz',
|
|
104
121
|
account_sid: 'yyyy',
|
|
105
122
|
},
|
|
106
123
|
{
|
|
107
124
|
alert_type: AlertType.CALL_LIMIT,
|
|
125
|
+
service_provider_sid: 'zzzzz',
|
|
108
126
|
account_sid: 'yyyy',
|
|
109
127
|
count: 50,
|
|
110
128
|
},
|
|
111
129
|
{
|
|
112
130
|
alert_type: AlertType.DEVICE_LIMIT,
|
|
131
|
+
service_provider_sid: 'zzzzz',
|
|
113
132
|
account_sid: 'yyyy',
|
|
114
133
|
count: 250,
|
|
115
134
|
},
|
|
116
135
|
{
|
|
117
136
|
alert_type: AlertType.API_LIMIT,
|
|
137
|
+
service_provider_sid: 'zzzzz',
|
|
118
138
|
account_sid: 'yyyy',
|
|
119
139
|
count: 120,
|
|
120
140
|
}
|
|
@@ -126,24 +146,40 @@ test('write timeseries data', async(t) => {
|
|
|
126
146
|
t.ok(result.data.length === 12, 'queried alerts');
|
|
127
147
|
t.ok(result.data[0].target_sid === null)
|
|
128
148
|
|
|
149
|
+
// Make sure that page, page_size and days support both string and int
|
|
150
|
+
result = await queryAlerts({account_sid: 'yyyy', page: '1', page_size: '10', days: '7'});
|
|
151
|
+
t.ok(result.data.length === 10, 'queried alerts');
|
|
152
|
+
|
|
153
|
+
result = await queryAlerts({account_sid: 'yyyy', page: '2', page_size: '10', days: '7'});
|
|
154
|
+
t.ok(result.data.length === 2, 'queried alerts');
|
|
155
|
+
|
|
129
156
|
result = await queryAlerts({account_sid: 'yyyy', target_sid: 'zzzz', page: 1, page_size: 25, days: 7});
|
|
130
157
|
t.ok(result.data.length === 1, 'queried alerts by target_sid');
|
|
131
158
|
t.ok(result.data[0].target_sid === 'zzzz')
|
|
132
159
|
|
|
160
|
+
result = await queryAlertsSP({service_provider_sid: 'zzzzz', page: 1, page_size: 25, days: 7});
|
|
161
|
+
t.ok(result.data.length === 12, 'queried alerts by service_provider_sid');
|
|
162
|
+
|
|
133
163
|
result = await writeCallCount(
|
|
134
164
|
{
|
|
135
165
|
calls_in_progress: 49,
|
|
166
|
+
service_provider_sid: 'zzzzz',
|
|
136
167
|
account_sid: 'yyyy'
|
|
137
168
|
});
|
|
138
169
|
result = await writeCallCount(
|
|
139
170
|
{
|
|
140
171
|
calls_in_progress: 50,
|
|
172
|
+
service_provider_sid: 'zzzzz',
|
|
141
173
|
account_sid: 'yyyy'
|
|
142
174
|
});
|
|
143
175
|
t.pass('wrote call counts');
|
|
144
176
|
|
|
145
177
|
result = await queryCallCounts({account_sid: 'yyyy', page: 1, page_size: 25, days: 7});
|
|
146
178
|
//console.log(JSON.stringify(result));
|
|
147
|
-
t.ok(result.data.length === 2, 'queried call counts');
|
|
179
|
+
t.ok(result.data.length === 2, 'queried call counts by account_sid');
|
|
180
|
+
|
|
181
|
+
result = await queryCallCountsSP({service_provider_sid: 'zzzzz', page: 1, page_size: 25, days: 7});
|
|
182
|
+
//console.log(JSON.stringify(result));
|
|
183
|
+
t.ok(result.data.length === 2, 'queried call counts by service provider sid');
|
|
148
184
|
|
|
149
185
|
});
|