From 57fd6a640aaee223adaa174ad5d1fb82f496a878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Wiedemann?= Date: Sun, 31 Jan 2021 17:22:27 +0000 Subject: [PATCH] feat(group_by): Add new `diff` function --- README.md | 1 + src/graphEntry.ts | 10 ++++++++++ src/types-config-ti.ts | 2 +- src/types-config.ts | 2 +- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ab137a2..12b7d83 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ The card stricly validates all the options available (but not for the `apex_conf | `sum` | v1.0.0 | Will return the sum of all the states in each bucket | | `median` | v1.0.0 | Will return the median of all the states in each bucket | | `delta` | v1.0.0 | Will return the delta between the biggest and smallest state in each bucket | +| `diff` | MEXT_VERSION | Will return the difference between the last and the first entry in the bucket | ### `chart_type` Options diff --git a/src/graphEntry.ts b/src/graphEntry.ts index 10075b0..74828bb 100644 --- a/src/graphEntry.ts +++ b/src/graphEntry.ts @@ -63,6 +63,7 @@ export default class GraphEntry { sum: this._sum, median: this._median, delta: this._delta, + diff: this._diff, }; this._index = index; this._cache = cache; @@ -377,6 +378,15 @@ export default class GraphEntry { return max === null || min === null ? null : max - min; } + private _diff(items: EntityCachePoints): number | null { + const first = this._first(items); + const last = this._last(items); + if (first === null || last === null) { + return null; + } + return last - first; + } + private _filterNulls(items: EntityCachePoints): EntityCachePoints { return items.filter((item) => item[1] !== null); } diff --git a/src/types-config-ti.ts b/src/types-config-ti.ts index 037536d..1f265ec 100644 --- a/src/types-config-ti.ts +++ b/src/types-config-ti.ts @@ -58,7 +58,7 @@ export const ChartCardPrettyTime = t.union(t.lit('millisecond'), t.lit('second') export const GroupByFill = t.union(t.lit('null'), t.lit('last'), t.lit('zero')); -export const GroupByFunc = t.union(t.lit('raw'), t.lit('avg'), t.lit('min'), t.lit('max'), t.lit('last'), t.lit('first'), t.lit('sum'), t.lit('median'), t.lit('delta')); +export const GroupByFunc = t.union(t.lit('raw'), t.lit('avg'), t.lit('min'), t.lit('max'), t.lit('last'), t.lit('first'), t.lit('sum'), t.lit('median'), t.lit('delta'), t.lit('diff')); export const ChartCardHeaderExternalConfig = t.iface([], { "show": t.opt("boolean"), diff --git a/src/types-config.ts b/src/types-config.ts index b89ff2b..a5d1d05 100644 --- a/src/types-config.ts +++ b/src/types-config.ts @@ -54,7 +54,7 @@ export type ChartCardPrettyTime = 'millisecond' | 'second' | 'minute' | 'hour' | export type GroupByFill = 'null' | 'last' | 'zero'; -export type GroupByFunc = 'raw' | 'avg' | 'min' | 'max' | 'last' | 'first' | 'sum' | 'median' | 'delta'; +export type GroupByFunc = 'raw' | 'avg' | 'min' | 'max' | 'last' | 'first' | 'sum' | 'median' | 'delta' | 'diff'; export interface ChartCardHeaderExternalConfig { show?: boolean;