@jambonz/time-series 0.1.7 → 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 +87 -1
- package/package.json +1 -1
- package/test/unit-tests.js +20 -2
package/index.js
CHANGED
|
@@ -49,6 +49,15 @@ const schemas = {
|
|
|
49
49
|
'account_sid',
|
|
50
50
|
'alert_type'
|
|
51
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
|
+
]
|
|
52
61
|
}
|
|
53
62
|
};
|
|
54
63
|
|
|
@@ -70,9 +79,33 @@ const writeData = async(client) => {
|
|
|
70
79
|
}
|
|
71
80
|
};
|
|
72
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
|
+
};
|
|
73
106
|
|
|
74
107
|
const createCdrQuery = ({account_sid, page, page_size, trunk, direction, answered, days, start, end}) => {
|
|
75
|
-
let sql = `SELECT * from cdrs WHERE account_sid = '${account_sid}'
|
|
108
|
+
let sql = `SELECT * from cdrs WHERE account_sid = '${account_sid}'`;
|
|
76
109
|
if (trunk) sql += `AND trunk = '${trunk}' `;
|
|
77
110
|
if (direction) sql += `AND direction = '${direction}' `;
|
|
78
111
|
if (['true', 'false'].includes(answered)) sql += `AND answered = '${answered}' `;
|
|
@@ -136,6 +169,57 @@ const initDatabase = async(client, dbName) => {
|
|
|
136
169
|
client.locals.initialized = true;
|
|
137
170
|
};
|
|
138
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
|
+
|
|
139
223
|
const writeCdrs = async(client, cdrs) => {
|
|
140
224
|
if (!client.locals.initialized) await initDatabase(client, 'cdrs');
|
|
141
225
|
cdrs = (Array.isArray(cdrs) ? cdrs : [cdrs])
|
|
@@ -319,6 +403,8 @@ module.exports = (logger, opts) => {
|
|
|
319
403
|
}
|
|
320
404
|
|
|
321
405
|
return {
|
|
406
|
+
writeCallCount: writeCallCount.bind(null, cdrClient),
|
|
407
|
+
queryCallCounts: queryCallCounts.bind(null, cdrClient),
|
|
322
408
|
writeCdrs: writeCdrs.bind(null, cdrClient),
|
|
323
409
|
queryCdrs: queryCdrs.bind(null, cdrClient),
|
|
324
410
|
writeAlerts: writeAlerts.bind(null, alertClient),
|
package/package.json
CHANGED
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
|
});
|