Explore: Add setting for default time offset (#90401)

* Add setting for explore for a different time offset

* fix linter

* Add validation for duration value
This commit is contained in:
Kristina 2024-07-17 11:47:49 -05:00 committed by GitHub
parent 1fc57d8fd5
commit 6eb695b258
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 24 additions and 3 deletions

View File

@ -1497,6 +1497,9 @@ max_annotations_to_keep =
# Enable the Explore section # Enable the Explore section
enabled = true enabled = true
# set the default offset for the time picker
defaultTimeOffset = 1h
#################################### Help ############################# #################################### Help #############################
[help] [help]
# Enable the Help section # Enable the Help section

View File

@ -1783,6 +1783,11 @@ For more information about this feature, refer to [Explore]({{< relref "../../ex
Enable or disable the Explore section. Default is `enabled`. 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] ## [help]
Configures the help section. Configures the help section.

View File

@ -233,6 +233,7 @@ export interface GrafanaConfig {
listDashboardScopesEndpoint?: string; listDashboardScopesEndpoint?: string;
listScopesEndpoint?: string; listScopesEndpoint?: string;
reportingStaticContext?: Record<string, string>; reportingStaticContext?: Record<string, string>;
exploreDefaultTimeOffset?: string;
// The namespace to use for kubernetes apiserver requests // The namespace to use for kubernetes apiserver requests
namespace: string; namespace: string;

View File

@ -180,6 +180,7 @@ export class GrafanaBootConfig implements GrafanaConfig {
localFileSystemAvailable: boolean | undefined; localFileSystemAvailable: boolean | undefined;
cloudMigrationIsTarget: boolean | undefined; cloudMigrationIsTarget: boolean | undefined;
reportingStaticContext?: Record<string, string>; reportingStaticContext?: Record<string, string>;
exploreDefaultTimeOffset = '1h';
/** /**
* Language used in Grafana's UI. This is after the user's preference (or deteceted locale) is resolved to one of * Language used in Grafana's UI. This is after the user's preference (or deteceted locale) is resolved to one of

View File

@ -204,6 +204,7 @@ type FrontendSettingsDTO struct {
TrustedTypesDefaultPolicyEnabled bool `json:"trustedTypesDefaultPolicyEnabled"` TrustedTypesDefaultPolicyEnabled bool `json:"trustedTypesDefaultPolicyEnabled"`
CSPReportOnlyEnabled bool `json:"cspReportOnlyEnabled"` CSPReportOnlyEnabled bool `json:"cspReportOnlyEnabled"`
DisableFrontendSandboxForPlugins []string `json:"disableFrontendSandboxForPlugins"` DisableFrontendSandboxForPlugins []string `json:"disableFrontendSandboxForPlugins"`
ExploreDefaultTimeOffset string `json:"exploreDefaultTimeOffset"`
Auth FrontendSettingsAuthDTO `json:"auth"` Auth FrontendSettingsAuthDTO `json:"auth"`

View File

@ -227,6 +227,7 @@ func (hs *HTTPServer) getFrontendSettings(c *contextmodel.ReqContext) (*dtos.Fro
RootFolderUID: accesscontrol.GeneralFolderUID, RootFolderUID: accesscontrol.GeneralFolderUID,
LocalFileSystemAvailable: hs.Cfg.LocalFileSystemAvailable, LocalFileSystemAvailable: hs.Cfg.LocalFileSystemAvailable,
ReportingStaticContext: hs.Cfg.ReportingStaticContext, ReportingStaticContext: hs.Cfg.ReportingStaticContext,
ExploreDefaultTimeOffset: hs.Cfg.ExploreDefaultTimeOffset,
BuildInfo: dtos.FrontendSettingsBuildInfoDTO{ BuildInfo: dtos.FrontendSettingsBuildInfoDTO{
HideVersion: hideVersion, HideVersion: hideVersion,

View File

@ -513,7 +513,8 @@ type Cfg struct {
AlertingMinInterval int64 AlertingMinInterval int64
// Explore UI // Explore UI
ExploreEnabled bool ExploreEnabled bool
ExploreDefaultTimeOffset string
// Help UI // Help UI
HelpEnabled bool HelpEnabled bool
@ -1173,6 +1174,14 @@ func (cfg *Cfg) parseINIFile(iniFile *ini.File) error {
explore := iniFile.Section("explore") explore := iniFile.Section("explore")
cfg.ExploreEnabled = explore.Key("enabled").MustBool(true) 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") help := iniFile.Section("help")
cfg.HelpEnabled = help.Key("enabled").MustBool(true) cfg.HelpEnabled = help.Key("enabled").MustBool(true)

View File

@ -20,7 +20,7 @@ import {
URLRange, URLRange,
URLRangeValue, URLRangeValue,
} from '@grafana/data'; } from '@grafana/data';
import { getDataSourceSrv } from '@grafana/runtime'; import { config, getDataSourceSrv } from '@grafana/runtime';
import { DataQuery, DataSourceJsonData, DataSourceRef, TimeZone } from '@grafana/schema'; import { DataQuery, DataSourceJsonData, DataSourceRef, TimeZone } from '@grafana/schema';
import { getLocalRichHistoryStorage } from 'app/core/history/richHistoryStorageProvider'; import { getLocalRichHistoryStorage } from 'app/core/history/richHistoryStorageProvider';
import { SortOrder } from 'app/core/utils/richHistory'; 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 MAX_HISTORY_AUTOCOMPLETE_ITEMS = 100;
export const DEFAULT_RANGE = { export const DEFAULT_RANGE = {
from: 'now-1h', from: `now-${config.exploreDefaultTimeOffset}`,
to: 'now', to: 'now',
}; };