iobroker.influxdb 2.6.2 → 3.0.2
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/README.md +178 -54
- package/admin/i18n/de/translations.json +33 -4
- package/admin/i18n/en/translations.json +33 -4
- package/admin/i18n/es/translations.json +32 -3
- package/admin/i18n/fr/translations.json +31 -2
- package/admin/i18n/it/translations.json +31 -2
- package/admin/i18n/nl/translations.json +31 -2
- package/admin/i18n/pl/translations.json +31 -2
- package/admin/i18n/pt/translations.json +31 -2
- package/admin/i18n/ru/translations.json +32 -3
- package/admin/i18n/zh-cn/translations.json +31 -2
- package/admin/jsonConfig.json +96 -16
- package/admin/jsonCustom.json +78 -196
- package/admin/words.js +36 -7
- package/io-package.json +140 -43
- package/lib/DatabaseInfluxDB2x.js +5 -4
- package/lib/aggregate.js +370 -60
- package/main.js +841 -419
- package/package.json +8 -8
- package/admin/custom_m.html +0 -237
- package/admin/index_m.html +0 -301
package/lib/aggregate.js
CHANGED
|
@@ -5,6 +5,11 @@
|
|
|
5
5
|
// THIS file should be identical with sql and history adapter's one
|
|
6
6
|
|
|
7
7
|
function initAggregate(options) {
|
|
8
|
+
let log = () => {};
|
|
9
|
+
if (options.debugLog) {
|
|
10
|
+
log = options.log || console.log;
|
|
11
|
+
}
|
|
12
|
+
|
|
8
13
|
//step; // 1 Step is 1 second
|
|
9
14
|
if (options.step === null) {
|
|
10
15
|
options.step = (options.end - options.start) / options.count;
|
|
@@ -18,9 +23,30 @@ function initAggregate(options) {
|
|
|
18
23
|
options.maxIndex = Math.ceil(((options.end - options.start) / options.step) - 1);
|
|
19
24
|
options.result = [];
|
|
20
25
|
options.averageCount = [];
|
|
26
|
+
options.quantileDatapoints = [];
|
|
27
|
+
options.integralDatapoints = [];
|
|
21
28
|
options.aggregate = options.aggregate || 'minmax';
|
|
22
29
|
options.overallLength = 0;
|
|
23
30
|
|
|
31
|
+
if (options.aggregate === 'percentile') {
|
|
32
|
+
if (typeof options.percentile !== 'number' || options.percentile < 0 || options.percentile > 100) {
|
|
33
|
+
options.percentile = 50;
|
|
34
|
+
}
|
|
35
|
+
options.quantile = options.percentile / 100; // Internally we use quantile for percentile too
|
|
36
|
+
}
|
|
37
|
+
if (options.aggregate === 'quantile') {
|
|
38
|
+
if (typeof options.quantile !== 'number' || options.quantile < 0 || options.quantile > 1) {
|
|
39
|
+
options.quantile = 0.5;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (options.aggregate === 'integral') {
|
|
43
|
+
if (typeof options.integralUnit !== 'number' || options.integralUnit <= 0) {
|
|
44
|
+
options.integralUnit = 60;
|
|
45
|
+
}
|
|
46
|
+
options.integralUnit *= 1000; // Convert to milliseconds
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
log(`${options.logId} Initialize: maxIndex = ${options.maxIndex}, step = ${options.step}, start = ${options.start}, end = ${options.end}`);
|
|
24
50
|
// pre-fill the result with timestamps (add one before start and one after end)
|
|
25
51
|
for (let i = 0; i <= options.maxIndex + 2; i++) {
|
|
26
52
|
options.result[i] = {
|
|
@@ -34,6 +60,13 @@ function initAggregate(options) {
|
|
|
34
60
|
if (options.aggregate === 'average') {
|
|
35
61
|
options.averageCount[i] = 0;
|
|
36
62
|
}
|
|
63
|
+
|
|
64
|
+
if (options.aggregate === 'percentile' || options.aggregate === 'quantile') {
|
|
65
|
+
options.quantileDatapoints[i] = [];
|
|
66
|
+
}
|
|
67
|
+
if (options.aggregate === 'integral') {
|
|
68
|
+
options.integralDatapoints[i] = [];
|
|
69
|
+
}
|
|
37
70
|
}
|
|
38
71
|
return options;
|
|
39
72
|
}
|
|
@@ -41,6 +74,12 @@ function initAggregate(options) {
|
|
|
41
74
|
function aggregation(options, data) {
|
|
42
75
|
let index;
|
|
43
76
|
let preIndex;
|
|
77
|
+
|
|
78
|
+
let collectedTooEarlyData = [];
|
|
79
|
+
let collectedTooLateData = [];
|
|
80
|
+
let preIndexValueFound = false;
|
|
81
|
+
let postIndexValueFound = false;
|
|
82
|
+
|
|
44
83
|
for (let i = 0; i < data.length; i++) {
|
|
45
84
|
if (!data[i]) {
|
|
46
85
|
continue;
|
|
@@ -54,8 +93,20 @@ function aggregation(options, data) {
|
|
|
54
93
|
// store all border values
|
|
55
94
|
if (preIndex < 0) {
|
|
56
95
|
index = 0;
|
|
96
|
+
// if the ts is even earlier than the "pre interval" ignore it, else we collect all data there
|
|
97
|
+
if (preIndex < -1) {
|
|
98
|
+
collectedTooEarlyData.push(data[i]);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
preIndexValueFound = true;
|
|
57
102
|
} else if (preIndex > options.maxIndex) {
|
|
58
103
|
index = options.maxIndex + 2;
|
|
104
|
+
// if the ts is even later than the "post interval" ignore it, else we collect all data there
|
|
105
|
+
if (preIndex > options.maxIndex + 1) {
|
|
106
|
+
collectedTooLateData.push(data[i]);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
postIndexValueFound = true;
|
|
59
110
|
} else {
|
|
60
111
|
index = preIndex + 1;
|
|
61
112
|
}
|
|
@@ -66,75 +117,109 @@ function aggregation(options, data) {
|
|
|
66
117
|
continue;
|
|
67
118
|
}
|
|
68
119
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step));
|
|
72
|
-
}
|
|
73
|
-
if (options.result[index].val.val === null || options.result[index].val.val < data[i].val) {
|
|
74
|
-
options.result[index].val.val = data[i].val;
|
|
75
|
-
}
|
|
76
|
-
} else if (options.aggregate === 'min') {
|
|
77
|
-
if (!options.result[index].val.ts) {
|
|
78
|
-
options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step));
|
|
79
|
-
}
|
|
80
|
-
if (options.result[index].val.val === null || options.result[index].val.val > data[i].val) {
|
|
81
|
-
options.result[index].val.val = data[i].val;
|
|
82
|
-
}
|
|
83
|
-
} else if (options.aggregate === 'average') {
|
|
84
|
-
if (!options.result[index].val.ts) {
|
|
85
|
-
options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step));
|
|
86
|
-
}
|
|
87
|
-
options.result[index].val.val += parseFloat(data[i].val);
|
|
88
|
-
options.averageCount[index]++;
|
|
89
|
-
} else if (options.aggregate === 'total') {
|
|
90
|
-
if (!options.result[index].val.ts) {
|
|
91
|
-
options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step));
|
|
92
|
-
}
|
|
93
|
-
options.result[index].val.val += parseFloat(data[i].val);
|
|
94
|
-
} else if (options.aggregate === 'minmax') {
|
|
95
|
-
if (options.result[index].min.ts === null) {
|
|
96
|
-
options.result[index].min.ts = data[i].ts;
|
|
97
|
-
options.result[index].min.val = data[i].val;
|
|
120
|
+
aggregationLogic(data[i], index, options);
|
|
121
|
+
}
|
|
98
122
|
|
|
99
|
-
|
|
100
|
-
|
|
123
|
+
// If no data was found in the pre interval, but we have earlier data, we put the latest of them in the pre interval
|
|
124
|
+
if (!preIndexValueFound && collectedTooEarlyData.length > 0) {
|
|
125
|
+
collectedTooEarlyData = collectedTooEarlyData.sort(sortByTs);
|
|
126
|
+
options.overallLength++;
|
|
127
|
+
aggregationLogic(collectedTooEarlyData[collectedTooEarlyData.length - 1], 0, options);
|
|
128
|
+
}
|
|
129
|
+
// If no data was found in the post interval, but we have later data, we put the earliest of them in the pre interval
|
|
130
|
+
if (!postIndexValueFound && collectedTooLateData.length > 0) {
|
|
131
|
+
collectedTooLateData = collectedTooLateData.sort(sortByTs);
|
|
132
|
+
options.overallLength++;
|
|
133
|
+
aggregationLogic(collectedTooLateData[0], options.maxIndex + 2, options);
|
|
134
|
+
}
|
|
101
135
|
|
|
102
|
-
|
|
103
|
-
|
|
136
|
+
return {result: options.result, step: options.step, sourceLength: data.length} ;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function aggregationLogic(data, index, options) {
|
|
140
|
+
let log = () => {};
|
|
141
|
+
if (options.debugLog) {
|
|
142
|
+
log = options.log || console.log;
|
|
143
|
+
}
|
|
104
144
|
|
|
105
|
-
|
|
106
|
-
|
|
145
|
+
if (options.aggregate !== 'minmax' && !options.result[index].val.ts) {
|
|
146
|
+
options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (options.aggregate === 'max') {
|
|
150
|
+
if (options.result[index].val.val === null || options.result[index].val.val < data.val) {
|
|
151
|
+
options.result[index].val.val = data.val;
|
|
152
|
+
}
|
|
153
|
+
} else if (options.aggregate === 'min') {
|
|
154
|
+
if (options.result[index].val.val === null || options.result[index].val.val > data.val) {
|
|
155
|
+
options.result[index].val.val = data.val;
|
|
156
|
+
}
|
|
157
|
+
} else if (options.aggregate === 'average') {
|
|
158
|
+
options.result[index].val.val += parseFloat(data.val);
|
|
159
|
+
options.averageCount[index]++;
|
|
160
|
+
} else if (options.aggregate === 'total') {
|
|
161
|
+
options.result[index].val.val += parseFloat(data.val);
|
|
162
|
+
} else if (options.aggregate === 'minmax') {
|
|
163
|
+
if (options.result[index].min.ts === null) {
|
|
164
|
+
options.result[index].min.ts = data.ts;
|
|
165
|
+
options.result[index].min.val = data.val;
|
|
166
|
+
|
|
167
|
+
options.result[index].max.ts = data.ts;
|
|
168
|
+
options.result[index].max.val = data.val;
|
|
169
|
+
|
|
170
|
+
options.result[index].start.ts = data.ts;
|
|
171
|
+
options.result[index].start.val = data.val;
|
|
172
|
+
|
|
173
|
+
options.result[index].end.ts = data.ts;
|
|
174
|
+
options.result[index].end.val = data.val;
|
|
175
|
+
} else {
|
|
176
|
+
if (data.val !== null) {
|
|
177
|
+
if (data.val > options.result[index].max.val) {
|
|
178
|
+
options.result[index].max.ts = data.ts;
|
|
179
|
+
options.result[index].max.val = data.val;
|
|
180
|
+
} else if (data.val < options.result[index].min.val) {
|
|
181
|
+
options.result[index].min.ts = data.ts;
|
|
182
|
+
options.result[index].min.val = data.val;
|
|
183
|
+
}
|
|
184
|
+
if (data.ts > options.result[index].end.ts) {
|
|
185
|
+
options.result[index].end.ts = data.ts;
|
|
186
|
+
options.result[index].end.val = data.val;
|
|
187
|
+
}
|
|
107
188
|
} else {
|
|
108
|
-
if (data[
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
options.result[index].max.val = data[i].val;
|
|
112
|
-
} else if (data[i].val < options.result[index].min.val) {
|
|
113
|
-
options.result[index].min.ts = data[i].ts;
|
|
114
|
-
options.result[index].min.val = data[i].val;
|
|
115
|
-
}
|
|
116
|
-
if (data[i].ts > options.result[index].end.ts) {
|
|
117
|
-
options.result[index].end.ts = data[i].ts;
|
|
118
|
-
options.result[index].end.val = data[i].val;
|
|
119
|
-
}
|
|
120
|
-
} else {
|
|
121
|
-
if (data[i].ts > options.result[index].end.ts) {
|
|
122
|
-
options.result[index].end.ts = data[i].ts;
|
|
123
|
-
options.result[index].end.val = null;
|
|
124
|
-
}
|
|
189
|
+
if (data.ts > options.result[index].end.ts) {
|
|
190
|
+
options.result[index].end.ts = data.ts;
|
|
191
|
+
options.result[index].end.val = null;
|
|
125
192
|
}
|
|
126
193
|
}
|
|
127
194
|
}
|
|
195
|
+
} else if (options.aggregate === 'percentile' || options.aggregate === 'quantile') {
|
|
196
|
+
options.quantileDatapoints[index].push(data.val);
|
|
197
|
+
log(`${options.logId} Quantile ${index}: Add ts= ${data.ts} val=${data.val}`);
|
|
198
|
+
} else if (options.aggregate === 'integral') {
|
|
199
|
+
options.integralDatapoints[index].push(data);
|
|
200
|
+
log(`${options.logId} Integral ${index}: Add ts= ${data.ts} val=${data.val}`);
|
|
128
201
|
}
|
|
129
|
-
|
|
130
|
-
return {result: options.result, step: options.step, sourceLength: data.length} ;
|
|
131
202
|
}
|
|
132
203
|
|
|
133
204
|
function finishAggregation(options) {
|
|
205
|
+
let log = () => {};
|
|
206
|
+
if (options.debugLog) {
|
|
207
|
+
log = options.log || console.log;
|
|
208
|
+
}
|
|
209
|
+
|
|
134
210
|
if (options.aggregate === 'minmax') {
|
|
211
|
+
let preBorderValueRemoved = false;
|
|
212
|
+
let postBorderValueRemoved = false;
|
|
213
|
+
const originalResultLength = options.result.length;
|
|
214
|
+
|
|
135
215
|
for (let ii = options.result.length - 1; ii >= 0; ii--) {
|
|
136
216
|
// no one value in this period
|
|
137
217
|
if (options.result[ii].start.ts === null) {
|
|
218
|
+
if (ii === 0) {
|
|
219
|
+
preBorderValueRemoved = true;
|
|
220
|
+
} else if (ii === originalResultLength - 1) {
|
|
221
|
+
postBorderValueRemoved = true;
|
|
222
|
+
}
|
|
138
223
|
options.result.splice(ii, 1);
|
|
139
224
|
} else {
|
|
140
225
|
// just one value in this period: max == min == start == end
|
|
@@ -273,7 +358,21 @@ function finishAggregation(options) {
|
|
|
273
358
|
}
|
|
274
359
|
}
|
|
275
360
|
}
|
|
361
|
+
if (options.removeBorderValues) { // we cut out the additional results
|
|
362
|
+
if (!preBorderValueRemoved) {
|
|
363
|
+
options.result.splice(0, 1);
|
|
364
|
+
}
|
|
365
|
+
if (!postBorderValueRemoved) {
|
|
366
|
+
options.result.splice(-1, 1);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
276
369
|
} else if (options.aggregate === 'average') {
|
|
370
|
+
if (options.removeBorderValues) { // we cut out the additional results
|
|
371
|
+
options.result.splice(0, 1);
|
|
372
|
+
options.averageCount.splice(0, 1);
|
|
373
|
+
options.result.splice(-1, 1);
|
|
374
|
+
options.averageCount.splice(-1, 1);
|
|
375
|
+
}
|
|
277
376
|
for (let k = options.result.length - 1; k >= 0; k--) {
|
|
278
377
|
if (options.result[k].val.ts) {
|
|
279
378
|
options.result[k] = {
|
|
@@ -283,9 +382,142 @@ function finishAggregation(options) {
|
|
|
283
382
|
} else {
|
|
284
383
|
// no one value in this interval
|
|
285
384
|
options.result.splice(k, 1);
|
|
385
|
+
options.averageCount.splice(k, 1);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
} else if (options.aggregate === 'integral') {
|
|
389
|
+
for (let k = 0; k < options.result.length; k++) {
|
|
390
|
+
let indexStartTs = options.start + ((k - 1) * options.step);
|
|
391
|
+
let indexEndTs = indexStartTs + options.step;
|
|
392
|
+
if (options.integralDatapoints[k].length) {
|
|
393
|
+
// Sort datapoints by ts first
|
|
394
|
+
options.integralDatapoints[k].sort(sortByTs);
|
|
395
|
+
}
|
|
396
|
+
// Make sure that we have entries that always start at the beginning of the interval
|
|
397
|
+
if ((!options.integralDatapoints[k].length || options.integralDatapoints[k][0].ts > indexStartTs) && options.integralDatapoints[k - 1] && options.integralDatapoints[k -1][options.integralDatapoints[k - 1].length - 1]) {
|
|
398
|
+
// if the first entry of this interval started somewhere in the start of the interval, add a start entry
|
|
399
|
+
// same if there is no entry at all in the timeframe, use last entry from interval before
|
|
400
|
+
options.integralDatapoints[k].unshift({
|
|
401
|
+
ts: indexStartTs,
|
|
402
|
+
val: options.integralDatapoints[k - 1][options.integralDatapoints[k - 1].length - 1].val
|
|
403
|
+
});
|
|
404
|
+
log(`${options.logId} Integral: ${k}: Added start entry for interval with ts=${indexStartTs}, val=${options.integralDatapoints[k][0].val}`);
|
|
405
|
+
} else if (options.integralDatapoints[k].length && options.integralDatapoints[k][0].ts > indexStartTs) {
|
|
406
|
+
options.integralDatapoints[k].unshift({
|
|
407
|
+
ts: indexStartTs,
|
|
408
|
+
val: options.integralDatapoints[k][0].val
|
|
409
|
+
});
|
|
410
|
+
log(`${options.logId} Integral: ${k}: Added start entry for interval with ts=${indexStartTs}, val=${options.integralDatapoints[k][0].val} with same value as first point in interval because no former datapoint was found`);
|
|
411
|
+
} else if (options.integralDatapoints[k].length && options.integralDatapoints[k][0].ts < indexStartTs) {
|
|
412
|
+
// if the first entry of this interval started before the start of the interval, search for the last value before the start of the interval, add as start entry
|
|
413
|
+
let preFirstIndex = null;
|
|
414
|
+
for (let kk = 0; kk < options.integralDatapoints[k].length; kk++) {
|
|
415
|
+
if (options.integralDatapoints[k][kk].ts >= indexStartTs) {
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
preFirstIndex = kk;
|
|
419
|
+
}
|
|
420
|
+
if (preFirstIndex !== null) {
|
|
421
|
+
options.integralDatapoints[k].splice(0, preFirstIndex, {
|
|
422
|
+
ts: indexStartTs,
|
|
423
|
+
val: options.integralDatapoints[k][preFirstIndex].val
|
|
424
|
+
});
|
|
425
|
+
log(`${options.logId} Integral: ${k}: Remove ${preFirstIndex + 1} entries and add start entry for interval with ts=${indexStartTs}, val=${options.integralDatapoints[k][0].val}`);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const vals = options.integralDatapoints[k].map(dp => `[${dp.ts}, ${dp.val}]`);
|
|
430
|
+
log(`${options.logId} Integral: ${k}: ${options.integralDatapoints[k].length} datapoints for interval for ${indexStartTs} - ${indexEndTs}: ${vals.join(',')}`);
|
|
431
|
+
|
|
432
|
+
if (!options.result[k].val.ts) {
|
|
433
|
+
options.result[k].val.ts = Math.round(options.start + (((k - 1) + 0.5) * options.step));
|
|
434
|
+
}
|
|
435
|
+
// Calculate Intervals and always calculate till the interval end (start made sure above already)
|
|
436
|
+
for (let kk = 0; kk < options.integralDatapoints[k].length; kk++) {
|
|
437
|
+
const valEndTs = options.integralDatapoints[k][kk + 1] ? Math.min(options.integralDatapoints[k][kk + 1].ts, indexEndTs) : indexEndTs;
|
|
438
|
+
let valDuration = valEndTs - options.integralDatapoints[k][kk].ts;
|
|
439
|
+
if (valDuration < 0) {
|
|
440
|
+
log(`${options.logId} Integral: ${k}[${kk}] data do not belong to this interval, ignore ${JSON.stringify(options.integralDatapoints[k][kk])} (vs. ${valEndTs})`)
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
if (valDuration === 0) {
|
|
444
|
+
log(`${options.logId} Integral: ${k}[${kk}] valDuration zero, ignore ${JSON.stringify(options.integralDatapoints[k][kk])}`)
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
let valStart = parseFloat(options.integralDatapoints[k][kk].val) || 0;
|
|
448
|
+
// End value is the next value, or if none, assume "linearity
|
|
449
|
+
let valEnd = parseFloat((options.integralDatapoints[k][kk + 1] ? options.integralDatapoints[k][kk + 1].val : (options.integralDatapoints[k + 1] && options.integralDatapoints[k + 1][0] ? options.integralDatapoints[k + 1][0].val : valStart))) || 0;
|
|
450
|
+
if (options.integralInterpolation !== 'linear' || valStart === valEnd) {
|
|
451
|
+
const integralAdd = valStart * valDuration / options.integralUnit;
|
|
452
|
+
// simple rectangle linear interpolation
|
|
453
|
+
log(`${options.logId} Integral: ${k}[${kk}] : Add ${integralAdd} from val=${valStart} for ${valDuration}`);
|
|
454
|
+
options.result[k].val.val += integralAdd;
|
|
455
|
+
} else if ((valStart >= 0 && valEnd >= 0) || (valStart <= 0 && valEnd <= 0)) {
|
|
456
|
+
// start and end are both positive or both negative, or one is 0
|
|
457
|
+
let multiplier = 1;
|
|
458
|
+
if (valStart <= 0 && valEnd <= 0) {
|
|
459
|
+
multiplier = -1; // correct the sign at the end
|
|
460
|
+
valStart = -valStart;
|
|
461
|
+
valEnd = -valEnd;
|
|
462
|
+
}
|
|
463
|
+
const minVal = Math.min(valStart, valEnd);
|
|
464
|
+
const maxVal = Math.max(valStart, valEnd);
|
|
465
|
+
const rectPart = minVal * valDuration / options.integralUnit;
|
|
466
|
+
const trianglePart = (maxVal - minVal) * valDuration * 0.5 / options.integralUnit;
|
|
467
|
+
const integralAdd = (rectPart + trianglePart) * multiplier;
|
|
468
|
+
log(`${options.logId} Integral: ${k}[${kk}] : Add R${rectPart} + T${trianglePart} => ${integralAdd} from val=${valStart} to ${valEnd} for ${valDuration}`);
|
|
469
|
+
options.result[k].val.val += integralAdd;
|
|
470
|
+
} else {
|
|
471
|
+
// Values are on different sides of 0, so we need to find the 0 crossing
|
|
472
|
+
const zeroCrossing = Math.abs((valStart * valDuration) / (valEnd - valStart));
|
|
473
|
+
// Then calculate two linear segments, one from 0 to the crossing, and one from the crossing to the end
|
|
474
|
+
const trianglePart1 = valStart * zeroCrossing * 0.5 / options.integralUnit;
|
|
475
|
+
const trianglePart2 = valEnd * (valDuration - zeroCrossing) * 0.5 / options.integralUnit;
|
|
476
|
+
const integralAdd = trianglePart1 + trianglePart2;
|
|
477
|
+
log(`${options.logId} Integral: ${k}[${kk}] : Add T${trianglePart1} + T${trianglePart2} => ${integralAdd} from val=${valStart} to ${valEnd} for ${valDuration} (zero crossing ${zeroCrossing})`);
|
|
478
|
+
options.result[k].val.val += integralAdd;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
options.result[k] = {
|
|
482
|
+
ts: options.result[k].val.ts,
|
|
483
|
+
val: options.result[k].val.val
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
if (options.removeBorderValues) { // we cut out the additional results
|
|
487
|
+
options.result.splice(0, 1);
|
|
488
|
+
options.result.splice(-1, 1);
|
|
489
|
+
}
|
|
490
|
+
for (let j = options.result.length - 1; j >= 0; j--) {
|
|
491
|
+
if (options.result[j].val === null) {
|
|
492
|
+
// no one value in this interval
|
|
493
|
+
options.result.splice(j, 1);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
} else if (options.aggregate === 'percentile' || options.aggregate === 'quantile') {
|
|
497
|
+
if (options.removeBorderValues) { // we cut out the additional results
|
|
498
|
+
options.result.splice(0, 1);
|
|
499
|
+
options.quantileDatapoints.splice(0, 1);
|
|
500
|
+
options.result.splice(-1, 1);
|
|
501
|
+
options.quantileDatapoints.splice(-1, 1);
|
|
502
|
+
}
|
|
503
|
+
for (let k = options.result.length - 1; k >= 0; k--) {
|
|
504
|
+
if (options.result[k].val.ts) {
|
|
505
|
+
options.result[k] = {
|
|
506
|
+
ts: options.result[k].val.ts,
|
|
507
|
+
val: quantile(options.quantile, options.quantileDatapoints[k])
|
|
508
|
+
};
|
|
509
|
+
log(`${options.logId} Quantile ${k} ${options.result[k].ts}: ${options.quantileDatapoints[k].join(', ')} -> ${options.result[k].val}`);
|
|
510
|
+
} else {
|
|
511
|
+
// no one value in this interval
|
|
512
|
+
options.result.splice(k, 1);
|
|
513
|
+
options.quantileDatapoints.splice(k, 1);
|
|
286
514
|
}
|
|
287
515
|
}
|
|
288
516
|
} else {
|
|
517
|
+
if (options.removeBorderValues) { // we cut out the additional results
|
|
518
|
+
options.result.splice(0, 1);
|
|
519
|
+
options.result.splice(-1, 1);
|
|
520
|
+
}
|
|
289
521
|
for (let j = options.result.length - 1; j >= 0; j--) {
|
|
290
522
|
if (options.result[j].val.ts) {
|
|
291
523
|
options.result[j] = {
|
|
@@ -303,6 +535,12 @@ function finishAggregation(options) {
|
|
|
303
535
|
}
|
|
304
536
|
|
|
305
537
|
function beautify(options) {
|
|
538
|
+
let log = () => {};
|
|
539
|
+
if (options.debugLog) {
|
|
540
|
+
log = options.log || console.log;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
log(`${options.logId} Beautify: ${options.result.length} results`);
|
|
306
544
|
let preFirstValue = null;
|
|
307
545
|
let postLastValue = null;
|
|
308
546
|
|
|
@@ -354,10 +592,10 @@ function beautify(options) {
|
|
|
354
592
|
}
|
|
355
593
|
|
|
356
594
|
// check start and stop
|
|
357
|
-
if (options.result.length && options.aggregate !== 'none') {
|
|
595
|
+
if (options.result.length && options.aggregate !== 'none' && !options.removeBorderValues) {
|
|
358
596
|
const firstTS = options.result[0].ts;
|
|
359
597
|
|
|
360
|
-
if (firstTS > options.start) {
|
|
598
|
+
if (firstTS > options.start && !options.removeBorderValues) {
|
|
361
599
|
if (preFirstValue) {
|
|
362
600
|
const firstY = options.result[0].val;
|
|
363
601
|
// if steps
|
|
@@ -375,6 +613,7 @@ function beautify(options) {
|
|
|
375
613
|
// interpolate
|
|
376
614
|
const y = preFirstValue.val + (firstY - preFirstValue.val) * (options.start - preFirstValue.ts) / (firstTS - preFirstValue.ts);
|
|
377
615
|
options.result.unshift({ts: options.start, val: y, i: true});
|
|
616
|
+
log(`${options.logId} interpolate ${y} from ${preFirstValue.val} to ${firstY} as first return value`);
|
|
378
617
|
} else {
|
|
379
618
|
options.result.unshift({ts: options.start, val: null});
|
|
380
619
|
}
|
|
@@ -394,7 +633,7 @@ function beautify(options) {
|
|
|
394
633
|
}
|
|
395
634
|
|
|
396
635
|
const lastTS = options.result[options.result.length - 1].ts;
|
|
397
|
-
if (lastTS < options.end) {
|
|
636
|
+
if (lastTS < options.end && !options.removeBorderValues) {
|
|
398
637
|
if (postLastValue) {
|
|
399
638
|
// if steps
|
|
400
639
|
if (options.aggregate === 'onchange' || !options.aggregate) {
|
|
@@ -413,6 +652,7 @@ function beautify(options) {
|
|
|
413
652
|
// make interpolation
|
|
414
653
|
const _y = lastY + (postLastValue.val - lastY) * (options.end - lastTS) / (postLastValue.ts - lastTS);
|
|
415
654
|
options.result.push({ts: options.end, val: _y, i: true});
|
|
655
|
+
log(`${options.logId} interpolate ${_y} from ${lastY} to ${postLastValue.val} as last return value`);
|
|
416
656
|
} else {
|
|
417
657
|
options.result.push({ts: options.end, val: null});
|
|
418
658
|
}
|
|
@@ -435,7 +675,15 @@ function beautify(options) {
|
|
|
435
675
|
}
|
|
436
676
|
} else if (options.aggregate === 'none') {
|
|
437
677
|
if (options.count && options.result.length > options.count) {
|
|
438
|
-
options.result = options.result.slice(
|
|
678
|
+
options.result = options.result.slice(-options.count);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
if (options.addId) {
|
|
683
|
+
for (let i = 0; i < options.result.length; i++) {
|
|
684
|
+
if (!options.result[i].id && options.id) {
|
|
685
|
+
options.result[i].id = options.index || options.id;
|
|
686
|
+
}
|
|
439
687
|
}
|
|
440
688
|
}
|
|
441
689
|
}
|
|
@@ -458,7 +706,7 @@ function sendResponse(adapter, msg, options, data, startTime) {
|
|
|
458
706
|
if (data[0]) {
|
|
459
707
|
options.start = options.start || data[0].ts;
|
|
460
708
|
|
|
461
|
-
if (!options.aggregate || options.aggregate === 'onchange' || options.aggregate === 'none') {
|
|
709
|
+
if (!options.aggregate || options.aggregate === 'onchange' || options.aggregate === 'none' || options.preAggregated) {
|
|
462
710
|
aggregateData = {result: data, step: 0, sourceLength: data.length};
|
|
463
711
|
|
|
464
712
|
// convert ack from 0/1 to false/true
|
|
@@ -472,7 +720,7 @@ function sendResponse(adapter, msg, options, data, startTime) {
|
|
|
472
720
|
beautify(options);
|
|
473
721
|
|
|
474
722
|
if (options.aggregate === 'none' && options.count && options.result.length > options.count) {
|
|
475
|
-
options.result = options.result.slice(
|
|
723
|
+
options.result = options.result.slice(-options.count);
|
|
476
724
|
}
|
|
477
725
|
aggregateData.result = options.result;
|
|
478
726
|
} else {
|
|
@@ -571,6 +819,68 @@ function sendResponseCounter(adapter, msg, options, data, startTime) {
|
|
|
571
819
|
}
|
|
572
820
|
}
|
|
573
821
|
|
|
822
|
+
/**
|
|
823
|
+
* Get quantile value from an array.
|
|
824
|
+
*
|
|
825
|
+
* @param {Number} q - quantile
|
|
826
|
+
* @param {Array|TypedArray} list - list of sorted values (ascending)
|
|
827
|
+
*
|
|
828
|
+
* @return {number} Quantile value
|
|
829
|
+
*/
|
|
830
|
+
function getQuantileValue(q, list) {
|
|
831
|
+
if (q === 0) return list[0];
|
|
832
|
+
|
|
833
|
+
const index = list.length * q;
|
|
834
|
+
if (Number.isInteger(index)) {
|
|
835
|
+
// mean of two middle numbers
|
|
836
|
+
return (list[index - 1] + list[index]) / 2;
|
|
837
|
+
}
|
|
838
|
+
return list[Math.ceil(index - 1)];
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* Calculate quantile for given array of values.
|
|
843
|
+
*
|
|
844
|
+
* @template T
|
|
845
|
+
* @param {Number|Array<Number>} qOrPs - quantile or a list of quantiles
|
|
846
|
+
* @param {Array<T>|Array<Number>|TypedArray} list - array of values
|
|
847
|
+
* @param {function(T): Number} [fn] - optional function to extract a value from an array item
|
|
848
|
+
*
|
|
849
|
+
* @return {Number|T|Array<Number>|Array<T>}
|
|
850
|
+
*/
|
|
851
|
+
function quantile(qOrPs, list, fn) {
|
|
852
|
+
const q = Array.isArray(qOrPs) ? qOrPs : [qOrPs];
|
|
853
|
+
|
|
854
|
+
list = list.slice().sort(function (a, b) {
|
|
855
|
+
if (fn) {
|
|
856
|
+
a = fn(a);
|
|
857
|
+
b = fn(b);
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
a = Number.isNaN(a) ? Number.NEGATIVE_INFINITY : a;
|
|
861
|
+
b = Number.isNaN(b) ? Number.NEGATIVE_INFINITY : b;
|
|
862
|
+
|
|
863
|
+
if (a > b) return 1;
|
|
864
|
+
if (a < b) return -1;
|
|
865
|
+
|
|
866
|
+
return 0;
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
if (q.length === 1) {
|
|
870
|
+
return getQuantileValue(q[0], list);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
return q.map(function (q) {
|
|
874
|
+
return getQuantileValue(q, list);
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
function sortByTs(a, b) {
|
|
879
|
+
const aTs = a.ts;
|
|
880
|
+
const bTs = b.ts;
|
|
881
|
+
return (aTs < bTs) ? -1 : ((aTs > bTs) ? 1 : 0);
|
|
882
|
+
}
|
|
883
|
+
|
|
574
884
|
module.exports.sendResponseCounter = sendResponseCounter;
|
|
575
885
|
module.exports.sendResponse = sendResponse;
|
|
576
886
|
module.exports.initAggregate = initAggregate;
|