Commit d4e9a8c0 by kay delaney Committed by GitHub

Explore: Introduces storage keys for last used data source on per-orgId basis (#17934)

* Explore: Introduces storage keys for last used data source on per-orgId basis
Closes #17903
parent 57c220c9
...@@ -53,6 +53,7 @@ export const DEFAULT_UI_STATE = { ...@@ -53,6 +53,7 @@ export const DEFAULT_UI_STATE = {
const MAX_HISTORY_ITEMS = 100; const MAX_HISTORY_ITEMS = 100;
export const LAST_USED_DATASOURCE_KEY = 'grafana.explore.datasource'; export const LAST_USED_DATASOURCE_KEY = 'grafana.explore.datasource';
export const lastUsedDatasourceKeyForOrgId = (orgId: number) => `${LAST_USED_DATASOURCE_KEY}.${orgId}`;
/** /**
* Returns an Explore-URL that contains a panel's queries and the dashboard time range. * Returns an Explore-URL that contains a panel's queries and the dashboard time range.
......
...@@ -41,11 +41,11 @@ import { ...@@ -41,11 +41,11 @@ import {
} from 'app/types/explore'; } from 'app/types/explore';
import { StoreState } from 'app/types'; import { StoreState } from 'app/types';
import { import {
LAST_USED_DATASOURCE_KEY,
ensureQueries, ensureQueries,
DEFAULT_RANGE, DEFAULT_RANGE,
DEFAULT_UI_STATE, DEFAULT_UI_STATE,
getTimeRangeFromUrl, getTimeRangeFromUrl,
lastUsedDatasourceKeyForOrgId,
} from 'app/core/utils/explore'; } from 'app/core/utils/explore';
import { Emitter } from 'app/core/utils/emitter'; import { Emitter } from 'app/core/utils/emitter';
import { ExploreToolbar } from './ExploreToolbar'; import { ExploreToolbar } from './ExploreToolbar';
...@@ -307,7 +307,7 @@ function mapStateToProps(state: StoreState, { exploreId }: ExploreProps) { ...@@ -307,7 +307,7 @@ function mapStateToProps(state: StoreState, { exploreId }: ExploreProps) {
} = item; } = item;
const { datasource, queries, range: urlRange, mode: urlMode, ui } = (urlState || {}) as ExploreUrlState; const { datasource, queries, range: urlRange, mode: urlMode, ui } = (urlState || {}) as ExploreUrlState;
const initialDatasource = datasource || store.get(LAST_USED_DATASOURCE_KEY); const initialDatasource = datasource || store.get(lastUsedDatasourceKeyForOrgId(state.user.orgId));
const initialQueries: DataQuery[] = ensureQueries(queries); const initialQueries: DataQuery[] = ensureQueries(queries);
const initialRange = urlRange ? getTimeRangeFromUrl(urlRange, timeZone).raw : DEFAULT_RANGE; const initialRange = urlRange ? getTimeRangeFromUrl(urlRange, timeZone).raw : DEFAULT_RANGE;
......
...@@ -6,13 +6,13 @@ import store from 'app/core/store'; ...@@ -6,13 +6,13 @@ import store from 'app/core/store';
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv'; import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
import { Emitter } from 'app/core/core'; import { Emitter } from 'app/core/core';
import { import {
LAST_USED_DATASOURCE_KEY,
ensureQueries, ensureQueries,
generateEmptyQuery, generateEmptyQuery,
parseUrlState, parseUrlState,
getTimeRange, getTimeRange,
getTimeRangeFromUrl, getTimeRangeFromUrl,
generateNewKeyAndAddRefIdIfMissing, generateNewKeyAndAddRefIdIfMissing,
lastUsedDatasourceKeyForOrgId,
} from 'app/core/utils/explore'; } from 'app/core/utils/explore';
// Types // Types
...@@ -104,6 +104,7 @@ export function changeDatasource(exploreId: ExploreId, datasource: string): Thun ...@@ -104,6 +104,7 @@ export function changeDatasource(exploreId: ExploreId, datasource: string): Thun
const currentDataSourceInstance = getState().explore[exploreId].datasourceInstance; const currentDataSourceInstance = getState().explore[exploreId].datasourceInstance;
const queries = getState().explore[exploreId].queries; const queries = getState().explore[exploreId].queries;
const orgId = getState().user.orgId;
dispatch(updateDatasourceInstanceAction({ exploreId, datasourceInstance: newDataSourceInstance })); dispatch(updateDatasourceInstanceAction({ exploreId, datasourceInstance: newDataSourceInstance }));
...@@ -113,7 +114,7 @@ export function changeDatasource(exploreId: ExploreId, datasource: string): Thun ...@@ -113,7 +114,7 @@ export function changeDatasource(exploreId: ExploreId, datasource: string): Thun
dispatch(changeRefreshInterval(exploreId, offOption.value)); dispatch(changeRefreshInterval(exploreId, offOption.value));
} }
await dispatch(loadDatasource(exploreId, newDataSourceInstance)); await dispatch(loadDatasource(exploreId, newDataSourceInstance, orgId));
dispatch(runQueries(exploreId)); dispatch(runQueries(exploreId));
}; };
} }
...@@ -263,12 +264,14 @@ export function initializeExplore( ...@@ -263,12 +264,14 @@ export function initializeExplore(
*/ */
export const loadDatasourceReady = ( export const loadDatasourceReady = (
exploreId: ExploreId, exploreId: ExploreId,
instance: DataSourceApi instance: DataSourceApi,
orgId: number
): ActionOf<LoadDatasourceReadyPayload> => { ): ActionOf<LoadDatasourceReadyPayload> => {
const historyKey = `grafana.explore.history.${instance.meta.id}`; const historyKey = `grafana.explore.history.${instance.meta.id}`;
const history = store.getObject(historyKey, []); const history = store.getObject(historyKey, []);
// Save last-used datasource // Save last-used datasource
store.set(LAST_USED_DATASOURCE_KEY, instance.name);
store.set(lastUsedDatasourceKeyForOrgId(orgId), instance.name);
return loadDatasourceReadyAction({ return loadDatasourceReadyAction({
exploreId, exploreId,
...@@ -346,7 +349,7 @@ export const reconnectDatasource = (exploreId: ExploreId): ThunkResult<void> => ...@@ -346,7 +349,7 @@ export const reconnectDatasource = (exploreId: ExploreId): ThunkResult<void> =>
/** /**
* Main action to asynchronously load a datasource. Dispatches lots of smaller actions for feedback. * Main action to asynchronously load a datasource. Dispatches lots of smaller actions for feedback.
*/ */
export function loadDatasource(exploreId: ExploreId, instance: DataSourceApi): ThunkResult<void> { export function loadDatasource(exploreId: ExploreId, instance: DataSourceApi, orgId: number): ThunkResult<void> {
return async (dispatch, getState) => { return async (dispatch, getState) => {
const datasourceName = instance.name; const datasourceName = instance.name;
...@@ -373,7 +376,7 @@ export function loadDatasource(exploreId: ExploreId, instance: DataSourceApi): T ...@@ -373,7 +376,7 @@ export function loadDatasource(exploreId: ExploreId, instance: DataSourceApi): T
return; return;
} }
dispatch(loadDatasourceReady(exploreId, instance)); dispatch(loadDatasourceReady(exploreId, instance, orgId));
}; };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment