@jambonz/time-series 0.1.5 → 0.1.8
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 +91 -3
- package/package.json +2 -3
- package/test/docker-compose-testbed.yaml +1 -1
- package/test/unit-tests.js +20 -2
package/index.js
CHANGED
|
@@ -12,7 +12,8 @@ const AlertType = {
|
|
|
12
12
|
CARRIER_NOT_PROVISIONED: 'no-carrier',
|
|
13
13
|
CALL_LIMIT: 'call-limit',
|
|
14
14
|
DEVICE_LIMIT: 'device-limit',
|
|
15
|
-
API_LIMIT: 'api-limit'
|
|
15
|
+
API_LIMIT: 'api-limit',
|
|
16
|
+
ACCOUNT_INACTIVE: 'account is inactive or suspended'
|
|
16
17
|
};
|
|
17
18
|
|
|
18
19
|
const schemas = {
|
|
@@ -27,7 +28,8 @@ const schemas = {
|
|
|
27
28
|
duration: Influx.FieldType.INTEGER,
|
|
28
29
|
terminated_at: Influx.FieldType.INTEGER,
|
|
29
30
|
termination_reason: Influx.FieldType.STRING,
|
|
30
|
-
remote_host: Influx.FieldType.STRING
|
|
31
|
+
remote_host: Influx.FieldType.STRING,
|
|
32
|
+
trace_id: Influx.FieldType.STRING
|
|
31
33
|
},
|
|
32
34
|
tags: [
|
|
33
35
|
'account_sid',
|
|
@@ -47,6 +49,15 @@ const schemas = {
|
|
|
47
49
|
'account_sid',
|
|
48
50
|
'alert_type'
|
|
49
51
|
]
|
|
52
|
+
},
|
|
53
|
+
call_counts: {
|
|
54
|
+
measurement: 'call_counts',
|
|
55
|
+
fields: {
|
|
56
|
+
calls_in_progress: Influx.FieldType.INTEGER,
|
|
57
|
+
},
|
|
58
|
+
tags: [
|
|
59
|
+
'account_sid'
|
|
60
|
+
]
|
|
50
61
|
}
|
|
51
62
|
};
|
|
52
63
|
|
|
@@ -68,9 +79,33 @@ const writeData = async(client) => {
|
|
|
68
79
|
}
|
|
69
80
|
};
|
|
70
81
|
|
|
82
|
+
const createCallCountsQuery = ({account_sid, page, page_size, days, start, end}) => {
|
|
83
|
+
let sql = `SELECT * from call_counts WHERE account_sid = '${account_sid}'`;
|
|
84
|
+
if (days) sql += `AND time > now() - ${days}d `;
|
|
85
|
+
else {
|
|
86
|
+
if (start) sql += `AND time >= '${start}' `;
|
|
87
|
+
if (end) sql += `AND time <= '${end}' `;
|
|
88
|
+
}
|
|
89
|
+
sql += ' ORDER BY time DESC';
|
|
90
|
+
if (page_size) sql += ` LIMIT ${page_size}`;
|
|
91
|
+
if (page) sql += ` OFFSET ${(page - 1) * page_size}`;
|
|
92
|
+
//console.log(sql);
|
|
93
|
+
return sql;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const createCallCountsCountQuery = ({account_sid, days, start, end}) => {
|
|
97
|
+
let sql = `SELECT COUNT(calls_in_progress) from call_counts WHERE account_sid = '${account_sid}' `;
|
|
98
|
+
if (days) sql += `AND time > now() - ${days}d `;
|
|
99
|
+
else {
|
|
100
|
+
if (start) sql += `AND time >= '${start}' `;
|
|
101
|
+
if (end) sql += `AND time <= '${end}' `;
|
|
102
|
+
}
|
|
103
|
+
//console.log(sql);
|
|
104
|
+
return sql;
|
|
105
|
+
};
|
|
71
106
|
|
|
72
107
|
const createCdrQuery = ({account_sid, page, page_size, trunk, direction, answered, days, start, end}) => {
|
|
73
|
-
let sql = `SELECT * from cdrs WHERE account_sid = '${account_sid}'
|
|
108
|
+
let sql = `SELECT * from cdrs WHERE account_sid = '${account_sid}'`;
|
|
74
109
|
if (trunk) sql += `AND trunk = '${trunk}' `;
|
|
75
110
|
if (direction) sql += `AND direction = '${direction}' `;
|
|
76
111
|
if (['true', 'false'].includes(answered)) sql += `AND answered = '${answered}' `;
|
|
@@ -134,6 +169,57 @@ const initDatabase = async(client, dbName) => {
|
|
|
134
169
|
client.locals.initialized = true;
|
|
135
170
|
};
|
|
136
171
|
|
|
172
|
+
const writeCallCount = async(client, count) => {
|
|
173
|
+
if (!client.locals.initialized) await initDatabase(client, 'call_counts');
|
|
174
|
+
const {account_sid, ...fields} = count;
|
|
175
|
+
const data = {
|
|
176
|
+
measurement: 'call_counts',
|
|
177
|
+
fields,
|
|
178
|
+
tags: {
|
|
179
|
+
account_sid
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
client.locals.data = [...client.locals.data, ...[data]];
|
|
183
|
+
if (client.locals.data.length >= client.locals.commitSize) {
|
|
184
|
+
await writeData(client);
|
|
185
|
+
}
|
|
186
|
+
return;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const queryCallCounts = async(client, opts) => {
|
|
190
|
+
if (!client.locals.initialized) await initDatabase(client, 'call_counts');
|
|
191
|
+
const response = {
|
|
192
|
+
total: 0,
|
|
193
|
+
page_size: opts.page_size,
|
|
194
|
+
page: opts.page,
|
|
195
|
+
data: []
|
|
196
|
+
};
|
|
197
|
+
const sqlTotal = createCallCountsCountQuery(opts);
|
|
198
|
+
const obj = await client.queryRaw(sqlTotal);
|
|
199
|
+
//console.log(`sqlTotal: ${sqlTotal}, results: ${JSON.stringify(obj)}`);
|
|
200
|
+
if (!obj.results || !obj.results[0].series) return response;
|
|
201
|
+
response.total = obj.results[0].series[0].values[0][1];
|
|
202
|
+
|
|
203
|
+
const sql = createCallCountsQuery(opts);
|
|
204
|
+
const res = await client.queryRaw(sql);
|
|
205
|
+
//console.log(`sql: ${sqlTotal}, results: ${JSON.stringify(res)}`);
|
|
206
|
+
if (res.results[0].series && res.results[0].series.length) {
|
|
207
|
+
const {columns, values} = res.results[0].series[0];
|
|
208
|
+
const data = values.map((v) => {
|
|
209
|
+
const obj = {};
|
|
210
|
+
v.forEach((val, idx) => {
|
|
211
|
+
v.forEach((val, idx) => {
|
|
212
|
+
const key = columns[idx];
|
|
213
|
+
obj[key] = val;
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
return obj;
|
|
217
|
+
});
|
|
218
|
+
response.data = data;
|
|
219
|
+
}
|
|
220
|
+
return response;
|
|
221
|
+
};
|
|
222
|
+
|
|
137
223
|
const writeCdrs = async(client, cdrs) => {
|
|
138
224
|
if (!client.locals.initialized) await initDatabase(client, 'cdrs');
|
|
139
225
|
cdrs = (Array.isArray(cdrs) ? cdrs : [cdrs])
|
|
@@ -317,6 +403,8 @@ module.exports = (logger, opts) => {
|
|
|
317
403
|
}
|
|
318
404
|
|
|
319
405
|
return {
|
|
406
|
+
writeCallCount: writeCallCount.bind(null, cdrClient),
|
|
407
|
+
queryCallCounts: queryCallCounts.bind(null, cdrClient),
|
|
320
408
|
writeCdrs: writeCdrs.bind(null, cdrClient),
|
|
321
409
|
queryCdrs: queryCdrs.bind(null, cdrClient),
|
|
322
410
|
writeAlerts: writeAlerts.bind(null, alertClient),
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jambonz/time-series",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "write and query data to time series daetabase",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "node test/
|
|
7
|
+
"test": "node test/",
|
|
8
8
|
"coverage": "./node_modules/.bin/nyc --reporter html --report-dir ./coverage npm run test",
|
|
9
9
|
"jslint": "eslint index.js"
|
|
10
10
|
},
|
|
@@ -18,7 +18,6 @@
|
|
|
18
18
|
"eslint": "^7.23.0",
|
|
19
19
|
"eslint-plugin-promise": "^4.3.1",
|
|
20
20
|
"nyc": "^15.1.0",
|
|
21
|
-
"tap-spec": "^5.0.0",
|
|
22
21
|
"tape": "^5.2.2"
|
|
23
22
|
}
|
|
24
23
|
}
|
package/test/unit-tests.js
CHANGED
|
@@ -3,6 +3,8 @@ const Influx = require('influx');
|
|
|
3
3
|
const consoleLogger = {error: console.error, info: console.log, debug: console.log};
|
|
4
4
|
|
|
5
5
|
const {
|
|
6
|
+
writeCallCount,
|
|
7
|
+
queryCallCounts,
|
|
6
8
|
writeCdrs,
|
|
7
9
|
queryCdrs,
|
|
8
10
|
writeAlerts,
|
|
@@ -10,7 +12,7 @@ const {
|
|
|
10
12
|
AlertType
|
|
11
13
|
} = require('..')(consoleLogger, '127.0.0.1', {commitSize: 1});
|
|
12
14
|
|
|
13
|
-
test('write
|
|
15
|
+
test('write timeseries data', async(t) => {
|
|
14
16
|
let result = await writeCdrs([{
|
|
15
17
|
from: 'me',
|
|
16
18
|
to: 'you',
|
|
@@ -115,6 +117,22 @@ test('write cdr data', async(t) => {
|
|
|
115
117
|
|
|
116
118
|
result = await queryAlerts({account_sid: 'yyyy', page: 1, page_size: 25, days: 7});
|
|
117
119
|
//console.log(JSON.stringify(result));
|
|
118
|
-
t.ok(result.data.length === 11, 'queried alerts')
|
|
120
|
+
t.ok(result.data.length === 11, 'queried alerts');
|
|
121
|
+
|
|
122
|
+
result = await writeCallCount(
|
|
123
|
+
{
|
|
124
|
+
calls_in_progress: 49,
|
|
125
|
+
account_sid: 'yyyy'
|
|
126
|
+
});
|
|
127
|
+
result = await writeCallCount(
|
|
128
|
+
{
|
|
129
|
+
calls_in_progress: 50,
|
|
130
|
+
account_sid: 'yyyy'
|
|
131
|
+
});
|
|
132
|
+
t.pass('wrote call counts');
|
|
133
|
+
|
|
134
|
+
result = await queryCallCounts({account_sid: 'yyyy', page: 1, page_size: 25, days: 7});
|
|
135
|
+
//console.log(JSON.stringify(result));
|
|
136
|
+
t.ok(result.data.length === 2, 'queried call counts');
|
|
119
137
|
|
|
120
138
|
});
|