From 67ebdcbcc395cc2548a3148c5f7770b4d185307c Mon Sep 17 00:00:00 2001 From: David Kaltschmidt Date: Mon, 31 Dec 2018 18:30:32 +0000 Subject: [PATCH] Explore: Remember last use datasource - use local storage to store last used database - renamed `datasourceName` property to `initialDatasourceId` for consistency - adapted tests --- public/app/core/utils/explore.test.ts | 9 ++++----- public/app/core/utils/explore.ts | 2 +- public/app/features/explore/Explore.tsx | 21 +++++++++++++++------ public/app/types/explore.ts | 2 +- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/public/app/core/utils/explore.test.ts b/public/app/core/utils/explore.test.ts index 7ceebbd8047..fcd92681004 100644 --- a/public/app/core/utils/explore.test.ts +++ b/public/app/core/utils/explore.test.ts @@ -14,7 +14,6 @@ const DEFAULT_EXPLORE_STATE: ExploreState = { datasourceError: null, datasourceLoading: null, datasourceMissing: false, - datasourceName: '', exploreDatasources: [], graphInterval: 1000, history: [], @@ -69,7 +68,7 @@ describe('state functions', () => { it('returns url parameter value for a state object', () => { const state = { ...DEFAULT_EXPLORE_STATE, - datasourceName: 'foo', + initialDatasourceId: 'foo', range: { from: 'now-5h', to: 'now', @@ -94,7 +93,7 @@ describe('state functions', () => { it('returns url parameter value for a state object', () => { const state = { ...DEFAULT_EXPLORE_STATE, - datasourceName: 'foo', + initialDatasourceId: 'foo', range: { from: 'now-5h', to: 'now', @@ -120,7 +119,7 @@ describe('state functions', () => { it('can parse the serialized state into the original state', () => { const state = { ...DEFAULT_EXPLORE_STATE, - datasourceName: 'foo', + initialDatasourceId: 'foo', range: { from: 'now - 5h', to: 'now', @@ -144,7 +143,7 @@ describe('state functions', () => { const resultState = { ...rest, datasource: DEFAULT_EXPLORE_STATE.datasource, - datasourceName: datasource, + initialDatasourceId: datasource, initialQueries: queries, }; diff --git a/public/app/core/utils/explore.ts b/public/app/core/utils/explore.ts index b7de48ac479..1927c616d50 100644 --- a/public/app/core/utils/explore.ts +++ b/public/app/core/utils/explore.ts @@ -105,7 +105,7 @@ export function parseUrlState(initial: string | undefined): ExploreUrlState { export function serializeStateToUrlParam(state: ExploreState, compact?: boolean): string { const urlState: ExploreUrlState = { - datasource: state.datasourceName, + datasource: state.initialDatasourceId, queries: state.initialQueries.map(clearQueryKeys), range: state.range, }; diff --git a/public/app/features/explore/Explore.tsx b/public/app/features/explore/Explore.tsx index f56d9cabd16..11e9becb9bb 100644 --- a/public/app/features/explore/Explore.tsx +++ b/public/app/features/explore/Explore.tsx @@ -40,6 +40,8 @@ import ErrorBoundary from './ErrorBoundary'; import { Alert } from './Error'; import TimePicker, { parseTime } from './TimePicker'; +const LAST_USED_DATASOURCE_KEY = 'grafana.explore.datasource'; + interface ExploreProps { datasourceSrv: DatasourceSrv; onChangeSplit: (split: boolean, state?: ExploreState) => void; @@ -90,6 +92,10 @@ interface ExploreProps { export class Explore extends React.PureComponent { el: any; exploreEvents: Emitter; + /** + * Set via URL or local storage + */ + initialDatasourceId: string; /** * Current query expressions of the rows including their modifications, used for running queries. * Not kept in component state to prevent edit-render roundtrips. @@ -115,6 +121,7 @@ export class Explore extends React.PureComponent { initialQueries = splitState.initialQueries; } else { const { datasource, queries, range } = props.urlState as ExploreUrlState; + const initialDatasourceId = datasource || store.get(LAST_USED_DATASOURCE_KEY); initialQueries = ensureQueries(queries); const initialRange = { from: parseTime(range.from), to: parseTime(range.to) } || { ...DEFAULT_RANGE }; // Millies step for helper bar charts @@ -124,10 +131,10 @@ export class Explore extends React.PureComponent { datasourceError: null, datasourceLoading: null, datasourceMissing: false, - datasourceName: datasource, exploreDatasources: [], graphInterval: initialGraphInterval, graphResult: [], + initialDatasourceId, initialQueries, history: [], logsResult: null, @@ -151,7 +158,7 @@ export class Explore extends React.PureComponent { async componentDidMount() { const { datasourceSrv } = this.props; - const { datasourceName } = this.state; + const { initialDatasourceId } = this.state; if (!datasourceSrv) { throw new Error('No datasource service passed as props.'); } @@ -165,10 +172,10 @@ export class Explore extends React.PureComponent { if (datasources.length > 0) { this.setState({ datasourceLoading: true, exploreDatasources }); - // Priority: datasource in url, default datasource, first explore datasource + // Priority for datasource preselection: URL, localstorage, default datasource let datasource; - if (datasourceName) { - datasource = await datasourceSrv.get(datasourceName); + if (initialDatasourceId) { + datasource = await datasourceSrv.get(initialDatasourceId); } else { datasource = await datasourceSrv.get(); } @@ -253,13 +260,15 @@ export class Explore extends React.PureComponent { supportsLogs, supportsTable, datasourceLoading: false, - datasourceName: datasource.name, + initialDatasourceId: datasource.name, initialQueries: nextQueries, logsHighlighterExpressions: undefined, showingStartPage: Boolean(StartPage), }, () => { if (datasourceError === null) { + // Save last-used datasource + store.set(LAST_USED_DATASOURCE_KEY, datasource.name); this.onSubmit(); } } diff --git a/public/app/types/explore.ts b/public/app/types/explore.ts index 647b3e99398..7ea0462900d 100644 --- a/public/app/types/explore.ts +++ b/public/app/types/explore.ts @@ -155,11 +155,11 @@ export interface ExploreState { datasourceError: any; datasourceLoading: boolean | null; datasourceMissing: boolean; - datasourceName?: string; exploreDatasources: DataSourceSelectItem[]; graphInterval: number; // in ms graphResult?: any[]; history: HistoryItem[]; + initialDatasourceId?: string; initialQueries: DataQuery[]; logsHighlighterExpressions?: string[]; logsResult?: LogsModel;