Skip to content

Commit

Permalink
feat(group_by): Add new diff function
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider committed Jan 31, 2021
1 parent d4cd7a3 commit 57fd6a6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions src/graphEntry.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/types-config-ti.ts
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion src/types-config.ts
Expand Up @@ -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;
Expand Down

0 comments on commit 57fd6a6

Please sign in to comment.