-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgraphEntry.ts
466 lines (415 loc) · 15.7 KB
/
graphEntry.ts
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
import { HomeAssistant } from 'custom-card-helpers';
import { ChartCardSeriesConfig, EntityCachePoints, EntityEntryCache, HassHistory, HistoryBuckets } from './types';
import { compress, decompress, log } from './utils';
import localForage from 'localforage';
import { HassEntity } from 'home-assistant-js-websocket';
import { DateRange } from 'moment-range';
import { HOUR_24, moment } from './const';
import parse from 'parse-duration';
import SparkMD5 from 'spark-md5';
import { ChartCardSpanExtConfig } from './types-config';
import * as pjson from '../package.json';
export default class GraphEntry {
private _computedHistory?: EntityCachePoints;
private _hass?: HomeAssistant;
private _entityID: string;
private _entityState?: HassEntity;
private _updating = false;
private _cache = true;
// private _hoursToShow: number;
private _graphSpan: number;
private _useCompress = false;
private _index: number;
private _config: ChartCardSeriesConfig;
private _timeRange: DateRange;
private _func: (item: EntityCachePoints) => number;
private _realStart: Date;
private _realEnd: Date;
private _groupByDurationMs: number;
private _md5Config: string;
constructor(
index: number,
graphSpan: number,
cache: boolean,
config: ChartCardSeriesConfig,
span: ChartCardSpanExtConfig | undefined,
) {
const aggregateFuncMap = {
avg: this._average,
max: this._maximum,
min: this._minimum,
first: this._first,
last: this._last,
sum: this._sum,
median: this._median,
delta: this._delta,
diff: this._diff,
};
this._index = index;
this._cache = cache;
this._entityID = config.entity;
this._graphSpan = graphSpan;
this._config = config;
const now = new Date();
const now2 = new Date(now);
this._func = aggregateFuncMap[config.group_by.func];
now2.setTime(now2.getTime() - HOUR_24);
this._timeRange = moment.range(now, now2);
this._realEnd = new Date();
this._realStart = new Date();
// Valid because tested during init;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this._groupByDurationMs = parse(this._config.group_by.duration)!;
this._md5Config = SparkMD5.hash(`${this._graphSpan}${JSON.stringify(this._config)}${JSON.stringify(span)}`);
}
set hass(hass: HomeAssistant) {
this._hass = hass;
this._entityState = this._hass.states[this._entityID];
}
get history(): EntityCachePoints {
return this._computedHistory || [];
}
get index(): number {
return this._index;
}
get start(): Date {
return this._realStart;
}
get end(): Date {
return this._realEnd;
}
set cache(cache: boolean) {
this._cache = cache;
}
get min(): number | undefined {
if (!this._computedHistory || this._computedHistory.length === 0) return undefined;
return Math.min(...this._computedHistory.flatMap((item) => (item[1] === null ? [] : [item[1]])));
}
get max(): number | undefined {
if (!this._computedHistory || this._computedHistory.length === 0) return undefined;
return Math.max(...this._computedHistory.flatMap((item) => (item[1] === null ? [] : [item[1]])));
}
private async _getCache(key: string, compressed: boolean): Promise<EntityEntryCache | undefined> {
const data: EntityEntryCache | undefined | null = await localForage.getItem(
`${key}_${this._md5Config}${compressed ? '' : '-raw'}`,
);
return data ? (compressed ? decompress(data) : data) : undefined;
}
private async _setCache(
key: string,
data: EntityEntryCache,
compressed: boolean,
): Promise<string | EntityEntryCache> {
return compressed
? localForage.setItem(`${key}_${this._md5Config}`, compress(data))
: localForage.setItem(`${key}_${this._md5Config}-raw`, data);
}
public async _updateHistory(start: Date, end: Date): Promise<boolean> {
let startHistory = new Date(start);
if (this._config.group_by.func !== 'raw') {
const range = end.getTime() - start.getTime();
const nbBuckets = Math.abs(range / this._groupByDurationMs) + (range % this._groupByDurationMs > 0 ? 1 : 0);
startHistory = new Date(end.getTime() - nbBuckets * this._groupByDurationMs);
}
if (!this._entityState || this._updating) return false;
this._updating = true;
this._timeRange = moment.range(startHistory, end);
let history: EntityEntryCache | undefined = undefined;
if (this._config.data_generator) {
history = this._generateData(start, end);
} else {
this._realStart = new Date(start);
this._realEnd = new Date(end);
let skipInitialState = false;
history = this._cache ? await this._getCache(this._entityID, this._useCompress) : undefined;
if (history && history.span === this._graphSpan) {
const currDataIndex = history.data.findIndex((item) => item && new Date(item[0]).getTime() > start.getTime());
if (currDataIndex !== -1) {
// skip initial state when fetching recent/not-cached data
skipInitialState = true;
}
if (currDataIndex > 4) {
// >4 so that the graph has some more history
history.data = history.data.slice(currDataIndex === 0 ? 0 : currDataIndex - 4);
} else if (currDataIndex === -1) {
// there was no state which could be used in current graph so clearing
history.data = [];
}
} else {
history = undefined;
}
const newHistory = await this._fetchRecent(
// if data in cache, get data from last data's time + 1ms
history && history.data && history.data.length !== 0 && history.data.slice(-1)[0]
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
new Date(history.data.slice(-1)[0]![0] + 1)
: startHistory,
end,
this._config.attribute ? false : skipInitialState,
this._config.attribute ? true : false,
);
if (newHistory && newHistory[0] && newHistory[0].length > 0) {
/*
hack because HA doesn't return anything if skipInitialState is false
when retrieving for attributes so we retrieve it and we remove it.
*/
if (this._config.attribute && skipInitialState) {
newHistory[0].shift();
}
let lastNonNull: number | null = null;
if (history && history.data && history.data.length > 0) {
lastNonNull = history.data[history.data.length - 1][1];
}
const newStateHistory: EntityCachePoints = newHistory[0].map((item) => {
let currentState: unknown = null;
if (this._config.attribute) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (item.attributes && item.attributes![this._config.attribute]) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
currentState = item.attributes![this._config.attribute];
}
} else {
currentState = item.state;
}
if (this._config.transform) {
currentState = this._applyTransform(currentState);
}
let stateParsed: number | null = parseFloat(currentState as string);
stateParsed = !Number.isNaN(stateParsed) ? stateParsed : null;
if (stateParsed === null) {
if (this._config.fill_raw === 'zero') {
stateParsed = 0;
} else if (this._config.fill_raw === 'last') {
stateParsed = lastNonNull;
}
} else {
lastNonNull = stateParsed;
}
if (this._config.attribute) {
return [new Date(item.last_updated).getTime(), !Number.isNaN(stateParsed) ? stateParsed : null];
} else {
return [new Date(item.last_changed).getTime(), !Number.isNaN(stateParsed) ? stateParsed : null];
}
});
if (history?.data.length) {
history.span = this._graphSpan;
history.last_fetched = new Date();
history.card_version = pjson.version;
if (history.data.length !== 0) {
history.data.push(...newStateHistory);
}
} else {
history = {
span: this._graphSpan,
card_version: pjson.version,
last_fetched: new Date(),
data: newStateHistory,
};
}
if (this._cache) {
this._setCache(this._entityID, history, this._useCompress).catch((err) => {
log(err);
localForage.clear();
});
}
}
}
if (!history || history.data.length === 0) {
this._updating = false;
this._computedHistory = undefined;
return false;
}
if (this._config.group_by.func !== 'raw') {
this._computedHistory = this._dataBucketer(history).map((bucket) => {
return [bucket.timestamp, this._func(bucket.data)];
});
} else {
this._computedHistory = history.data;
}
this._updating = false;
return true;
}
private _applyTransform(value: unknown): number | null {
return new Function('x', 'hass', `'use strict'; ${this._config.transform}`).call(this, value, this._hass);
}
private async _fetchRecent(
start: Date | undefined,
end: Date | undefined,
skipInitialState: boolean,
withAttributes = false,
): Promise<HassHistory | undefined> {
let url = 'history/period';
if (start) url += `/${start.toISOString()}`;
url += `?filter_entity_id=${this._entityID}`;
if (end) url += `&end_time=${end.toISOString()}`;
if (skipInitialState) url += '&skip_initial_state';
if (!withAttributes) url += '&minimal_response';
if (withAttributes) url += '&significant_changes_only=0';
return this._hass?.callApi('GET', url);
}
private _generateData(start: Date, end: Date): EntityEntryCache {
let data;
try {
data = new Function(
'entity',
'start',
'end',
'hass',
'moment',
`'use strict'; ${this._config.data_generator}`,
).call(this, this._entityState, start, end, this._hass, moment);
} catch (e) {
const funcTrimmed =
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this._config.data_generator!.length <= 100
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this._config.data_generator!.trim()
: // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
`${this._config.data_generator!.trim().substring(0, 98)}...`;
e.message = `${e.name}: ${e.message} in '${funcTrimmed}'`;
e.name = 'Error';
throw e;
}
return {
span: 0,
card_version: pjson.version,
last_fetched: new Date(),
data,
};
}
private _dataBucketer(history: EntityEntryCache): HistoryBuckets {
const ranges = Array.from(this._timeRange.reverseBy('milliseconds', { step: this._groupByDurationMs })).reverse();
// const res: EntityCachePoints[] = [[]];
let buckets: HistoryBuckets = [];
ranges.forEach((range, index) => {
buckets[index] = { timestamp: range.valueOf(), data: [] };
});
let lastNotNullValue: number | null = null;
history?.data.forEach((entry) => {
let properEntry = entry;
// Fill null values
if (properEntry[1] === null) {
if (this._config.group_by.fill === 'last') {
properEntry = [entry[0], lastNotNullValue];
} else if (this._config.group_by.fill === 'zero') {
properEntry = [entry[0], 0];
}
} else {
lastNotNullValue = properEntry[1];
}
buckets.some((bucket, index) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (bucket.timestamp > properEntry![0] && index > 0) {
buckets[index - 1].data.push(properEntry);
return true;
}
return false;
});
});
let lastNonNullBucketValue: number | null = null;
const now = new Date().getTime();
buckets.forEach((bucket) => {
if (bucket.data.length === 0) {
if (this._config.group_by.fill === 'last' && bucket.timestamp <= now) {
bucket.data[0] = [bucket.timestamp, lastNonNullBucketValue];
} else if (this._config.group_by.fill === 'zero' && bucket.timestamp <= now) {
bucket.data[0] = [bucket.timestamp, 0];
} else if (this._config.group_by.fill === 'null') {
bucket.data[0] = [bucket.timestamp, null];
}
} else {
lastNonNullBucketValue = bucket.data.slice(-1)[0][1];
}
});
buckets.pop();
// Could probably do better than double reverse...
// This is to stip any value at the end which is empty or null
// to make extend_to_end work with buckets
if (
(buckets.length > 0 && buckets[buckets.length - 1].data.length === 0) ||
(buckets[buckets.length - 1].data.length > 0 &&
buckets[buckets.length - 1].data[buckets[buckets.length - 1].data.length - 1][1] === null)
)
buckets = buckets
.reverse()
.flatMap((bucket) => {
if (bucket.data[1] === null) return [];
if (bucket.data.length === 0) return [];
else return [bucket];
})
.reverse();
return buckets;
}
private _sum(items: EntityCachePoints): number {
if (items.length === 0) return 0;
let lastIndex = 0;
return items.reduce((sum, entry, index) => {
let val = 0;
if (entry && entry[1] === null) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
val = items[lastIndex][1]!;
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
val = entry[1]!;
lastIndex = index;
}
return sum + val;
}, 0);
}
private _average(items: EntityCachePoints): number | null {
const nonNull = this._filterNulls(items);
if (nonNull.length === 0) return null;
return this._sum(nonNull) / nonNull.length;
}
private _minimum(items: EntityCachePoints): number | null {
let min: number | null = null;
items.forEach((item) => {
if (item[1] !== null)
if (min === null) min = item[1];
else min = Math.min(item[1], min);
});
return min;
}
private _maximum(items: EntityCachePoints): number | null {
let max: number | null = null;
items.forEach((item) => {
if (item[1] !== null)
if (max === null) max = item[1];
else max = Math.max(item[1], max);
});
return max;
}
private _last(items: EntityCachePoints): number | null {
if (items.length === 0) return null;
return items.slice(-1)[0][1];
}
private _first(items: EntityCachePoints): number | null {
if (items.length === 0) return null;
return items[0][1];
}
private _median(items: EntityCachePoints) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const itemsDup = this._filterNulls([...items]).sort((a, b) => a[1]! - b[1]!);
const mid = Math.floor((itemsDup.length - 1) / 2);
if (itemsDup.length % 2 === 1) return itemsDup[mid][1];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return (itemsDup[mid][1]! + itemsDup[mid + 1][1]!) / 2;
}
private _delta(items: EntityCachePoints): number | null {
const max = this._maximum(items);
const min = this._minimum(items);
return max === null || min === null ? null : max - min;
}
private _diff(items: EntityCachePoints): number | null {
const noNulls = this._filterNulls(items);
const first = this._first(noNulls);
const last = this._last(noNulls);
if (first === null || last === null) {
return null;
}
return last - first;
}
private _filterNulls(items: EntityCachePoints): EntityCachePoints {
return items.filter((item) => item[1] !== null);
}
}