@jambonz/time-series 0.2.0 → 0.2.1

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
 
@@ -62,6 +65,15 @@ const schemas = {
62
65
  'service_provider_sid',
63
66
  'account_sid'
64
67
  ]
68
+ },
69
+ sp_call_counts: {
70
+ measurement: 'sp_call_counts',
71
+ fields: {
72
+ calls_in_progress: Influx.FieldType.INTEGER,
73
+ },
74
+ tags: [
75
+ 'service_provider_sid'
76
+ ]
65
77
  }
66
78
  };
67
79
 
@@ -85,7 +97,7 @@ const writeData = async(client) => {
85
97
 
86
98
  /* for Service Provider */
87
99
  const createCallCountsQuerySP = ({page, page_size, days, start, end}) => {
88
- let sql = 'SELECT * from call_counts WHERE service_provider_sid = $service_provider_sid ';
100
+ let sql = 'SELECT * from sp_call_counts WHERE service_provider_sid = $service_provider_sid ';
89
101
  if (days) sql += 'AND time > $timestamp ';
90
102
  else {
91
103
  if (start) sql += 'AND time >= $start ';
@@ -98,7 +110,7 @@ const createCallCountsQuerySP = ({page, page_size, days, start, end}) => {
98
110
  };
99
111
 
100
112
  const createCallCountsCountQuerySP = ({days, start, end}) => {
101
- let sql = 'SELECT COUNT(calls_in_progress) from call_counts WHERE service_provider_sid = $service_provider_sid ';
113
+ let sql = 'SELECT COUNT(calls_in_progress) from sp_call_counts WHERE service_provider_sid = $service_provider_sid ';
102
114
  if (days) sql += 'AND time > $timestamp ';
103
115
  else {
104
116
  if (start) sql += 'AND time >= $start ';
@@ -191,7 +203,8 @@ const createCdrCountQuery = ({trunk, direction, answered, days, start, end}) =>
191
203
 
192
204
  /* for Service Provider */
193
205
  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 ';
206
+ // eslint-disable-next-line max-len
207
+ let sql = 'SELECT service_provider_sid, message, detail FROM alerts WHERE service_provider_sid = $service_provider_sid ';
195
208
  if (target_sid) sql += 'AND target_sid = $target_sid ';
196
209
  if (alert_type) sql += 'AND alert_type = $alert_type ';
197
210
  if (days) sql += 'AND time > $timestamp ';
@@ -271,8 +284,25 @@ const writeCallCount = async(client, count) => {
271
284
  return;
272
285
  };
273
286
 
287
+ const writeCallCountSP = async(client, count) => {
288
+ if (!client.locals.initialized) await initDatabase(client, 'sp_call_counts');
289
+ const {service_provider_sid, ...fields} = count;
290
+ const data = {
291
+ measurement: 'sp_call_counts',
292
+ fields,
293
+ tags: {
294
+ service_provider_sid
295
+ }
296
+ };
297
+ client.locals.data = [...client.locals.data, ...[data]];
298
+ if (client.locals.data.length >= client.locals.commitSize) {
299
+ await writeData(client);
300
+ }
301
+ return;
302
+ };
303
+
274
304
  const queryCallCountsSP = async(client, opts) => {
275
- if (!client.locals.initialized) await initDatabase(client, 'call_counts');
305
+ if (!client.locals.initialized) await initDatabase(client, 'sp_call_counts');
276
306
  const response = {
277
307
  total: 0,
278
308
  page_size: opts.page_size,
@@ -487,15 +517,29 @@ const writeAlerts = async(client, alerts) => {
487
517
  case AlertType.CARRIER_NOT_PROVISIONED:
488
518
  message = 'outbound call failure: no carriers have been provisioned';
489
519
  break;
490
- case AlertType.CALL_LIMIT:
491
- message = `you have exceeded your provisioned call limit of ${count}; please consider upgrading your plan`;
520
+ case AlertType.ACCOUNT_CALL_LIMIT:
521
+ message = `you have exceeded your account call limit of ${count}; please consider upgrading your plan`;
522
+ break;
523
+ case AlertType.ACCOUNT_DEVICE_LIMIT:
524
+ message =
525
+ // eslint-disable-next-line max-len
526
+ `you have exceeded your account limit of ${count} registered devices; please consider upgrading your plan`;
527
+ break;
528
+ case AlertType.ACCOUNT_API_LIMIT:
529
+ message = `you have exceeded your account api limit of ${count}; please consider upgrading your plan`;
492
530
  break;
493
- case AlertType.DEVICE_LIMIT:
531
+ case AlertType.SP_CALL_LIMIT:
532
+ // eslint-disable-next-line max-len
533
+ message = `you have exceeded your service provider call limit of ${count}; please consider upgrading your plan`;
534
+ break;
535
+ case AlertType.SP_DEVICE_LIMIT:
494
536
  message =
495
- `you have exceeded your device registration limit of ${count}; please consider upgrading your plan`;
537
+ // eslint-disable-next-line max-len
538
+ `you have exceeded your service provider limit of ${count} registered devices; please consider upgrading your plan`;
496
539
  break;
497
- case AlertType.API_LIMIT:
498
- message = `you have exceeded your api limit of ${count}; please consider upgrading your plan`;
540
+ case AlertType.SP_API_LIMIT:
541
+ // eslint-disable-next-line max-len
542
+ message = `you have exceeded your service provider api limit of ${count}; please consider upgrading your plan`;
499
543
  break;
500
544
  default:
501
545
  break;
@@ -595,6 +639,7 @@ module.exports = (logger, opts) => {
595
639
  const cdrClient = new Influx.InfluxDB({database: 'cdrs', schemas: schemas.cdr, ...opts});
596
640
  const alertClient = new Influx.InfluxDB({database: 'alerts', schemas: schemas.alerts, ...opts});
597
641
  const callCountClient = new Influx.InfluxDB({database: 'call_counts', schemas: schemas.call_counts, ...opts});
642
+ const callCountSPClient = new Influx.InfluxDB({database: 'sp_call_counts', schemas: schemas.sp_call_counts, ...opts});
598
643
 
599
644
  cdrClient.locals = {
600
645
  db: 'cdrs',
@@ -612,6 +657,14 @@ module.exports = (logger, opts) => {
612
657
  commitInterval: opts.commitInterval || 10,
613
658
  data: []
614
659
  };
660
+ callCountSPClient.locals = {
661
+ db: 'sp_call_counts',
662
+ initialized: false,
663
+ writing: false,
664
+ commitSize: opts.commitSize || 1,
665
+ commitInterval: opts.commitInterval || 10,
666
+ data: []
667
+ };
615
668
  callCountClient.locals = {
616
669
  db: 'call_counts',
617
670
  initialized: false,
@@ -629,8 +682,9 @@ module.exports = (logger, opts) => {
629
682
 
630
683
  return {
631
684
  writeCallCount: writeCallCount.bind(null, callCountClient),
685
+ writeCallCountSP: writeCallCountSP.bind(null, callCountSPClient),
632
686
  queryCallCounts: queryCallCounts.bind(null, callCountClient),
633
- queryCallCountsSP: queryCallCountsSP.bind(null, callCountClient),
687
+ queryCallCountsSP: queryCallCountsSP.bind(null, callCountSPClient),
634
688
  writeCdrs: writeCdrs.bind(null, cdrClient),
635
689
  queryCdrsSP: queryCdrsSP.bind(null, cdrClient),
636
690
  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.1",
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,
@@ -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
  });