-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
uptime-card.ts
497 lines (416 loc) · 14 KB
/
uptime-card.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
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
/* eslint-disable @typescript-eslint/camelcase */
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
LitElement,
html,
customElement,
property,
CSSResult,
TemplateResult,
PropertyValues,
internalProperty,
svg,
} from 'lit-element';
import {
HomeAssistant,
hasConfigOrEntityChanged,
LovelaceCardEditor,
getLovelace
} from 'custom-card-helpers';
import { HassEntity } from 'home-assistant-js-websocket'
import localForage from 'localforage/src/localforage';
import { CARD_VERSION, DEFAULT_COLOR, DEFAULT_CONFIG, DEFAULT_SHOW, DEFAULT_BAR, DEFAULT_ICON } from "./const";
import './editor';
import style from './style';
import type { CardConfig } from "./types/config"
import type { CacheData, Point, ApiPoint, Period, Repartition } from './types/card';
import { wrap, unwrap } from "./utils";
/* eslint no-console: 0 */
console.info(
`%c Uptime card version ${CARD_VERSION} `,
'color: orange; font-weight: bold; background: black'
);
// This puts your card into the UI card picker dialog
(window as any).customCards = (window as any).customCards || [];
(window as any).customCards.push({
type: 'uptime-card',
name: 'Uptime Card',
description: 'The uptime card show you the history of your binary sensors in a cool way.',
});
@customElement('uptime-card')
export class UptimeCard extends LitElement {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
return document.createElement('uptime-card-editor');
}
public static getStubConfig(): object {
return {};
}
@property({ attribute: false }) public _hass!: HomeAssistant;
@internalProperty() private config!: CardConfig;
@internalProperty() private sensor?: HassEntity;
@internalProperty() private interval!: NodeJS.Timeout;
@internalProperty() private cache!: CacheData;
@internalProperty() private initialized = false;
public set hass(hass: HomeAssistant) {
this._hass = hass;
this.sensor = hass.states[this.config.entity];
this.initialized = true;
this.updateData();
}
public setConfig(config: CardConfig): void {
if (!config) {
throw new Error("Invalid configuration !");
}
this.config = {
...DEFAULT_CONFIG,
...config,
color: { ...DEFAULT_COLOR, ...config.color },
alias: { ...config.alias },
show: { ...DEFAULT_SHOW, ...config.show },
bar: { ...DEFAULT_BAR, ...config.bar },
tap_action: { action: 'more-info' }
};
if (this.initialized) this.updateData()
}
protected shouldUpdate(changedProps: PropertyValues): boolean {
if (!this.config) {
return false;
}
return hasConfigOrEntityChanged(this, changedProps, false);
}
protected firstUpdated(changedProps: PropertyValues): void {
changedProps;
this.updateData()
}
public connectedCallback(): void {
super.connectedCallback();
if (this.config.update_interval) {
this.interval = setInterval(
() => this.updateData(),
this.config.update_interval * 1000,
);
}
}
public disconnectedCallback(): void {
if (this.interval) {
clearInterval(this.interval);
}
super.disconnectedCallback();
}
private async updateData(): Promise<void> {
const { entity, hours_to_show } = this.config;
this.sensor = this._hass.states[this.config.entity];
if (this.sensor == undefined) {
this.cache = {
points: [],
lastFetched: -1,
lastChanged: -1,
hoursToShow: hours_to_show
}
return;
}
const data: CacheData = await this.getCache(entity);
const now = new Date().getTime()
let cache: CacheData;
if (data == undefined) {
// First time we see the entity
const lastChanged = new Date(this.sensor.last_changed).getTime();
const point = { x: lastChanged, y: this.sensor.state };
cache = {
points: [point],
lastFetched: now,
lastChanged: lastChanged,
hoursToShow: hours_to_show
}
} else {
// When the state is updated
const oldestHistory = data.hoursToShow < hours_to_show
const lastFetched = oldestHistory ? this.getMinimumDate() : data.lastFetched;
const fetchedPoints = await this.fetchRecent(entity, new Date(lastFetched), new Date());
const points = oldestHistory ? fetchedPoints : [...data.points, ...fetchedPoints];
const index = points.findIndex(point => point.x > this.getMinimumDate());
const usefulPoints = index == 0 ? points : points.slice(index - 1);
const cleanedPoints = this.cleanPoints(usefulPoints);
if (cleanedPoints.length > 0) {
cache = {
points: cleanedPoints,
lastFetched: now,
lastChanged: cleanedPoints[cleanedPoints.length - 1].x,
hoursToShow: hours_to_show
};
} else {
const lastChanged = new Date(this.sensor.last_changed).getTime();
const point = { x: lastChanged, y: this.sensor.state };
cache = {
points: [point],
lastFetched: now,
lastChanged: lastChanged,
hoursToShow: hours_to_show
};
}
}
await this.setCache(entity, cache)
this.cache = cache
}
async getCache(key: string): Promise<any> {
const data: string | null = await localForage.getItem(key);
if (data == undefined) {
console.warn(`Can't load the key ${key} from cache.`)
return null;
}
else return unwrap(data);
}
async setCache(key: string, data: any): Promise<any> {
return localForage.setItem(key, wrap(data))
}
private isOk(state: string): boolean | undefined {
const { ok, ko } = this.config
if (ok == state) return true;
else if (ko == state) return false;
else {
if (ok == undefined && ko == undefined) return undefined;
else if (ok == undefined) return true;
else if (ko == undefined) return false;
return undefined;
}
}
private findBarPeriod(index: number): Period {
const { bar } = this.config
const millisecondsPerBar = this.getUptimeSize() / bar.amount
const now = new Date().getTime()
const to = now - ((bar.amount - 1 - index) * millisecondsPerBar)
return {
from: to - millisecondsPerBar + 1,
to: to,
}
}
private findBarRepartition(period: Period): Repartition {
const firstPoint = this.cache.points.findIndex(point => point.x >= period.from);
const lastPoint = this.cache.points.findIndex(point => point.x > period.to);
const noneRepartition: Repartition = { ok: 0, ko: 0, none: 100 };
let usefulPoints: Point[];
if (this.cache.points.length == 0) return noneRepartition;
// All points are before period.from
else if (firstPoint == -1) usefulPoints = [this.cache.points[this.cache.points.length - 1]];
// All points are after period.to
else if (firstPoint == 0 && lastPoint == 0) return noneRepartition;
else {
const isNotFirst = firstPoint == 0 ? 0 : 1
const last = lastPoint == -1 ? this.cache.points.length : lastPoint
usefulPoints = this.cache.points.slice(firstPoint - isNotFirst, last);
}
const repartition: Repartition = { ok: 0, ko: 0, none: 0 };
const total = period.to - period.from
for (let i = 0; i < usefulPoints.length; i++) {
const upper = usefulPoints[i + 1] ? usefulPoints[i + 1].x : period.to;
const lower = Math.max(usefulPoints[i].x, period.from);
const amount = upper - lower;
if (this.isOk(usefulPoints[i].y) == true) repartition.ok += amount;
else if (this.isOk(usefulPoints[i].y) == false) repartition.ko += amount;
else repartition.none += amount;
}
const ok = repartition.ok / total * 100;
const ko = repartition.ko / total * 100;
const none = 100 - (ok + ko);
return { ok: ok, ko: ko, none: none };
}
private getUptimeSize(): number {
const { hours_to_show } = this.config;
return hours_to_show * 3.6e+6;
}
private getMinimumDate(): number {
return new Date().getTime() - this.getUptimeSize();
}
private getColor(state?: string): string {
const { color } = this.config
let c: string;
if (state == undefined) return color.none
if (this.isOk(state) == true) return color.ok
else if (this.isOk(state) == false) return color.ko
return color.none
}
private async fetchRecent(entity: string, start: Date, end: Date): Promise<Point[]> {
let url = 'history/period';
if (start) url += `/${start.toISOString()}`;
url += `?filter_entity_id=${entity}`;
if (end) url += `&end_time=${end.toISOString()}`;
url += '&minimal_response';
const result: ApiPoint[][] = await this._hass.callApi('GET', url);
return result[0].map(result => { return { x: new Date(result.last_changed).getTime(), y: result.state } });
}
private cleanPoints(points: Point[]): Point[] {
const cleanedPoints: Point[] = []
let lastPointState: string | undefined = undefined
for (const point of points) {
if (point.y != lastPointState) {
cleanedPoints.push(point);
lastPointState = point.y;
}
}
return cleanedPoints
}
/**
* Rendering
*/
protected render(): TemplateResult {
const { bar } = this.config
const repartitions = [...Array(bar.amount).keys()].map(
(_, idx) => {
const period = this.findBarPeriod(idx)
return this.findBarRepartition(period)
}
)
return html`
<ha-card class="flex">
${this.renderHeader()}
${this.renderState()}
${this.renderTimeline(repartitions)}
${this.renderFooter(repartitions)}
</ha-card>
`;
}
private renderHeader(): TemplateResult | string {
const { show } = this.config
return show.header ? html`
<div class="header flex">
${this.renderName()} ${this.renderIcon()}
</div>
`: "";
}
private renderName(): TemplateResult {
const { name } = this.config
return html`
<div class="name">
<span>${name}</span>
</div>
`;
}
private renderState(): TemplateResult {
const { alias, show, color } = this.config
let currentStatus: string;
let currentColor: string;
if (this.sensor == undefined) {
currentStatus = "Unknown"
currentColor = color.none
} else {
if (this.isOk(this.sensor.state) == true && alias.ok) currentStatus = alias.ok;
else if (this.isOk(this.sensor.state) == false && alias.ko) currentStatus = alias.ko;
else if (this.isOk(this.sensor.state) == undefined) currentStatus = "Unknown"
else currentStatus = this.sensor.state
currentColor = this.getColor(this.sensor.state);
}
const currentColorCss = `color: ${currentColor};`
return show.status ? html`
<div class="status">
<span style=${currentColorCss}>${currentStatus}</span>
</div>
`: html``;
}
private renderIcon(): TemplateResult | string {
const { icon, show } = this.config;
const currentIcon = icon || this.sensor?.attributes.icon || DEFAULT_ICON;
return show.icon ? html`
<div class="icon flex">
<ha-icon .icon=${currentIcon}></ha-icon>
</div>
` : '';
}
private renderTimeline(repartitions: Repartition[]): TemplateResult | string {
const { show, color, bar, severity } = this.config;
const width = 500;
const spacingTotalWidth = bar.spacing * (bar.amount - 1);
const barWidth = Math.floor((width - spacingTotalWidth) / bar.amount)
const leftTotalWidth = width - spacingTotalWidth - barWidth * bar.amount
if (show.timeline == false) return ""
const bars = repartitions.map(
(repartition, idx) => {
let barColor: string;
if (repartition.none == 100) barColor = color.none;
else if (repartition.ok == 100) barColor = color.ok;
else if (repartition.ko >= severity) barColor = color.ko;
else barColor = color.half
return this.renderBar(idx * (barWidth + bar.spacing) + (leftTotalWidth / 2), 0, barWidth, bar.height, barColor, bar.round)
}
)
return html`
<div class="timeline">
<svg width='100%' height="100%"} viewBox='0 0 ${width} ${bar.height}'>
${bars}
</svg>
</div>
`
}
private renderBar(x: number, y: number, width: number, height: number, color: string, round: number): TemplateResult {
return svg`<rect
class='bar'
x=${x}
y=${y}
rx=${round}
ry=${round}
height=${height}
width=${width}
fill=${color}
></rect>`;
}
private renderFooter(repartitions: Repartition[]): TemplateResult | string {
const { show } = this.config;
if (show.footer == false) return ""
const sumOk = repartitions.reduce((prev, curr) => prev + curr.ok, 0)
const uptime = (sumOk / repartitions.length).toFixed(2);
const minimalDate = this.generateMinimalDate()
return html`
<div class="footer">
<div class="footer-text">${minimalDate}</div>
${this.renderLine()}
<div class="footer-text">${uptime}% uptime</div>
${this.renderLine()}
<div class="footer-text">Now</div>
</div>`;
}
private generateMinimalDate(): string {
const { hours_to_show } = this.config
if (hours_to_show == 0) return "Now"
else if (hours_to_show % 168 == 0) {
const week = hours_to_show / 168
if (week == 1) {
return `${week} week ago`
}
return `${week} weeks ago`
}
else if (hours_to_show % 24 == 0) {
const day = hours_to_show / 24
if (day == 1) {
return `${day} day ago`
}
return `${day} days ago`
}
else {
if (hours_to_show == 1) {
return `${hours_to_show} hour ago`
}
return `${hours_to_show} hours ago`
}
}
private renderLine(): TemplateResult {
return html`<div class="line"></div>`
}
private _showWarning(warning: string): TemplateResult {
return html`
<hui-warning>${warning}</hui-warning>
`;
}
private _showError(error: string): TemplateResult {
const errorCard = document.createElement('hui-error-card');
errorCard.setConfig({
type: 'error',
error,
origConfig: this.config,
});
return html`
${errorCard}
`;
}
static get styles(): CSSResult {
return style;
}
}