Skip to content

Commit

Permalink
feat: Add delta and median function
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider committed Jan 24, 2021
1 parent 4e3cdc6 commit 4a77e55
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/graphEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export default class GraphEntry {
first: this._first,
last: this._last,
sum: this._sum,
median: this._median,
delta: this._delta,
};
this._index = index;
this._cache = cache;
Expand Down Expand Up @@ -177,7 +179,7 @@ export default class GraphEntry {
this._history = history;
if (this._config.group_by.func !== 'raw') {
this._computedHistory = this._dataBucketer().map((bucket) => {
return [(new Date(bucket.timestamp) as any) as number, this._func(bucket.data)];
return [(new Date(bucket.timestamp) as unknown) as number, this._func(bucket.data)];
});
}
this._updating = false;
Expand Down Expand Up @@ -295,6 +297,20 @@ export default class GraphEntry {
return items[0][1];
}

private _median(items: EntityCachePoints) {
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];
// 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 _filterNulls(items: EntityCachePoints): EntityCachePoints {
return items.filter((item) => item[1] !== null);
}
Expand Down
2 changes: 1 addition & 1 deletion src/types-config-ti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const ChartCardSeriesExternalConfig = t.iface([], {

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'));
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 ChartCardHeaderExternalConfig = t.iface([], {
"show": t.opt("boolean"),
Expand Down
2 changes: 1 addition & 1 deletion src/types-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface ChartCardSeriesExternalConfig {

export type GroupByFill = 'null' | 'last' | 'zero';

export type GroupByFunc = 'raw' | 'avg' | 'min' | 'max' | 'last' | 'first' | 'sum';
export type GroupByFunc = 'raw' | 'avg' | 'min' | 'max' | 'last' | 'first' | 'sum' | 'median' | 'delta';

export interface ChartCardHeaderExternalConfig {
show?: boolean;
Expand Down

0 comments on commit 4a77e55

Please sign in to comment.