From 05f9eb0766bec4b750592f0f0e2ef4d68fcd1939 Mon Sep 17 00:00:00 2001 From: Dominik Prokop Date: Mon, 28 Jan 2019 11:06:50 +0100 Subject: [PATCH 1/3] Update datasource before the loading has started --- .../app/features/explore/state/actionTypes.ts | 14 +++++++++++-- public/app/features/explore/state/actions.ts | 21 +++++++++++++++++-- public/app/features/explore/state/reducers.ts | 8 +++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/public/app/features/explore/state/actionTypes.ts b/public/app/features/explore/state/actionTypes.ts index 4e1d658f072..0de3981b40e 100644 --- a/public/app/features/explore/state/actionTypes.ts +++ b/public/app/features/explore/state/actionTypes.ts @@ -1,6 +1,6 @@ // Types import { Emitter } from 'app/core/core'; -import { RawTimeRange, TimeRange, DataQuery, DataSourceSelectItem } from '@grafana/ui/src/types'; +import { RawTimeRange, TimeRange, DataQuery, DataSourceSelectItem, DataSourceApi } from '@grafana/ui/src/types'; import { ExploreId, ExploreItemState, @@ -41,6 +41,7 @@ export enum ActionTypes { ToggleGraph = 'explore/TOGGLE_GRAPH', ToggleLogs = 'explore/TOGGLE_LOGS', ToggleTable = 'explore/TOGGLE_TABLE', + UpdateDatasourceInstance = 'explore/UPDATE_DATASOURCE_INSTANCE', } export interface AddQueryRowAction { @@ -270,6 +271,14 @@ export interface ToggleLogsAction { }; } +export interface UpdateDatasourceInstanceAction { + type: ActionTypes.UpdateDatasourceInstance; + payload: { + exploreId: ExploreId; + datasourceInstance: DataSourceApi; + }; +} + export type Action = | AddQueryRowAction | ChangeQueryAction @@ -297,4 +306,5 @@ export type Action = | SplitOpenAction | ToggleGraphAction | ToggleLogsAction - | ToggleTableAction; + | ToggleTableAction + | UpdateDatasourceInstanceAction; diff --git a/public/app/features/explore/state/actions.ts b/public/app/features/explore/state/actions.ts index d4c42ffa9c7..c653f5492f8 100644 --- a/public/app/features/explore/state/actions.ts +++ b/public/app/features/explore/state/actions.ts @@ -21,7 +21,7 @@ import { updateLocation } from 'app/core/actions'; // Types import { StoreState } from 'app/types'; -import { DataQuery, DataSourceSelectItem, QueryHint } from '@grafana/ui/src/types'; +import { DataQuery, DataSourceSelectItem, QueryHint } from '@grafana/ui/src/types'; import { getDatasourceSrv } from 'app/features/plugins/datasource_srv'; import { ExploreId, @@ -46,9 +46,9 @@ import { LoadDatasourceSuccessAction, QueryTransactionStartAction, ScanStopAction, + UpdateDatasourceInstanceAction, } from './actionTypes'; - type ThunkResult = ThunkAction; /** @@ -65,6 +65,7 @@ export function addQueryRow(exploreId: ExploreId, index: number): AddQueryRowAct export function changeDatasource(exploreId: ExploreId, datasource: string): ThunkResult { return async dispatch => { const instance = await getDatasourceSrv().get(datasource); + dispatch(updateDatasourceInstance(exploreId, instance)); dispatch(loadDatasource(exploreId, instance)); }; } @@ -263,6 +264,22 @@ export const loadDatasourceSuccess = ( }; }; +/** + * Updates datasource instance before datasouce loading has started + */ +export function updateDatasourceInstance( + exploreId: ExploreId, + instance: DataSourceApi +): UpdateDatasourceInstanceAction { + return { + type: ActionTypes.UpdateDatasourceInstance, + payload: { + exploreId, + datasourceInstance: instance, + }, + }; +} + /** * Main action to asynchronously load a datasource. Dispatches lots of smaller actions for feedback. */ diff --git a/public/app/features/explore/state/reducers.ts b/public/app/features/explore/state/reducers.ts index 8885f972d06..f65f2e26ee6 100644 --- a/public/app/features/explore/state/reducers.ts +++ b/public/app/features/explore/state/reducers.ts @@ -176,6 +176,14 @@ export const itemReducer = (state, action: Action): ExploreItemState => { }; } + case ActionTypes.UpdateDatasourceInstance: { + const { datasourceInstance } = action.payload; + return { + ...state, + datasourceInstance, + }; + } + case ActionTypes.LoadDatasourceFailure: { return { ...state, datasourceError: action.payload.error, datasourceLoading: false }; } From 91d926f180e5ab9d9c360c0ac6a49c8b3a35d759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 28 Jan 2019 15:26:15 +0100 Subject: [PATCH 2/3] single import for types from @grafana/ui --- public/app/features/explore/state/actions.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/public/app/features/explore/state/actions.ts b/public/app/features/explore/state/actions.ts index 35e81bb8e5b..0b17bed2d1f 100644 --- a/public/app/features/explore/state/actions.ts +++ b/public/app/features/explore/state/actions.ts @@ -4,6 +4,8 @@ import { ThunkAction } from 'redux-thunk'; // Services & Utils import store from 'app/core/store'; +import { getDatasourceSrv } from 'app/features/plugins/datasource_srv'; +import { Emitter } from 'app/core/core'; import { LAST_USED_DATASOURCE_KEY, clearQueryKeys, @@ -21,8 +23,14 @@ import { updateLocation } from 'app/core/actions'; // Types import { StoreState } from 'app/types'; -import { DataQuery, DataSourceSelectItem, QueryHint } from '@grafana/ui/src/types'; -import { getDatasourceSrv } from 'app/features/plugins/datasource_srv'; +import { + RawTimeRange, + TimeRange, + DataSourceApi, + DataQuery, + DataSourceSelectItem, + QueryHint, +} from '@grafana/ui/src/types'; import { ExploreId, ExploreUrlState, @@ -32,8 +40,6 @@ import { QueryTransaction, } from 'app/types/explore'; -import { Emitter } from 'app/core/core'; -import { RawTimeRange, TimeRange, DataSourceApi } from '@grafana/ui'; import { Action as ThunkableAction, ActionTypes, From a1a905cbb1a8109ee86b9ec92f1d714030dba4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 28 Jan 2019 16:31:38 +0100 Subject: [PATCH 3/3] Removed the initial data source as I could not see it being used anywhere --- public/app/features/explore/Explore.tsx | 3 --- public/app/features/explore/state/actionTypes.ts | 4 +--- public/app/features/explore/state/actions.ts | 14 +++++++------- public/app/features/explore/state/reducers.ts | 9 ++++----- public/app/types/explore.ts | 16 +++++----------- 5 files changed, 17 insertions(+), 29 deletions(-) diff --git a/public/app/features/explore/Explore.tsx b/public/app/features/explore/Explore.tsx index 20ab8ee67b9..909c4e81b8b 100644 --- a/public/app/features/explore/Explore.tsx +++ b/public/app/features/explore/Explore.tsx @@ -45,7 +45,6 @@ interface ExploreProps { datasourceLoading: boolean | null; datasourceMissing: boolean; exploreId: ExploreId; - initialDatasource?: string; initialQueries: DataQuery[]; initializeExplore: typeof initializeExplore; initialized: boolean; @@ -251,7 +250,6 @@ function mapStateToProps(state: StoreState, { exploreId }) { datasourceInstance, datasourceLoading, datasourceMissing, - initialDatasource, initialQueries, initialized, range, @@ -266,7 +264,6 @@ function mapStateToProps(state: StoreState, { exploreId }) { datasourceInstance, datasourceLoading, datasourceMissing, - initialDatasource, initialQueries, initialized, range, diff --git a/public/app/features/explore/state/actionTypes.ts b/public/app/features/explore/state/actionTypes.ts index d768da6db6b..757f946f37a 100644 --- a/public/app/features/explore/state/actionTypes.ts +++ b/public/app/features/explore/state/actionTypes.ts @@ -101,7 +101,6 @@ export interface InitializeExploreAction { payload: { exploreId: ExploreId; containerWidth: number; - datasource: string; eventBridge: Emitter; exploreDatasources: DataSourceSelectItem[]; queries: DataQuery[]; @@ -125,7 +124,7 @@ export interface LoadDatasourcePendingAction { type: ActionTypes.LoadDatasourcePending; payload: { exploreId: ExploreId; - datasourceName: string; + requestedDatasourceName: string; }; } @@ -143,7 +142,6 @@ export interface LoadDatasourceSuccessAction { StartPage?: any; datasourceInstance: any; history: HistoryItem[]; - initialDatasource: string; initialQueries: DataQuery[]; logsHighlighterExpressions?: any[]; showingStartPage: boolean; diff --git a/public/app/features/explore/state/actions.ts b/public/app/features/explore/state/actions.ts index 0b17bed2d1f..71d3391dd2a 100644 --- a/public/app/features/explore/state/actions.ts +++ b/public/app/features/explore/state/actions.ts @@ -144,7 +144,7 @@ export function highlightLogsExpression(exploreId: ExploreId, expressions: strin */ export function initializeExplore( exploreId: ExploreId, - datasource: string, + datasourceName: string, queries: DataQuery[], range: RawTimeRange, containerWidth: number, @@ -164,7 +164,7 @@ export function initializeExplore( payload: { exploreId, containerWidth, - datasource, + datasourceName, eventBridge, exploreDatasources, queries, @@ -174,9 +174,9 @@ export function initializeExplore( if (exploreDatasources.length >= 1) { let instance; - if (datasource) { + if (datasourceName) { try { - instance = await getDatasourceSrv().get(datasource); + instance = await getDatasourceSrv().get(datasourceName); } catch (error) { console.error(error); } @@ -185,6 +185,7 @@ export function initializeExplore( if (!instance) { instance = await getDatasourceSrv().get(); } + dispatch(updateDatasourceInstance(exploreId, instance)); dispatch(loadDatasource(exploreId, instance)); } else { dispatch(loadDatasourceMissing(exploreId)); @@ -223,11 +224,11 @@ export const loadDatasourceMissing = (exploreId: ExploreId): LoadDatasourceMissi /** * Start the async process of loading a datasource to display a loading indicator */ -export const loadDatasourcePending = (exploreId: ExploreId, datasourceName: string): LoadDatasourcePendingAction => ({ +export const loadDatasourcePending = (exploreId: ExploreId, requestedDatasourceName: string): LoadDatasourcePendingAction => ({ type: ActionTypes.LoadDatasourcePending, payload: { exploreId, - datasourceName, + requestedDatasourceName, }, }); @@ -260,7 +261,6 @@ export const loadDatasourceSuccess = ( StartPage, datasourceInstance: instance, history, - initialDatasource: instance.name, initialQueries: queries, showingStartPage: Boolean(StartPage), supportsGraph, diff --git a/public/app/features/explore/state/reducers.ts b/public/app/features/explore/state/reducers.ts index d3cc81f96a8..98ded365ad0 100644 --- a/public/app/features/explore/state/reducers.ts +++ b/public/app/features/explore/state/reducers.ts @@ -24,6 +24,7 @@ export const makeExploreItemState = (): ExploreItemState => ({ StartPage: undefined, containerWidth: 0, datasourceInstance: null, + requestedDatasourceName: null, datasourceError: null, datasourceLoading: null, datasourceMissing: false, @@ -162,14 +163,13 @@ export const itemReducer = (state, action: Action): ExploreItemState => { } case ActionTypes.InitializeExplore: { - const { containerWidth, datasource, eventBridge, exploreDatasources, queries, range } = action.payload; + const { containerWidth, eventBridge, exploreDatasources, queries, range } = action.payload; return { ...state, containerWidth, eventBridge, exploreDatasources, range, - initialDatasource: datasource, initialQueries: queries, initialized: true, modifiedQueries: queries.slice(), @@ -181,6 +181,7 @@ export const itemReducer = (state, action: Action): ExploreItemState => { return { ...state, datasourceInstance, + datasourceName: datasourceInstance.name, }; } @@ -193,7 +194,7 @@ export const itemReducer = (state, action: Action): ExploreItemState => { } case ActionTypes.LoadDatasourcePending: { - return { ...state, datasourceLoading: true, requestedDatasourceName: action.payload.datasourceName }; + return { ...state, datasourceLoading: true, requestedDatasourceName: action.payload.requestedDatasourceName }; } case ActionTypes.LoadDatasourceSuccess: { @@ -202,7 +203,6 @@ export const itemReducer = (state, action: Action): ExploreItemState => { StartPage, datasourceInstance, history, - initialDatasource, initialQueries, showingStartPage, supportsGraph, @@ -217,7 +217,6 @@ export const itemReducer = (state, action: Action): ExploreItemState => { StartPage, datasourceInstance, history, - initialDatasource, initialQueries, showingStartPage, supportsGraph, diff --git a/public/app/types/explore.ts b/public/app/types/explore.ts index ce5ea1047dd..34b7ff08c99 100644 --- a/public/app/types/explore.ts +++ b/public/app/types/explore.ts @@ -110,7 +110,11 @@ export interface ExploreItemState { /** * Datasource instance that has been selected. Datasource-specific logic can be run on this object. */ - datasourceInstance: DataSourceApi; + datasourceInstance: DataSourceApi | null; + /** + * Current data source name or null if default + */ + requestedDatasourceName: string | null; /** * Error to be shown when datasource loading or testing failed. */ @@ -139,10 +143,6 @@ export interface ExploreItemState { * History of recent queries. Datasource-specific and initialized via localStorage. */ history: HistoryItem[]; - /** - * Initial datasource for this Explore, e.g., set via URL. - */ - initialDatasource?: string; /** * Initial queries for this Explore, e.g., set via URL. Each query will be * converted to a query row. Query edits should be tracked in `modifiedQueries` though. @@ -181,12 +181,6 @@ export interface ExploreItemState { * when query rows are removed. */ queryTransactions: QueryTransaction[]; - /** - * Tracks datasource when selected in the datasource selector. - * Allows the selection to be discarded if something went wrong during the asynchronous - * loading of the datasource. - */ - requestedDatasourceName?: string; /** * Time range for this Explore. Managed by the time picker and used by all query runs. */