From 6eb695b2588650ceeb913a3875c6977bb0062489 Mon Sep 17 00:00:00 2001 From: Kristina Date: Wed, 17 Jul 2024 11:47:49 -0500 Subject: [PATCH] Explore: Add setting for default time offset (#90401) * Add setting for explore for a different time offset * fix linter * Add validation for duration value --- conf/defaults.ini | 3 +++ .../sources/setup-grafana/configure-grafana/_index.md | 5 +++++ packages/grafana-data/src/types/config.ts | 1 + packages/grafana-runtime/src/config.ts | 1 + pkg/api/dtos/frontend_settings.go | 1 + pkg/api/frontendsettings.go | 1 + pkg/setting/setting.go | 11 ++++++++++- public/app/features/explore/state/utils.ts | 4 ++-- 8 files changed, 24 insertions(+), 3 deletions(-) diff --git a/conf/defaults.ini b/conf/defaults.ini index f66d8a10620..0cd2ecde138 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -1497,6 +1497,9 @@ max_annotations_to_keep = # Enable the Explore section enabled = true +# set the default offset for the time picker +defaultTimeOffset = 1h + #################################### Help ############################# [help] # Enable the Help section diff --git a/docs/sources/setup-grafana/configure-grafana/_index.md b/docs/sources/setup-grafana/configure-grafana/_index.md index 2959d0215e1..adc65ef6f7b 100644 --- a/docs/sources/setup-grafana/configure-grafana/_index.md +++ b/docs/sources/setup-grafana/configure-grafana/_index.md @@ -1783,6 +1783,11 @@ For more information about this feature, refer to [Explore]({{< relref "../../ex Enable or disable the Explore section. Default is `enabled`. +### defaultTimeOffset + +Set a default time offset from now on the time picker. Default is 1 hour. +This setting should be expressed as a duration. Examples: 1h (hour), 1d (day), 1w (week), 1M (month). + ## [help] Configures the help section. diff --git a/packages/grafana-data/src/types/config.ts b/packages/grafana-data/src/types/config.ts index f4eab1b74a5..aa327bbf113 100644 --- a/packages/grafana-data/src/types/config.ts +++ b/packages/grafana-data/src/types/config.ts @@ -233,6 +233,7 @@ export interface GrafanaConfig { listDashboardScopesEndpoint?: string; listScopesEndpoint?: string; reportingStaticContext?: Record; + exploreDefaultTimeOffset?: string; // The namespace to use for kubernetes apiserver requests namespace: string; diff --git a/packages/grafana-runtime/src/config.ts b/packages/grafana-runtime/src/config.ts index 305e128d186..20da3e00667 100644 --- a/packages/grafana-runtime/src/config.ts +++ b/packages/grafana-runtime/src/config.ts @@ -180,6 +180,7 @@ export class GrafanaBootConfig implements GrafanaConfig { localFileSystemAvailable: boolean | undefined; cloudMigrationIsTarget: boolean | undefined; reportingStaticContext?: Record; + exploreDefaultTimeOffset = '1h'; /** * Language used in Grafana's UI. This is after the user's preference (or deteceted locale) is resolved to one of diff --git a/pkg/api/dtos/frontend_settings.go b/pkg/api/dtos/frontend_settings.go index 9d233cbb55b..e241d9af7cb 100644 --- a/pkg/api/dtos/frontend_settings.go +++ b/pkg/api/dtos/frontend_settings.go @@ -204,6 +204,7 @@ type FrontendSettingsDTO struct { TrustedTypesDefaultPolicyEnabled bool `json:"trustedTypesDefaultPolicyEnabled"` CSPReportOnlyEnabled bool `json:"cspReportOnlyEnabled"` DisableFrontendSandboxForPlugins []string `json:"disableFrontendSandboxForPlugins"` + ExploreDefaultTimeOffset string `json:"exploreDefaultTimeOffset"` Auth FrontendSettingsAuthDTO `json:"auth"` diff --git a/pkg/api/frontendsettings.go b/pkg/api/frontendsettings.go index 6eb4ae8ee6a..18fca78c7fb 100644 --- a/pkg/api/frontendsettings.go +++ b/pkg/api/frontendsettings.go @@ -227,6 +227,7 @@ func (hs *HTTPServer) getFrontendSettings(c *contextmodel.ReqContext) (*dtos.Fro RootFolderUID: accesscontrol.GeneralFolderUID, LocalFileSystemAvailable: hs.Cfg.LocalFileSystemAvailable, ReportingStaticContext: hs.Cfg.ReportingStaticContext, + ExploreDefaultTimeOffset: hs.Cfg.ExploreDefaultTimeOffset, BuildInfo: dtos.FrontendSettingsBuildInfoDTO{ HideVersion: hideVersion, diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index aa37813cc6e..be3204b4aea 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -513,7 +513,8 @@ type Cfg struct { AlertingMinInterval int64 // Explore UI - ExploreEnabled bool + ExploreEnabled bool + ExploreDefaultTimeOffset string // Help UI HelpEnabled bool @@ -1173,6 +1174,14 @@ func (cfg *Cfg) parseINIFile(iniFile *ini.File) error { explore := iniFile.Section("explore") cfg.ExploreEnabled = explore.Key("enabled").MustBool(true) + exploreDefaultTimeOffset := valueAsString(explore, "defaultTimeOffset", "1h") + // we want to ensure the value parses as a duration, but we send it forward as a string to the frontend + if _, err := gtime.ParseDuration(exploreDefaultTimeOffset); err != nil { + return err + } else { + cfg.ExploreDefaultTimeOffset = exploreDefaultTimeOffset + } + help := iniFile.Section("help") cfg.HelpEnabled = help.Key("enabled").MustBool(true) diff --git a/public/app/features/explore/state/utils.ts b/public/app/features/explore/state/utils.ts index 9ee49059123..464e4b0ecdf 100644 --- a/public/app/features/explore/state/utils.ts +++ b/public/app/features/explore/state/utils.ts @@ -20,7 +20,7 @@ import { URLRange, URLRangeValue, } from '@grafana/data'; -import { getDataSourceSrv } from '@grafana/runtime'; +import { config, getDataSourceSrv } from '@grafana/runtime'; import { DataQuery, DataSourceJsonData, DataSourceRef, TimeZone } from '@grafana/schema'; import { getLocalRichHistoryStorage } from 'app/core/history/richHistoryStorageProvider'; import { SortOrder } from 'app/core/utils/richHistory'; @@ -36,7 +36,7 @@ import { loadSupplementaryQueries } from '../utils/supplementaryQueries'; export const MAX_HISTORY_AUTOCOMPLETE_ITEMS = 100; export const DEFAULT_RANGE = { - from: 'now-1h', + from: `now-${config.exploreDefaultTimeOffset}`, to: 'now', };