Skip to content

Commit

Permalink
Merge pull request #81 from VictoriaMetrics/issue-47/time-range-variable
Browse files Browse the repository at this point in the history
Fix time range update for variables
  • Loading branch information
hagen1778 authored Jul 14, 2023
2 parents d3a37a0 + a6cee87 commit f37f762
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## tip

* BUGFIX: respect the time filter change on updating dashboard variables. See [this issue](https://github.com/VictoriaMetrics/grafana-datasource/issues/47)

## [v0.2.0](https://github.com/VictoriaMetrics/grafana-datasource/releases/tag/v0.2.0)

* FEATURE: add the ability to define expressions for each panel so that users can define WITH templates once and then reuse them. See [this issue](https://github.com/VictoriaMetrics/grafana-datasource/issues/35).
Expand Down
15 changes: 12 additions & 3 deletions src/services/TimeSrv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ import { getShiftedTimeRange, getZoomedTimeRange } from '../utils/timePicker';
import appEvents from "./app_events";
import { contextSrv, ContextSrv } from "./context_srv";

declare module '@grafana/runtime' {
interface TemplateSrv {
timeRange: TimeRange;
}
}

export const getTimeRange = (
time: { from: DateTime | string; to: DateTime | string },
timeModel?: TimeModel
Expand All @@ -63,6 +69,10 @@ export const getTimeRange = (
};
};

const getCurrentTimeRange = (): RawTimeRange => {
return getTemplateSrv()?.timeRange?.raw || getDefaultTimeRange().raw;
}

export interface TimeModel {
time: any;
fiscalYearStartMonth?: number;
Expand All @@ -83,8 +93,7 @@ export class TimeSrv {
private autoRefreshBlocked?: boolean;

constructor(private contextSrv: ContextSrv) {
// @ts-ignore default time
this.time = getTemplateSrv()?.timeRange?.raw || getDefaultTimeRange().raw;
this.time = getCurrentTimeRange()
this.refreshTimeModel = this.refreshTimeModel.bind(this);

appEvents.subscribe(ZoomOutEvent, (e) => {
Expand Down Expand Up @@ -378,7 +387,7 @@ export class TimeSrv {
};

timeRange(): TimeRange {
return getTimeRange(this.time, this.timeModel);
return getTimeRange(getCurrentTimeRange(), this.timeModel)
}

zoomOut(factor: number, updateUrl = true) {
Expand Down

0 comments on commit f37f762

Please sign in to comment.