@jambonz/time-series 0.2.0 → 0.2.2

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
@@ -11,9 +11,12 @@ const AlertType = {
11
11
  TTS_FAILURE: 'tts-failure',
12
12
  STT_FAILURE: 'stt-failure',
13
13
  CARRIER_NOT_PROVISIONED: 'no-carrier',
14
- CALL_LIMIT: 'call-limit',
15
- DEVICE_LIMIT: 'device-limit',
16
- API_LIMIT: 'api-limit',
14
+ ACCOUNT_CALL_LIMIT: 'account-call-limit',
15
+ ACCOUNT_DEVICE_LIMIT: 'account-device-limit',
16
+ ACCOUNT_API_LIMIT: 'account-api-limit',
17
+ SP_CALL_LIMIT: 'service-provider-call-limit',
18
+ SP_DEVICE_LIMIT: 'service-provider-device-limit',
19
+ SP_API_LIMIT: 'service-provider-api-limit',
17
20
  ACCOUNT_INACTIVE: 'account is inactive or suspended'
18
21
  };
19
22
 
@@ -22,6 +25,7 @@ const schemas = {
22
25
  measurement: 'cdrs',
23
26
  fields: {
24
27
  call_sid: Influx.FieldType.STRING,
28
+ application_sid: Influx.FieldType.STRING,
25
29
  from: Influx.FieldType.STRING,
26
30
  to: Influx.FieldType.STRING,
27
31
  sip_callid: Influx.FieldType.STRING,
@@ -62,6 +66,15 @@ const schemas = {
62
66
  'service_provider_sid',
63
67
  'account_sid'
64
68
  ]
69
+ },
70
+ sp_call_counts: {
71
+ measurement: 'sp_call_counts',
72
+ fields: {
73
+ calls_in_progress: Influx.FieldType.INTEGER,
74
+ },
75
+ tags: [
76
+ 'service_provider_sid'
77
+ ]
65
78
  }
66
79
  };
67
80
 
@@ -85,7 +98,7 @@ const writeData = async(client) => {
85
98
 
86
99
  /* for Service Provider */
87
100
  const createCallCountsQuerySP = ({page, page_size, days, start, end}) => {
88
- let sql = 'SELECT * from call_counts WHERE service_provider_sid = $service_provider_sid ';
101
+ let sql = 'SELECT * from sp_call_counts WHERE service_provider_sid = $service_provider_sid ';
89
102
  if (days) sql += 'AND time > $timestamp ';
90
103
  else {
91
104
  if (start) sql += 'AND time >= $start ';
@@ -98,7 +111,7 @@ const createCallCountsQuerySP = ({page, page_size, days, start, end}) => {
98
111
  };
99
112
 
100
113
  const createCallCountsCountQuerySP = ({days, start, end}) => {
101
- let sql = 'SELECT COUNT(calls_in_progress) from call_counts WHERE service_provider_sid = $service_provider_sid ';
114
+ let sql = 'SELECT COUNT(calls_in_progress) from sp_call_counts WHERE service_provider_sid = $service_provider_sid ';
102
115
  if (days) sql += 'AND time > $timestamp ';
103
116
  else {
104
117
  if (start) sql += 'AND time >= $start ';
@@ -191,7 +204,8 @@ const createCdrCountQuery = ({trunk, direction, answered, days, start, end}) =>
191
204
 
192
205
  /* for Service Provider */
193
206
  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 ';
207
+ // eslint-disable-next-line max-len
208
+ let sql = 'SELECT service_provider_sid, message, detail FROM alerts WHERE service_provider_sid = $service_provider_sid ';
195
209
  if (target_sid) sql += 'AND target_sid = $target_sid ';
196
210
  if (alert_type) sql += 'AND alert_type = $alert_type ';
197
211
  if (days) sql += 'AND time > $timestamp ';
@@ -271,8 +285,25 @@ const writeCallCount = async(client, count) => {
271
285
  return;
272
286
  };
273
287
 
288
+ const writeCallCountSP = async(client, count) => {
289
+ if (!client.locals.initialized) await initDatabase(client, 'sp_call_counts');
290
+ const {service_provider_sid, ...fields} = count;
291
+ const data = {
292
+ measurement: 'sp_call_counts',
293
+ fields,
294
+ tags: {
295
+ service_provider_sid
296
+ }
297
+ };
298
+ client.locals.data = [...client.locals.data, ...[data]];
299
+ if (client.locals.data.length >= client.locals.commitSize) {
300
+ await writeData(client);
301
+ }
302
+ return;
303
+ };
304
+
274
305
  const queryCallCountsSP = async(client, opts) => {
275
- if (!client.locals.initialized) await initDatabase(client, 'call_counts');
306
+ if (!client.locals.initialized) await initDatabase(client, 'sp_call_counts');
276
307
  const response = {
277
308
  total: 0,
278
309
  page_size: opts.page_size,
@@ -420,6 +451,7 @@ const queryCdrs = async(client, opts) => {
420
451
 
421
452
  const sql = createCdrQuery(opts);
422
453
  const res = await client.queryRaw(sql, { placeholders: params});
454
+ //console.log(JSON.stringify(res.results[0]));
423
455
  if (res.results[0].series && res.results[0].series.length) {
424
456
  const {columns, values} = res.results[0].series[0];
425
457
  const data = values.map((v) => {
@@ -487,15 +519,29 @@ const writeAlerts = async(client, alerts) => {
487
519
  case AlertType.CARRIER_NOT_PROVISIONED:
488
520
  message = 'outbound call failure: no carriers have been provisioned';
489
521
  break;
490
- case AlertType.CALL_LIMIT:
491
- message = `you have exceeded your provisioned call limit of ${count}; please consider upgrading your plan`;
522
+ case AlertType.ACCOUNT_CALL_LIMIT:
523
+ message = `you have exceeded your account call limit of ${count}; please consider upgrading your plan`;
524
+ break;
525
+ case AlertType.ACCOUNT_DEVICE_LIMIT:
526
+ message =
527
+ // eslint-disable-next-line max-len
528
+ `you have exceeded your account limit of ${count} registered devices; please consider upgrading your plan`;
529
+ break;
530
+ case AlertType.ACCOUNT_API_LIMIT:
531
+ message = `you have exceeded your account api limit of ${count}; please consider upgrading your plan`;
492
532
  break;
493
- case AlertType.DEVICE_LIMIT:
533
+ case AlertType.SP_CALL_LIMIT:
534
+ // eslint-disable-next-line max-len
535
+ message = `you have exceeded your service provider call limit of ${count}; please consider upgrading your plan`;
536
+ break;
537
+ case AlertType.SP_DEVICE_LIMIT:
494
538
  message =
495
- `you have exceeded your device registration limit of ${count}; please consider upgrading your plan`;
539
+ // eslint-disable-next-line max-len
540
+ `you have exceeded your service provider limit of ${count} registered devices; please consider upgrading your plan`;
496
541
  break;
497
- case AlertType.API_LIMIT:
498
- message = `you have exceeded your api limit of ${count}; please consider upgrading your plan`;
542
+ case AlertType.SP_API_LIMIT:
543
+ // eslint-disable-next-line max-len
544
+ message = `you have exceeded your service provider api limit of ${count}; please consider upgrading your plan`;
499
545
  break;
500
546
  default:
501
547
  break;
@@ -595,6 +641,7 @@ module.exports = (logger, opts) => {
595
641
  const cdrClient = new Influx.InfluxDB({database: 'cdrs', schemas: schemas.cdr, ...opts});
596
642
  const alertClient = new Influx.InfluxDB({database: 'alerts', schemas: schemas.alerts, ...opts});
597
643
  const callCountClient = new Influx.InfluxDB({database: 'call_counts', schemas: schemas.call_counts, ...opts});
644
+ const callCountSPClient = new Influx.InfluxDB({database: 'sp_call_counts', schemas: schemas.sp_call_counts, ...opts});
598
645
 
599
646
  cdrClient.locals = {
600
647
  db: 'cdrs',
@@ -612,6 +659,14 @@ module.exports = (logger, opts) => {
612
659
  commitInterval: opts.commitInterval || 10,
613
660
  data: []
614
661
  };
662
+ callCountSPClient.locals = {
663
+ db: 'sp_call_counts',
664
+ initialized: false,
665
+ writing: false,
666
+ commitSize: opts.commitSize || 1,
667
+ commitInterval: opts.commitInterval || 10,
668
+ data: []
669
+ };
615
670
  callCountClient.locals = {
616
671
  db: 'call_counts',
617
672
  initialized: false,
@@ -629,8 +684,9 @@ module.exports = (logger, opts) => {
629
684
 
630
685
  return {
631
686
  writeCallCount: writeCallCount.bind(null, callCountClient),
687
+ writeCallCountSP: writeCallCountSP.bind(null, callCountSPClient),
632
688
  queryCallCounts: queryCallCounts.bind(null, callCountClient),
633
- queryCallCountsSP: queryCallCountsSP.bind(null, callCountClient),
689
+ queryCallCountsSP: queryCallCountsSP.bind(null, callCountSPClient),
634
690
  writeCdrs: writeCdrs.bind(null, cdrClient),
635
691
  queryCdrsSP: queryCdrsSP.bind(null, cdrClient),
636
692
  queryCdrs: queryCdrs.bind(null, cdrClient),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jambonz/time-series",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "write and query data to time series daetabase",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -4,6 +4,7 @@ const consoleLogger = {error: console.error, info: console.log, debug: console.l
4
4
 
5
5
  const {
6
6
  writeCallCount,
7
+ writeCallCountSP,
7
8
  queryCallCounts,
8
9
  queryCallCountsSP,
9
10
  writeCdrs,
@@ -32,7 +33,6 @@ test('write timeseries data', async(t) => {
32
33
  trunk: 'device',
33
34
  service_provider_sid: 'zzzzz',
34
35
  account_sid: 'xxxx',
35
- call_sid: 'foo'
36
36
  },
37
37
  {
38
38
  from: 'me2',
@@ -50,12 +50,13 @@ test('write timeseries data', async(t) => {
50
50
  trunk: 'twilio',
51
51
  service_provider_sid: 'zzzzz',
52
52
  account_sid: 'yyyy',
53
- call_sid: 'bar'
53
+ call_sid: 'bar',
54
+ application_sid: 'app-sid'
54
55
  }]);
55
56
  t.pass('wrote cdr');
56
57
 
57
58
  result = await queryCdrs({account_sid: 'xxxx', page: 1, page_size:25});
58
- //npm tesconsole.log(JSON.stringify(result));
59
+ //console.log(JSON.stringify(result));
59
60
  t.ok(result.data.length === 1, 'queried cdrs by account sid')
60
61
 
61
62
  result = await queryCdrs({account_sid: 'yyyy', trunk: 'twilio', page: 1, page_size:25});
@@ -121,22 +122,37 @@ test('write timeseries data', async(t) => {
121
122
  account_sid: 'yyyy',
122
123
  },
123
124
  {
124
- alert_type: AlertType.CALL_LIMIT,
125
+ alert_type: AlertType.SP_CALL_LIMIT,
126
+ service_provider_sid: 'zzzzz',
127
+ count: 50,
128
+ },
129
+ {
130
+ alert_type: AlertType.ACCOUNT_CALL_LIMIT,
125
131
  service_provider_sid: 'zzzzz',
126
132
  account_sid: 'yyyy',
127
133
  count: 50,
128
134
  },
129
135
  {
130
- alert_type: AlertType.DEVICE_LIMIT,
136
+ alert_type: AlertType.SP_DEVICE_LIMIT,
137
+ service_provider_sid: 'zzzzz',
138
+ count: 500,
139
+ },
140
+ {
141
+ alert_type: AlertType.ACCOUNT_DEVICE_LIMIT,
131
142
  service_provider_sid: 'zzzzz',
132
143
  account_sid: 'yyyy',
133
144
  count: 250,
134
145
  },
135
146
  {
136
- alert_type: AlertType.API_LIMIT,
147
+ alert_type: AlertType.ACCOUNT_API_LIMIT,
137
148
  service_provider_sid: 'zzzzz',
138
149
  account_sid: 'yyyy',
139
150
  count: 120,
151
+ },
152
+ {
153
+ alert_type: AlertType.SP_API_LIMIT,
154
+ service_provider_sid: 'zzzzz',
155
+ count: 300,
140
156
  }
141
157
  ]);
142
158
  t.pass('wrote alerts');
@@ -158,7 +174,8 @@ test('write timeseries data', async(t) => {
158
174
  t.ok(result.data[0].target_sid === 'zzzz')
159
175
 
160
176
  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');
177
+ //console.log(result);
178
+ t.ok(result.data.length === 15, 'queried alerts by service_provider_sid');
162
179
 
163
180
  result = await writeCallCount(
164
181
  {
@@ -172,14 +189,25 @@ test('write timeseries data', async(t) => {
172
189
  service_provider_sid: 'zzzzz',
173
190
  account_sid: 'yyyy'
174
191
  });
175
- t.pass('wrote call counts');
192
+ t.pass('wrote call counts for account');
176
193
 
177
- result = await queryCallCounts({account_sid: 'yyyy', page: 1, page_size: 25, days: 7});
178
- //console.log(JSON.stringify(result));
179
- t.ok(result.data.length === 2, 'queried call counts by account_sid');
194
+ result = await writeCallCountSP(
195
+ {
196
+ calls_in_progress: 500,
197
+ service_provider_sid: 'zzzzz'
198
+ });
199
+ result = await writeCallCountSP(
200
+ {
201
+ calls_in_progress: 501,
202
+ service_provider_sid: 'zzzzz'
203
+ });
204
+ t.pass('wrote call counts for service provider');
180
205
 
181
206
  result = await queryCallCountsSP({service_provider_sid: 'zzzzz', page: 1, page_size: 25, days: 7});
182
207
  //console.log(JSON.stringify(result));
183
208
  t.ok(result.data.length === 2, 'queried call counts by service provider sid');
184
209
 
210
+ result = await queryCallCounts({account_sid: 'yyyy', page: 1, page_size: 25, days: 7});
211
+ //console.log(JSON.stringify(result));
212
+ t.ok(result.data.length === 2, 'queried call counts by account_sid');
185
213
  });