iobroker.sql 2.2.0 → 3.0.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/lib/aggregate.js CHANGED
@@ -10,7 +10,7 @@ function initAggregate(options) {
10
10
  log = options.log || console.log;
11
11
  }
12
12
 
13
- //step; // 1 Step is 1 second
13
+ // step; // 1 Step is 1 second
14
14
  if (options.step === null) {
15
15
  options.step = (options.end - options.start) / options.count;
16
16
  }
@@ -54,7 +54,7 @@ function initAggregate(options) {
54
54
  err.message += `: ${options.maxIndex + 2}`;
55
55
  throw err;
56
56
  }
57
- // We define the array length but do not prefill values, we do that on runtime when needed
57
+ // We define the array length but do not prefill values, we do that on runtime when needed
58
58
  options.result[0] = {
59
59
  val: {ts: null, val: null},
60
60
  max: {ts: null, val: null},
@@ -108,7 +108,7 @@ function aggregation(options, data) {
108
108
  // store all border values
109
109
  if (preIndex < 0) {
110
110
  index = 0;
111
- // if the ts is even earlier than the "pre interval" ignore it, else we collect all data there
111
+ // if the ts is even earlier than the "pre-interval" ignore it, else we collect all data there
112
112
  if (preIndex < -1) {
113
113
  collectedTooEarlyData.push(data[i]);
114
114
  continue;
@@ -116,7 +116,7 @@ function aggregation(options, data) {
116
116
  preIndexValueFound = true;
117
117
  } else if (preIndex > options.maxIndex) {
118
118
  index = options.maxIndex + 2;
119
- // if the ts is even later than the "post interval" ignore it, else we collect all data there
119
+ // if the ts is even later than the "post-interval" ignore it, else we collect all data there
120
120
  if (preIndex > options.maxIndex + 1) {
121
121
  collectedTooLateData.push(data[i]);
122
122
  continue;
@@ -127,16 +127,16 @@ function aggregation(options, data) {
127
127
  }
128
128
  options.overallLength++;
129
129
 
130
- if (options.result[index] === undefined) { // lazy initialization of datastructure
130
+ if (options.result[index] === undefined) { // lazy initialization of data structure
131
131
  options.result[index] = {
132
132
  val: {ts: null, val: null},
133
133
  max: {ts: null, val: null},
134
134
  min: {ts: null, val: null},
135
135
  start: {ts: null, val: null},
136
- end: {ts: null, val: null}
136
+ end: {ts: null, val: null},
137
137
  };
138
138
 
139
- if (options.aggregate === 'average') {
139
+ if (options.aggregate === 'average' || options.aggregate === 'count') {
140
140
  options.averageCount[index] = 0;
141
141
  }
142
142
 
@@ -151,20 +151,20 @@ function aggregation(options, data) {
151
151
  aggregationLogic(data[i], index, options);
152
152
  }
153
153
 
154
- // If no data was found in the pre interval, but we have earlier data, we put the latest of them in the pre interval
154
+ // If no data was found in the pre-interval, but we have earlier data, we put the latest of them in the pre-interval
155
155
  if (!preIndexValueFound && collectedTooEarlyData.length > 0) {
156
156
  collectedTooEarlyData = collectedTooEarlyData.sort(sortByTs);
157
157
  options.overallLength++;
158
158
  aggregationLogic(collectedTooEarlyData[collectedTooEarlyData.length - 1], 0, options);
159
159
  }
160
- // If no data was found in the post interval, but we have later data, we put the earliest of them in the pre interval
160
+ // If no data was found in the post-interval, but we have later data, we put the earliest of them in the post-interval
161
161
  if (!postIndexValueFound && collectedTooLateData.length > 0) {
162
162
  collectedTooLateData = collectedTooLateData.sort(sortByTs);
163
163
  options.overallLength++;
164
164
  aggregationLogic(collectedTooLateData[0], options.maxIndex + 2, options);
165
165
  }
166
166
 
167
- return {result: options.result, step: options.step, sourceLength: data.length} ;
167
+ return { result: options.result, step: options.step, sourceLength: data.length };
168
168
  }
169
169
 
170
170
  function aggregationLogic(data, index, options) {
@@ -193,6 +193,8 @@ function aggregationLogic(data, index, options) {
193
193
  } else if (options.aggregate === 'average') {
194
194
  options.result[index].val.val += parseFloat(data.val);
195
195
  options.averageCount[index]++;
196
+ } else if (options.aggregate === 'count') {
197
+ options.averageCount[index]++;
196
198
  } else if (options.aggregate === 'total') {
197
199
  options.result[index].val.val += parseFloat(data.val);
198
200
  } else if (options.aggregate === 'minmax') {
@@ -260,7 +262,7 @@ function finishAggregation(options) {
260
262
  } else if (ii === originalResultLength - 1) {
261
263
  postBorderValueRemoved = true;
262
264
  }
263
- //options.result.splice(ii, 1);
265
+ // options.result.splice(ii, 1);
264
266
  continue;
265
267
  }
266
268
  // just one value in this period: max == min == start == end
@@ -427,8 +429,33 @@ function finishAggregation(options) {
427
429
  });
428
430
  } else {
429
431
  // no one value in this interval
430
- //options.result.splice(k, 1);
431
- //options.averageCount.splice(k, 1); // not needed to clean up because not used anymore afterwards
432
+ // options.result.splice(k, 1);
433
+ // options.averageCount.splice(k, 1); // not needed to clean up because not used anymore afterwards
434
+ }
435
+ }
436
+ options.result = finalResult;
437
+ } else if (options.aggregate === 'count') {
438
+ let startIndex = 0;
439
+ let endIndex = options.result.length;
440
+ const finalResult = [];
441
+ if (options.removeBorderValues) { // we cut out the additional results
442
+ // options.result.splice(0, 1);
443
+ // options.averageCount.splice(0, 1);
444
+ // options.result.length--;
445
+ // options.averageCount.length--;
446
+ startIndex++;
447
+ endIndex--;
448
+ }
449
+ for (let k = startIndex; k < endIndex; k++) {
450
+ if (options.result[k] !== undefined && options.result[k].val.ts) {
451
+ finalResult.push({
452
+ ts: options.result[k].val.ts,
453
+ val: options.averageCount[k],
454
+ });
455
+ } else {
456
+ // no one value in this interval
457
+ // options.result.splice(k, 1);
458
+ // options.averageCount.splice(k, 1); // not needed to clean up because not used anymore afterward
432
459
  }
433
460
  }
434
461
  options.result = finalResult;
@@ -442,7 +469,7 @@ function finishAggregation(options) {
442
469
  let indexStartTs = options.start + ((k - 1) * options.step);
443
470
  let indexEndTs = indexStartTs + options.step;
444
471
  if (options.integralDatapoints[k] && options.integralDatapoints[k].length) {
445
- // Sort datapoints by ts first
472
+ // Sort data points by ts first
446
473
  options.integralDatapoints[k].sort(sortByTs);
447
474
  }
448
475
  // Make sure that we have entries that always start at the beginning of the interval
@@ -562,10 +589,12 @@ function finishAggregation(options) {
562
589
  let endIndex = options.result.length;
563
590
  const finalResult = [];
564
591
  if (options.removeBorderValues) { // we cut out the additional results
565
- /*options.result.splice(0, 1);
592
+ /*
593
+ options.result.splice(0, 1);
566
594
  options.quantileDatapoints.splice(0, 1);
567
595
  options.result.length--
568
- options.quantileDatapoints.length--;*/
596
+ options.quantileDatapoints.length--;
597
+ */
569
598
  startIndex++;
570
599
  endIndex--;
571
600
  }
@@ -579,8 +608,8 @@ function finishAggregation(options) {
579
608
  finalResult.push(point);
580
609
  } else {
581
610
  // no one value in this interval
582
- //options.result.splice(k, 1);
583
- //options.quantileDatapoints.splice(k, 1); // not needed to clean up because not used anymore afterwards
611
+ // options.result.splice(k, 1);
612
+ // options.quantileDatapoints.splice(k, 1); // not needed to clean up because not used anymore afterward
584
613
  }
585
614
  }
586
615
  options.result = finalResult;
@@ -714,7 +743,7 @@ function beautify(options) {
714
743
  if (postLastValue) {
715
744
  // if steps
716
745
  if (options.aggregate === 'onchange' || !options.aggregate) {
717
- // if more data following, draw line to the end of chart
746
+ // if more data following, draw line to the end of the chart
718
747
  if (postLastValue.ts !== lastTS) {
719
748
  options.result.push({ts: options.end, val: postLastValue.val});
720
749
  } else {
@@ -939,8 +968,12 @@ function quantile(qOrPs, list, fn) {
939
968
  a = Number.isNaN(a) ? Number.NEGATIVE_INFINITY : a;
940
969
  b = Number.isNaN(b) ? Number.NEGATIVE_INFINITY : b;
941
970
 
942
- if (a > b) return 1;
943
- if (a < b) return -1;
971
+ if (a > b) {
972
+ return 1;
973
+ }
974
+ if (a < b) {
975
+ return -1;
976
+ }
944
977
 
945
978
  return 0;
946
979
  });
@@ -955,9 +988,7 @@ function quantile(qOrPs, list, fn) {
955
988
  }
956
989
 
957
990
  function sortByTs(a, b) {
958
- const aTs = a.ts;
959
- const bTs = b.ts;
960
- return (aTs < bTs) ? -1 : ((aTs > bTs) ? 1 : 0);
991
+ return a.ts - b.ts;
961
992
  }
962
993
 
963
994
  module.exports.sendResponseCounter = sendResponseCounter;
@@ -966,3 +997,4 @@ module.exports.initAggregate = initAggregate;
966
997
  module.exports.aggregation = aggregation;
967
998
  module.exports.beautify = beautify;
968
999
  module.exports.finishAggregation = finishAggregation;
1000
+ module.exports.sortByTs = sortByTs;
package/lib/mssql.js CHANGED
@@ -40,11 +40,15 @@ exports.insert = function (dbname, index, values) {
40
40
  insertValues[value.db] = insertValues[value.db] || [];
41
41
 
42
42
  if (!value.state || value.state.val === null || value.state.val === undefined) {
43
- value.state.val = "NULL";
44
- } else if (value.db === "ts_string") {
45
- value.state.val = "'" + value.state.val.toString().replace(/'/g, '') + "'";
46
- } else if (value.db === "ts_bool") {
43
+ value.state.val = 'NULL';
44
+ } else if (value.db === 'ts_string') {
45
+ value.state.val = `'${value.state.val.toString().replace(/'/g, '')}'`;
46
+ } else if (value.db === 'ts_bool') {
47
47
  value.state.val = value.state.val ? 1 : 0;
48
+ } else if (value.db === 'ts_number') {
49
+ if (isNaN(value.state.val)) {
50
+ value.state.val = 'NULL';
51
+ }
48
52
  }
49
53
 
50
54
  if (value.db === 'ts_counter') {
@@ -74,9 +78,9 @@ exports.retention = function (dbname, index, db, retention) {
74
78
  const d = new Date();
75
79
  d.setSeconds(-retention);
76
80
  let query = `DELETE FROM ${dbname}.dbo.${db} WHERE`;
77
- query += ` id=` + index;
78
- query += ` AND ts < ` + d.getTime();
79
- query += `;`;
81
+ query += ` id=${index}`;
82
+ query += ` AND ts < ${d.getTime()}`;
83
+ query += ';';
80
84
  return query;
81
85
  };
82
86
 
@@ -131,13 +135,13 @@ exports.getCounterDiff = function (dbname, options) {
131
135
  exports.getHistory = function (dbname, db, options) {
132
136
  let query = `SELECT * FROM (SELECT `;
133
137
  if ((!options.start && options.count) || (options.aggregate === 'none' && options.count)) {
134
- query += ` TOP ` + options.count;
138
+ query += ` TOP ${options.count}`;
135
139
  }
136
140
  query += ` ts, val` +
137
- (!options.id ? (`, ${db}.id as id`) : '') +
141
+ (!options.id ? `, ${db}.id as id` : '') +
138
142
  (options.ack ? `, ack` : '') +
139
- (options.from ? (`, ${dbname}.dbo.sources.name as 'from'`) : '') +
140
- (options.q ? `, q` : '') + ` FROM ${dbname}.dbo.` + db;
143
+ (options.from ? `, ${dbname}.dbo.sources.name as 'from'` : '') +
144
+ (options.q ? `, q` : '') + ` FROM ${dbname}.dbo.${db}`;
141
145
 
142
146
  if (options.from) {
143
147
  query += ` INNER JOIN ${dbname}.dbo.sources ON ${dbname}.dbo.sources.id=${dbname}.dbo.${db}._from`;
@@ -146,13 +150,13 @@ exports.getHistory = function (dbname, db, options) {
146
150
  let where = '';
147
151
 
148
152
  if (options.id) {
149
- where += ` ${dbname}.dbo.${db}.id=` + options.id;
153
+ where += ` ${dbname}.dbo.${db}.id=${options.id}`;
150
154
  }
151
155
  if (options.end) {
152
- where += (where ? ` AND` : '') + ` ${dbname}.dbo.${db}.ts < ` + options.end;
156
+ where += `${where ? ` AND` : ''} ${dbname}.dbo.${db}.ts < ${options.end}`;
153
157
  }
154
158
  if (options.start) {
155
- where += (where ? ` AND` : '') + ` ${dbname}.dbo.${db}.ts >= ` + options.start;
159
+ where += `${where ? ` AND` : ''} ${dbname}.dbo.${db}.ts >= ${options.start}`;
156
160
  }
157
161
  if ((!options.start && options.count) || (options.aggregate === 'none' && options.count)) {
158
162
  where += ` ORDER BY ts`;
@@ -169,30 +173,32 @@ exports.getHistory = function (dbname, db, options) {
169
173
  let subQuery;
170
174
  let subWhere;
171
175
  subQuery = ` SELECT TOP 1 ts, val` +
172
- (!options.id ? (`, ${db}.id as id`) : '') +
176
+ (!options.id ? `, ${db}.id as id` : '') +
173
177
  (options.ack ? `, ack` : '') +
174
- (options.from ? (`, ${dbname}.dbo.sources.name as 'from'`) : '') +
175
- (options.q ? `, q` : '') + ` FROM ${dbname}.dbo.` + db;
178
+ (options.from ? `, ${dbname}.dbo.sources.name as 'from'` : '') +
179
+ (options.q ? `, q` : '') + ` FROM ${dbname}.dbo.${db}`;
176
180
  if (options.from) {
177
181
  subQuery += ` INNER JOIN ${dbname}.dbo.sources ON ${dbname}.dbo.sources.id=${dbname}.dbo.${db}._from`;
178
182
  }
179
183
  subWhere = '';
180
184
  if (options.id) {
181
- subWhere += ` ${dbname}.dbo.${db}.id=` + options.id;
185
+ subWhere += ` ${dbname}.dbo.${db}.id=${options.id}`;
182
186
  }
183
187
  if (options.ignoreNull) {
184
188
  //subWhere += (subWhere ? ` AND` : '') + ` val <> NULL`;
185
189
  }
186
190
  subWhere += `${subWhere ? ` AND` : ''} ${dbname}.dbo.${db}.ts < ` + options.start;
187
- if (subWhere) subQuery += ` WHERE ` + subWhere;
191
+ if (subWhere) {
192
+ subQuery += ` WHERE ${subWhere}`;
193
+ }
188
194
  subQuery += ` ORDER BY ${dbname}.dbo.${db}.ts DESC`;
189
195
  where += ` UNION ALL SELECT * FROM (${subQuery}) a`;
190
196
 
191
197
  // add next value after end
192
198
  subQuery = ` SELECT TOP 1 ts, val` +
193
- (!options.id ? (`, ${db}.id as id`) : '') +
199
+ (!options.id ? `, ${db}.id as id` : '') +
194
200
  (options.ack ? `, ack` : '') +
195
- (options.from ? (`, ${dbname}.dbo.sources.name as 'from'`) : '') +
201
+ (options.from ? `, ${dbname}.dbo.sources.name as 'from'` : '') +
196
202
  (options.q ? `, q` : '') + ` FROM ${dbname}.dbo.${db}`;
197
203
  if (options.from) {
198
204
  subQuery += ` INNER JOIN ${dbname}.dbo.sources ON ${dbname}.dbo.sources.id=${dbname}.dbo.${db}._from`;
@@ -204,8 +210,10 @@ exports.getHistory = function (dbname, db, options) {
204
210
  if (options.ignoreNull) {
205
211
  //subWhere += (subWhere ? ` AND` : '') + ` val <> NULL`;
206
212
  }
207
- subWhere += (subWhere ? ` AND` : '') + ` ${dbname}.dbo.${db}.ts >= ${options.end}`;
208
- if (subWhere) subQuery += ` WHERE ${subWhere}`;
213
+ subWhere += `${subWhere ? ` AND` : ''} ${dbname}.dbo.${db}.ts >= ${options.end}`;
214
+ if (subWhere) {
215
+ subQuery += ` WHERE ${subWhere}`;
216
+ }
209
217
  subQuery += ` ORDER BY ${dbname}.dbo.${db}.ts ASC`;
210
218
  where += ` UNION ALL SELECT * FROM (${subQuery}) b`;
211
219
  }
@@ -225,7 +233,7 @@ exports.getHistory = function (dbname, db, options) {
225
233
 
226
234
  exports.delete = function (dbname, db, index, start, end) {
227
235
  let query = `DELETE FROM ${dbname}.dbo.${db} WHERE`;
228
- query += " id=" + index;
236
+ query += ` id=${index}`;
229
237
  if (start && end) {
230
238
  query += ` AND ${dbname}.dbo.${db}.ts>=${start} AND ${dbname}.dbo.${db}.ts<=${end}`;
231
239
  } else if (start) {
@@ -249,22 +257,22 @@ exports.update = function (dbname, index, state, from, db) {
249
257
  let query = `UPDATE ${dbname}.dbo.${db} SET `;
250
258
  let vals = [];
251
259
  if (state.val !== undefined) {
252
- vals.push("val=" + state.val);
260
+ vals.push(`val=${state.val}`);
253
261
  }
254
262
  if (state.q !== undefined) {
255
- vals.push("q=" + state.q);
263
+ vals.push(`q=${state.q}`);
256
264
  }
257
265
  if (from !== undefined) {
258
- vals.push("_from=" + from);
266
+ vals.push(`_from=${from}`);
259
267
  }
260
268
  if (state.ack !== undefined) {
261
- vals.push("ack=" + (state.ack ? 1 : 0));
269
+ vals.push(`ack=${state.ack ? 1 : 0}`);
262
270
  }
263
271
  query += vals.join(', ');
264
- query += " WHERE ";
265
- query += " id=" + index;
266
- query += " AND ts=" + state.ts;
267
- query += ";";
272
+ query += ' WHERE ';
273
+ query += ` id=${index}`;
274
+ query += ` AND ts=${state.ts}`;
275
+ query += ';';
268
276
 
269
277
  return query;
270
278
  };