@jambonz/time-series 0.1.12 → 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 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,6 +83,31 @@ const writeData = async(client) => {
80
83
  }
81
84
  };
82
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 ';
90
+ else {
91
+ if (start) sql += 'AND time >= $start ';
92
+ if (end) sql += 'AND time <= $end ';
93
+ }
94
+ sql += ' ORDER BY time DESC';
95
+ if (page_size) sql += ' LIMIT $page_size';
96
+ if (page) sql += ' OFFSET $offset';
97
+ return sql;
98
+ };
99
+
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 ';
103
+ else {
104
+ if (start) sql += 'AND time >= $start ';
105
+ if (end) sql += 'AND time <= $end ';
106
+ }
107
+ return sql;
108
+ };
109
+
110
+ /* for Account */
83
111
  const createCallCountsQuery = ({page, page_size, days, start, end}) => {
84
112
  let sql = 'SELECT * from call_counts WHERE account_sid = $account_sid ';
85
113
  if (days) sql += 'AND time > $timestamp ';
@@ -103,6 +131,36 @@ const createCallCountsCountQuery = ({days, start, end}) => {
103
131
  return sql;
104
132
  };
105
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 */
106
164
  const createCdrQuery = ({page, page_size, trunk, direction, answered, days, start, end}) => {
107
165
  let sql = 'SELECT * from cdrs WHERE account_sid = $account_sid ';
108
166
  if (trunk) sql += 'AND trunk = $trunk ';
@@ -131,6 +189,35 @@ const createCdrCountQuery = ({trunk, direction, answered, days, start, end}) =>
131
189
  return sql;
132
190
  };
133
191
 
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 ';
198
+ else {
199
+ if (start) sql += 'AND time >= $start ';
200
+ if (end) sql += 'AND time <= $end ';
201
+ }
202
+ sql += ' ORDER BY time DESC';
203
+ if (page_size) sql += ' LIMIT $page_size';
204
+ if (page) sql += ' OFFSET $offset';
205
+ return sql;
206
+ };
207
+
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 ';
213
+ else {
214
+ if (start) sql += 'AND time >= $start ';
215
+ if (end) sql += 'AND time <= $end ';
216
+ }
217
+ return sql;
218
+ };
219
+
220
+ /* for Account */
134
221
  const createAlertsQuery = ({target_sid, alert_type, page, page_size, days, start, end}) => {
135
222
  let sql = 'SELECT * FROM alerts WHERE account_sid = $account_sid ';
136
223
  if (target_sid) sql += 'AND target_sid = $target_sid ';
@@ -168,11 +255,12 @@ const initDatabase = async(client, dbName) => {
168
255
 
169
256
  const writeCallCount = async(client, count) => {
170
257
  if (!client.locals.initialized) await initDatabase(client, 'call_counts');
171
- const {account_sid, ...fields} = count;
258
+ const {service_provider_sid, account_sid, ...fields} = count;
172
259
  const data = {
173
260
  measurement: 'call_counts',
174
261
  fields,
175
262
  tags: {
263
+ service_provider_sid,
176
264
  account_sid
177
265
  }
178
266
  };
@@ -183,6 +271,41 @@ const writeCallCount = async(client, count) => {
183
271
  return;
184
272
  };
185
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
+
186
309
  const queryCallCounts = async(client, opts) => {
187
310
  if (!client.locals.initialized) await initDatabase(client, 'call_counts');
188
311
  const response = {
@@ -222,7 +345,7 @@ const writeCdrs = async(client, cdrs) => {
222
345
  if (!client.locals.initialized) await initDatabase(client, 'cdrs');
223
346
  cdrs = (Array.isArray(cdrs) ? cdrs : [cdrs])
224
347
  .map((cdr) => {
225
- 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;
226
349
  return {
227
350
  measurement: 'cdrs',
228
351
  timestamp: new Date(attempted_at),
@@ -231,6 +354,7 @@ const writeCdrs = async(client, cdrs) => {
231
354
  direction,
232
355
  host,
233
356
  trunk,
357
+ service_provider_sid,
234
358
  account_sid,
235
359
  answered
236
360
  }
@@ -244,6 +368,41 @@ const writeCdrs = async(client, cdrs) => {
244
368
  return;
245
369
  };
246
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
+
247
406
  const queryCdrs = async(client, opts) => {
248
407
  if (!client.locals.initialized) await initDatabase(client, 'cdrs');
249
408
  const response = {
@@ -283,7 +442,18 @@ const writeAlerts = async(client, alerts) => {
283
442
  if (!client.locals.initialized) await initDatabase(client, 'alerts');
284
443
  alerts = (Array.isArray(alerts) ? alerts : [alerts])
285
444
  .map((alert) => {
286
- const {alert_type, account_sid, target_sid, url, status, vendor, count, detail, timestamp} = alert;
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;
287
457
  let message = alert.message;
288
458
  if (!message) {
289
459
  switch (alert_type) {
@@ -333,7 +503,7 @@ const writeAlerts = async(client, alerts) => {
333
503
  }
334
504
  let fields = { message };
335
505
  if (target_sid) fields = Object.assign(fields, {target_sid});
336
- 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 }};
337
507
  if (timestamp) obj.timestamp = timestamp;
338
508
  if (detail) obj.fields.detail = detail;
339
509
  return obj;
@@ -354,6 +524,38 @@ const generateBindParameters = (opts) => {
354
524
  return params;
355
525
  };
356
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
+
357
559
  const queryAlerts = async(client, opts) => {
358
560
  if (!client.locals.initialized) await initDatabase(client, 'alerts');
359
561
  const response = {
@@ -428,10 +630,13 @@ module.exports = (logger, opts) => {
428
630
  return {
429
631
  writeCallCount: writeCallCount.bind(null, callCountClient),
430
632
  queryCallCounts: queryCallCounts.bind(null, callCountClient),
633
+ queryCallCountsSP: queryCallCountsSP.bind(null, callCountClient),
431
634
  writeCdrs: writeCdrs.bind(null, cdrClient),
635
+ queryCdrsSP: queryCdrsSP.bind(null, cdrClient),
432
636
  queryCdrs: queryCdrs.bind(null, cdrClient),
433
637
  writeAlerts: writeAlerts.bind(null, alertClient),
434
638
  queryAlerts: queryAlerts.bind(null, alertClient),
639
+ queryAlertsSP: queryAlertsSP.bind(null, alertClient),
435
640
  AlertType: { ...AlertType }
436
641
  };
437
642
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jambonz/time-series",
3
- "version": "0.1.12",
3
+ "version": "0.2.0",
4
4
  "description": "write and query data to time series daetabase",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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
  }
@@ -137,20 +157,29 @@ test('write timeseries data', async(t) => {
137
157
  t.ok(result.data.length === 1, 'queried alerts by target_sid');
138
158
  t.ok(result.data[0].target_sid === 'zzzz')
139
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
+
140
163
  result = await writeCallCount(
141
164
  {
142
165
  calls_in_progress: 49,
166
+ service_provider_sid: 'zzzzz',
143
167
  account_sid: 'yyyy'
144
168
  });
145
169
  result = await writeCallCount(
146
170
  {
147
171
  calls_in_progress: 50,
172
+ service_provider_sid: 'zzzzz',
148
173
  account_sid: 'yyyy'
149
174
  });
150
175
  t.pass('wrote call counts');
151
176
 
152
177
  result = await queryCallCounts({account_sid: 'yyyy', page: 1, page_size: 25, days: 7});
153
178
  //console.log(JSON.stringify(result));
154
- 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');
155
184
 
156
185
  });