From de923640a43fd419eff29553bc7678b55f16b6a8 Mon Sep 17 00:00:00 2001 From: MGJamJam Date: Thu, 25 Jul 2024 10:19:40 -0300 Subject: [PATCH] add adjusting of grafana timeframe after query fetching --- src/datasource.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/datasource.ts b/src/datasource.ts index 3082e55..c953815 100644 --- a/src/datasource.ts +++ b/src/datasource.ts @@ -90,6 +90,7 @@ export class DataSource extends DataSourceApi< async query(options: DataQueryRequest): Promise { const { range } = options; const isRelativeRangeFrom = this.isRelativeRangeFrom(range.raw); + let momentTimeUnit = undefined; //filter disabled queries const enabledQueries = (options.targets = filter(options.targets, (t) => !t.hide)); @@ -104,7 +105,7 @@ export class DataSource extends DataSourceApi< : undefined; // create new moment object to not mutate the original grafana object with startOf() to not change - // the grafana graph as this would change the timeframe for all the following queries + // the grafana graph at this point as this would change the timeframe for all the following queries let queryFrom = moment(range!.from.valueOf()); const queryTo = range!.to; @@ -115,7 +116,7 @@ export class DataSource extends DataSourceApi< // to allow higher granularity if interval is selected by user flooringInterval = getSmallerInterval(interval, flooringInterval); } - const momentTimeUnit = getMomentTimeUnitForQueryInterval(flooringInterval); + momentTimeUnit = getMomentTimeUnitForQueryInterval(flooringInterval); if (momentTimeUnit != null) { queryFrom.startOf(momentTimeUnit); } @@ -205,6 +206,11 @@ export class DataSource extends DataSourceApi< }); }); + // round down grafana start time to adjust the grafana graph and show first data point + if (momentTimeUnit != null) { + range.from.startOf(momentTimeUnit); + } + return Promise.all(promises).then((data) => ({ data })); }