iobroker.sql 4.0.4 → 4.1.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 +92 -10
- package/admin/custom/assets/Components-nTLElZ7y.js +1 -0
- package/admin/custom/assets/DataBrowser-CyjKP_L-.js +1 -0
- package/admin/custom/assets/_virtual_mf-localSharedImportMap___mfe_internal__SqlComponentsSet__mf_owner__1-BKlHk-3o.js +1 -0
- package/admin/custom/assets/_virtual_mf___mfe_internal__SqlComponentsSet__mf_owner__1__loadShare___mf_0_iobroker_mf_1_gui_mf_2_components__loadShare__.js-1_ecMmwM.js +1243 -0
- package/admin/custom/assets/_virtual_mf___mfe_internal__SqlComponentsSet__mf_owner__1__loadShare___mf_0_iobroker_mf_1_json_mf_2_config__loadShare__.js-BJcL2b_b.js +1494 -0
- package/admin/custom/assets/_virtual_mf___mfe_internal__SqlComponentsSet__mf_owner__1__loadShare__react_mf_2_dom_mf_1_client__loadShare__.js-DOWbo5mU.js +9 -0
- package/admin/custom/assets/hostInit-CJsaB1F7.js +1 -0
- package/admin/custom/assets/index-D4ovp6PK.js +1 -0
- package/admin/custom/assets/lib-B-9Z42f0.js +3 -0
- package/admin/custom/assets/mf-entry-bootstrap-0-9382e792.js +41 -0
- package/admin/custom/assets/rolldown-runtime-C0FnF6B9.js +1 -0
- package/admin/custom/assets/virtualExposes-C4J_LR3_.js +1 -0
- package/admin/custom/assets/virtual_mf-REMOTE_ENTRY_ID___mfe_internal__SqlComponentsSet__customComponents_js-C7xpzcn4.js +1 -0
- package/admin/custom/assets/virtual_mf-exposes___mfe_internal__SqlComponentsSet__customComponents_js-DBI8_iGe.js +2 -0
- package/admin/custom/assets/vite-preload-helper-B7qeedMF.js +1 -0
- package/admin/custom/customComponents.js +12 -0
- package/admin/custom/i18n/de.json +30 -0
- package/admin/custom/i18n/en.json +30 -0
- package/admin/custom/i18n/es.json +30 -0
- package/admin/custom/i18n/fr.json +30 -0
- package/admin/custom/i18n/it.json +30 -0
- package/admin/custom/i18n/nl.json +30 -0
- package/admin/custom/i18n/pl.json +30 -0
- package/admin/custom/i18n/pt.json +30 -0
- package/admin/custom/i18n/ru.json +30 -0
- package/admin/custom/i18n/zh-cn.json +30 -0
- package/admin/custom/mf-manifest.json +1 -0
- package/admin/i18n/{de/translations.json → de.json} +7 -1
- package/admin/i18n/{en/translations.json → en.json} +7 -1
- package/admin/i18n/{es/translations.json → es.json} +7 -1
- package/admin/i18n/{fr/translations.json → fr.json} +7 -1
- package/admin/i18n/{it/translations.json → it.json} +7 -1
- package/admin/i18n/{nl/translations.json → nl.json} +7 -1
- package/admin/i18n/{pl/translations.json → pl.json} +7 -1
- package/admin/i18n/{pt/translations.json → pt.json} +7 -1
- package/admin/i18n/{ru/translations.json → ru.json} +7 -1
- package/admin/i18n/{zh-cn/translations.json → zh-cn.json} +7 -1
- package/admin/jsonConfig.json +14 -0
- package/admin/jsonCustom.json +18 -0
- package/build/lib/mssql.js +22 -0
- package/build/lib/mssql.js.map +1 -1
- package/build/lib/mysql.js +22 -0
- package/build/lib/mysql.js.map +1 -1
- package/build/lib/postgresql.js +22 -0
- package/build/lib/postgresql.js.map +1 -1
- package/build/lib/sqlite.js +22 -0
- package/build/lib/sqlite.js.map +1 -1
- package/build/main.js +308 -83
- package/build/main.js.map +1 -1
- package/io-package.json +27 -27
- package/package.json +14 -7
- package/build/lib/aggregate.js +0 -1490
- package/build/lib/aggregate.js.map +0 -1
package/build/lib/aggregate.js
DELETED
|
@@ -1,1490 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// THIS file should be identical with SQL and history adapter's one
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.calcDiff = calcDiff;
|
|
5
|
-
exports.initAggregate = initAggregate;
|
|
6
|
-
exports.aggregation = aggregation;
|
|
7
|
-
exports.finishAggregationForIntegralEx = finishAggregationForIntegralEx;
|
|
8
|
-
exports.finishAggregationForIntegral = finishAggregationForIntegral;
|
|
9
|
-
exports.finishAggregationPercentile = finishAggregationPercentile;
|
|
10
|
-
exports.finishAggregation = finishAggregation;
|
|
11
|
-
exports.beautify = beautify;
|
|
12
|
-
exports.sendResponse = sendResponse;
|
|
13
|
-
exports.sendResponseCounter = sendResponseCounter;
|
|
14
|
-
exports.sortByTs = sortByTs;
|
|
15
|
-
/**
|
|
16
|
-
* Calculate the square of triangle between two data points on chart
|
|
17
|
-
* val | |
|
|
18
|
-
* | | /-----|-----
|
|
19
|
-
* | | /__--/######|
|
|
20
|
-
* |----|--/############|
|
|
21
|
-
* | |###############|
|
|
22
|
-
* +----o---------------n--->time
|
|
23
|
-
* Square is the #, deltaT = x2 - x1, DeltaY = y2 - y1
|
|
24
|
-
*/
|
|
25
|
-
function calcDiff(oldVal, newVal) {
|
|
26
|
-
// (Xnew - Xold) / 3600000 (to hours)
|
|
27
|
-
const deltaT = (newVal.ts - oldVal.ts) / 3_600_000; // ms => hours
|
|
28
|
-
// if deltaT is negative, we have a problem as the time cannot go back
|
|
29
|
-
if (deltaT < 0) {
|
|
30
|
-
return { square: 0, deltaT: 0 };
|
|
31
|
-
}
|
|
32
|
-
const square = ((newVal.val || 0) + (oldVal.val || 0)) * (deltaT * 0.5);
|
|
33
|
-
return { square, deltaT };
|
|
34
|
-
}
|
|
35
|
-
function interpolate2points(p1, p2, ts) {
|
|
36
|
-
const dx = p2.ts - p1.ts;
|
|
37
|
-
// threat null as zero
|
|
38
|
-
const dy = (p2.val || 0) - (p1.val || 0);
|
|
39
|
-
if (!dx) {
|
|
40
|
-
return p1.val;
|
|
41
|
-
}
|
|
42
|
-
return (dy * (ts - p1.ts)) / dx + p1.val;
|
|
43
|
-
}
|
|
44
|
-
function initAggregate(initialOptions, id, timeIntervals, log) {
|
|
45
|
-
const options = initialOptions;
|
|
46
|
-
options.log = log;
|
|
47
|
-
options.id = id; // id is needed for because of addId option
|
|
48
|
-
if (!options.log) {
|
|
49
|
-
options.log = () => { };
|
|
50
|
-
// To save the complex outputs
|
|
51
|
-
options.logDebug = false;
|
|
52
|
-
}
|
|
53
|
-
// step; // 1 Step is 1 second
|
|
54
|
-
if (options.step === null || options.step === undefined || options.step <= 0) {
|
|
55
|
-
options.step = (options.end - options.start) / options.count;
|
|
56
|
-
}
|
|
57
|
-
// Limit 2000
|
|
58
|
-
if ((options.end - options.start) / options.step > options.limit) {
|
|
59
|
-
options.step = (options.end - options.start) / options.limit;
|
|
60
|
-
}
|
|
61
|
-
if (timeIntervals) {
|
|
62
|
-
options.timeIntervals = timeIntervals;
|
|
63
|
-
options.maxIndex = options.timeIntervals.length - 1;
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
// MaxIndex is the index, that can really called without -1
|
|
67
|
-
options.maxIndex = Math.ceil((options.end - options.start) / options.step - 1);
|
|
68
|
-
}
|
|
69
|
-
options.processing = [];
|
|
70
|
-
options.result = []; // finalResult
|
|
71
|
-
options.averageCount = [];
|
|
72
|
-
options.quantileDataPoints = [];
|
|
73
|
-
options.integralDataPoints = [];
|
|
74
|
-
options.totalIntegralDataPoints = [];
|
|
75
|
-
options.aggregate = options.aggregate || 'minmax';
|
|
76
|
-
options.overallLength = 0;
|
|
77
|
-
options.currentTimeInterval = 0;
|
|
78
|
-
if (options.aggregate === 'percentile') {
|
|
79
|
-
if (typeof options.percentile !== 'number' || options.percentile < 0 || options.percentile > 100) {
|
|
80
|
-
options.percentile = 50;
|
|
81
|
-
}
|
|
82
|
-
options.quantile = options.percentile / 100; // Internally we use quantile for percentile too
|
|
83
|
-
}
|
|
84
|
-
if (options.aggregate === 'quantile') {
|
|
85
|
-
if (typeof options.quantile !== 'number' || options.quantile < 0 || options.quantile > 1) {
|
|
86
|
-
options.quantile = 0.5;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
if (options.aggregate === 'integral') {
|
|
90
|
-
if (typeof options.integralUnit !== 'number' || options.integralUnit <= 0) {
|
|
91
|
-
options.integralUnit = 60;
|
|
92
|
-
}
|
|
93
|
-
options.integralUnit *= 1000; // Convert to milliseconds
|
|
94
|
-
}
|
|
95
|
-
if (options.logDebug && options.log) {
|
|
96
|
-
options.log(`Initialize: maxIndex = ${options.maxIndex}, step = ${!timeIntervals ? options.step : 'smart'}, start = ${options.start}, end = ${options.end}`);
|
|
97
|
-
}
|
|
98
|
-
// pre-fill the result with timestamps (add one before start and one after end)
|
|
99
|
-
try {
|
|
100
|
-
options.processing.length = options.maxIndex + 2;
|
|
101
|
-
}
|
|
102
|
-
catch (err) {
|
|
103
|
-
err.message += `: ${options.maxIndex + 2}`;
|
|
104
|
-
throw err;
|
|
105
|
-
}
|
|
106
|
-
// We define the array length, but do not prefill values, we do that on runtime when needed
|
|
107
|
-
options.processing[0] = {
|
|
108
|
-
val: { ts: null, val: null },
|
|
109
|
-
max: { ts: null, val: null },
|
|
110
|
-
min: { ts: null, val: null },
|
|
111
|
-
start: { ts: null, val: null },
|
|
112
|
-
end: { ts: null, val: null },
|
|
113
|
-
};
|
|
114
|
-
options.processing[options.maxIndex + 2] = {
|
|
115
|
-
val: { ts: null, val: null },
|
|
116
|
-
max: { ts: null, val: null },
|
|
117
|
-
min: { ts: null, val: null },
|
|
118
|
-
start: { ts: null, val: null },
|
|
119
|
-
end: { ts: null, val: null },
|
|
120
|
-
};
|
|
121
|
-
if (options.aggregate === 'average') {
|
|
122
|
-
options.averageCount[0] = 0;
|
|
123
|
-
options.averageCount[options.maxIndex + 2] = 0;
|
|
124
|
-
}
|
|
125
|
-
if (options.aggregate === 'percentile' || options.aggregate === 'quantile') {
|
|
126
|
-
options.quantileDataPoints[0] = [];
|
|
127
|
-
options.quantileDataPoints[options.maxIndex + 2] = [];
|
|
128
|
-
}
|
|
129
|
-
if (options.aggregate === 'integral') {
|
|
130
|
-
options.integralDataPoints[0] = [];
|
|
131
|
-
options.integralDataPoints[options.maxIndex + 2] = [];
|
|
132
|
-
}
|
|
133
|
-
return options;
|
|
134
|
-
}
|
|
135
|
-
function aggregation(options, data) {
|
|
136
|
-
let index;
|
|
137
|
-
let preIndex;
|
|
138
|
-
let collectedTooEarlyData = [];
|
|
139
|
-
let collectedTooLateData = [];
|
|
140
|
-
let preIndexValueFound = false;
|
|
141
|
-
let postIndexValueFound = false;
|
|
142
|
-
for (let i = 0; i < data.length; i++) {
|
|
143
|
-
if (!data[i]) {
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
if (typeof data[i].ts !== 'number') {
|
|
147
|
-
data[i].ts = parseInt(data[i].ts, 10);
|
|
148
|
-
}
|
|
149
|
-
if (options.timeIntervals) {
|
|
150
|
-
// We have specific time intervals and collects all data according to this information
|
|
151
|
-
const dataTs = data[i].ts;
|
|
152
|
-
// Find a time interval for this timestamp
|
|
153
|
-
if (dataTs < options.start) {
|
|
154
|
-
index = 0;
|
|
155
|
-
}
|
|
156
|
-
else if (dataTs >= options.end) {
|
|
157
|
-
index = options.timeIntervals.length + 1;
|
|
158
|
-
}
|
|
159
|
-
else if (dataTs >= options.timeIntervals[options.currentTimeInterval].end) {
|
|
160
|
-
// Look for the next interval
|
|
161
|
-
options.currentTimeInterval++;
|
|
162
|
-
while (options.currentTimeInterval < options.timeIntervals.length) {
|
|
163
|
-
if (options.timeIntervals[options.currentTimeInterval].start <= dataTs &&
|
|
164
|
-
dataTs < options.timeIntervals[options.currentTimeInterval].end) {
|
|
165
|
-
break;
|
|
166
|
-
}
|
|
167
|
-
options.currentTimeInterval++;
|
|
168
|
-
}
|
|
169
|
-
index = options.currentTimeInterval + 1;
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
index = options.currentTimeInterval + 1;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
// Our intervals are equidistant and we can calculate them
|
|
177
|
-
preIndex = Math.floor((data[i].ts - options.start) / options.step);
|
|
178
|
-
// store all border values
|
|
179
|
-
if (preIndex < 0) {
|
|
180
|
-
index = 0;
|
|
181
|
-
// if the ts is even earlier than the "pre-interval" ignore it, else we collect all data there
|
|
182
|
-
if (preIndex < -1) {
|
|
183
|
-
collectedTooEarlyData.push(data[i]);
|
|
184
|
-
continue;
|
|
185
|
-
}
|
|
186
|
-
preIndexValueFound = true;
|
|
187
|
-
}
|
|
188
|
-
else if (preIndex > options.maxIndex) {
|
|
189
|
-
index = options.maxIndex + 2;
|
|
190
|
-
// if the ts is even later than the "post-interval" ignore it, else we collect all data there
|
|
191
|
-
if (preIndex > options.maxIndex + 1) {
|
|
192
|
-
collectedTooLateData.push(data[i]);
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
postIndexValueFound = true;
|
|
196
|
-
}
|
|
197
|
-
else {
|
|
198
|
-
index = preIndex + 1;
|
|
199
|
-
}
|
|
200
|
-
options.overallLength++;
|
|
201
|
-
}
|
|
202
|
-
// Init data for time slot
|
|
203
|
-
if (options.processing[index] === undefined) {
|
|
204
|
-
// lazy initialization of data structure
|
|
205
|
-
options.processing[index] = {
|
|
206
|
-
val: { ts: null, val: null },
|
|
207
|
-
max: { ts: null, val: null },
|
|
208
|
-
min: { ts: null, val: null },
|
|
209
|
-
start: { ts: null, val: null },
|
|
210
|
-
end: { ts: null, val: null },
|
|
211
|
-
};
|
|
212
|
-
if (options.aggregate === 'average' || options.aggregate === 'count') {
|
|
213
|
-
options.averageCount[index] = 0;
|
|
214
|
-
}
|
|
215
|
-
if (options.aggregate === 'percentile' || options.aggregate === 'quantile') {
|
|
216
|
-
options.quantileDataPoints[index] = [];
|
|
217
|
-
}
|
|
218
|
-
if (options.aggregate === 'integral') {
|
|
219
|
-
options.integralDataPoints[index] = [];
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
aggregationLogic(data[i], index, options);
|
|
223
|
-
}
|
|
224
|
-
// If no data was found in the pre-interval, but we have earlier data, we put the latest of them in the pre-interval
|
|
225
|
-
if (!preIndexValueFound && collectedTooEarlyData.length > 0) {
|
|
226
|
-
collectedTooEarlyData = collectedTooEarlyData.sort(sortByTs);
|
|
227
|
-
options.overallLength++;
|
|
228
|
-
aggregationLogic(collectedTooEarlyData[collectedTooEarlyData.length - 1], 0, options);
|
|
229
|
-
}
|
|
230
|
-
// If no data was found in the post-interval, but we have later data, we put the earliest of them in the post-interval
|
|
231
|
-
if (!postIndexValueFound && collectedTooLateData.length > 0) {
|
|
232
|
-
collectedTooLateData = collectedTooLateData.sort(sortByTs);
|
|
233
|
-
options.overallLength++;
|
|
234
|
-
aggregationLogic(collectedTooLateData[0], options.maxIndex + 2, options);
|
|
235
|
-
}
|
|
236
|
-
return { processing: options.processing, step: options.step, sourceLength: data.length };
|
|
237
|
-
}
|
|
238
|
-
/** Execute logic for every entry in the initial series array */
|
|
239
|
-
function aggregationLogic(data, index, options) {
|
|
240
|
-
if (!options.processing || !options.processing[index]) {
|
|
241
|
-
if (options.logDebug && options.log) {
|
|
242
|
-
options.log(`Data index ${index} not initialized, ignore!`);
|
|
243
|
-
}
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
if (options.aggregate !== 'minmax' && !options.processing[index].val.ts) {
|
|
247
|
-
if (options.timeIntervals) {
|
|
248
|
-
if (index === 0) {
|
|
249
|
-
// If it is pre interval, make an estimation
|
|
250
|
-
options.processing[0].val.ts =
|
|
251
|
-
options.timeIntervals[0].start -
|
|
252
|
-
Math.round((options.timeIntervals[0].end - options.timeIntervals[0].start) / 2);
|
|
253
|
-
}
|
|
254
|
-
else if (index > options.timeIntervals.length) {
|
|
255
|
-
// If it is post interval, make an estimation
|
|
256
|
-
options.processing[index].val.ts =
|
|
257
|
-
options.timeIntervals[options.timeIntervals.length - 1].end +
|
|
258
|
-
Math.round((options.timeIntervals[options.timeIntervals.length - 1].end -
|
|
259
|
-
options.timeIntervals[options.timeIntervals.length - 1].start) /
|
|
260
|
-
2);
|
|
261
|
-
}
|
|
262
|
-
else {
|
|
263
|
-
// Get the middle of the interval
|
|
264
|
-
options.processing[index].val.ts =
|
|
265
|
-
options.timeIntervals[index - 1].start +
|
|
266
|
-
Math.round((options.timeIntervals[index - 1].end - options.timeIntervals[index - 1].start) / 2);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
else {
|
|
270
|
-
options.processing[index].val.ts = Math.round(options.start + (index - 1 + 0.5) * options.step);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
if (options.aggregate === 'max') {
|
|
274
|
-
if (options.processing[index].val.val === null || options.processing[index].val.val < data.val) {
|
|
275
|
-
options.processing[index].val.val = data.val;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
else if (options.aggregate === 'min') {
|
|
279
|
-
if (options.processing[index].val.val === null || options.processing[index].val.val > data.val) {
|
|
280
|
-
options.processing[index].val.val = data.val;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
else if (options.aggregate === 'average') {
|
|
284
|
-
options.processing[index].val.val += parseFloat(data.val);
|
|
285
|
-
options.averageCount[index]++;
|
|
286
|
-
}
|
|
287
|
-
else if (options.aggregate === 'count') {
|
|
288
|
-
options.averageCount[index]++;
|
|
289
|
-
}
|
|
290
|
-
else if (options.aggregate === 'total') {
|
|
291
|
-
options.processing[index].val.val += parseFloat(data.val);
|
|
292
|
-
}
|
|
293
|
-
else if (options.aggregate === 'minmax') {
|
|
294
|
-
if (options.processing[index].min.ts === null) {
|
|
295
|
-
options.processing[index].min.ts = data.ts;
|
|
296
|
-
options.processing[index].min.val = data.val;
|
|
297
|
-
options.processing[index].max.ts = data.ts;
|
|
298
|
-
options.processing[index].max.val = data.val;
|
|
299
|
-
options.processing[index].start.ts = data.ts;
|
|
300
|
-
options.processing[index].start.val = data.val;
|
|
301
|
-
options.processing[index].end.ts = data.ts;
|
|
302
|
-
options.processing[index].end.val = data.val;
|
|
303
|
-
}
|
|
304
|
-
else {
|
|
305
|
-
if (data.val !== null && data.val !== undefined) {
|
|
306
|
-
if (data.val > options.processing[index].max.val) {
|
|
307
|
-
options.processing[index].max.ts = data.ts;
|
|
308
|
-
options.processing[index].max.val = data.val;
|
|
309
|
-
}
|
|
310
|
-
else if (data.val < options.processing[index].min.val) {
|
|
311
|
-
options.processing[index].min.ts = data.ts;
|
|
312
|
-
options.processing[index].min.val = data.val;
|
|
313
|
-
}
|
|
314
|
-
if (data.ts > options.processing[index].end.ts) {
|
|
315
|
-
options.processing[index].end.ts = data.ts;
|
|
316
|
-
options.processing[index].end.val = data.val;
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
else {
|
|
320
|
-
if (data.ts > options.processing[index].end.ts) {
|
|
321
|
-
options.processing[index].end.ts = data.ts;
|
|
322
|
-
options.processing[index].end.val = null;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
else if (options.aggregate === 'percentile' || options.aggregate === 'quantile') {
|
|
328
|
-
options.quantileDataPoints[index].push(data.val || 0);
|
|
329
|
-
if (options.logDebug && options.log) {
|
|
330
|
-
options.log(`Quantile ${index}: Add ts= ${data.ts} val=${data.val}`);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
else if (options.aggregate === 'integral') {
|
|
334
|
-
options.integralDataPoints[index].push(data);
|
|
335
|
-
if (options.logDebug && options.log) {
|
|
336
|
-
options.log(`Integral ${index}: Add ts= ${data.ts} val=${data.val}`);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
else if (options.aggregate === 'integralTotal') {
|
|
340
|
-
options.totalIntegralDataPoints?.push(data);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
/**
|
|
344
|
-
* finishAggregationForIntegralEx
|
|
345
|
-
*
|
|
346
|
-
* Purpose:
|
|
347
|
-
* - Calculate integrals per defined time intervals (smart intervals mode per hour).
|
|
348
|
-
* - Options must contain `timeIntervals` and `integralDataPoints`.
|
|
349
|
-
* - Produces `options.result` as an array with one entry per `timeIntervals` element.
|
|
350
|
-
*
|
|
351
|
-
* Input:
|
|
352
|
-
* - options: InternalHistoryOptions already populated by previous aggregation steps.
|
|
353
|
-
* Required fields:
|
|
354
|
-
* - timeIntervals: TimeInterval[] (array of intervals to calculate over)
|
|
355
|
-
* - integralDataPoints: IobDataEntry[][] where length == timeIntervals.length + 2
|
|
356
|
-
* (one pre-interval array at index 0, one post-interval array at last index)
|
|
357
|
-
* - logDebug (optional) and log (optional) for detailed debug output
|
|
358
|
-
*
|
|
359
|
-
* Output:
|
|
360
|
-
* - options.result: IobDataEntry[] of length = timeIntervals.length
|
|
361
|
-
* Each entry:
|
|
362
|
-
* - ts: representative timestamp (interval midpoint)
|
|
363
|
-
* - val: integral value for that interval (number)
|
|
364
|
-
* - time: ISO string of ts when logDebug is enabled (optional)
|
|
365
|
-
*
|
|
366
|
-
* Behaviour / Algorithm overview:
|
|
367
|
-
* 1. Find the latest data point that lies in the pre-interval (index 0).
|
|
368
|
-
* That point is used as the `current` known value to interpolate forward.
|
|
369
|
-
* 2. Initialize finalResult with one entry per time interval; each entry's `val` starts at 0
|
|
370
|
-
* and `ts` is set to the interval midpoint (used as representative timestamp).
|
|
371
|
-
* 3. If no pre-interval `current` point exists, nothing to integrate -> return finalResult of zeros.
|
|
372
|
-
* 4. Iterate intervals from the first interval that might contain data (index found in step 1):
|
|
373
|
-
* - For interval i, obtain the bucket workDP stored at `integralDataPoints[i + 1]`.
|
|
374
|
-
* - If bucket has data and `current` exists:
|
|
375
|
-
* - Interpolate a point at interval start using linear interpolation between `current` and bucket[0].
|
|
376
|
-
* - Insert that interpolated start point at the beginning of bucket.
|
|
377
|
-
* - Update `current` to the last point in this bucket (the newest in that bucket).
|
|
378
|
-
* - Find the next known data point `next` in later buckets (search forward) and remember its interval index.
|
|
379
|
-
* - If no `next` found, assume no further data -> stop filling remaining intervals.
|
|
380
|
-
* - Ensure the bucket has at least a start point; if empty, compute and push a start point interpolated between `current` and `next`.
|
|
381
|
-
* - Always compute and push an end point for the interval at `workTi.end - 1`, interpolated between `current` and `next`.
|
|
382
|
-
* 5. After all intervals that can be filled have a start and end point, compute the integral for each interval:
|
|
383
|
-
* - For each bucket (workDP) compute sum of trapezoidal areas between consecutive points using `calcDiff(...).square`.
|
|
384
|
-
* - Set finalResult[i].val = sum for that interval.
|
|
385
|
-
*
|
|
386
|
-
* Key assumptions and details:
|
|
387
|
-
* - `integralDataPoints` array length is `timeIntervals.length + 2`:
|
|
388
|
-
* index 0 = pre-interval (values before start),
|
|
389
|
-
* indices 1..timeIntervals.length = buckets for each interval,
|
|
390
|
-
* index timeIntervals.length + 1 = post-interval (values after end)
|
|
391
|
-
* - The function uses linear interpolation (via interpolate2points) to estimate values at interval borders.
|
|
392
|
-
* - Interval representative timestamp (`ts`) is computed as start + round((end - start) / 2).
|
|
393
|
-
* - End timestamp for each interval when interpolating is `workTi.end - 1` (inclusive millisecond before next interval).
|
|
394
|
-
* - If no `current` (no pre-interval data), return early leaving all zeros.
|
|
395
|
-
* - If no `next` is found while iterating forward, the function stops filling further intervals,
|
|
396
|
-
* because it assumes no future data to interpolate to.
|
|
397
|
-
* - Debug logs (if `options.logDebug`) include created interpolated points and interval contents.
|
|
398
|
-
*
|
|
399
|
-
* Edge cases:
|
|
400
|
-
* - Zero-length intervals (end == start) are not expected in `timeIntervals`.
|
|
401
|
-
* - If `interpolate2points` gets identical timestamps it returns p1.val (guard in helper).
|
|
402
|
-
* - The function mutates `options.integralDataPoints` by inserting start/end interpolated entries.
|
|
403
|
-
* - If `workDP` remains undefined for an interval, `finalResult` keeps that interval's value as 0.
|
|
404
|
-
*/
|
|
405
|
-
function finishAggregationForIntegralEx(options) {
|
|
406
|
-
// The first interval is pre-interval: its data is used only to determine the initial "current" value.
|
|
407
|
-
let index = 0;
|
|
408
|
-
let workDP;
|
|
409
|
-
let next = null;
|
|
410
|
-
let current = null;
|
|
411
|
-
const finalResult = [];
|
|
412
|
-
if (!options.integralDataPoints || !options.timeIntervals) {
|
|
413
|
-
throw new Error('finishAggregationForIntegralEx: options.integralDataPoints or options.timeIntervals missing');
|
|
414
|
-
}
|
|
415
|
-
// 1) Find the first non-empty integral bucket starting from pre-interval (index 0).
|
|
416
|
-
// The last element of the first non-empty bucket is the newest value before options.start.
|
|
417
|
-
// We must remember, that options.integralDataPoints is longer than options.timeIntervals on 2. It hast pre- and post- values
|
|
418
|
-
do {
|
|
419
|
-
workDP = options.integralDataPoints[index];
|
|
420
|
-
if (workDP?.length) {
|
|
421
|
-
// It must be the newest value before options.start
|
|
422
|
-
current = workDP[workDP.length - 1];
|
|
423
|
-
break;
|
|
424
|
-
}
|
|
425
|
-
index++;
|
|
426
|
-
} while (index < options.integralDataPoints.length);
|
|
427
|
-
// 2) Initialize finalResult entries for every time interval with ts = midpoint and val = 0
|
|
428
|
-
for (let i = 0; i < options.timeIntervals.length; i++) {
|
|
429
|
-
const oneInterval = {
|
|
430
|
-
// compute midpoint deterministically: start + round((end - start) / 2)
|
|
431
|
-
ts: options.timeIntervals[i].start +
|
|
432
|
-
Math.round((options.timeIntervals[i].end - options.timeIntervals[i].start) / 2),
|
|
433
|
-
val: 0,
|
|
434
|
-
};
|
|
435
|
-
// Add ISO string for easier debugging if requested
|
|
436
|
-
oneInterval.time = new Date(oneInterval.ts).toISOString();
|
|
437
|
-
finalResult.push(oneInterval);
|
|
438
|
-
}
|
|
439
|
-
// assign preliminary result array back to options
|
|
440
|
-
options.result = finalResult;
|
|
441
|
-
// 3) If there is no value before start, nothing to integrate -> keep zeroed result
|
|
442
|
-
if (!current) {
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
let workTi;
|
|
446
|
-
// Holds index of the interval where the next known data was found during forward search
|
|
447
|
-
let nextIntervalIndex = null;
|
|
448
|
-
// 4) Iterate intervals starting from the one where we might have data (index)
|
|
449
|
-
// Calculate for every interval the start
|
|
450
|
-
for (let i = index; i < options.timeIntervals.length; i++) {
|
|
451
|
-
// bucket for interval i is stored at integralDataPoints[i + 1]
|
|
452
|
-
workDP = options.integralDataPoints[i + 1];
|
|
453
|
-
workTi = options.timeIntervals[i];
|
|
454
|
-
// If this bucket already contains points, and we have a prior 'current', insert an interpolated start point
|
|
455
|
-
if (workDP?.length && current) {
|
|
456
|
-
// calculate the first value in this interval
|
|
457
|
-
const firstValue = interpolate2points(current, workDP[0], workTi.start);
|
|
458
|
-
const time = { ts: workTi.start, val: firstValue };
|
|
459
|
-
if (options.logDebug && options.log) {
|
|
460
|
-
time.time = new Date(time.ts).toISOString();
|
|
461
|
-
}
|
|
462
|
-
// Insert at beginning to make sure bucket starts at interval boundary
|
|
463
|
-
workDP.unshift(time);
|
|
464
|
-
// Update current to newest in this bucket for subsequent interpolation
|
|
465
|
-
current = workDP[workDP.length - 1];
|
|
466
|
-
}
|
|
467
|
-
// Find the next known datapoint in later buckets (only search if previous found next is not usable)
|
|
468
|
-
if (nextIntervalIndex === null || nextIntervalIndex <= i) {
|
|
469
|
-
next = null;
|
|
470
|
-
let j = i + 1;
|
|
471
|
-
do {
|
|
472
|
-
// check bucket j + 1 because of pre- / post-encodings (buckets for intervals are shifted by 1)
|
|
473
|
-
if (options.integralDataPoints[j + 1]?.length) {
|
|
474
|
-
// next is the earliest datapoint in that later bucket
|
|
475
|
-
next = options.integralDataPoints[j + 1][0];
|
|
476
|
-
nextIntervalIndex = j;
|
|
477
|
-
break;
|
|
478
|
-
}
|
|
479
|
-
j++;
|
|
480
|
-
} while (j <= options.timeIntervals.length);
|
|
481
|
-
}
|
|
482
|
-
// If no next datapoint exists, assume no more future data available -> stop filling further intervals
|
|
483
|
-
if (!next) {
|
|
484
|
-
// We assume, that no more data will come
|
|
485
|
-
break;
|
|
486
|
-
}
|
|
487
|
-
// Ensure the bucket exists
|
|
488
|
-
options.integralDataPoints[i + 1] = options.integralDataPoints[i + 1] || [];
|
|
489
|
-
workDP = options.integralDataPoints[i + 1];
|
|
490
|
-
// If this bucket is empty, compute an interpolated start point between current and next
|
|
491
|
-
if (!workDP.length) {
|
|
492
|
-
// place first value
|
|
493
|
-
// calculate the first value in this interval
|
|
494
|
-
const firstValue = interpolate2points(current, next, workTi.start);
|
|
495
|
-
const time = { ts: workTi.start, val: firstValue };
|
|
496
|
-
if (options.logDebug && options.log) {
|
|
497
|
-
time.time = new Date(time.ts).toISOString();
|
|
498
|
-
}
|
|
499
|
-
workDP.push(time);
|
|
500
|
-
}
|
|
501
|
-
// Always compute an interpolated end point at workTi.end - 1 to make the bucket closed for integral calculation
|
|
502
|
-
const lastValue = interpolate2points(current, next, workTi.end - 1);
|
|
503
|
-
const time = { ts: workTi.end - 1, val: lastValue };
|
|
504
|
-
if (options.logDebug && options.log) {
|
|
505
|
-
time.time = new Date(time.ts).toISOString();
|
|
506
|
-
}
|
|
507
|
-
workDP.push(time);
|
|
508
|
-
}
|
|
509
|
-
// 5) Now that start and end points are ensured where possible, compute integral for each interval
|
|
510
|
-
for (let i = 0; i < options.timeIntervals.length; i++) {
|
|
511
|
-
workDP = options.integralDataPoints[i + 1];
|
|
512
|
-
if (!workDP) {
|
|
513
|
-
// leave finalResult[i].val as 0 when no bucket exists
|
|
514
|
-
continue;
|
|
515
|
-
}
|
|
516
|
-
finalResult[i].val = calcIntegralForPeriod(workDP);
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
/**
|
|
520
|
-
* This function calculates integral for one time period.
|
|
521
|
-
*/
|
|
522
|
-
function calcIntegralForPeriod(workDP) {
|
|
523
|
-
let sum = 0;
|
|
524
|
-
for (let i = 0; i < workDP.length - 1; i++) {
|
|
525
|
-
sum += calcDiff(workDP[i], workDP[i + 1]).square;
|
|
526
|
-
}
|
|
527
|
-
return sum;
|
|
528
|
-
}
|
|
529
|
-
function finishAggregationForIntegral(options) {
|
|
530
|
-
let preBorderValueRemoved = false;
|
|
531
|
-
let postBorderValueRemoved = false;
|
|
532
|
-
const originalResultLength = options.processing.length;
|
|
533
|
-
const finalResult = [];
|
|
534
|
-
// If timeIntervals are used, delegate to the specialized implementation.
|
|
535
|
-
if (options.timeIntervals) {
|
|
536
|
-
finishAggregationForIntegralEx(options);
|
|
537
|
-
return;
|
|
538
|
-
}
|
|
539
|
-
if (!options.processing) {
|
|
540
|
-
return;
|
|
541
|
-
}
|
|
542
|
-
// Iterate all processing slots (including pre/post border entries)
|
|
543
|
-
for (let k = 0; k < options.processing.length; k++) {
|
|
544
|
-
let indexStartTs;
|
|
545
|
-
let indexEndTs;
|
|
546
|
-
/*if (options.timeIntervals) {
|
|
547
|
-
if (k === 0) {
|
|
548
|
-
indexEndTs = options.timeIntervals as TimeInterval[][0].start;
|
|
549
|
-
indexStartTs =
|
|
550
|
-
options.timeIntervals[0].start - (options.timeIntervals[0].end - options.timeIntervals[0].start);
|
|
551
|
-
} else if (k >= options.timeIntervals.length) {
|
|
552
|
-
indexStartTs = options.timeIntervals[options.timeIntervals.length - 1].end;
|
|
553
|
-
indexEndTs =
|
|
554
|
-
indexStartTs + (indexStartTs - options.timeIntervals[options.timeIntervals.length - 1].start);
|
|
555
|
-
} else {
|
|
556
|
-
indexStartTs = options.timeIntervals[k - 1].start;
|
|
557
|
-
indexEndTs = options.timeIntervals[k - 1].end;
|
|
558
|
-
}
|
|
559
|
-
} else */ {
|
|
560
|
-
indexStartTs = options.start + (k - 1) * options.step;
|
|
561
|
-
indexEndTs = indexStartTs + options.step;
|
|
562
|
-
}
|
|
563
|
-
const len = options.integralDataPoints[k]?.length;
|
|
564
|
-
if (len) {
|
|
565
|
-
// Sort data points by ts first
|
|
566
|
-
options.integralDataPoints[k].sort(sortByTs);
|
|
567
|
-
}
|
|
568
|
-
// Make sure that we have entries that always start at the beginning of the interval
|
|
569
|
-
if ((!len || options.integralDataPoints[k][0].ts > indexStartTs) &&
|
|
570
|
-
options.integralDataPoints[k - 1] &&
|
|
571
|
-
options.integralDataPoints[k - 1][options.integralDataPoints[k - 1].length - 1]) {
|
|
572
|
-
// if the first entry of this interval started somewhere in the start of the interval, add a start entry
|
|
573
|
-
// same if there is no entry at all in the timeframe, use last entry from interval before
|
|
574
|
-
options.integralDataPoints[k] = options.integralDataPoints[k] || [];
|
|
575
|
-
const time = {
|
|
576
|
-
ts: indexStartTs,
|
|
577
|
-
val: options.integralDataPoints[k - 1][options.integralDataPoints[k - 1].length - 1].val,
|
|
578
|
-
};
|
|
579
|
-
if (options.logDebug && options.log) {
|
|
580
|
-
time.time = new Date(time.ts).toISOString();
|
|
581
|
-
}
|
|
582
|
-
options.integralDataPoints[k].unshift(time);
|
|
583
|
-
if (options.logDebug && options.log) {
|
|
584
|
-
options.log(`Integral: ${k}: Added start entry for interval with ts=${indexStartTs}, val=${options.integralDataPoints[k][0].val}`);
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
else if (len && options.integralDataPoints[k][0].ts > indexStartTs) {
|
|
588
|
-
const time = {
|
|
589
|
-
ts: indexStartTs,
|
|
590
|
-
val: options.integralDataPoints[k][0].val,
|
|
591
|
-
};
|
|
592
|
-
if (options.logDebug && options.log) {
|
|
593
|
-
time.time = new Date(time.ts).toISOString();
|
|
594
|
-
}
|
|
595
|
-
options.integralDataPoints[k].unshift(time);
|
|
596
|
-
if (options.logDebug && options.log) {
|
|
597
|
-
options.log(`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`);
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
else if (len && options.integralDataPoints[k][0].ts < indexStartTs) {
|
|
601
|
-
// 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
|
|
602
|
-
let preFirstIndex = null;
|
|
603
|
-
for (let kk = 0; kk < options.integralDataPoints[k].length; kk++) {
|
|
604
|
-
if (options.integralDataPoints[k][kk].ts >= indexStartTs) {
|
|
605
|
-
break;
|
|
606
|
-
}
|
|
607
|
-
preFirstIndex = kk;
|
|
608
|
-
}
|
|
609
|
-
if (preFirstIndex !== null) {
|
|
610
|
-
const time = {
|
|
611
|
-
ts: indexStartTs,
|
|
612
|
-
val: options.integralDataPoints[k][preFirstIndex].val,
|
|
613
|
-
};
|
|
614
|
-
if (options.logDebug && options.log) {
|
|
615
|
-
time.time = new Date(time.ts).toISOString();
|
|
616
|
-
}
|
|
617
|
-
options.integralDataPoints[k].splice(0, preFirstIndex, time);
|
|
618
|
-
if (options.logDebug && options.log) {
|
|
619
|
-
options.log(`Integral: ${k}: Remove ${preFirstIndex + 1} entries and add start entry for interval with ts=${indexStartTs}, val=${options.integralDataPoints[k][0].val}`);
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
// get middle of the time interval
|
|
624
|
-
const ts = options.processing[k] !== undefined && options.processing[k].val.ts
|
|
625
|
-
? options.processing[k].val.ts
|
|
626
|
-
: Math.round(indexStartTs + (indexEndTs - indexStartTs) / 2);
|
|
627
|
-
const point = {
|
|
628
|
-
ts,
|
|
629
|
-
val: null,
|
|
630
|
-
};
|
|
631
|
-
const integralDataPoints = options.integralDataPoints[k] || [];
|
|
632
|
-
if (options.logDebug && options.log) {
|
|
633
|
-
const vals = integralDataPoints.map(dp => `[${dp.ts}, ${dp.val}]`);
|
|
634
|
-
options.log(`Integral: ${k}: ${integralDataPoints.length} data points for interval ${indexStartTs} - ${indexEndTs}: ${vals.join(',')}`);
|
|
635
|
-
}
|
|
636
|
-
// Calculate Intervals and always calculate till the interval end (start made sure above already)
|
|
637
|
-
for (let kk = 0; kk < integralDataPoints.length; kk++) {
|
|
638
|
-
// Determine the end timestamp for this segment: next point ts or interval end
|
|
639
|
-
const valEndTs = integralDataPoints[kk + 1]
|
|
640
|
-
? Math.min(integralDataPoints[kk + 1].ts, indexEndTs)
|
|
641
|
-
: indexEndTs;
|
|
642
|
-
// Ignore segments that don't belong or have zero duration
|
|
643
|
-
const valDuration = valEndTs - integralDataPoints[kk].ts;
|
|
644
|
-
if (valDuration < 0) {
|
|
645
|
-
if (options.logDebug && options.log) {
|
|
646
|
-
options.log(`Integral: ${k}[${kk}] segment outside interval, ignore ${JSON.stringify(integralDataPoints[kk])} (vs. ${valEndTs})`);
|
|
647
|
-
}
|
|
648
|
-
break;
|
|
649
|
-
}
|
|
650
|
-
if (valDuration === 0) {
|
|
651
|
-
if (options.logDebug && options.log) {
|
|
652
|
-
options.log(`Integral: ${k}[${kk}] zero duration, ignore ${JSON.stringify(integralDataPoints[kk])}`);
|
|
653
|
-
}
|
|
654
|
-
continue;
|
|
655
|
-
}
|
|
656
|
-
// Read segment start and end values (treat null as 0)
|
|
657
|
-
let valStart = parseFloat(integralDataPoints[kk].val) || 0;
|
|
658
|
-
// End value is the next value, or if none, assume "linearity"
|
|
659
|
-
let valEnd = parseFloat((integralDataPoints[kk + 1]
|
|
660
|
-
? integralDataPoints[kk + 1].val
|
|
661
|
-
: options.integralDataPoints[k + 1] && options.integralDataPoints[k + 1][0]
|
|
662
|
-
? options.integralDataPoints[k + 1][0].val
|
|
663
|
-
: valStart)) || 0;
|
|
664
|
-
// Accumulate integral according to interpolation mode
|
|
665
|
-
if (options.integralInterpolation !== 'linear' || valStart === valEnd) {
|
|
666
|
-
// Rectangle approximation: constant value = valStart
|
|
667
|
-
const integralAdd = (valStart * valDuration) / options.integralUnit;
|
|
668
|
-
// simple rectangle linear interpolation
|
|
669
|
-
if (options.logDebug && options.log) {
|
|
670
|
-
options.log(`Integral: ${k}[${kk}] : Add ${integralAdd} from val=${valStart} for ${valDuration}`);
|
|
671
|
-
}
|
|
672
|
-
point.val += integralAdd;
|
|
673
|
-
}
|
|
674
|
-
else if ((valStart >= 0 && valEnd >= 0) || (valStart <= 0 && valEnd <= 0)) {
|
|
675
|
-
// Both values on same side of zero: rectangle + triangle decomposition
|
|
676
|
-
let multiplier = 1;
|
|
677
|
-
if (valStart <= 0 && valEnd <= 0) {
|
|
678
|
-
multiplier = -1; // correct the sign at the end
|
|
679
|
-
valStart = -valStart;
|
|
680
|
-
valEnd = -valEnd;
|
|
681
|
-
}
|
|
682
|
-
const minVal = Math.min(valStart, valEnd);
|
|
683
|
-
const maxVal = Math.max(valStart, valEnd);
|
|
684
|
-
const rectPart = (minVal * valDuration) / options.integralUnit;
|
|
685
|
-
const trianglePart = ((maxVal - minVal) * valDuration * 0.5) / options.integralUnit;
|
|
686
|
-
const integralAdd = (rectPart + trianglePart) * multiplier;
|
|
687
|
-
if (options.logDebug && options.log) {
|
|
688
|
-
options.log(`Integral: ${k}[${kk}] : Add R${rectPart} + T${trianglePart} => ${integralAdd} from val=${valStart} to ${valEnd} for ${valDuration}`);
|
|
689
|
-
}
|
|
690
|
-
point.val += integralAdd;
|
|
691
|
-
}
|
|
692
|
-
else {
|
|
693
|
-
// Values are on different sides of 0, so we need to find the 0 crossing
|
|
694
|
-
const zeroCrossing = Math.abs((valStart * valDuration) / (valEnd - valStart));
|
|
695
|
-
// Then calculate two linear segments, one from 0 to the crossing, and one from the crossing to the end
|
|
696
|
-
const trianglePart1 = (valStart * zeroCrossing * 0.5) / options.integralUnit;
|
|
697
|
-
const trianglePart2 = (valEnd * (valDuration - zeroCrossing) * 0.5) / options.integralUnit;
|
|
698
|
-
const integralAdd = trianglePart1 + trianglePart2;
|
|
699
|
-
if (options.logDebug && options.log) {
|
|
700
|
-
options.log(`Integral: ${k}[${kk}] : Add T${trianglePart1} + T${trianglePart2} => ${integralAdd} from val=${valStart} to ${valEnd} for ${valDuration} (zero crossing ${zeroCrossing})`);
|
|
701
|
-
}
|
|
702
|
-
point.val += integralAdd;
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
/*
|
|
706
|
-
options.processing[k] = {
|
|
707
|
-
ts: options.processing[k].val.ts,
|
|
708
|
-
val: options.processing[k].val.val
|
|
709
|
-
}
|
|
710
|
-
*/
|
|
711
|
-
// If we produced a numeric value, append to final result; otherwise track removed border flags
|
|
712
|
-
if (point.val !== null) {
|
|
713
|
-
finalResult.push(point);
|
|
714
|
-
}
|
|
715
|
-
else if (k === 0) {
|
|
716
|
-
preBorderValueRemoved = true;
|
|
717
|
-
}
|
|
718
|
-
else if (k === originalResultLength - 1) {
|
|
719
|
-
postBorderValueRemoved = true;
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
// If requested, remove pre- / post-border values from the result
|
|
723
|
-
if (options.removeBorderValues) {
|
|
724
|
-
// we cut out the additional results
|
|
725
|
-
if (!preBorderValueRemoved) {
|
|
726
|
-
finalResult.splice(0, 1);
|
|
727
|
-
}
|
|
728
|
-
if (!postBorderValueRemoved) {
|
|
729
|
-
finalResult.length--;
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
options.result = finalResult;
|
|
733
|
-
}
|
|
734
|
-
function finishAggregationForMinMax(options) {
|
|
735
|
-
if (!options.processing) {
|
|
736
|
-
return;
|
|
737
|
-
}
|
|
738
|
-
let preBorderValueRemoved = false;
|
|
739
|
-
let postBorderValueRemoved = false;
|
|
740
|
-
const originalResultLength = options.processing.length;
|
|
741
|
-
const startIndex = 0;
|
|
742
|
-
const endIndex = options.processing.length;
|
|
743
|
-
const finalResult = [];
|
|
744
|
-
for (let ii = startIndex; ii < endIndex; ii++) {
|
|
745
|
-
// it is no one value in this period
|
|
746
|
-
if (options.processing[ii] === undefined || options.processing[ii].start.ts === null) {
|
|
747
|
-
if (ii === 0) {
|
|
748
|
-
preBorderValueRemoved = true;
|
|
749
|
-
}
|
|
750
|
-
else if (ii === originalResultLength - 1) {
|
|
751
|
-
postBorderValueRemoved = true;
|
|
752
|
-
}
|
|
753
|
-
// options.processing.splice(ii, 1);
|
|
754
|
-
continue;
|
|
755
|
-
}
|
|
756
|
-
// just one value in this period: max == min == start == end
|
|
757
|
-
if (options.processing[ii].start.ts === options.processing[ii].end.ts) {
|
|
758
|
-
finalResult.push({
|
|
759
|
-
ts: options.processing[ii].start.ts,
|
|
760
|
-
val: options.processing[ii].start.val,
|
|
761
|
-
});
|
|
762
|
-
}
|
|
763
|
-
else if (options.processing[ii].min.ts === options.processing[ii].max.ts) {
|
|
764
|
-
// if just 2 values: start == min == max, end
|
|
765
|
-
if (options.processing[ii].start.ts === options.processing[ii].min.ts ||
|
|
766
|
-
options.processing[ii].end.ts === options.processing[ii].min.ts) {
|
|
767
|
-
finalResult.push({
|
|
768
|
-
ts: options.processing[ii].start.ts,
|
|
769
|
-
val: options.processing[ii].start.val,
|
|
770
|
-
});
|
|
771
|
-
finalResult.push({
|
|
772
|
-
ts: options.processing[ii].end.ts,
|
|
773
|
-
val: options.processing[ii].end.val,
|
|
774
|
-
});
|
|
775
|
-
}
|
|
776
|
-
else {
|
|
777
|
-
// if just 3 values: start, min == max, end
|
|
778
|
-
finalResult.push({
|
|
779
|
-
ts: options.processing[ii].start.ts,
|
|
780
|
-
val: options.processing[ii].start.val,
|
|
781
|
-
});
|
|
782
|
-
finalResult.push({
|
|
783
|
-
ts: options.processing[ii].max.ts,
|
|
784
|
-
val: options.processing[ii].max.val,
|
|
785
|
-
});
|
|
786
|
-
finalResult.push({
|
|
787
|
-
ts: options.processing[ii].end.ts,
|
|
788
|
-
val: options.processing[ii].end.val,
|
|
789
|
-
});
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
|
-
else if (options.processing[ii].start.ts === options.processing[ii].max.ts) {
|
|
793
|
-
// just one value in this period: start == max, min == end
|
|
794
|
-
if (options.processing[ii].min.ts === options.processing[ii].end.ts) {
|
|
795
|
-
finalResult.push({
|
|
796
|
-
ts: options.processing[ii].start.ts,
|
|
797
|
-
val: options.processing[ii].start.val,
|
|
798
|
-
});
|
|
799
|
-
finalResult.push({
|
|
800
|
-
ts: options.processing[ii].end.ts,
|
|
801
|
-
val: options.processing[ii].end.val,
|
|
802
|
-
});
|
|
803
|
-
}
|
|
804
|
-
else {
|
|
805
|
-
// start == max, min, end
|
|
806
|
-
finalResult.push({
|
|
807
|
-
ts: options.processing[ii].start.ts,
|
|
808
|
-
val: options.processing[ii].start.val,
|
|
809
|
-
});
|
|
810
|
-
finalResult.push({
|
|
811
|
-
ts: options.processing[ii].min.ts,
|
|
812
|
-
val: options.processing[ii].min.val,
|
|
813
|
-
});
|
|
814
|
-
finalResult.push({
|
|
815
|
-
ts: options.processing[ii].end.ts,
|
|
816
|
-
val: options.processing[ii].end.val,
|
|
817
|
-
});
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
else if (options.processing[ii].end.ts === options.processing[ii].max.ts) {
|
|
821
|
-
// just one value in this period: start == min, max == end
|
|
822
|
-
if (options.processing[ii].min.ts === options.processing[ii].start.ts) {
|
|
823
|
-
finalResult.push({
|
|
824
|
-
ts: options.processing[ii].start.ts,
|
|
825
|
-
val: options.processing[ii].start.val,
|
|
826
|
-
});
|
|
827
|
-
finalResult.push({
|
|
828
|
-
ts: options.processing[ii].end.ts,
|
|
829
|
-
val: options.processing[ii].end.val,
|
|
830
|
-
});
|
|
831
|
-
}
|
|
832
|
-
else {
|
|
833
|
-
// start, min, max == end
|
|
834
|
-
finalResult.push({
|
|
835
|
-
ts: options.processing[ii].start.ts,
|
|
836
|
-
val: options.processing[ii].start.val,
|
|
837
|
-
});
|
|
838
|
-
finalResult.push({
|
|
839
|
-
ts: options.processing[ii].min.ts,
|
|
840
|
-
val: options.processing[ii].min.val,
|
|
841
|
-
});
|
|
842
|
-
finalResult.push({
|
|
843
|
-
ts: options.processing[ii].end.ts,
|
|
844
|
-
val: options.processing[ii].end.val,
|
|
845
|
-
});
|
|
846
|
-
}
|
|
847
|
-
}
|
|
848
|
-
else if (options.processing[ii].start.ts === options.processing[ii].min.ts ||
|
|
849
|
-
options.processing[ii].end.ts === options.processing[ii].min.ts) {
|
|
850
|
-
// just one value in this period: start == min, max, end
|
|
851
|
-
finalResult.push({
|
|
852
|
-
ts: options.processing[ii].start.ts,
|
|
853
|
-
val: options.processing[ii].start.val,
|
|
854
|
-
});
|
|
855
|
-
finalResult.push({
|
|
856
|
-
ts: options.processing[ii].max.ts,
|
|
857
|
-
val: options.processing[ii].max.val,
|
|
858
|
-
});
|
|
859
|
-
finalResult.push({
|
|
860
|
-
ts: options.processing[ii].end.ts,
|
|
861
|
-
val: options.processing[ii].end.val,
|
|
862
|
-
});
|
|
863
|
-
}
|
|
864
|
-
else {
|
|
865
|
-
finalResult.push({
|
|
866
|
-
ts: options.processing[ii].start.ts,
|
|
867
|
-
val: options.processing[ii].start.val,
|
|
868
|
-
});
|
|
869
|
-
// just one value in this period: start == min, max, end
|
|
870
|
-
if (options.processing[ii].max.ts > options.processing[ii].min.ts) {
|
|
871
|
-
finalResult.push({
|
|
872
|
-
ts: options.processing[ii].min.ts,
|
|
873
|
-
val: options.processing[ii].min.val,
|
|
874
|
-
});
|
|
875
|
-
finalResult.push({
|
|
876
|
-
ts: options.processing[ii].max.ts,
|
|
877
|
-
val: options.processing[ii].max.val,
|
|
878
|
-
});
|
|
879
|
-
}
|
|
880
|
-
else {
|
|
881
|
-
finalResult.push({
|
|
882
|
-
ts: options.processing[ii].max.ts,
|
|
883
|
-
val: options.processing[ii].max.val,
|
|
884
|
-
});
|
|
885
|
-
finalResult.push({
|
|
886
|
-
ts: options.processing[ii].min.ts,
|
|
887
|
-
val: options.processing[ii].min.val,
|
|
888
|
-
});
|
|
889
|
-
}
|
|
890
|
-
finalResult.push({
|
|
891
|
-
ts: options.processing[ii].end.ts,
|
|
892
|
-
val: options.processing[ii].end.val,
|
|
893
|
-
});
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
if (options.removeBorderValues) {
|
|
897
|
-
// we cut out the additional results
|
|
898
|
-
if (!preBorderValueRemoved) {
|
|
899
|
-
finalResult.splice(0, 1);
|
|
900
|
-
}
|
|
901
|
-
if (!postBorderValueRemoved) {
|
|
902
|
-
finalResult.length--;
|
|
903
|
-
}
|
|
904
|
-
}
|
|
905
|
-
options.result = finalResult;
|
|
906
|
-
}
|
|
907
|
-
function finishAggregationForAverage(options) {
|
|
908
|
-
// averages are always rounded to 2 decimals, independent of options.round.
|
|
909
|
-
// This file must stay identical with the history adapter's copy, so do not
|
|
910
|
-
// make it configurable here alone.
|
|
911
|
-
const round = 100;
|
|
912
|
-
let startIndex = 0;
|
|
913
|
-
if (!options.processing) {
|
|
914
|
-
return;
|
|
915
|
-
}
|
|
916
|
-
let endIndex = options.processing.length;
|
|
917
|
-
const finalResult = [];
|
|
918
|
-
if (options.removeBorderValues) {
|
|
919
|
-
// we cut out the additional results
|
|
920
|
-
// options.processing.splice(0, 1);
|
|
921
|
-
// options.averageCount.splice(0, 1);
|
|
922
|
-
// options.processing.length--;
|
|
923
|
-
// options.averageCount.length--;
|
|
924
|
-
startIndex++;
|
|
925
|
-
endIndex--;
|
|
926
|
-
}
|
|
927
|
-
for (let k = startIndex; k < endIndex; k++) {
|
|
928
|
-
if (options.processing[k] !== undefined && options.processing[k].val.ts) {
|
|
929
|
-
finalResult.push({
|
|
930
|
-
ts: options.processing[k].val.ts,
|
|
931
|
-
val: options.processing[k].val.val !== null
|
|
932
|
-
? Math.round((options.processing[k].val.val / options.averageCount[k]) * round) /
|
|
933
|
-
round
|
|
934
|
-
: null,
|
|
935
|
-
});
|
|
936
|
-
}
|
|
937
|
-
else {
|
|
938
|
-
// no one value in this interval
|
|
939
|
-
// options.processing.splice(k, 1);
|
|
940
|
-
// options.averageCount.splice(k, 1); // not needed to clean up because not used anymore afterwards
|
|
941
|
-
}
|
|
942
|
-
}
|
|
943
|
-
options.result = finalResult;
|
|
944
|
-
}
|
|
945
|
-
function finishAggregationForCount(options) {
|
|
946
|
-
let startIndex = 0;
|
|
947
|
-
if (!options.processing) {
|
|
948
|
-
return;
|
|
949
|
-
}
|
|
950
|
-
let endIndex = options.processing.length;
|
|
951
|
-
const finalResult = [];
|
|
952
|
-
if (options.removeBorderValues) {
|
|
953
|
-
// we cut out the additional results
|
|
954
|
-
// options.processing.splice(0, 1);
|
|
955
|
-
// options.averageCount.splice(0, 1);
|
|
956
|
-
// options.processing.length--;
|
|
957
|
-
// options.averageCount.length--;
|
|
958
|
-
startIndex++;
|
|
959
|
-
endIndex--;
|
|
960
|
-
}
|
|
961
|
-
for (let k = startIndex; k < endIndex; k++) {
|
|
962
|
-
if (options.processing[k] !== undefined && options.processing[k].val.ts) {
|
|
963
|
-
finalResult.push({
|
|
964
|
-
ts: options.processing[k].val.ts,
|
|
965
|
-
val: options.averageCount[k],
|
|
966
|
-
});
|
|
967
|
-
}
|
|
968
|
-
else {
|
|
969
|
-
// no one value in this interval
|
|
970
|
-
// options.processing.splice(k, 1);
|
|
971
|
-
// options.averageCount.splice(k, 1); // not needed to clean up because not used anymore afterward
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
options.result = finalResult;
|
|
975
|
-
}
|
|
976
|
-
function finishAggregationPercentile(options) {
|
|
977
|
-
let startIndex = 0;
|
|
978
|
-
if (!options.processing) {
|
|
979
|
-
return;
|
|
980
|
-
}
|
|
981
|
-
let endIndex = options.processing.length;
|
|
982
|
-
const finalResult = [];
|
|
983
|
-
if (options.removeBorderValues) {
|
|
984
|
-
// we cut out the additional results
|
|
985
|
-
/*
|
|
986
|
-
options.processing.splice(0, 1);
|
|
987
|
-
options.quantileDataPoints.splice(0, 1);
|
|
988
|
-
options.processing.length--
|
|
989
|
-
options.quantileDataPoints.length--;
|
|
990
|
-
*/
|
|
991
|
-
startIndex++;
|
|
992
|
-
endIndex--;
|
|
993
|
-
}
|
|
994
|
-
for (let k = startIndex; k < endIndex; k++) {
|
|
995
|
-
if (options.processing[k] !== undefined && options.processing[k].val.ts) {
|
|
996
|
-
const point = {
|
|
997
|
-
ts: options.processing[k].val.ts,
|
|
998
|
-
val: quantile(options.quantile, options.quantileDataPoints[k]),
|
|
999
|
-
};
|
|
1000
|
-
if (options.logDebug && options.log) {
|
|
1001
|
-
options.log(`Quantile ${k} ${point.ts}: ${options.quantileDataPoints[k].join(', ')} -> ${point.val}`);
|
|
1002
|
-
}
|
|
1003
|
-
finalResult.push(point);
|
|
1004
|
-
}
|
|
1005
|
-
else {
|
|
1006
|
-
// no one value in this interval
|
|
1007
|
-
// options.processing.splice(k, 1);
|
|
1008
|
-
// options.quantileDataPoints.splice(k, 1); // not needed to clean up because not used anymore afterward
|
|
1009
|
-
}
|
|
1010
|
-
}
|
|
1011
|
-
options.result = finalResult;
|
|
1012
|
-
}
|
|
1013
|
-
function finishAggregationTotalIntegral(options) {
|
|
1014
|
-
// calculate first entry
|
|
1015
|
-
if (options.totalIntegralDataPoints?.[0] && options.totalIntegralDataPoints[0].ts !== options.start) {
|
|
1016
|
-
if (options.totalIntegralDataPoints[0] &&
|
|
1017
|
-
options.totalIntegralDataPoints[0].ts < options.start &&
|
|
1018
|
-
options.totalIntegralDataPoints[1] &&
|
|
1019
|
-
options.totalIntegralDataPoints[1].ts > options.start) {
|
|
1020
|
-
const y1 = options.totalIntegralDataPoints[0].val;
|
|
1021
|
-
const y2 = options.totalIntegralDataPoints[1].val;
|
|
1022
|
-
const x1 = options.totalIntegralDataPoints[0].ts;
|
|
1023
|
-
const x2 = options.totalIntegralDataPoints[1].ts;
|
|
1024
|
-
const val = y1 + ((y2 - y1) * (options.start - x1)) / (x2 - x1);
|
|
1025
|
-
options.totalIntegralDataPoints[0] = {
|
|
1026
|
-
ts: options.start,
|
|
1027
|
-
val,
|
|
1028
|
-
};
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
// calculate last entry
|
|
1032
|
-
const len = options.totalIntegralDataPoints?.length || 0;
|
|
1033
|
-
if (options.totalIntegralDataPoints &&
|
|
1034
|
-
options.totalIntegralDataPoints[len - 1] &&
|
|
1035
|
-
options.totalIntegralDataPoints[len - 1].ts !== options.end) {
|
|
1036
|
-
if (options.totalIntegralDataPoints[len - 1] &&
|
|
1037
|
-
options.totalIntegralDataPoints[len - 1].ts > options.end &&
|
|
1038
|
-
options.totalIntegralDataPoints[len - 2] &&
|
|
1039
|
-
options.totalIntegralDataPoints[len - 2].ts < options.end) {
|
|
1040
|
-
const y1 = options.totalIntegralDataPoints[len - 2].val;
|
|
1041
|
-
const y2 = options.totalIntegralDataPoints[len - 1].val;
|
|
1042
|
-
const x1 = options.totalIntegralDataPoints[len - 2].ts;
|
|
1043
|
-
const x2 = options.totalIntegralDataPoints[len - 1].ts;
|
|
1044
|
-
const val = y1 + ((y2 - y1) * (options.start - x1)) / (x2 - x1);
|
|
1045
|
-
options.totalIntegralDataPoints[len - 1] = {
|
|
1046
|
-
ts: options.end,
|
|
1047
|
-
val,
|
|
1048
|
-
};
|
|
1049
|
-
}
|
|
1050
|
-
else if (options.totalIntegralDataPoints[len - 1] &&
|
|
1051
|
-
options.totalIntegralDataPoints[len - 1].ts < options.end) {
|
|
1052
|
-
// assume that now we have the same value as before
|
|
1053
|
-
options.totalIntegralDataPoints.push({
|
|
1054
|
-
ts: options.end,
|
|
1055
|
-
val: options.totalIntegralDataPoints[len - 1].val,
|
|
1056
|
-
});
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
let integral = 0;
|
|
1060
|
-
if (options.totalIntegralDataPoints) {
|
|
1061
|
-
for (let i = 1; i < options.totalIntegralDataPoints.length; i++) {
|
|
1062
|
-
integral += calcDiff(options.totalIntegralDataPoints[i - 1], options.totalIntegralDataPoints[i]).square;
|
|
1063
|
-
}
|
|
1064
|
-
}
|
|
1065
|
-
options.result = [
|
|
1066
|
-
{
|
|
1067
|
-
ts: options.end,
|
|
1068
|
-
val: integral,
|
|
1069
|
-
},
|
|
1070
|
-
];
|
|
1071
|
-
}
|
|
1072
|
-
function finishAggregationForSimple(options) {
|
|
1073
|
-
let startIndex = 0;
|
|
1074
|
-
if (!options.processing) {
|
|
1075
|
-
return;
|
|
1076
|
-
}
|
|
1077
|
-
let endIndex = options.processing.length;
|
|
1078
|
-
const finalResult = [];
|
|
1079
|
-
if (options.removeBorderValues) {
|
|
1080
|
-
// we cut out the additional results
|
|
1081
|
-
// options.processing.splice(0, 1);
|
|
1082
|
-
// options.processing.length--;
|
|
1083
|
-
startIndex++;
|
|
1084
|
-
endIndex--;
|
|
1085
|
-
}
|
|
1086
|
-
for (let j = startIndex; j < endIndex; j++) {
|
|
1087
|
-
if (options.processing[j] !== undefined && options.processing[j].val.ts) {
|
|
1088
|
-
finalResult.push({
|
|
1089
|
-
ts: options.processing[j].val.ts,
|
|
1090
|
-
val: options.processing[j].val.val,
|
|
1091
|
-
});
|
|
1092
|
-
}
|
|
1093
|
-
else {
|
|
1094
|
-
// no one value in this interval
|
|
1095
|
-
// options.processing.splice(j, 1);
|
|
1096
|
-
}
|
|
1097
|
-
}
|
|
1098
|
-
options.result = finalResult;
|
|
1099
|
-
}
|
|
1100
|
-
function finishAggregation(options) {
|
|
1101
|
-
if (options.aggregate === 'minmax') {
|
|
1102
|
-
finishAggregationForMinMax(options);
|
|
1103
|
-
}
|
|
1104
|
-
else if (options.aggregate === 'average') {
|
|
1105
|
-
finishAggregationForAverage(options);
|
|
1106
|
-
}
|
|
1107
|
-
else if (options.aggregate === 'count') {
|
|
1108
|
-
finishAggregationForCount(options);
|
|
1109
|
-
}
|
|
1110
|
-
else if (options.aggregate === 'integral') {
|
|
1111
|
-
finishAggregationForIntegral(options);
|
|
1112
|
-
}
|
|
1113
|
-
else if (options.aggregate === 'percentile' || options.aggregate === 'quantile') {
|
|
1114
|
-
finishAggregationPercentile(options);
|
|
1115
|
-
}
|
|
1116
|
-
else if (options.aggregate === 'integralTotal') {
|
|
1117
|
-
finishAggregationTotalIntegral(options);
|
|
1118
|
-
}
|
|
1119
|
-
else {
|
|
1120
|
-
finishAggregationForSimple(options);
|
|
1121
|
-
}
|
|
1122
|
-
// free memory
|
|
1123
|
-
// @ts-expect-error ignore
|
|
1124
|
-
options.processing = null;
|
|
1125
|
-
beautify(options);
|
|
1126
|
-
}
|
|
1127
|
-
/**
|
|
1128
|
-
* Beautify the result - remove null values, add start and end values if needed.
|
|
1129
|
-
* Also round the values if requested and add ID to every value if requested.
|
|
1130
|
-
*/
|
|
1131
|
-
function beautify(options) {
|
|
1132
|
-
if (options.logDebug && options.log) {
|
|
1133
|
-
options.log(`Beautify: ${options.result?.length} results`);
|
|
1134
|
-
}
|
|
1135
|
-
let preFirstValue = null;
|
|
1136
|
-
let postLastValue = null;
|
|
1137
|
-
if (options.ignoreNull === 'true') {
|
|
1138
|
-
// include nulls and replace them with last value
|
|
1139
|
-
options.ignoreNull = true;
|
|
1140
|
-
}
|
|
1141
|
-
else if (options.ignoreNull === 'false') {
|
|
1142
|
-
// include nulls
|
|
1143
|
-
options.ignoreNull = false;
|
|
1144
|
-
}
|
|
1145
|
-
else if (options.ignoreNull === '0') {
|
|
1146
|
-
// include nulls and replace them with 0
|
|
1147
|
-
options.ignoreNull = 0;
|
|
1148
|
-
}
|
|
1149
|
-
else if (options.ignoreNull !== true && options.ignoreNull !== false && options.ignoreNull !== 0) {
|
|
1150
|
-
options.ignoreNull = false;
|
|
1151
|
-
}
|
|
1152
|
-
// process null values, remove points outside the span and find first points after end and before start
|
|
1153
|
-
if (options.result) {
|
|
1154
|
-
for (let i = 0; i < options.result.length; i++) {
|
|
1155
|
-
if (options.ignoreNull !== false) {
|
|
1156
|
-
// if value is null
|
|
1157
|
-
if (options.result[i].val === null) {
|
|
1158
|
-
// null value must be replaced with last not null value
|
|
1159
|
-
if (options.ignoreNull === true) {
|
|
1160
|
-
// remove value
|
|
1161
|
-
options.result.splice(i, 1);
|
|
1162
|
-
i--;
|
|
1163
|
-
continue;
|
|
1164
|
-
}
|
|
1165
|
-
else {
|
|
1166
|
-
// null value must be replaced with 0
|
|
1167
|
-
options.result[i].val = options.ignoreNull;
|
|
1168
|
-
}
|
|
1169
|
-
}
|
|
1170
|
-
}
|
|
1171
|
-
// remove all not requested points
|
|
1172
|
-
if (options.result[i].ts < options.start) {
|
|
1173
|
-
preFirstValue = options.result[i].val !== null ? options.result[i] : null;
|
|
1174
|
-
options.result.splice(i, 1);
|
|
1175
|
-
i--;
|
|
1176
|
-
continue;
|
|
1177
|
-
}
|
|
1178
|
-
postLastValue = options.result[i].val !== null ? options.result[i] : null;
|
|
1179
|
-
if (options.result[i].ts > options.end) {
|
|
1180
|
-
options.result.splice(i, options.result.length - i);
|
|
1181
|
-
break;
|
|
1182
|
-
}
|
|
1183
|
-
if (options.round && options.result[i].val && typeof options.result[i].val === 'number') {
|
|
1184
|
-
options.result[i].val = Math.round(options.result[i].val * options.round) / options.round;
|
|
1185
|
-
}
|
|
1186
|
-
}
|
|
1187
|
-
}
|
|
1188
|
-
// check start and stop
|
|
1189
|
-
if (options.result?.length && options.aggregate !== 'none' && !options.removeBorderValues) {
|
|
1190
|
-
const firstTS = options.result[0].ts;
|
|
1191
|
-
if (firstTS > options.start && !options.removeBorderValues) {
|
|
1192
|
-
if (preFirstValue) {
|
|
1193
|
-
const firstY = options.result[0].val;
|
|
1194
|
-
// if steps
|
|
1195
|
-
if (options.aggregate === 'onchange' || !options.aggregate) {
|
|
1196
|
-
if (preFirstValue.ts !== firstTS) {
|
|
1197
|
-
options.result.unshift({ ts: options.start, val: preFirstValue.val });
|
|
1198
|
-
}
|
|
1199
|
-
else {
|
|
1200
|
-
if (options.ignoreNull) {
|
|
1201
|
-
options.result.unshift({ ts: options.start, val: firstY });
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
}
|
|
1205
|
-
else {
|
|
1206
|
-
if (preFirstValue.ts !== firstTS) {
|
|
1207
|
-
if (firstY !== null) {
|
|
1208
|
-
// interpolate
|
|
1209
|
-
const y = preFirstValue.val +
|
|
1210
|
-
((firstY - preFirstValue.val) * (options.start - preFirstValue.ts)) /
|
|
1211
|
-
(firstTS - preFirstValue.ts);
|
|
1212
|
-
options.result.unshift({ ts: options.start, val: y, i: true });
|
|
1213
|
-
if (options.logDebug && options.log) {
|
|
1214
|
-
options.log(`interpolate ${y} from ${preFirstValue.val} to ${firstY} as first return value`);
|
|
1215
|
-
}
|
|
1216
|
-
}
|
|
1217
|
-
else {
|
|
1218
|
-
options.result.unshift({ ts: options.start, val: null });
|
|
1219
|
-
}
|
|
1220
|
-
}
|
|
1221
|
-
else {
|
|
1222
|
-
if (options.ignoreNull) {
|
|
1223
|
-
options.result.unshift({ ts: options.start, val: firstY });
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1226
|
-
}
|
|
1227
|
-
}
|
|
1228
|
-
else {
|
|
1229
|
-
if (options.ignoreNull) {
|
|
1230
|
-
options.result.unshift({ ts: options.start, val: options.result[0].val });
|
|
1231
|
-
}
|
|
1232
|
-
else {
|
|
1233
|
-
options.result.unshift({ ts: options.start, val: null });
|
|
1234
|
-
}
|
|
1235
|
-
}
|
|
1236
|
-
}
|
|
1237
|
-
const lastTS = options.result[options.result.length - 1].ts;
|
|
1238
|
-
if (lastTS < options.end && !options.removeBorderValues) {
|
|
1239
|
-
if (postLastValue) {
|
|
1240
|
-
// if steps
|
|
1241
|
-
if (options.aggregate === 'onchange' || !options.aggregate) {
|
|
1242
|
-
// if more data following, draw line to the end of the chart
|
|
1243
|
-
if (postLastValue.ts !== lastTS) {
|
|
1244
|
-
options.result.push({ ts: options.end, val: postLastValue.val });
|
|
1245
|
-
}
|
|
1246
|
-
else {
|
|
1247
|
-
if (options.ignoreNull) {
|
|
1248
|
-
options.result.push({ ts: options.end, val: postLastValue.val });
|
|
1249
|
-
}
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1252
|
-
else {
|
|
1253
|
-
if (postLastValue.ts !== lastTS) {
|
|
1254
|
-
const lastY = options.result[options.result.length - 1].val;
|
|
1255
|
-
if (lastY !== null) {
|
|
1256
|
-
// make interpolation
|
|
1257
|
-
const _y = lastY +
|
|
1258
|
-
((postLastValue.val - lastY) * (options.end - lastTS)) / (postLastValue.ts - lastTS);
|
|
1259
|
-
options.result.push({ ts: options.end, val: _y, i: true });
|
|
1260
|
-
if (options.logDebug && options.log) {
|
|
1261
|
-
options.log(`interpolate ${_y} from ${lastY} to ${postLastValue.val} as last return value`);
|
|
1262
|
-
}
|
|
1263
|
-
}
|
|
1264
|
-
else {
|
|
1265
|
-
options.result.push({ ts: options.end, val: null });
|
|
1266
|
-
}
|
|
1267
|
-
}
|
|
1268
|
-
else {
|
|
1269
|
-
if (options.ignoreNull) {
|
|
1270
|
-
options.result.push({ ts: options.end, val: postLastValue.val });
|
|
1271
|
-
}
|
|
1272
|
-
}
|
|
1273
|
-
}
|
|
1274
|
-
}
|
|
1275
|
-
else {
|
|
1276
|
-
if (options.ignoreNull) {
|
|
1277
|
-
const lastY = options.result[options.result.length - 1].val;
|
|
1278
|
-
// if no more data, that means do not draw line
|
|
1279
|
-
options.result.push({ ts: options.end, val: lastY });
|
|
1280
|
-
}
|
|
1281
|
-
else {
|
|
1282
|
-
// if no more data, that means do not draw line
|
|
1283
|
-
options.result.push({ ts: options.end, val: null });
|
|
1284
|
-
}
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
}
|
|
1288
|
-
else if (options.aggregate === 'none') {
|
|
1289
|
-
if (options.count && options.result && options.result.length > options.count) {
|
|
1290
|
-
options.result.splice(0, options.result.length - options.count);
|
|
1291
|
-
}
|
|
1292
|
-
}
|
|
1293
|
-
if (options.addId && options.result && options.id) {
|
|
1294
|
-
for (let i = 0; i < options.result.length; i++) {
|
|
1295
|
-
if (!options.result[i].id) {
|
|
1296
|
-
options.result[i].id = options.id;
|
|
1297
|
-
}
|
|
1298
|
-
}
|
|
1299
|
-
}
|
|
1300
|
-
}
|
|
1301
|
-
/**
|
|
1302
|
-
* Sends the history to the caller
|
|
1303
|
-
*
|
|
1304
|
-
* @param adapter Adapter instance
|
|
1305
|
-
* @param msg ioBroker message to respond to
|
|
1306
|
-
* @param id State ID
|
|
1307
|
-
* @param initialOptions get history options
|
|
1308
|
-
* @param dataOrError Array or error string
|
|
1309
|
-
* @param startTime Start time of the request just to measure duration
|
|
1310
|
-
* @param logId Optional log ID to prefix log messages
|
|
1311
|
-
*/
|
|
1312
|
-
function sendResponse(adapter, msg, id, initialOptions, dataOrError, startTime, logId) {
|
|
1313
|
-
if (typeof dataOrError === 'string') {
|
|
1314
|
-
adapter.log.error(dataOrError);
|
|
1315
|
-
adapter.sendTo(msg.from, msg.command, {
|
|
1316
|
-
result: [],
|
|
1317
|
-
step: 0,
|
|
1318
|
-
error: dataOrError,
|
|
1319
|
-
sessionId: initialOptions.sessionId,
|
|
1320
|
-
}, msg.callback);
|
|
1321
|
-
return;
|
|
1322
|
-
}
|
|
1323
|
-
let log;
|
|
1324
|
-
if (logId) {
|
|
1325
|
-
log = (text) => {
|
|
1326
|
-
adapter.log.debug(`${logId}: ${text}`);
|
|
1327
|
-
};
|
|
1328
|
-
}
|
|
1329
|
-
// We now know that dataOrError is IobDataEntry[]
|
|
1330
|
-
const data = dataOrError;
|
|
1331
|
-
if (initialOptions.count && !initialOptions.start && data.length > initialOptions.count) {
|
|
1332
|
-
data.splice(0, data.length - initialOptions.count);
|
|
1333
|
-
}
|
|
1334
|
-
if (data[0]) {
|
|
1335
|
-
let result;
|
|
1336
|
-
initialOptions.start ||= data[0].ts;
|
|
1337
|
-
let step = initialOptions.step || 0;
|
|
1338
|
-
const sourceLength = data.length;
|
|
1339
|
-
// "onchange" means "return the raw values" - there is no bucket logic for it in aggregationLogic(),
|
|
1340
|
-
// so it must be passed through here together with "none". Aggregating it would produce one entry per
|
|
1341
|
-
// interval with val === null, i.e. an empty chart.
|
|
1342
|
-
if (!initialOptions.aggregate ||
|
|
1343
|
-
initialOptions.aggregate === 'onchange' ||
|
|
1344
|
-
initialOptions.aggregate === 'none' ||
|
|
1345
|
-
initialOptions.preAggregated) {
|
|
1346
|
-
const options = initAggregate(initialOptions, id, undefined, log);
|
|
1347
|
-
options.result = data;
|
|
1348
|
-
step = 0;
|
|
1349
|
-
// convert ack from 0/1 to false/true
|
|
1350
|
-
if (initialOptions.ack) {
|
|
1351
|
-
for (let i = 0; i < data.length; i++) {
|
|
1352
|
-
data[i].ack = !!data[i].ack;
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
beautify(options);
|
|
1356
|
-
if (options.aggregate === 'none' && options.count && options.result.length > options.count) {
|
|
1357
|
-
options.result.splice(0, options.result.length - options.count);
|
|
1358
|
-
}
|
|
1359
|
-
result = options.result;
|
|
1360
|
-
adapter.log.debug(`Send with no aggregation: ${result.length} of: ${sourceLength} in: ${Date.now() - startTime}ms`);
|
|
1361
|
-
}
|
|
1362
|
-
else {
|
|
1363
|
-
const options = initAggregate(initialOptions, id, undefined, log);
|
|
1364
|
-
aggregation(options, data);
|
|
1365
|
-
finishAggregation(options);
|
|
1366
|
-
result = options.result;
|
|
1367
|
-
adapter.log.debug(`Send after aggregation: ${result.length} of: ${sourceLength} in: ${Date.now() - startTime}ms`);
|
|
1368
|
-
}
|
|
1369
|
-
adapter.sendTo(msg.from, msg.command, {
|
|
1370
|
-
result,
|
|
1371
|
-
step,
|
|
1372
|
-
sessionId: initialOptions.sessionId,
|
|
1373
|
-
}, msg.callback);
|
|
1374
|
-
}
|
|
1375
|
-
else {
|
|
1376
|
-
adapter.log.info('No Data');
|
|
1377
|
-
adapter.sendTo(msg.from, msg.command, { result: [], step: null, sessionId: initialOptions.sessionId }, msg.callback);
|
|
1378
|
-
}
|
|
1379
|
-
}
|
|
1380
|
-
function sendResponseCounter(adapter, msg, options, dataOrError) {
|
|
1381
|
-
// data
|
|
1382
|
-
// 1586713810000 100
|
|
1383
|
-
// 1586713810010 200
|
|
1384
|
-
// 1586713810040 500
|
|
1385
|
-
// 1586713810050 0
|
|
1386
|
-
// 1586713810090 400
|
|
1387
|
-
// 1586713810100 0
|
|
1388
|
-
// 1586713810110 100
|
|
1389
|
-
if (typeof dataOrError === 'string') {
|
|
1390
|
-
adapter.log.error(dataOrError);
|
|
1391
|
-
return adapter.sendTo(msg.from, msg.command, {
|
|
1392
|
-
result: [],
|
|
1393
|
-
error: dataOrError,
|
|
1394
|
-
sessionId: options.sessionId,
|
|
1395
|
-
}, msg.callback);
|
|
1396
|
-
}
|
|
1397
|
-
const data = dataOrError;
|
|
1398
|
-
if (data[0] && data[1]) {
|
|
1399
|
-
// first | start | afterFirst | ...... | last | end | afterLast
|
|
1400
|
-
// 5 | | 8 | 9 | 1 | 3 | | 5
|
|
1401
|
-
// | 5+(8-5/tsDiff) | | 9 | 1 | | 3+(5-3/tsDiff) |
|
|
1402
|
-
// (9 - 6.5) + (4 - 1)
|
|
1403
|
-
if (data[1].ts === options.start) {
|
|
1404
|
-
data.splice(0, 1);
|
|
1405
|
-
}
|
|
1406
|
-
if (data[0].ts < options.start && data[0].val > data[1].val) {
|
|
1407
|
-
data.splice(0, 1);
|
|
1408
|
-
}
|
|
1409
|
-
// interpolate from first to start time
|
|
1410
|
-
if (data[0].ts < options.start) {
|
|
1411
|
-
const val = data[0].val +
|
|
1412
|
-
(data[1].val - data[0].val) * ((options.start - data[0].ts) / (data[1].ts - data[0].ts));
|
|
1413
|
-
data.splice(0, 1);
|
|
1414
|
-
data.unshift({ ts: options.start, val, i: true });
|
|
1415
|
-
}
|
|
1416
|
-
if (data[data.length - 2] !== undefined && data[data.length - 2].ts === options.end) {
|
|
1417
|
-
data.length--;
|
|
1418
|
-
}
|
|
1419
|
-
const veryLast = data[data.length - 1];
|
|
1420
|
-
const beforeLast = data[data.length - 2];
|
|
1421
|
-
// interpolate from end time to last
|
|
1422
|
-
if (veryLast !== undefined && beforeLast !== undefined && options.end < veryLast.ts) {
|
|
1423
|
-
const val = beforeLast.val +
|
|
1424
|
-
(veryLast.val - beforeLast.val) * ((options.end - beforeLast.ts) / (veryLast.ts - beforeLast.ts));
|
|
1425
|
-
data.length--;
|
|
1426
|
-
data.push({ ts: options.end, val, i: true });
|
|
1427
|
-
}
|
|
1428
|
-
// at this point we expect [6.5, 9, 1, 4]
|
|
1429
|
-
// at this point we expect [150, 200, 500, 0, 400, 0, 50]
|
|
1430
|
-
let sum = 0;
|
|
1431
|
-
if (data.length > 1) {
|
|
1432
|
-
let val = data[data.length - 1].val;
|
|
1433
|
-
for (let i = data.length - 2; i >= 0; i--) {
|
|
1434
|
-
if (data[i].val < val) {
|
|
1435
|
-
sum += val - data[i].val;
|
|
1436
|
-
}
|
|
1437
|
-
val = data[i].val;
|
|
1438
|
-
}
|
|
1439
|
-
}
|
|
1440
|
-
adapter.sendTo(msg.from, msg.command, { result: sum, sessionId: options.sessionId }, msg.callback);
|
|
1441
|
-
}
|
|
1442
|
-
else {
|
|
1443
|
-
adapter.log.info('No Data');
|
|
1444
|
-
adapter.sendTo(msg.from, msg.command, { result: 0, step: null, sessionId: options.sessionId }, msg.callback);
|
|
1445
|
-
}
|
|
1446
|
-
}
|
|
1447
|
-
/**
|
|
1448
|
-
* Get quantile value from an array.
|
|
1449
|
-
*
|
|
1450
|
-
* @param q - quantile
|
|
1451
|
-
* @param list - list of sorted values (ascending)
|
|
1452
|
-
* @returns Quantile value
|
|
1453
|
-
*/
|
|
1454
|
-
function getQuantileValue(q, list) {
|
|
1455
|
-
if (!q) {
|
|
1456
|
-
return list[0];
|
|
1457
|
-
}
|
|
1458
|
-
const index = list.length * q;
|
|
1459
|
-
if (Number.isInteger(index)) {
|
|
1460
|
-
// mean of two middle numbers
|
|
1461
|
-
return (list[index - 1] + list[index]) / 2;
|
|
1462
|
-
}
|
|
1463
|
-
return list[Math.ceil(index - 1)];
|
|
1464
|
-
}
|
|
1465
|
-
/**
|
|
1466
|
-
* Calculate quantile for given array of values.
|
|
1467
|
-
*
|
|
1468
|
-
* @template T
|
|
1469
|
-
* @param q - quantile or a list of quantiles
|
|
1470
|
-
* @param list - array of values
|
|
1471
|
-
*/
|
|
1472
|
-
function quantile(q, list) {
|
|
1473
|
-
list = list.slice().sort(function (a, b) {
|
|
1474
|
-
a = Number.isNaN(a) ? Number.NEGATIVE_INFINITY : a;
|
|
1475
|
-
b = Number.isNaN(b) ? Number.NEGATIVE_INFINITY : b;
|
|
1476
|
-
if (a > b) {
|
|
1477
|
-
return 1;
|
|
1478
|
-
}
|
|
1479
|
-
if (a < b) {
|
|
1480
|
-
return -1;
|
|
1481
|
-
}
|
|
1482
|
-
return 0;
|
|
1483
|
-
});
|
|
1484
|
-
return getQuantileValue(q, list);
|
|
1485
|
-
}
|
|
1486
|
-
/** Sort function for IobDataEntry by timestamp */
|
|
1487
|
-
function sortByTs(a, b) {
|
|
1488
|
-
return a.ts - b.ts;
|
|
1489
|
-
}
|
|
1490
|
-
//# sourceMappingURL=aggregate.js.map
|