@jambonz/time-series 0.1.9 → 0.1.10
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 +12 -4
- package/package.json +1 -1
- package/test/unit-tests.js +12 -1
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@ const AlertType = {
|
|
|
5
5
|
WEBHOOK_CONNECTION_FAILURE: 'webhook-connection-failure',
|
|
6
6
|
WEBHOOK_URL_NOTFOUND: 'webhook-url-notfound',
|
|
7
7
|
WEBHOOK_AUTH_FAILURE: 'webhook-auth-failure',
|
|
8
|
+
INVALID_APP_PAYLOAD: 'invalid-app-payload',
|
|
8
9
|
TTS_NOT_PROVISIONED: 'no-tts',
|
|
9
10
|
STT_NOT_PROVISIONED: 'no-stt',
|
|
10
11
|
TTS_FAILURE: 'tts-failure',
|
|
@@ -134,8 +135,9 @@ const createCdrCountQuery = ({account_sid, trunk, direction, answered, days, sta
|
|
|
134
135
|
return sql;
|
|
135
136
|
};
|
|
136
137
|
|
|
137
|
-
const createAlertsQuery = ({account_sid, alert_type, page, page_size, days, start, end}) => {
|
|
138
|
+
const createAlertsQuery = ({account_sid, target_sid, alert_type, page, page_size, days, start, end}) => {
|
|
138
139
|
let sql = `SELECT * FROM alerts WHERE account_sid = '${account_sid}' `;
|
|
140
|
+
if (target_sid) sql += `AND target_sid = '${target_sid}' `;
|
|
139
141
|
if (alert_type) sql += `AND alert_type = '${alert_type}' `;
|
|
140
142
|
if (days) sql += `AND time > now() - ${days}d `;
|
|
141
143
|
else {
|
|
@@ -149,8 +151,9 @@ const createAlertsQuery = ({account_sid, alert_type, page, page_size, days, star
|
|
|
149
151
|
return sql;
|
|
150
152
|
};
|
|
151
153
|
|
|
152
|
-
const createAlertsCountQuery = ({account_sid, alert_type, days, start, end}) => {
|
|
154
|
+
const createAlertsCountQuery = ({account_sid, target_sid, alert_type, days, start, end}) => {
|
|
153
155
|
let sql = `SELECT COUNT(message) FROM alerts WHERE account_sid = '${account_sid}' `;
|
|
156
|
+
if (target_sid) sql += `AND target_sid = '${target_sid}' `;
|
|
154
157
|
if (alert_type) sql += `AND alert_type = '${alert_type}' `;
|
|
155
158
|
if (days) sql += `AND time > now() - ${days}d `;
|
|
156
159
|
else {
|
|
@@ -284,7 +287,7 @@ const writeAlerts = async(client, alerts) => {
|
|
|
284
287
|
if (!client.locals.initialized) await initDatabase(client, 'alerts');
|
|
285
288
|
alerts = (Array.isArray(alerts) ? alerts : [alerts])
|
|
286
289
|
.map((alert) => {
|
|
287
|
-
const {alert_type, account_sid, url, status, vendor, count, detail, timestamp} = alert;
|
|
290
|
+
const {alert_type, account_sid, target_sid, url, status, vendor, count, detail, timestamp} = alert;
|
|
288
291
|
let message = alert.message;
|
|
289
292
|
if (!message) {
|
|
290
293
|
switch (alert_type) {
|
|
@@ -300,6 +303,9 @@ const writeAlerts = async(client, alerts) => {
|
|
|
300
303
|
case AlertType.WEBHOOK_URL_NOTFOUND:
|
|
301
304
|
message = `webhook url not found: ${url}`;
|
|
302
305
|
break;
|
|
306
|
+
case AlertType.INVALID_APP_PAYLOAD:
|
|
307
|
+
message = `${url} return invalid app payload`;
|
|
308
|
+
break;
|
|
303
309
|
case AlertType.TTS_NOT_PROVISIONED:
|
|
304
310
|
message = `text to speech credentials for ${vendor} have not been provisioned`;
|
|
305
311
|
break;
|
|
@@ -329,7 +335,9 @@ const writeAlerts = async(client, alerts) => {
|
|
|
329
335
|
break;
|
|
330
336
|
}
|
|
331
337
|
}
|
|
332
|
-
|
|
338
|
+
let fields = { message };
|
|
339
|
+
if (target_sid) fields = Object.assign(fields, {target_sid});
|
|
340
|
+
const obj = {measurement: 'alerts', fields: fields, tags: { alert_type, account_sid }};
|
|
333
341
|
if (timestamp) obj.timestamp = timestamp;
|
|
334
342
|
if (detail) obj.fields.detail = detail;
|
|
335
343
|
return obj;
|
package/package.json
CHANGED
package/test/unit-tests.js
CHANGED
|
@@ -73,6 +73,12 @@ test('write timeseries data', async(t) => {
|
|
|
73
73
|
account_sid: 'yyyy',
|
|
74
74
|
url: 'http://foo.bar'
|
|
75
75
|
},
|
|
76
|
+
{
|
|
77
|
+
alert_type: AlertType.INVALID_APP_PAYLOAD,
|
|
78
|
+
account_sid: 'yyyy',
|
|
79
|
+
target_sid: 'zzzz',
|
|
80
|
+
message: 'invalid app payload'
|
|
81
|
+
},
|
|
76
82
|
{
|
|
77
83
|
alert_type: AlertType.TTS_NOT_PROVISIONED,
|
|
78
84
|
account_sid: 'yyyy',
|
|
@@ -117,7 +123,12 @@ test('write timeseries data', async(t) => {
|
|
|
117
123
|
|
|
118
124
|
result = await queryAlerts({account_sid: 'yyyy', page: 1, page_size: 25, days: 7});
|
|
119
125
|
//console.log(JSON.stringify(result));
|
|
120
|
-
t.ok(result.data.length ===
|
|
126
|
+
t.ok(result.data.length === 12, 'queried alerts');
|
|
127
|
+
t.ok(result.data[0].target_sid === null)
|
|
128
|
+
|
|
129
|
+
result = await queryAlerts({account_sid: 'yyyy', target_sid: 'zzzz', page: 1, page_size: 25, days: 7});
|
|
130
|
+
t.ok(result.data.length === 1, 'queried alerts by target_sid');
|
|
131
|
+
t.ok(result.data[0].target_sid === 'zzzz')
|
|
121
132
|
|
|
122
133
|
result = await writeCallCount(
|
|
123
134
|
{
|