Skip to content

Commit

Permalink
Merge pull request #1 from Wesley-Vos/feat/statistics_diff
Browse files Browse the repository at this point in the history
Implemented diff graph for statistics
  • Loading branch information
Wesley-Vos authored Oct 21, 2022
2 parents b9f992a + 074b485 commit 2440264
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
36 changes: 29 additions & 7 deletions src/graphEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,32 @@ export default class GraphEntry {
);

// if data in cache, get data from last data's time + 1ms
const fetchStart = usableCache
let fetchStart = usableCache
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
new Date(history!.data[history!.data.length - 1][0] + 1)
: new Date(startHistory.getTime() + (this._config.group_by.func !== 'raw' ? 0 : -1));
: new Date(startHistory.getTime()
+ (this._config.group_by.func !== 'raw' ? 0 : -1));
const fetchEnd = end;

let newStateHistory: EntityCachePoints = [];
let updateGraphHistory = false;

if (this._config.statistics) {

// console.log(start, end)
// console.log(fetchStart, JSON.stringify(this._config))
if (this._config.statistics.type === "diff") {
if (this._config.statistics.period === '5minute') {
fetchStart = new Date(fetchStart.getTime() - 450000); // 2min30s
} else if (!this._config.statistics?.period || this._config.statistics.period === 'hour') {
fetchStart = new Date(fetchStart.getTime() - 5400000); // 30min
} else if (this._config.statistics.period === 'day') {
fetchStart = new Date(fetchStart.getTime() - 100400000); // 12h
} else {
fetchStart = new Date(fetchStart.getTime() - 3888000000); // 15d
}
}

const newHistory = await this._fetchStatistics(fetchStart, fetchEnd, this._config.statistics.period);
if (newHistory && newHistory.length > 0) {
updateGraphHistory = true;
Expand All @@ -264,11 +280,17 @@ export default class GraphEntry {
}
newStateHistory = newHistory.map((item) => {
let stateParsed: number | null = null;
[lastNonNull, stateParsed] = this._transformAndFill(
item[this._config.statistics?.type || DEFAULT_STATISTICS_TYPE],
item,
lastNonNull,
);
if (this._config.statistics?.type === "diff") {
stateParsed = (item["sum"] ?? 0) - (lastNonNull ?? 0);
lastNonNull = item["sum"]
// console.log(new Date(item.start), item["sum"], lastNonNull, stateParsed)
} else {
[lastNonNull, stateParsed] = this._transformAndFill(
item[this._config.statistics?.type || DEFAULT_STATISTICS_TYPE],
item,
lastNonNull,
);
}

let displayDate: Date | null = null;
const startDate = new Date(item.start);
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 @@ -77,7 +77,7 @@ export const ChartCardAllSeriesExternalConfig = t.iface([], {
"invert": t.opt("boolean"),
"data_generator": t.opt("string"),
"statistics": t.opt(t.iface([], {
"type": t.opt(t.union(t.lit('mean'), t.lit('max'), t.lit('min'), t.lit('sum'), t.lit('state'))),
"type": t.opt(t.union(t.lit('mean'), t.lit('max'), t.lit('min'), t.lit('sum'), t.lit('state'), t.lit('diff'))),
"period": t.opt("StatisticsPeriod"),
"align": t.opt(t.union(t.lit('start'), t.lit('end'), t.lit('middle'))),
})),
Expand Down
2 changes: 1 addition & 1 deletion src/types-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export interface ChartCardAllSeriesExternalConfig {
invert?: boolean;
data_generator?: string;
statistics?: {
type?: 'mean' | 'max' | 'min' | 'sum' | 'state';
type?: 'mean' | 'max' | 'min' | 'sum' | 'state' | 'diff';
period?: StatisticsPeriod;
align?: 'start' | 'end' | 'middle';
};
Expand Down

0 comments on commit 2440264

Please sign in to comment.