|
@@ -53,6 +53,7 @@ import {
|
|
|
QueryTransactionStartAction,
|
|
QueryTransactionStartAction,
|
|
|
ScanStopAction,
|
|
ScanStopAction,
|
|
|
UpdateDatasourceInstanceAction,
|
|
UpdateDatasourceInstanceAction,
|
|
|
|
|
+ SetInitialQueriesAction,
|
|
|
} from './actionTypes';
|
|
} from './actionTypes';
|
|
|
|
|
|
|
|
type ThunkResult<R> = ThunkAction<R, StoreState, undefined, ThunkableAction>;
|
|
type ThunkResult<R> = ThunkAction<R, StoreState, undefined, ThunkableAction>;
|
|
@@ -69,10 +70,14 @@ export function addQueryRow(exploreId: ExploreId, index: number): AddQueryRowAct
|
|
|
* Loads a new datasource identified by the given name.
|
|
* Loads a new datasource identified by the given name.
|
|
|
*/
|
|
*/
|
|
|
export function changeDatasource(exploreId: ExploreId, datasource: string): ThunkResult<void> {
|
|
export function changeDatasource(exploreId: ExploreId, datasource: string): ThunkResult<void> {
|
|
|
- return async dispatch => {
|
|
|
|
|
- const instance = await getDatasourceSrv().get(datasource);
|
|
|
|
|
- dispatch(updateDatasourceInstance(exploreId, instance));
|
|
|
|
|
- dispatch(loadDatasource(exploreId, instance));
|
|
|
|
|
|
|
+ return async (dispatch, getState) => {
|
|
|
|
|
+ const newDataSourceInstance = await getDatasourceSrv().get(datasource);
|
|
|
|
|
+ const currentDataSourceInstance = getState().explore[exploreId].datasourceInstance;
|
|
|
|
|
+ const modifiedQueries = getState().explore[exploreId].modifiedQueries;
|
|
|
|
|
+
|
|
|
|
|
+ dispatch(importQueries(exploreId, modifiedQueries, currentDataSourceInstance, newDataSourceInstance));
|
|
|
|
|
+ dispatch(updateDatasourceInstance(exploreId, newDataSourceInstance));
|
|
|
|
|
+ dispatch(loadDatasource(exploreId, newDataSourceInstance));
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -174,6 +179,7 @@ export function initializeExplore(
|
|
|
|
|
|
|
|
if (exploreDatasources.length >= 1) {
|
|
if (exploreDatasources.length >= 1) {
|
|
|
let instance;
|
|
let instance;
|
|
|
|
|
+
|
|
|
if (datasourceName) {
|
|
if (datasourceName) {
|
|
|
try {
|
|
try {
|
|
|
instance = await getDatasourceSrv().get(datasourceName);
|
|
instance = await getDatasourceSrv().get(datasourceName);
|
|
@@ -185,6 +191,7 @@ export function initializeExplore(
|
|
|
if (!instance) {
|
|
if (!instance) {
|
|
|
instance = await getDatasourceSrv().get();
|
|
instance = await getDatasourceSrv().get();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
dispatch(updateDatasourceInstance(exploreId, instance));
|
|
dispatch(updateDatasourceInstance(exploreId, instance));
|
|
|
dispatch(loadDatasource(exploreId, instance));
|
|
dispatch(loadDatasource(exploreId, instance));
|
|
|
} else {
|
|
} else {
|
|
@@ -224,7 +231,10 @@ export const loadDatasourceMissing = (exploreId: ExploreId): LoadDatasourceMissi
|
|
|
/**
|
|
/**
|
|
|
* Start the async process of loading a datasource to display a loading indicator
|
|
* Start the async process of loading a datasource to display a loading indicator
|
|
|
*/
|
|
*/
|
|
|
-export const loadDatasourcePending = (exploreId: ExploreId, requestedDatasourceName: string): LoadDatasourcePendingAction => ({
|
|
|
|
|
|
|
+export const loadDatasourcePending = (
|
|
|
|
|
+ exploreId: ExploreId,
|
|
|
|
|
+ requestedDatasourceName: string
|
|
|
|
|
+): LoadDatasourcePendingAction => ({
|
|
|
type: ActionTypes.LoadDatasourcePending,
|
|
type: ActionTypes.LoadDatasourcePending,
|
|
|
payload: {
|
|
payload: {
|
|
|
exploreId,
|
|
exploreId,
|
|
@@ -232,6 +242,16 @@ export const loadDatasourcePending = (exploreId: ExploreId, requestedDatasourceN
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+export const setInitialQueries = (exploreId: ExploreId, queries: DataQuery[]): SetInitialQueriesAction => {
|
|
|
|
|
+ return {
|
|
|
|
|
+ type: ActionTypes.SetInitialQueries,
|
|
|
|
|
+ payload: {
|
|
|
|
|
+ exploreId,
|
|
|
|
|
+ queries,
|
|
|
|
|
+ },
|
|
|
|
|
+ };
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Datasource loading was successfully completed. The instance is stored in the state as well in case we need to
|
|
* Datasource loading was successfully completed. The instance is stored in the state as well in case we need to
|
|
|
* run datasource-specific code. Existing queries are imported to the new datasource if an importer exists,
|
|
* run datasource-specific code. Existing queries are imported to the new datasource if an importer exists,
|
|
@@ -239,8 +259,8 @@ export const loadDatasourcePending = (exploreId: ExploreId, requestedDatasourceN
|
|
|
*/
|
|
*/
|
|
|
export const loadDatasourceSuccess = (
|
|
export const loadDatasourceSuccess = (
|
|
|
exploreId: ExploreId,
|
|
exploreId: ExploreId,
|
|
|
- instance: any,
|
|
|
|
|
- queries: DataQuery[]
|
|
|
|
|
|
|
+ instance: any
|
|
|
|
|
+ // queries: DataQuery[]
|
|
|
): LoadDatasourceSuccessAction => {
|
|
): LoadDatasourceSuccessAction => {
|
|
|
// Capabilities
|
|
// Capabilities
|
|
|
const supportsGraph = instance.meta.metrics;
|
|
const supportsGraph = instance.meta.metrics;
|
|
@@ -261,7 +281,7 @@ export const loadDatasourceSuccess = (
|
|
|
StartPage,
|
|
StartPage,
|
|
|
datasourceInstance: instance,
|
|
datasourceInstance: instance,
|
|
|
history,
|
|
history,
|
|
|
- initialQueries: queries,
|
|
|
|
|
|
|
+ // initialQueries: queries,
|
|
|
showingStartPage: Boolean(StartPage),
|
|
showingStartPage: Boolean(StartPage),
|
|
|
supportsGraph,
|
|
supportsGraph,
|
|
|
supportsLogs,
|
|
supportsLogs,
|
|
@@ -286,6 +306,29 @@ export function updateDatasourceInstance(
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+export function importQueries(
|
|
|
|
|
+ exploreId: ExploreId,
|
|
|
|
|
+ queries: DataQuery[],
|
|
|
|
|
+ sourceDataSource: DataSourceApi,
|
|
|
|
|
+ targetDataSource: DataSourceApi
|
|
|
|
|
+) {
|
|
|
|
|
+ return async dispatch => {
|
|
|
|
|
+ let importedQueries = queries;
|
|
|
|
|
+ // Check if queries can be imported from previously selected datasource
|
|
|
|
|
+ if (sourceDataSource.meta.id === targetDataSource.meta.id) {
|
|
|
|
|
+ // Keep same queries if same type of datasource
|
|
|
|
|
+ importedQueries = [...queries];
|
|
|
|
|
+ } else if (targetDataSource.importQueries) {
|
|
|
|
|
+ // Datasource-specific importers
|
|
|
|
|
+ importedQueries = await targetDataSource.importQueries(queries, sourceDataSource.meta);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // Default is blank queries
|
|
|
|
|
+ importedQueries = ensureQueries();
|
|
|
|
|
+ }
|
|
|
|
|
+ dispatch(setInitialQueries(exploreId, importedQueries));
|
|
|
|
|
+ };
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 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.
|
|
|
*/
|
|
*/
|
|
@@ -319,21 +362,6 @@ export function loadDatasource(exploreId: ExploreId, instance: DataSourceApi): T
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Check if queries can be imported from previously selected datasource
|
|
// Check if queries can be imported from previously selected datasource
|
|
|
- const queries = getState().explore[exploreId].modifiedQueries;
|
|
|
|
|
- let importedQueries = queries;
|
|
|
|
|
- const origin = getState().explore[exploreId].datasourceInstance;
|
|
|
|
|
- if (origin) {
|
|
|
|
|
- if (origin.meta.id === instance.meta.id) {
|
|
|
|
|
- // Keep same queries if same type of datasource
|
|
|
|
|
- importedQueries = [...queries];
|
|
|
|
|
- } else if (instance.importQueries) {
|
|
|
|
|
- // Datasource-specific importers
|
|
|
|
|
- importedQueries = await instance.importQueries(queries, origin.meta);
|
|
|
|
|
- } else {
|
|
|
|
|
- // Default is blank queries
|
|
|
|
|
- importedQueries = ensureQueries();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
if (datasourceName !== getState().explore[exploreId].requestedDatasourceName) {
|
|
if (datasourceName !== getState().explore[exploreId].requestedDatasourceName) {
|
|
|
// User already changed datasource again, discard results
|
|
// User already changed datasource again, discard results
|
|
@@ -341,12 +369,12 @@ export function loadDatasource(exploreId: ExploreId, instance: DataSourceApi): T
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Reset edit state with new queries
|
|
// Reset edit state with new queries
|
|
|
- const nextQueries = importedQueries.map((q, i) => ({
|
|
|
|
|
- ...importedQueries[i],
|
|
|
|
|
- ...generateEmptyQuery(i),
|
|
|
|
|
- }));
|
|
|
|
|
|
|
+ // const nextQueries = importedQueries.map((q, i) => ({
|
|
|
|
|
+ // ...importedQueries[i],
|
|
|
|
|
+ // ...generateEmptyQuery(i),
|
|
|
|
|
+ // }));
|
|
|
|
|
|
|
|
- dispatch(loadDatasourceSuccess(exploreId, instance, nextQueries));
|
|
|
|
|
|
|
+ dispatch(loadDatasourceSuccess(exploreId, instance /*, nextQueries*/));
|
|
|
dispatch(runQueries(exploreId));
|
|
dispatch(runQueries(exploreId));
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|