@jambonz/time-series 0.1.10 → 0.1.11

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.
Files changed (2) hide show
  1. package/index.js +62 -58
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -80,87 +80,81 @@ const writeData = async(client) => {
80
80
  }
81
81
  };
82
82
 
83
- const createCallCountsQuery = ({account_sid, page, page_size, days, start, end}) => {
84
- let sql = `SELECT * from call_counts WHERE account_sid = '${account_sid}'`;
85
- if (days) sql += `AND time > now() - ${days}d `;
83
+ const createCallCountsQuery = ({page, page_size, days, start, end}) => {
84
+ let sql = 'SELECT * from call_counts WHERE account_sid = $account_sid ';
85
+ if (days) sql += 'AND time > $timestamp ';
86
86
  else {
87
- if (start) sql += `AND time >= '${start}' `;
88
- if (end) sql += `AND time <= '${end}' `;
87
+ if (start) sql += 'AND time >= $start ';
88
+ if (end) sql += 'AND time <= $end ';
89
89
  }
90
90
  sql += ' ORDER BY time DESC';
91
- if (page_size) sql += ` LIMIT ${page_size}`;
92
- if (page) sql += ` OFFSET ${(page - 1) * page_size}`;
93
- //console.log(sql);
91
+ if (page_size) sql += ' LIMIT $page_size';
92
+ if (page) sql += ' OFFSET $offset';
94
93
  return sql;
95
94
  };
96
95
 
97
- const createCallCountsCountQuery = ({account_sid, days, start, end}) => {
98
- let sql = `SELECT COUNT(calls_in_progress) from call_counts WHERE account_sid = '${account_sid}' `;
99
- if (days) sql += `AND time > now() - ${days}d `;
96
+ const createCallCountsCountQuery = ({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 > $timestamp ';
100
99
  else {
101
- if (start) sql += `AND time >= '${start}' `;
102
- if (end) sql += `AND time <= '${end}' `;
100
+ if (start) sql += 'AND time >= $start ';
101
+ if (end) sql += 'AND time <= $end ';
103
102
  }
104
- //console.log(sql);
105
103
  return sql;
106
104
  };
107
105
 
108
- const createCdrQuery = ({account_sid, page, page_size, trunk, direction, answered, days, start, end}) => {
109
- let sql = `SELECT * from cdrs WHERE account_sid = '${account_sid}'`;
110
- if (trunk) sql += `AND trunk = '${trunk}' `;
111
- if (direction) sql += `AND direction = '${direction}' `;
112
- if (['true', 'false'].includes(answered)) sql += `AND answered = '${answered}' `;
113
- if (days) sql += `AND time > now() - ${days}d `;
106
+ const createCdrQuery = ({page, page_size, trunk, direction, answered, days, start, end}) => {
107
+ let sql = 'SELECT * from cdrs WHERE account_sid = $account_sid ';
108
+ if (trunk) sql += 'AND trunk = $trunk ';
109
+ if (direction) sql += 'AND direction = $direction ';
110
+ if (['true', 'false'].includes(answered)) sql += 'AND answered = $answered ';
111
+ if (days) sql += 'AND time > $timestamp ';
114
112
  else {
115
- if (start) sql += `AND time >= '${start}' `;
116
- if (end) sql += `AND time <= '${end}' `;
113
+ if (start) sql += 'AND time >= $start ';
114
+ if (end) sql += 'AND time <= $end ';
117
115
  }
118
116
  sql += ' ORDER BY time DESC';
119
- if (page_size) sql += ` LIMIT ${page_size}`;
120
- if (page) sql += ` OFFSET ${(page - 1) * page_size}`;
121
- //console.log(sql);
117
+ if (page_size) sql += ' LIMIT $page_size';
118
+ if (page) sql += ' OFFSET $offset';
122
119
  return sql;
123
120
  };
124
- const createCdrCountQuery = ({account_sid, trunk, direction, answered, days, start, end}) => {
125
- let sql = `SELECT COUNT(sip_callid) from cdrs WHERE account_sid = '${account_sid}' `;
126
- if (trunk) sql += `AND trunk = '${trunk}' `;
127
- if (direction) sql += `AND direction = '${direction}' `;
128
- if (['true', 'false'].includes(answered)) sql += `AND answered = '${answered}' `;
129
- if (days) sql += `AND time > now() - ${days}d `;
121
+ const createCdrCountQuery = ({trunk, direction, answered, days, start, end}) => {
122
+ let sql = 'SELECT COUNT(sip_callid) from cdrs WHERE account_sid = $account_sid ';
123
+ if (trunk) sql += 'AND trunk = $trunk ';
124
+ if (direction) sql += 'AND direction = $direction ';
125
+ if (['true', 'false'].includes(answered)) sql += 'AND answered = $answered ';
126
+ if (days) sql += 'AND time > $timestamp ';
130
127
  else {
131
- if (start) sql += `AND time >= '${start}' `;
132
- if (end) sql += `AND time <= '${end}' `;
128
+ if (start) sql += 'AND time >= $start ';
129
+ if (end) sql += 'AND time <= $end ';
133
130
  }
134
- //console.log(sql);
135
131
  return sql;
136
132
  };
137
133
 
138
- const createAlertsQuery = ({account_sid, target_sid, alert_type, page, page_size, days, start, end}) => {
139
- let sql = `SELECT * FROM alerts WHERE account_sid = '${account_sid}' `;
140
- if (target_sid) sql += `AND target_sid = '${target_sid}' `;
141
- if (alert_type) sql += `AND alert_type = '${alert_type}' `;
142
- if (days) sql += `AND time > now() - ${days}d `;
134
+ const createAlertsQuery = ({target_sid, alert_type, page, page_size, days, start, end}) => {
135
+ let sql = 'SELECT * FROM alerts WHERE account_sid = $account_sid ';
136
+ if (target_sid) sql += 'AND target_sid = $target_sid ';
137
+ if (alert_type) sql += 'AND alert_type = $alert_type ';
138
+ if (days) sql += 'AND time > $timestamp ';
143
139
  else {
144
- if (start) sql += `AND time >= '${start}' `;
145
- if (end) sql += `AND time <= '${end}' `;
140
+ if (start) sql += 'AND time >= $start ';
141
+ if (end) sql += 'AND time <= $end ';
146
142
  }
147
143
  sql += ' ORDER BY time DESC';
148
- if (page_size) sql += ` LIMIT ${page_size}`;
149
- if (page) sql += ` OFFSET ${(page - 1) * page_size}`;
150
- //console.log(sql);
144
+ if (page_size) sql += ' LIMIT $page_size';
145
+ if (page) sql += ' OFFSET $offset';
151
146
  return sql;
152
147
  };
153
148
 
154
- const createAlertsCountQuery = ({account_sid, target_sid, alert_type, days, start, end}) => {
155
- let sql = `SELECT COUNT(message) FROM alerts WHERE account_sid = '${account_sid}' `;
156
- if (target_sid) sql += `AND target_sid = '${target_sid}' `;
157
- if (alert_type) sql += `AND alert_type = '${alert_type}' `;
158
- if (days) sql += `AND time > now() - ${days}d `;
149
+ const createAlertsCountQuery = ({target_sid, alert_type, days, start, end}) => {
150
+ let sql = 'SELECT COUNT(message) FROM alerts WHERE account_sid = $account_sid ';
151
+ if (target_sid) sql += 'AND target_sid = $target_sid ';
152
+ if (alert_type) sql += 'AND alert_type = $alert_type ';
153
+ if (days) sql += 'AND time > $timestamp ';
159
154
  else {
160
- if (start) sql += `AND time >= '${start}' `;
161
- if (end) sql += `AND time <= '${end}' `;
155
+ if (start) sql += 'AND time >= $start ';
156
+ if (end) sql += 'AND time <= $end ';
162
157
  }
163
- //console.log(sql);
164
158
  return sql;
165
159
  };
166
160
 
@@ -197,14 +191,15 @@ const queryCallCounts = async(client, opts) => {
197
191
  page: opts.page,
198
192
  data: []
199
193
  };
194
+ const params = generateBindParameters(opts);
200
195
  const sqlTotal = createCallCountsCountQuery(opts);
201
- const obj = await client.queryRaw(sqlTotal);
196
+ const obj = await client.queryRaw(sqlTotal, { placeholders: params});
202
197
  //console.log(`sqlTotal: ${sqlTotal}, results: ${JSON.stringify(obj)}`);
203
198
  if (!obj.results || !obj.results[0].series) return response;
204
199
  response.total = obj.results[0].series[0].values[0][1];
205
200
 
206
201
  const sql = createCallCountsQuery(opts);
207
- const res = await client.queryRaw(sql);
202
+ const res = await client.queryRaw(sql, { placeholders: params});
208
203
  //console.log(`sql: ${sqlTotal}, results: ${JSON.stringify(res)}`);
209
204
  if (res.results[0].series && res.results[0].series.length) {
210
205
  const {columns, values} = res.results[0].series[0];
@@ -257,14 +252,15 @@ const queryCdrs = async(client, opts) => {
257
252
  page: opts.page,
258
253
  data: []
259
254
  };
255
+ const params = generateBindParameters(opts);
260
256
  const sqlTotal = createCdrCountQuery(opts);
261
- const obj = await client.queryRaw(sqlTotal);
257
+ const obj = await client.queryRaw(sqlTotal, { placeholders: params});
262
258
  //console.log(`sql: ${sqlTotal}, results: ${JSON.stringify(obj)}`);
263
259
  if (!obj.results || !obj.results[0].series) return response;
264
260
  response.total = obj.results[0].series[0].values[0][1];
265
261
 
266
262
  const sql = createCdrQuery(opts);
267
- const res = await client.queryRaw(sql);
263
+ const res = await client.queryRaw(sql, { placeholders: params});
268
264
  if (res.results[0].series && res.results[0].series.length) {
269
265
  const {columns, values} = res.results[0].series[0];
270
266
  const data = values.map((v) => {
@@ -350,6 +346,13 @@ const writeAlerts = async(client, alerts) => {
350
346
  return;
351
347
  };
352
348
 
349
+ const generateBindParameters = (opts) => {
350
+ const params = {...opts};
351
+ if (opts.days) params.timestamp = Date.now() * 1000000 - opts.days * 24 * 60 * 60 * 1000000000;
352
+ if (opts.page) params.offset = opts.page - 1 >= 0 && opts.page_size >= 0 ? (opts.page - 1) * opts.page_size : 0;
353
+ return params;
354
+ };
355
+
353
356
  const queryAlerts = async(client, opts) => {
354
357
  if (!client.locals.initialized) await initDatabase(client, 'alerts');
355
358
  const response = {
@@ -358,14 +361,15 @@ const queryAlerts = async(client, opts) => {
358
361
  page: opts.page,
359
362
  data: []
360
363
  };
364
+ const params = generateBindParameters(opts);
361
365
  const sqlTotal = createAlertsCountQuery(opts);
362
- const obj = await client.queryRaw(sqlTotal);
366
+ const obj = await client.queryRaw(sqlTotal, { placeholders: params});
363
367
  //console.log(`query total alerts: ${sqlTotal}: ${JSON.stringify(obj)}`);
364
368
  if (!obj.results || !obj.results[0].series) return response;
365
369
  response.total = obj.results[0].series[0].values[0][1];
366
370
 
367
371
  const sql = createAlertsQuery(opts);
368
- const res = await client.queryRaw(sql);
372
+ const res = await client.queryRaw(sql, { placeholders: params});
369
373
  if (res.results[0].series && res.results[0].series.length) {
370
374
  const {columns, values} = res.results[0].series[0];
371
375
  const data = values.map((v) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jambonz/time-series",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "write and query data to time series daetabase",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -12,7 +12,7 @@
12
12
  "license": "MIT",
13
13
  "dependencies": {
14
14
  "debug": "^4.3.1",
15
- "influx": "^5.8.0"
15
+ "influx": "^5.9.3"
16
16
  },
17
17
  "devDependencies": {
18
18
  "eslint": "^7.23.0",