-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathaggregate.js
More file actions
1045 lines (964 loc) · 44.4 KB
/
aggregate.js
File metadata and controls
1045 lines (964 loc) · 44.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
'use strict';
// THIS file should be identical with sql and history adapter's one
function initAggregate(options) {
let log = () => {};
if (options.debugLog) {
log = options.log || console.log;
}
// step; // 1 Step is 1 second
if (options.step === null) {
options.step = (options.end - options.start) / options.count;
}
// Limit 2000
if ((options.end - options.start) / options.step > options.limit) {
options.step = (options.end - options.start) / options.limit;
}
options.maxIndex = Math.ceil(((options.end - options.start) / options.step) - 1);
options.result = [];
options.averageCount = [];
options.quantileDatapoints = [];
options.integralDatapoints = [];
options.aggregate = options.aggregate || 'minmax';
options.overallLength = 0;
if (options.aggregate === 'percentile') {
if (typeof options.percentile !== 'number' || options.percentile < 0 || options.percentile > 100) {
options.percentile = 50;
}
options.quantile = options.percentile / 100; // Internally we use quantile for percentile too
}
if (options.aggregate === 'quantile') {
if (typeof options.quantile !== 'number' || options.quantile < 0 || options.quantile > 1) {
options.quantile = 0.5;
}
}
if (options.aggregate === 'integral') {
if (typeof options.integralUnit !== 'number' || options.integralUnit <= 0) {
options.integralUnit = 60;
}
options.integralUnit *= 1000; // Convert to milliseconds
}
log(`${options.logId} Initialize: maxIndex = ${options.maxIndex}, step = ${options.step}, start = ${options.start}, end = ${options.end}`);
// pre-fill the result with timestamps (add one before start and one after end)
try {
options.result.length = options.maxIndex + 2;
} catch (err) {
err.message += `: ${options.maxIndex + 2}`;
throw err;
}
// We define the array length but do not prefill values, we do that on runtime when needed
options.result[0] = {
val: {ts: null, val: null},
max: {ts: null, val: null},
min: {ts: null, val: null},
start: {ts: null, val: null},
end: {ts: null, val: null}
};
options.result[options.maxIndex + 2] = {
val: {ts: null, val: null},
max: {ts: null, val: null},
min: {ts: null, val: null},
start: {ts: null, val: null},
end: {ts: null, val: null}
};
if (options.aggregate === 'average') {
options.averageCount[0] = 0;
options.averageCount[options.maxIndex + 2] = 0;
}
if (options.aggregate === 'percentile' || options.aggregate === 'quantile') {
options.quantileDatapoints[0] = [];
options.quantileDatapoints[options.maxIndex + 2] = [];
}
if (options.aggregate === 'integral') {
options.integralDatapoints[0] = [];
options.integralDatapoints[options.maxIndex + 2] = [];
}
return options;
}
function aggregation(options, data) {
let index;
let preIndex;
let collectedTooEarlyData = [];
let collectedTooLateData = [];
let preIndexValueFound = false;
let postIndexValueFound = false;
for (let i = 0; i < data.length; i++) {
if (!data[i]) {
continue;
}
if (typeof data[i].ts !== 'number') {
data[i].ts = parseInt(data[i].ts, 10);
}
preIndex = Math.floor((data[i].ts - options.start) / options.step);
// store all border values
if (preIndex < 0) {
index = 0;
// if the ts is even earlier than the "pre-interval" ignore it, else we collect all data there
if (preIndex < -1) {
collectedTooEarlyData.push(data[i]);
continue;
}
preIndexValueFound = true;
} else if (preIndex > options.maxIndex) {
index = options.maxIndex + 2;
// if the ts is even later than the "post-interval" ignore it, else we collect all data there
if (preIndex > options.maxIndex + 1) {
collectedTooLateData.push(data[i]);
continue;
}
postIndexValueFound = true;
} else {
index = preIndex + 1;
}
options.overallLength++;
if (options.result[index] === undefined) { // lazy initialization of data structure
options.result[index] = {
val: {ts: null, val: null},
max: {ts: null, val: null},
min: {ts: null, val: null},
start: {ts: null, val: null},
end: {ts: null, val: null},
};
if (options.aggregate === 'average' || options.aggregate === 'count') {
options.averageCount[index] = 0;
}
if (options.aggregate === 'percentile' || options.aggregate === 'quantile') {
options.quantileDatapoints[index] = [];
}
if (options.aggregate === 'integral') {
options.integralDatapoints[index] = [];
}
}
aggregationLogic(data[i], index, options);
}
// If no data was found in the pre-interval, but we have earlier data, we put the latest of them in the pre-interval
if (!preIndexValueFound && collectedTooEarlyData.length > 0) {
collectedTooEarlyData = collectedTooEarlyData.sort(sortByTs);
options.overallLength++;
aggregationLogic(collectedTooEarlyData[collectedTooEarlyData.length - 1], 0, options);
}
// If no data was found in the post-interval, but we have later data, we put the earliest of them in the post-interval
if (!postIndexValueFound && collectedTooLateData.length > 0) {
collectedTooLateData = collectedTooLateData.sort(sortByTs);
options.overallLength++;
aggregationLogic(collectedTooLateData[0], options.maxIndex + 2, options);
}
return { result: options.result, step: options.step, sourceLength: data.length };
}
function aggregationLogic(data, index, options) {
let log = () => {};
if (options.debugLog) {
log = options.log || console.log;
}
if (!options.result[index]) {
log(`${options.logId} Data index ${index} not initialized, ignore!`);
return;
}
if (options.aggregate !== 'minmax' && !options.result[index].val.ts) {
options.result[index].val.ts = Math.round(options.start + (((index - 1) + 0.5) * options.step));
}
if (options.aggregate === 'max') {
if (options.result[index].val.val === null || options.result[index].val.val < data.val) {
options.result[index].val.val = data.val;
}
} else if (options.aggregate === 'min') {
if (options.result[index].val.val === null || options.result[index].val.val > data.val) {
options.result[index].val.val = data.val;
}
} else if (options.aggregate === 'average') {
options.result[index].val.val += parseFloat(data.val);
options.averageCount[index]++;
} else if (options.aggregate === 'count') {
options.averageCount[index]++;
} else if (options.aggregate === 'total') {
options.result[index].val.val += parseFloat(data.val);
} else if (options.aggregate === 'minmax') {
if (options.result[index].min.ts === null) {
options.result[index].min.ts = data.ts;
options.result[index].min.val = data.val;
options.result[index].max.ts = data.ts;
options.result[index].max.val = data.val;
options.result[index].start.ts = data.ts;
options.result[index].start.val = data.val;
options.result[index].end.ts = data.ts;
options.result[index].end.val = data.val;
} else {
if (data.val !== null) {
if (data.val > options.result[index].max.val) {
options.result[index].max.ts = data.ts;
options.result[index].max.val = data.val;
} else if (data.val < options.result[index].min.val) {
options.result[index].min.ts = data.ts;
options.result[index].min.val = data.val;
}
if (data.ts > options.result[index].end.ts) {
options.result[index].end.ts = data.ts;
options.result[index].end.val = data.val;
}
} else {
if (data.ts > options.result[index].end.ts) {
options.result[index].end.ts = data.ts;
options.result[index].end.val = null;
}
}
}
} else if (options.aggregate === 'percentile' || options.aggregate === 'quantile') {
options.quantileDatapoints[index].push(data.val);
log(`${options.logId} Quantile ${index}: Add ts= ${data.ts} val=${data.val}`);
} else if (options.aggregate === 'integral') {
options.integralDatapoints[index].push(data);
log(`${options.logId} Integral ${index}: Add ts= ${data.ts} val=${data.val}`);
}
}
function finishAggregation(options) {
let log = () => {};
if (options.debugLog) {
log = options.log || console.log;
}
if (options.aggregate === 'minmax') {
let preBorderValueRemoved = false;
let postBorderValueRemoved = false;
const originalResultLength = options.result.length;
let startIndex = 0;
let endIndex = options.result.length;
const finalResult = [];
for (let ii = startIndex; ii < endIndex; ii++) {
// no one value in this period
if (options.result[ii] === undefined || options.result[ii].start.ts === null) {
if (ii === 0) {
preBorderValueRemoved = true;
} else if (ii === originalResultLength - 1) {
postBorderValueRemoved = true;
}
// options.result.splice(ii, 1);
continue;
}
// just one value in this period: max == min == start == end
if (options.result[ii].start.ts === options.result[ii].end.ts) {
finalResult.push({
ts: options.result[ii].start.ts,
val: options.result[ii].start.val
});
} else
if (options.result[ii].min.ts === options.result[ii].max.ts) {
// if just 2 values: start == min == max, end
if (options.result[ii].start.ts === options.result[ii].min.ts ||
options.result[ii].end.ts === options.result[ii].min.ts) {
finalResult.push({
ts: options.result[ii].start.ts,
val: options.result[ii].start.val
});
finalResult.push({
ts: options.result[ii].end.ts,
val: options.result[ii].end.val
});
} // if just 3 values: start, min == max, end
else {
finalResult.push({
ts: options.result[ii].start.ts,
val: options.result[ii].start.val
});
finalResult.push({
ts: options.result[ii].max.ts,
val: options.result[ii].max.val
});
finalResult.push({
ts: options.result[ii].end.ts,
val: options.result[ii].end.val
});
}
} else
if (options.result[ii].start.ts === options.result[ii].max.ts) {
// just one value in this period: start == max, min == end
if (options.result[ii].min.ts === options.result[ii].end.ts) {
finalResult.push({
ts: options.result[ii].start.ts,
val: options.result[ii].start.val
});
finalResult.push({
ts: options.result[ii].end.ts,
val: options.result[ii].end.val
});
} // start == max, min, end
else {
finalResult.push({
ts: options.result[ii].start.ts,
val: options.result[ii].start.val
});
finalResult.push({
ts: options.result[ii].min.ts,
val: options.result[ii].min.val
});
finalResult.push({
ts: options.result[ii].end.ts,
val: options.result[ii].end.val
});
}
} else
if (options.result[ii].end.ts === options.result[ii].max.ts) {
// just one value in this period: start == min, max == end
if (options.result[ii].min.ts === options.result[ii].start.ts) {
finalResult.push({
ts: options.result[ii].start.ts,
val: options.result[ii].start.val
});
finalResult.push({
ts: options.result[ii].end.ts,
val: options.result[ii].end.val
});
} // start, min, max == end
else {
finalResult.push({
ts: options.result[ii].start.ts,
val: options.result[ii].start.val
});
finalResult.push({
ts: options.result[ii].min.ts,
val: options.result[ii].min.val
});
finalResult.push({
ts: options.result[ii].end.ts,
val: options.result[ii].end.val
});
}
} else
if (options.result[ii].start.ts === options.result[ii].min.ts ||
options.result[ii].end.ts === options.result[ii].min.ts) {
// just one value in this period: start == min, max, end
finalResult.push({
ts: options.result[ii].start.ts,
val: options.result[ii].start.val
});
finalResult.push({
ts: options.result[ii].max.ts,
val: options.result[ii].max.val
});
finalResult.push({
ts: options.result[ii].end.ts,
val: options.result[ii].end.val
});
} else {
finalResult.push({
ts: options.result[ii].start.ts,
val: options.result[ii].start.val
});
// just one value in this period: start == min, max, end
if (options.result[ii].max.ts > options.result[ii].min.ts) {
finalResult.push({
ts: options.result[ii].min.ts,
val: options.result[ii].min.val
});
finalResult.push({
ts: options.result[ii].max.ts,
val: options.result[ii].max.val
});
} else {
finalResult.push({
ts: options.result[ii].max.ts,
val: options.result[ii].max.val
});
finalResult.push({
ts: options.result[ii].min.ts,
val: options.result[ii].min.val
});
}
finalResult.push({
ts: options.result[ii].end.ts,
val: options.result[ii].end.val
});
}
}
if (options.removeBorderValues) { // we cut out the additional results
if (!preBorderValueRemoved) {
finalResult.splice(0, 1);
}
if (!postBorderValueRemoved) {
finalResult.length--;
}
}
options.result = finalResult;
} else if (options.aggregate === 'average') {
let startIndex = 0;
let endIndex = options.result.length;
const finalResult = [];
if (options.removeBorderValues) { // we cut out the additional results
// options.result.splice(0, 1);
// options.averageCount.splice(0, 1);
// options.result.length--;
// options.averageCount.length--;
startIndex++;
endIndex--;
}
for (let k = startIndex; k < endIndex; k++) {
if (options.result[k] !== undefined && options.result[k].val.ts) {
finalResult.push({
ts: options.result[k].val.ts,
val: options.result[k].val.val !== null ? Math.round(options.result[k].val.val / options.averageCount[k] * 100) / 100 : null
});
} else {
// no one value in this interval
// options.result.splice(k, 1);
// options.averageCount.splice(k, 1); // not needed to clean up because not used anymore afterwards
}
}
options.result = finalResult;
} else if (options.aggregate === 'count') {
let startIndex = 0;
let endIndex = options.result.length;
const finalResult = [];
if (options.removeBorderValues) { // we cut out the additional results
// options.result.splice(0, 1);
// options.averageCount.splice(0, 1);
// options.result.length--;
// options.averageCount.length--;
startIndex++;
endIndex--;
}
for (let k = startIndex; k < endIndex; k++) {
if (options.result[k] !== undefined && options.result[k].val.ts) {
finalResult.push({
ts: options.result[k].val.ts,
val: options.averageCount[k],
});
} else {
// no one value in this interval
// options.result.splice(k, 1);
// options.averageCount.splice(k, 1); // not needed to clean up because not used anymore afterward
}
}
options.result = finalResult;
} else if (options.aggregate === 'integral') {
let preBorderValueRemoved = false;
let postBorderValueRemoved = false;
const originalResultLength = options.result.length;
const finalResult = [];
for (let k = 0; k < options.result.length; k++) {
let indexStartTs = options.start + ((k - 1) * options.step);
let indexEndTs = indexStartTs + options.step;
if (options.integralDatapoints[k] && options.integralDatapoints[k].length) {
// Sort data points by ts first
options.integralDatapoints[k].sort(sortByTs);
}
// Make sure that we have entries that always start at the beginning of the interval
if ((!options.integralDatapoints[k] || !options.integralDatapoints[k].length || options.integralDatapoints[k][0].ts > indexStartTs) && options.integralDatapoints[k - 1] && options.integralDatapoints[k -1][options.integralDatapoints[k - 1].length - 1]) {
// if the first entry of this interval started somewhere in the start of the interval, add a start entry
// same if there is no entry at all in the timeframe, use last entry from interval before
options.integralDatapoints[k] = options.integralDatapoints[k] || [];
options.integralDatapoints[k].unshift({
ts: indexStartTs,
val: options.integralDatapoints[k - 1][options.integralDatapoints[k - 1].length - 1].val
});
log(`${options.logId} Integral: ${k}: Added start entry for interval with ts=${indexStartTs}, val=${options.integralDatapoints[k][0].val}`);
} else if (options.integralDatapoints[k] && options.integralDatapoints[k].length && options.integralDatapoints[k][0].ts > indexStartTs) {
options.integralDatapoints[k].unshift({
ts: indexStartTs,
val: options.integralDatapoints[k][0].val
});
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`);
} else if (options.integralDatapoints[k] && options.integralDatapoints[k].length && options.integralDatapoints[k][0].ts < indexStartTs) {
// 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
let preFirstIndex = null;
for (let kk = 0; kk < options.integralDatapoints[k].length; kk++) {
if (options.integralDatapoints[k][kk].ts >= indexStartTs) {
break;
}
preFirstIndex = kk;
}
if (preFirstIndex !== null) {
options.integralDatapoints[k].splice(0, preFirstIndex, {
ts: indexStartTs,
val: options.integralDatapoints[k][preFirstIndex].val
});
log(`${options.logId} Integral: ${k}: Remove ${preFirstIndex + 1} entries and add start entry for interval with ts=${indexStartTs}, val=${options.integralDatapoints[k][0].val}`);
}
}
const point = {
ts: options.result[k] !== undefined && options.result[k].val.ts ? options.result[k].val.ts : Math.round(options.start + (((k - 1) + 0.5) * options.step)),
val: null
}
const integralDatapoints = options.integralDatapoints[k] || [];
const vals = integralDatapoints.map(dp => `[${dp.ts}, ${dp.val}]`);
log(`${options.logId} Integral: ${k}: ${integralDatapoints.length} datapoints for interval for ${indexStartTs} - ${indexEndTs}: ${vals.join(',')}`);
// Calculate Intervals and always calculate till the interval end (start made sure above already)
for (let kk = 0; kk < integralDatapoints.length; kk++) {
const valEndTs = integralDatapoints[kk + 1] ? Math.min(integralDatapoints[kk + 1].ts, indexEndTs) : indexEndTs;
let valDuration = valEndTs - integralDatapoints[kk].ts;
if (valDuration < 0) {
log(`${options.logId} Integral: ${k}[${kk}] data do not belong to this interval, ignore ${JSON.stringify(integralDatapoints[kk])} (vs. ${valEndTs})`)
break;
}
if (valDuration === 0) {
log(`${options.logId} Integral: ${k}[${kk}] valDuration zero, ignore ${JSON.stringify(integralDatapoints[kk])}`)
continue;
}
let valStart = parseFloat(integralDatapoints[kk].val) || 0;
// End value is the next value, or if none, assume "linearity
let valEnd = parseFloat((integralDatapoints[kk + 1] ? integralDatapoints[kk + 1].val : (options.integralDatapoints[k + 1] && options.integralDatapoints[k + 1][0] ? options.integralDatapoints[k + 1][0].val : valStart))) || 0;
if (options.integralInterpolation !== 'linear' || valStart === valEnd) {
const integralAdd = valStart * valDuration / options.integralUnit;
// simple rectangle linear interpolation
log(`${options.logId} Integral: ${k}[${kk}] : Add ${integralAdd} from val=${valStart} for ${valDuration}`);
point.val += integralAdd;
} else if ((valStart >= 0 && valEnd >= 0) || (valStart <= 0 && valEnd <= 0)) {
// start and end are both positive or both negative, or one is 0
let multiplier = 1;
if (valStart <= 0 && valEnd <= 0) {
multiplier = -1; // correct the sign at the end
valStart = -valStart;
valEnd = -valEnd;
}
const minVal = Math.min(valStart, valEnd);
const maxVal = Math.max(valStart, valEnd);
const rectPart = minVal * valDuration / options.integralUnit;
const trianglePart = (maxVal - minVal) * valDuration * 0.5 / options.integralUnit;
const integralAdd = (rectPart + trianglePart) * multiplier;
log(`${options.logId} Integral: ${k}[${kk}] : Add R${rectPart} + T${trianglePart} => ${integralAdd} from val=${valStart} to ${valEnd} for ${valDuration}`);
point.val += integralAdd;
} else {
// Values are on different sides of 0, so we need to find the 0 crossing
const zeroCrossing = Math.abs((valStart * valDuration) / (valEnd - valStart));
// Then calculate two linear segments, one from 0 to the crossing, and one from the crossing to the end
const trianglePart1 = valStart * zeroCrossing * 0.5 / options.integralUnit;
const trianglePart2 = valEnd * (valDuration - zeroCrossing) * 0.5 / options.integralUnit;
const integralAdd = trianglePart1 + trianglePart2;
log(`${options.logId} Integral: ${k}[${kk}] : Add T${trianglePart1} + T${trianglePart2} => ${integralAdd} from val=${valStart} to ${valEnd} for ${valDuration} (zero crossing ${zeroCrossing})`);
point.val += integralAdd;
}
}
/*options.result[k] = {
ts: options.result[k].val.ts,
val: options.result[k].val.val
}*/
if (point.val !== null) {
finalResult.push(point);
} else {
if (k === 0) {
preBorderValueRemoved = true;
} else if (k === originalResultLength - 1) {
postBorderValueRemoved = true;
}
}
}
if (options.removeBorderValues) { // we cut out the additional results
if (!preBorderValueRemoved) {
finalResult.splice(0, 1);
}
if (!postBorderValueRemoved) {
finalResult.length--;
}
}
options.result = finalResult;
} else if (options.aggregate === 'percentile' || options.aggregate === 'quantile') {
let startIndex = 0;
let endIndex = options.result.length;
const finalResult = [];
if (options.removeBorderValues) { // we cut out the additional results
/*
options.result.splice(0, 1);
options.quantileDatapoints.splice(0, 1);
options.result.length--
options.quantileDatapoints.length--;
*/
startIndex++;
endIndex--;
}
for (let k = startIndex; k < endIndex; k++) {
if (options.result[k] !== undefined && options.result[k].val.ts) {
const point = {
ts: options.result[k].val.ts,
val: quantile(options.quantile, options.quantileDatapoints[k])
};
log(`${options.logId} Quantile ${k} ${point.ts}: ${options.quantileDatapoints[k].join(', ')} -> ${point.val}`);
finalResult.push(point);
} else {
// no one value in this interval
// options.result.splice(k, 1);
// options.quantileDatapoints.splice(k, 1); // not needed to clean up because not used anymore afterward
}
}
options.result = finalResult;
} else {
let startIndex = 0;
let endIndex = options.result.length;
const finalResult = [];
if (options.removeBorderValues) { // we cut out the additional results
// options.result.splice(0, 1);
// options.result.length--;
startIndex++;
endIndex--;
}
for (let j = startIndex; j < endIndex; j++) {
if (options.result[j] !== undefined && options.result[j].val.ts) {
finalResult.push({
ts: options.result[j].val.ts,
val: options.result[j].val.val
});
} else {
// no one value in this interval
//options.result.splice(j, 1);
}
}
options.result = finalResult;
}
beautify(options);
}
function beautify(options) {
let log = () => {};
if (options.debugLog) {
log = options.log || console.log;
}
log(`${options.logId} Beautify: ${options.result.length} results`);
let preFirstValue = null;
let postLastValue = null;
let removedBorderValues = []; // Keep track of removed border values for potential restoration
if (options.ignoreNull === 'true') { // include nulls and replace them with last value
options.ignoreNull = true;
} else
if (options.ignoreNull === 'false') { // include nulls
options.ignoreNull = false;
} else
if (options.ignoreNull === '0') { // include nulls and replace them with 0
options.ignoreNull = 0;
} else
if (options.ignoreNull !== true && options.ignoreNull !== false && options.ignoreNull !== 0) {
options.ignoreNull = false;
}
// process null values, remove points outside the span and find first points after end and before start
for (let i = 0; i < options.result.length; i++) {
if (options.ignoreNull !== false) {
// if value is null
if (options.result[i].val === null) {
// null value must be replaced with last not null value
if (options.ignoreNull === true) {
// remove value
options.result.splice(i, 1);
i--;
continue;
} else {
// null value must be replaced with 0
options.result[i].val = options.ignoreNull;
}
}
}
// remove all not requested points
if (options.result[i].ts < options.start) {
preFirstValue = options.result[i].val !== null ? options.result[i] : null;
removedBorderValues.push(options.result[i]); // Keep track of removed values
options.result.splice(i, 1);
i--;
continue;
}
postLastValue = options.result[i].val !== null ? options.result[i] : null;
if (options.result[i].ts > options.end) {
// Keep track of all values being removed after the end time
for (let j = i; j < options.result.length; j++) {
removedBorderValues.push(options.result[j]);
}
options.result.splice(i, options.result.length - i);
break;
}
}
// Special handling for removeBorderValues: true with count specified
// If we removed all data due to time filtering but had data originally, and removeBorderValues is true,
// we should preserve the closest data point(s) to satisfy the count requirement
if (options.removeBorderValues && options.count && options.result.length === 0 && removedBorderValues.length > 0) {
log(`${options.logId} removeBorderValues + count edge case: restoring up to ${options.count} closest data points`);
// Sort removed border values by distance from the time range
const sortedByDistance = removedBorderValues.map(item => {
let distance;
if (item.ts < options.start) {
distance = options.start - item.ts;
} else if (item.ts > options.end) {
distance = item.ts - options.end;
} else {
distance = 0; // Should not happen since these were removed as border values
}
return { item, distance };
}).sort((a, b) => a.distance - b.distance);
// Restore up to 'count' closest data points
for (let i = 0; i < Math.min(options.count, sortedByDistance.length); i++) {
options.result.push(sortedByDistance[i].item);
}
// Sort the result by timestamp to maintain order
options.result.sort((a, b) => a.ts - b.ts);
}
// check start and stop
if (options.result.length && options.aggregate !== 'none' && !options.removeBorderValues) {
const firstTS = options.result[0].ts;
if (firstTS > options.start && !options.removeBorderValues) {
if (preFirstValue) {
const firstY = options.result[0].val;
// if steps
if (options.aggregate === 'onchange' || !options.aggregate) {
if (preFirstValue.ts !== firstTS) {
options.result.unshift({ts: options.start, val: preFirstValue.val});
} else {
if (options.ignoreNull) {
options.result.unshift({ts: options.start, val: firstY});
}
}
} else {
if (preFirstValue.ts !== firstTS) {
if (firstY !== null) {
// interpolate
const y = preFirstValue.val + (firstY - preFirstValue.val) * (options.start - preFirstValue.ts) / (firstTS - preFirstValue.ts);
options.result.unshift({ts: options.start, val: y, i: true});
log(`${options.logId} interpolate ${y} from ${preFirstValue.val} to ${firstY} as first return value`);
} else {
options.result.unshift({ts: options.start, val: null});
}
} else {
if (options.ignoreNull) {
options.result.unshift({ts: options.start, val: firstY});
}
}
}
} else {
if (options.ignoreNull) {
options.result.unshift({ts: options.start, val: options.result[0].val});
} else {
options.result.unshift({ts: options.start, val: null});
}
}
}
const lastTS = options.result[options.result.length - 1].ts;
if (lastTS < options.end && !options.removeBorderValues) {
if (postLastValue) {
// if steps
if (options.aggregate === 'onchange' || !options.aggregate) {
// if more data following, draw line to the end of the chart
if (postLastValue.ts !== lastTS) {
options.result.push({ts: options.end, val: postLastValue.val});
} else {
if (options.ignoreNull) {
options.result.push({ts: options.end, val: postLastValue.val});
}
}
} else {
if (postLastValue.ts !== lastTS) {
const lastY = options.result[options.result.length - 1].val;
if (lastY !== null) {
// make interpolation
const _y = lastY + (postLastValue.val - lastY) * (options.end - lastTS) / (postLastValue.ts - lastTS);
options.result.push({ts: options.end, val: _y, i: true});
log(`${options.logId} interpolate ${_y} from ${lastY} to ${postLastValue.val} as last return value`);
} else {
options.result.push({ts: options.end, val: null});
}
} else {
if (options.ignoreNull) {
options.result.push({ts: options.end, val: postLastValue.val});
}
}
}
} else {
if (options.ignoreNull) {
const lastY = options.result[options.result.length - 1].val;
// if no more data, that means do not draw line
options.result.push({ts: options.end, val: lastY});
} else {
// if no more data, that means do not draw line
options.result.push({ts: options.end, val: null});
}
}
}
} else if (options.aggregate === 'none') {
if (options.count && options.result.length > options.count) {
options.result.splice(0, options.result.length - options.count);
}
}
if (options.addId) {
for (let i = 0; i < options.result.length; i++) {
if (!options.result[i].id && options.id) {
options.result[i].id = options.index || options.id;
}
}
}
}
function sendResponse(adapter, msg, options, data, startTime) {
let aggregateData;
if (typeof data === 'string') {
adapter.log.error(data);
return adapter.sendTo(msg.from, msg.command, {
result: [],
step: 0,
error: data,
sessionId: options.sessionId
}, msg.callback);
}
if (options.count && !options.start && data.length > options.count) {
data.splice(0, data.length - options.count);
}
if (data[0]) {
options.start = options.start || data[0].ts;
if (!options.aggregate || options.aggregate === 'onchange' || options.aggregate === 'none' || options.preAggregated) {
aggregateData = {result: data, step: 0, sourceLength: data.length};
// convert ack from 0/1 to false/true
if (options.ack && aggregateData.result) {
for (let i = 0; i < aggregateData.result.length; i++) {
aggregateData.result[i].ack = !!aggregateData.result[i].ack;
}
}
options.result = aggregateData.result;
if (options.debugLog) {
(options.log || console.log)(`${options.logId} Before beautify: ${options.result.length} results, count: ${options.count}, removeBorderValues: ${options.removeBorderValues}`);
}
beautify(options);
if (options.debugLog) {
(options.log || console.log)(`${options.logId} After beautify: ${options.result.length} results`);
}
if (options.aggregate === 'none' && options.count && options.result.length > options.count) {
if (options.debugLog) {
(options.log || console.log)(`${options.logId} Applying count limit: ${options.result.length} -> ${options.count}, removeBorderValues: ${options.removeBorderValues}`);
}
options.result.splice(0, options.result.length - options.count);
}
aggregateData.result = options.result;
} else {
initAggregate(options);
aggregateData = aggregation(options, data);
finishAggregation(options);
aggregateData.result = options.result;
}
adapter.log.debug(`Send: ${aggregateData.result.length} of: ${aggregateData.sourceLength} in: ${Date.now() - startTime}ms`);
adapter.sendTo(msg.from, msg.command, {
result: aggregateData.result,
step: aggregateData.step,
error: null,
sessionId: options.sessionId
}, msg.callback);
} else {
adapter.log.info('No Data');
adapter.sendTo(msg.from, msg.command, {result: [], step: null, error: null, sessionId: options.sessionId}, msg.callback);
}
}
function sendResponseCounter(adapter, msg, options, data, startTime) {
// data
// 1586713810000 100
// 1586713810010 200
// 1586713810040 500
// 1586713810050 0
// 1586713810090 400
// 1586713810100 0
// 1586713810110 100
if (typeof data === 'string') {
adapter.log.error(data);
return adapter.sendTo(msg.from, msg.command, {
result: [],
error: data,
sessionId: options.sessionId
}, msg.callback);
}
if (data[0] && data[1]) {
// first | start | afterFirst | ...... | last | end | afterLast
// 5 | | 8 | 9 | 1 | 3 | | 5
// | 5+(8-5/tsDiff) | | 9 | 1 | | 3+(5-3/tsDiff) |
// (9 - 6.5) + (4 - 1)
if (data[1].ts === options.start) {
data.splice(0, 1);
}
if (data[0].ts < options.start && data[0].val > data[1].val) {
data.splice(0, 1);
}
// interpolate from first to start time
if (data[0].ts < options.start) {
const val = data[0].val + (data[1].val - data[0].val) * ((options.start - data[0].ts) / (data[1].ts - data[0].ts));
data.splice(0, 1);
data.unshift({ts: options.start, val, i: true});
}
if (data[data.length - 2] !== undefined && data[data.length - 2].ts === options.end) {
data.length--;
}
const veryLast = data[data.length - 1];
const beforeLast = data[data.length - 2];
// interpolate from end time to last
if (veryLast !== undefined && beforeLast !== undefined && options.end < veryLast.ts) {
const val = beforeLast.val + (veryLast.val - beforeLast.val) * ((options.end - beforeLast.ts) / (veryLast.ts - beforeLast.ts));
data.length--;
data.push({ts: options.end, val, i: true});
}
// at this point we expect [6.5, 9, 1, 4]
// at this point we expect [150, 200, 500, 0, 400, 0, 50]
let sum = 0;
if (data.length > 1) {
let val = data[data.length - 1].val;
for (let i = data.length - 2; i >= 0; i--) {
if (data[i].val < val) {
sum += val - data[i].val;
}
val = data[i].val;
}
}
adapter.sendTo(msg.from, msg.command, {
result: sum,
error: null,
sessionId: options.sessionId
}, msg.callback);
} else {
adapter.log.info('No Data');
adapter.sendTo(msg.from, msg.command, {result: 0, step: null, error: null, sessionId: options.sessionId}, msg.callback);
}
}
/**
* Get quantile value from an array.
*
* @param {Number} q - quantile
* @param {Array|TypedArray} list - list of sorted values (ascending)
*
* @return {number} Quantile value
*/
function getQuantileValue(q, list) {
if (q === 0) return list[0];
const index = list.length * q;
if (Number.isInteger(index)) {
// mean of two middle numbers
return (list[index - 1] + list[index]) / 2;
}
return list[Math.ceil(index - 1)];
}
/**
* Calculate quantile for given array of values.
*
* @template T
* @param {Number|Array<Number>} qOrPs - quantile or a list of quantiles
* @param {Array<T>|Array<Number>|TypedArray} list - array of values
* @param {function(T): Number} [fn] - optional function to extract a value from an array item