2019-01-17 12:17:29 -06:00
|
|
|
// Libraries
|
2019-01-10 07:24:31 -06:00
|
|
|
import _ from 'lodash';
|
|
|
|
import { ThunkAction } from 'redux-thunk';
|
|
|
|
|
2019-01-17 12:17:29 -06:00
|
|
|
// Services & Utils
|
|
|
|
import store from 'app/core/store';
|
2019-01-28 08:26:15 -06:00
|
|
|
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
|
|
|
|
import { Emitter } from 'app/core/core';
|
2019-01-10 07:24:31 -06:00
|
|
|
import {
|
|
|
|
LAST_USED_DATASOURCE_KEY,
|
2019-01-12 16:22:28 -06:00
|
|
|
clearQueryKeys,
|
2019-01-10 07:24:31 -06:00
|
|
|
ensureQueries,
|
|
|
|
generateEmptyQuery,
|
|
|
|
hasNonEmptyQuery,
|
|
|
|
makeTimeSeriesList,
|
|
|
|
updateHistory,
|
|
|
|
buildQueryTransaction,
|
2019-01-12 16:22:28 -06:00
|
|
|
serializeStateToUrlParam,
|
2019-01-10 07:24:31 -06:00
|
|
|
} from 'app/core/utils/explore';
|
|
|
|
|
2019-01-17 12:17:29 -06:00
|
|
|
// Actions
|
2019-01-12 16:22:28 -06:00
|
|
|
import { updateLocation } from 'app/core/actions';
|
2019-01-17 12:17:29 -06:00
|
|
|
|
|
|
|
// Types
|
2019-01-17 10:59:47 -06:00
|
|
|
import { StoreState } from 'app/types';
|
2019-01-28 08:26:15 -06:00
|
|
|
import {
|
|
|
|
RawTimeRange,
|
|
|
|
TimeRange,
|
|
|
|
DataSourceApi,
|
|
|
|
DataQuery,
|
|
|
|
DataSourceSelectItem,
|
|
|
|
QueryHint,
|
|
|
|
} from '@grafana/ui/src/types';
|
2019-01-10 07:24:31 -06:00
|
|
|
import {
|
2019-01-11 11:26:56 -06:00
|
|
|
ExploreId,
|
2019-01-12 16:28:51 -06:00
|
|
|
ExploreUrlState,
|
2019-01-10 07:24:31 -06:00
|
|
|
RangeScanner,
|
|
|
|
ResultType,
|
|
|
|
QueryOptions,
|
|
|
|
QueryTransaction,
|
|
|
|
} from 'app/types/explore';
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
import {
|
|
|
|
Action as ThunkableAction,
|
|
|
|
ActionTypes,
|
|
|
|
AddQueryRowAction,
|
|
|
|
ChangeSizeAction,
|
|
|
|
HighlightLogsExpressionAction,
|
|
|
|
LoadDatasourceFailureAction,
|
|
|
|
LoadDatasourceMissingAction,
|
|
|
|
LoadDatasourcePendingAction,
|
|
|
|
LoadDatasourceSuccessAction,
|
|
|
|
QueryTransactionStartAction,
|
|
|
|
ScanStopAction,
|
2019-01-28 04:06:50 -06:00
|
|
|
UpdateDatasourceInstanceAction,
|
2019-01-29 08:34:28 -06:00
|
|
|
SetInitialQueriesAction,
|
2019-01-15 12:52:53 -06:00
|
|
|
} from './actionTypes';
|
|
|
|
|
|
|
|
type ThunkResult<R> = ThunkAction<R, StoreState, undefined, ThunkableAction>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a query row after the row with the given index.
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function addQueryRow(exploreId: ExploreId, index: number): AddQueryRowAction {
|
2019-01-10 07:24:31 -06:00
|
|
|
const query = generateEmptyQuery(index + 1);
|
2019-01-16 03:21:11 -06:00
|
|
|
return { type: ActionTypes.AddQueryRow, payload: { exploreId, index, query } };
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Loads a new datasource identified by the given name.
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function changeDatasource(exploreId: ExploreId, datasource: string): ThunkResult<void> {
|
2019-01-29 08:34:28 -06:00
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const newDataSourceInstance = await getDatasourceSrv().get(datasource);
|
|
|
|
const currentDataSourceInstance = getState().explore[exploreId].datasourceInstance;
|
|
|
|
const modifiedQueries = getState().explore[exploreId].modifiedQueries;
|
|
|
|
|
2019-01-29 08:52:36 -06:00
|
|
|
await dispatch(importQueries(exploreId, modifiedQueries, currentDataSourceInstance, newDataSourceInstance));
|
|
|
|
|
2019-01-29 08:34:28 -06:00
|
|
|
dispatch(updateDatasourceInstance(exploreId, newDataSourceInstance));
|
|
|
|
dispatch(loadDatasource(exploreId, newDataSourceInstance));
|
2019-01-10 07:24:31 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Query change handler for the query row with the given index.
|
|
|
|
* If `override` is reset the query modifications and run the queries. Use this to set queries via a link.
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function changeQuery(
|
|
|
|
exploreId: ExploreId,
|
|
|
|
query: DataQuery,
|
|
|
|
index: number,
|
|
|
|
override: boolean
|
|
|
|
): ThunkResult<void> {
|
2019-01-10 07:24:31 -06:00
|
|
|
return dispatch => {
|
|
|
|
// Null query means reset
|
|
|
|
if (query === null) {
|
|
|
|
query = { ...generateEmptyQuery(index) };
|
|
|
|
}
|
|
|
|
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.ChangeQuery, payload: { exploreId, query, index, override } });
|
2019-01-10 07:24:31 -06:00
|
|
|
if (override) {
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(runQueries(exploreId));
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Keep track of the Explore container size, in particular the width.
|
|
|
|
* The width will be used to calculate graph intervals (number of datapoints).
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function changeSize(
|
|
|
|
exploreId: ExploreId,
|
|
|
|
{ height, width }: { height: number; width: number }
|
|
|
|
): ChangeSizeAction {
|
2019-01-16 03:21:11 -06:00
|
|
|
return { type: ActionTypes.ChangeSize, payload: { exploreId, height, width } };
|
2019-01-11 11:26:56 -06:00
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Change the time range of Explore. Usually called from the Timepicker or a graph interaction.
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function changeTime(exploreId: ExploreId, range: TimeRange): ThunkResult<void> {
|
|
|
|
return dispatch => {
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.ChangeTime, payload: { exploreId, range } });
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(runQueries(exploreId));
|
|
|
|
};
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Clear all queries and results.
|
|
|
|
*/
|
|
|
|
export function clearQueries(exploreId: ExploreId): ThunkResult<void> {
|
2019-01-10 07:24:31 -06:00
|
|
|
return dispatch => {
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(scanStop(exploreId));
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.ClearQueries, payload: { exploreId } });
|
2019-01-12 16:22:28 -06:00
|
|
|
dispatch(stateSave());
|
2019-01-11 11:26:56 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Highlight expressions in the log results
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function highlightLogsExpression(exploreId: ExploreId, expressions: string[]): HighlightLogsExpressionAction {
|
2019-01-16 03:21:11 -06:00
|
|
|
return { type: ActionTypes.HighlightLogsExpression, payload: { exploreId, expressions } };
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Initialize Explore state with state from the URL and the React component.
|
|
|
|
* Call this only on components for with the Explore state has not been initialized.
|
|
|
|
*/
|
2019-01-10 07:24:31 -06:00
|
|
|
export function initializeExplore(
|
2019-01-11 11:26:56 -06:00
|
|
|
exploreId: ExploreId,
|
2019-01-28 09:31:38 -06:00
|
|
|
datasourceName: string,
|
2019-01-10 07:24:31 -06:00
|
|
|
queries: DataQuery[],
|
|
|
|
range: RawTimeRange,
|
|
|
|
containerWidth: number,
|
|
|
|
eventBridge: Emitter
|
|
|
|
): ThunkResult<void> {
|
|
|
|
return async dispatch => {
|
|
|
|
const exploreDatasources: DataSourceSelectItem[] = getDatasourceSrv()
|
|
|
|
.getExternal()
|
|
|
|
.map(ds => ({
|
|
|
|
value: ds.name,
|
|
|
|
name: ds.name,
|
|
|
|
meta: ds.meta,
|
|
|
|
}));
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.InitializeExplore,
|
2019-01-16 03:21:11 -06:00
|
|
|
payload: {
|
|
|
|
exploreId,
|
|
|
|
containerWidth,
|
2019-01-28 09:31:38 -06:00
|
|
|
datasourceName,
|
2019-01-16 03:21:11 -06:00
|
|
|
eventBridge,
|
|
|
|
exploreDatasources,
|
|
|
|
queries,
|
|
|
|
range,
|
|
|
|
},
|
2019-01-10 07:24:31 -06:00
|
|
|
});
|
|
|
|
|
2019-01-17 05:07:42 -06:00
|
|
|
if (exploreDatasources.length >= 1) {
|
2019-01-10 07:24:31 -06:00
|
|
|
let instance;
|
2019-01-29 08:34:28 -06:00
|
|
|
|
2019-01-28 09:31:38 -06:00
|
|
|
if (datasourceName) {
|
2019-01-17 05:07:42 -06:00
|
|
|
try {
|
2019-01-28 09:31:38 -06:00
|
|
|
instance = await getDatasourceSrv().get(datasourceName);
|
2019-01-17 05:07:42 -06:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Checking on instance here because requested datasource could be deleted already
|
|
|
|
if (!instance) {
|
2019-01-10 07:24:31 -06:00
|
|
|
instance = await getDatasourceSrv().get();
|
|
|
|
}
|
2019-01-29 08:34:28 -06:00
|
|
|
|
2019-01-28 09:31:38 -06:00
|
|
|
dispatch(updateDatasourceInstance(exploreId, instance));
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(loadDatasource(exploreId, instance));
|
2019-01-10 07:24:31 -06:00
|
|
|
} else {
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(loadDatasourceMissing(exploreId));
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Initialize the wrapper split state
|
|
|
|
*/
|
2019-01-12 16:22:28 -06:00
|
|
|
export function initializeExploreSplit() {
|
|
|
|
return async dispatch => {
|
|
|
|
dispatch({ type: ActionTypes.InitializeExploreSplit });
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Display an error that happened during the selection of a datasource
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export const loadDatasourceFailure = (exploreId: ExploreId, error: string): LoadDatasourceFailureAction => ({
|
2019-01-10 07:24:31 -06:00
|
|
|
type: ActionTypes.LoadDatasourceFailure,
|
2019-01-16 03:21:11 -06:00
|
|
|
payload: {
|
|
|
|
exploreId,
|
|
|
|
error,
|
|
|
|
},
|
2019-01-10 07:24:31 -06:00
|
|
|
});
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Display an error when no datasources have been configured
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export const loadDatasourceMissing = (exploreId: ExploreId): LoadDatasourceMissingAction => ({
|
|
|
|
type: ActionTypes.LoadDatasourceMissing,
|
2019-01-16 03:21:11 -06:00
|
|
|
payload: { exploreId },
|
2019-01-11 11:26:56 -06:00
|
|
|
});
|
2019-01-10 07:24:31 -06:00
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Start the async process of loading a datasource to display a loading indicator
|
|
|
|
*/
|
2019-01-29 08:34:28 -06:00
|
|
|
export const loadDatasourcePending = (
|
|
|
|
exploreId: ExploreId,
|
|
|
|
requestedDatasourceName: string
|
|
|
|
): LoadDatasourcePendingAction => ({
|
2019-01-10 07:24:31 -06:00
|
|
|
type: ActionTypes.LoadDatasourcePending,
|
2019-01-16 03:21:11 -06:00
|
|
|
payload: {
|
|
|
|
exploreId,
|
2019-01-28 09:31:38 -06:00
|
|
|
requestedDatasourceName,
|
2019-01-16 03:21:11 -06:00
|
|
|
},
|
2019-01-10 07:24:31 -06:00
|
|
|
});
|
|
|
|
|
2019-01-29 08:34:28 -06:00
|
|
|
export const setInitialQueries = (exploreId: ExploreId, queries: DataQuery[]): SetInitialQueriesAction => {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.SetInitialQueries,
|
|
|
|
payload: {
|
|
|
|
exploreId,
|
|
|
|
queries,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
* e.g., Prometheus -> Loki queries.
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export const loadDatasourceSuccess = (
|
|
|
|
exploreId: ExploreId,
|
2019-01-29 08:35:32 -06:00
|
|
|
instance: any,
|
2019-01-11 11:26:56 -06:00
|
|
|
): LoadDatasourceSuccessAction => {
|
2019-01-10 07:24:31 -06:00
|
|
|
// Capabilities
|
|
|
|
const supportsGraph = instance.meta.metrics;
|
|
|
|
const supportsLogs = instance.meta.logs;
|
|
|
|
const supportsTable = instance.meta.tables;
|
|
|
|
// Custom components
|
|
|
|
const StartPage = instance.pluginExports.ExploreStartPage;
|
|
|
|
|
|
|
|
const historyKey = `grafana.explore.history.${instance.meta.id}`;
|
|
|
|
const history = store.getObject(historyKey, []);
|
|
|
|
// Save last-used datasource
|
|
|
|
store.set(LAST_USED_DATASOURCE_KEY, instance.name);
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: ActionTypes.LoadDatasourceSuccess,
|
2019-01-16 03:21:11 -06:00
|
|
|
payload: {
|
|
|
|
exploreId,
|
|
|
|
StartPage,
|
|
|
|
datasourceInstance: instance,
|
|
|
|
history,
|
|
|
|
showingStartPage: Boolean(StartPage),
|
|
|
|
supportsGraph,
|
|
|
|
supportsLogs,
|
|
|
|
supportsTable,
|
|
|
|
},
|
2019-01-10 07:24:31 -06:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-01-28 04:06:50 -06:00
|
|
|
/**
|
|
|
|
* Updates datasource instance before datasouce loading has started
|
|
|
|
*/
|
|
|
|
export function updateDatasourceInstance(
|
|
|
|
exploreId: ExploreId,
|
|
|
|
instance: DataSourceApi
|
|
|
|
): UpdateDatasourceInstanceAction {
|
|
|
|
return {
|
|
|
|
type: ActionTypes.UpdateDatasourceInstance,
|
|
|
|
payload: {
|
|
|
|
exploreId,
|
|
|
|
datasourceInstance: instance,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-29 08:34:28 -06:00
|
|
|
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();
|
|
|
|
}
|
2019-01-30 02:36:23 -06:00
|
|
|
|
|
|
|
const nextQueries = importedQueries.map((q, i) => ({
|
|
|
|
...importedQueries[i],
|
|
|
|
...generateEmptyQuery(i),
|
|
|
|
}));
|
|
|
|
|
|
|
|
dispatch(setInitialQueries(exploreId, nextQueries));
|
2019-01-29 08:34:28 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Main action to asynchronously load a datasource. Dispatches lots of smaller actions for feedback.
|
|
|
|
*/
|
2019-01-21 14:36:30 -06:00
|
|
|
export function loadDatasource(exploreId: ExploreId, instance: DataSourceApi): ThunkResult<void> {
|
2019-01-10 07:24:31 -06:00
|
|
|
return async (dispatch, getState) => {
|
2019-01-21 14:36:30 -06:00
|
|
|
const datasourceName = instance.name;
|
2019-01-10 07:24:31 -06:00
|
|
|
|
|
|
|
// Keep ID to track selection
|
2019-01-21 14:36:30 -06:00
|
|
|
dispatch(loadDatasourcePending(exploreId, datasourceName));
|
2019-01-10 07:24:31 -06:00
|
|
|
|
|
|
|
let datasourceError = null;
|
|
|
|
try {
|
|
|
|
const testResult = await instance.testDatasource();
|
|
|
|
datasourceError = testResult.status === 'success' ? null : testResult.message;
|
|
|
|
} catch (error) {
|
|
|
|
datasourceError = (error && error.statusText) || 'Network error';
|
|
|
|
}
|
2019-01-21 14:36:30 -06:00
|
|
|
|
2019-01-10 07:24:31 -06:00
|
|
|
if (datasourceError) {
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(loadDatasourceFailure(exploreId, datasourceError));
|
2019-01-10 07:24:31 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-21 14:36:30 -06:00
|
|
|
if (datasourceName !== getState().explore[exploreId].requestedDatasourceName) {
|
2019-01-10 07:24:31 -06:00
|
|
|
// User already changed datasource again, discard results
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (instance.init) {
|
|
|
|
instance.init();
|
|
|
|
}
|
|
|
|
|
2019-01-21 14:36:30 -06:00
|
|
|
if (datasourceName !== getState().explore[exploreId].requestedDatasourceName) {
|
2019-01-10 07:24:31 -06:00
|
|
|
// User already changed datasource again, discard results
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-29 08:35:32 -06:00
|
|
|
dispatch(loadDatasourceSuccess(exploreId, instance));
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(runQueries(exploreId));
|
2019-01-10 07:24:31 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Action to modify a query given a datasource-specific modifier action.
|
|
|
|
* @param exploreId Explore area
|
|
|
|
* @param modification Action object with a type, e.g., ADD_FILTER
|
|
|
|
* @param index Optional query row index. If omitted, the modification is applied to all query rows.
|
|
|
|
* @param modifier Function that executes the modification, typically `datasourceInstance.modifyQueries`.
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function modifyQueries(
|
|
|
|
exploreId: ExploreId,
|
|
|
|
modification: any,
|
|
|
|
index: number,
|
|
|
|
modifier: any
|
|
|
|
): ThunkResult<void> {
|
2019-01-10 07:24:31 -06:00
|
|
|
return dispatch => {
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.ModifyQueries, payload: { exploreId, modification, index, modifier } });
|
2019-01-10 07:24:31 -06:00
|
|
|
if (!modification.preventSubmit) {
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(runQueries(exploreId));
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Mark a query transaction as failed with an error extracted from the query response.
|
|
|
|
* The transaction will be marked as `done`.
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function queryTransactionFailure(
|
|
|
|
exploreId: ExploreId,
|
|
|
|
transactionId: string,
|
|
|
|
response: any,
|
|
|
|
datasourceId: string
|
|
|
|
): ThunkResult<void> {
|
2019-01-10 07:24:31 -06:00
|
|
|
return (dispatch, getState) => {
|
2019-01-11 11:26:56 -06:00
|
|
|
const { datasourceInstance, queryTransactions } = getState().explore[exploreId];
|
2019-01-10 07:24:31 -06:00
|
|
|
if (datasourceInstance.meta.id !== datasourceId || response.cancelled) {
|
|
|
|
// Navigated away, queries did not matter
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transaction might have been discarded
|
|
|
|
if (!queryTransactions.find(qt => qt.id === transactionId)) {
|
2019-01-16 03:21:11 -06:00
|
|
|
return;
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
console.error(response);
|
|
|
|
|
|
|
|
let error: string;
|
|
|
|
let errorDetails: string;
|
|
|
|
if (response.data) {
|
|
|
|
if (typeof response.data === 'string') {
|
|
|
|
error = response.data;
|
|
|
|
} else if (response.data.error) {
|
|
|
|
error = response.data.error;
|
|
|
|
if (response.data.response) {
|
|
|
|
errorDetails = response.data.response;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Error('Could not handle error response');
|
|
|
|
}
|
|
|
|
} else if (response.message) {
|
|
|
|
error = response.message;
|
|
|
|
} else if (typeof response === 'string') {
|
|
|
|
error = response;
|
|
|
|
} else {
|
|
|
|
error = 'Unknown error during query transaction. Please check JS console logs.';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark transactions as complete
|
|
|
|
const nextQueryTransactions = queryTransactions.map(qt => {
|
|
|
|
if (qt.id === transactionId) {
|
|
|
|
return {
|
|
|
|
...qt,
|
|
|
|
error,
|
|
|
|
errorDetails,
|
|
|
|
done: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return qt;
|
|
|
|
});
|
|
|
|
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.QueryTransactionFailure,
|
|
|
|
payload: { exploreId, queryTransactions: nextQueryTransactions },
|
|
|
|
});
|
2019-01-10 07:24:31 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Start a query transaction for the given result type.
|
|
|
|
* @param exploreId Explore area
|
|
|
|
* @param transaction Query options and `done` status.
|
|
|
|
* @param resultType Associate the transaction with a result viewer, e.g., Graph
|
|
|
|
* @param rowIndex Index is used to associate latency for this transaction with a query row
|
|
|
|
*/
|
2019-01-10 07:24:31 -06:00
|
|
|
export function queryTransactionStart(
|
2019-01-11 11:26:56 -06:00
|
|
|
exploreId: ExploreId,
|
2019-01-10 07:24:31 -06:00
|
|
|
transaction: QueryTransaction,
|
|
|
|
resultType: ResultType,
|
|
|
|
rowIndex: number
|
|
|
|
): QueryTransactionStartAction {
|
2019-01-16 03:21:11 -06:00
|
|
|
return { type: ActionTypes.QueryTransactionStart, payload: { exploreId, resultType, rowIndex, transaction } };
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Complete a query transaction, mark the transaction as `done` and store query state in URL.
|
|
|
|
* If the transaction was started by a scanner, it keeps on scanning for more results.
|
|
|
|
* Side-effect: the query is stored in localStorage.
|
|
|
|
* @param exploreId Explore area
|
|
|
|
* @param transactionId ID
|
|
|
|
* @param result Response from `datasourceInstance.query()`
|
|
|
|
* @param latency Duration between request and response
|
|
|
|
* @param queries Queries from all query rows
|
|
|
|
* @param datasourceId Origin datasource instance, used to discard results if current datasource is different
|
|
|
|
*/
|
2019-01-10 07:24:31 -06:00
|
|
|
export function queryTransactionSuccess(
|
2019-01-11 11:26:56 -06:00
|
|
|
exploreId: ExploreId,
|
2019-01-10 07:24:31 -06:00
|
|
|
transactionId: string,
|
|
|
|
result: any,
|
|
|
|
latency: number,
|
|
|
|
queries: DataQuery[],
|
|
|
|
datasourceId: string
|
|
|
|
): ThunkResult<void> {
|
|
|
|
return (dispatch, getState) => {
|
2019-01-11 11:26:56 -06:00
|
|
|
const { datasourceInstance, history, queryTransactions, scanner, scanning } = getState().explore[exploreId];
|
2019-01-10 07:24:31 -06:00
|
|
|
|
|
|
|
// If datasource already changed, results do not matter
|
|
|
|
if (datasourceInstance.meta.id !== datasourceId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transaction might have been discarded
|
|
|
|
const transaction = queryTransactions.find(qt => qt.id === transactionId);
|
|
|
|
if (!transaction) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get query hints
|
|
|
|
let hints: QueryHint[];
|
2019-01-17 12:17:29 -06:00
|
|
|
if (datasourceInstance.getQueryHints) {
|
2019-01-10 07:24:31 -06:00
|
|
|
hints = datasourceInstance.getQueryHints(transaction.query, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark transactions as complete and attach result
|
|
|
|
const nextQueryTransactions = queryTransactions.map(qt => {
|
|
|
|
if (qt.id === transactionId) {
|
|
|
|
return {
|
|
|
|
...qt,
|
|
|
|
hints,
|
|
|
|
latency,
|
|
|
|
result,
|
|
|
|
done: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return qt;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Side-effect: Saving history in localstorage
|
|
|
|
const nextHistory = updateHistory(history, datasourceId, queries);
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.QueryTransactionSuccess,
|
2019-01-16 03:21:11 -06:00
|
|
|
payload: {
|
|
|
|
exploreId,
|
|
|
|
history: nextHistory,
|
|
|
|
queryTransactions: nextQueryTransactions,
|
|
|
|
},
|
2019-01-10 07:24:31 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// Keep scanning for results if this was the last scanning transaction
|
|
|
|
if (scanning) {
|
|
|
|
if (_.size(result) === 0) {
|
|
|
|
const other = nextQueryTransactions.find(qt => qt.scanning && !qt.done);
|
|
|
|
if (!other) {
|
|
|
|
const range = scanner();
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.ScanRange, payload: { exploreId, range } });
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We can stop scanning if we have a result
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(scanStop(exploreId));
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Remove query row of the given index, as well as associated query results.
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function removeQueryRow(exploreId: ExploreId, index: number): ThunkResult<void> {
|
2019-01-10 07:24:31 -06:00
|
|
|
return dispatch => {
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.RemoveQueryRow, payload: { exploreId, index } });
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(runQueries(exploreId));
|
2019-01-10 07:24:31 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Main action to run queries and dispatches sub-actions based on which result viewers are active
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function runQueries(exploreId: ExploreId) {
|
2019-01-10 07:24:31 -06:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const {
|
|
|
|
datasourceInstance,
|
|
|
|
modifiedQueries,
|
|
|
|
showingLogs,
|
|
|
|
showingGraph,
|
|
|
|
showingTable,
|
|
|
|
supportsGraph,
|
|
|
|
supportsLogs,
|
|
|
|
supportsTable,
|
2019-01-11 11:26:56 -06:00
|
|
|
} = getState().explore[exploreId];
|
2019-01-10 07:24:31 -06:00
|
|
|
|
|
|
|
if (!hasNonEmptyQuery(modifiedQueries)) {
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.RunQueriesEmpty, payload: { exploreId } });
|
2019-01-28 08:27:35 -06:00
|
|
|
dispatch(stateSave()); // Remember to saves to state and update location
|
2019-01-10 07:24:31 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some datasource's query builders allow per-query interval limits,
|
|
|
|
// but we're using the datasource interval limit for now
|
|
|
|
const interval = datasourceInstance.interval;
|
|
|
|
|
|
|
|
// Keep table queries first since they need to return quickly
|
|
|
|
if (showingTable && supportsTable) {
|
|
|
|
dispatch(
|
|
|
|
runQueriesForType(
|
2019-01-11 11:26:56 -06:00
|
|
|
exploreId,
|
2019-01-10 07:24:31 -06:00
|
|
|
'Table',
|
|
|
|
{
|
|
|
|
interval,
|
|
|
|
format: 'table',
|
|
|
|
instant: true,
|
|
|
|
valueWithRefId: true,
|
|
|
|
},
|
|
|
|
data => data[0]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (showingGraph && supportsGraph) {
|
|
|
|
dispatch(
|
|
|
|
runQueriesForType(
|
2019-01-11 11:26:56 -06:00
|
|
|
exploreId,
|
2019-01-10 07:24:31 -06:00
|
|
|
'Graph',
|
|
|
|
{
|
|
|
|
interval,
|
|
|
|
format: 'time_series',
|
|
|
|
instant: false,
|
|
|
|
},
|
|
|
|
makeTimeSeriesList
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (showingLogs && supportsLogs) {
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(runQueriesForType(exploreId, 'Logs', { interval, format: 'logs' }));
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
2019-01-12 16:22:28 -06:00
|
|
|
dispatch(stateSave());
|
2019-01-10 07:24:31 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Helper action to build a query transaction object and handing the query to the datasource.
|
|
|
|
* @param exploreId Explore area
|
|
|
|
* @param resultType Result viewer that will be associated with this query result
|
|
|
|
* @param queryOptions Query options as required by the datasource's `query()` function.
|
|
|
|
* @param resultGetter Optional result extractor, e.g., if the result is a list and you only need the first element.
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
function runQueriesForType(
|
|
|
|
exploreId: ExploreId,
|
|
|
|
resultType: ResultType,
|
|
|
|
queryOptions: QueryOptions,
|
|
|
|
resultGetter?: any
|
|
|
|
) {
|
2019-01-10 07:24:31 -06:00
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const {
|
|
|
|
datasourceInstance,
|
|
|
|
eventBridge,
|
|
|
|
modifiedQueries: queries,
|
|
|
|
queryIntervals,
|
|
|
|
range,
|
|
|
|
scanning,
|
2019-01-11 11:26:56 -06:00
|
|
|
} = getState().explore[exploreId];
|
2019-01-10 07:24:31 -06:00
|
|
|
const datasourceId = datasourceInstance.meta.id;
|
|
|
|
|
|
|
|
// Run all queries concurrently
|
|
|
|
queries.forEach(async (query, rowIndex) => {
|
|
|
|
const transaction = buildQueryTransaction(
|
|
|
|
query,
|
|
|
|
rowIndex,
|
|
|
|
resultType,
|
|
|
|
queryOptions,
|
|
|
|
range,
|
|
|
|
queryIntervals,
|
|
|
|
scanning
|
|
|
|
);
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(queryTransactionStart(exploreId, transaction, resultType, rowIndex));
|
2019-01-10 07:24:31 -06:00
|
|
|
try {
|
|
|
|
const now = Date.now();
|
|
|
|
const res = await datasourceInstance.query(transaction.options);
|
|
|
|
eventBridge.emit('data-received', res.data || []);
|
|
|
|
const latency = Date.now() - now;
|
|
|
|
const results = resultGetter ? resultGetter(res.data) : res.data;
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(queryTransactionSuccess(exploreId, transaction.id, results, latency, queries, datasourceId));
|
2019-01-10 07:24:31 -06:00
|
|
|
} catch (response) {
|
|
|
|
eventBridge.emit('data-error', response);
|
2019-01-11 11:26:56 -06:00
|
|
|
dispatch(queryTransactionFailure(exploreId, transaction.id, response, datasourceId));
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Start a scan for more results using the given scanner.
|
|
|
|
* @param exploreId Explore area
|
|
|
|
* @param scanner Function that a) returns a new time range and b) triggers a query run for the new range
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function scanStart(exploreId: ExploreId, scanner: RangeScanner): ThunkResult<void> {
|
2019-01-10 07:24:31 -06:00
|
|
|
return dispatch => {
|
2019-01-15 12:52:53 -06:00
|
|
|
// Register the scanner
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.ScanStart, payload: { exploreId, scanner } });
|
2019-01-15 12:52:53 -06:00
|
|
|
// Scanning must trigger query run, and return the new range
|
2019-01-10 07:24:31 -06:00
|
|
|
const range = scanner();
|
2019-01-15 12:52:53 -06:00
|
|
|
// Set the new range to be displayed
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.ScanRange, payload: { exploreId, range } });
|
2019-01-10 07:24:31 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Stop any scanning for more results.
|
|
|
|
*/
|
2019-01-11 11:26:56 -06:00
|
|
|
export function scanStop(exploreId: ExploreId): ScanStopAction {
|
2019-01-16 03:21:11 -06:00
|
|
|
return { type: ActionTypes.ScanStop, payload: { exploreId } };
|
2019-01-10 07:24:31 -06:00
|
|
|
}
|
2019-01-12 16:22:28 -06:00
|
|
|
|
2019-01-15 12:52:53 -06:00
|
|
|
/**
|
|
|
|
* Reset queries to the given queries. Any modifications will be discarded.
|
|
|
|
* Use this action for clicks on query examples. Triggers a query run.
|
|
|
|
*/
|
|
|
|
export function setQueries(exploreId: ExploreId, rawQueries: DataQuery[]): ThunkResult<void> {
|
|
|
|
return dispatch => {
|
|
|
|
// Inject react keys into query objects
|
|
|
|
const queries = rawQueries.map(q => ({ ...q, ...generateEmptyQuery() }));
|
|
|
|
dispatch({
|
|
|
|
type: ActionTypes.SetQueries,
|
2019-01-16 03:21:11 -06:00
|
|
|
payload: {
|
|
|
|
exploreId,
|
|
|
|
queries,
|
|
|
|
},
|
2019-01-15 12:52:53 -06:00
|
|
|
});
|
|
|
|
dispatch(runQueries(exploreId));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close the split view and save URL state.
|
|
|
|
*/
|
|
|
|
export function splitClose(): ThunkResult<void> {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch({ type: ActionTypes.SplitClose });
|
|
|
|
dispatch(stateSave());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the split view and copy the left state to be the right state.
|
|
|
|
* The right state is automatically initialized.
|
|
|
|
* The copy keeps all query modifications but wipes the query results.
|
|
|
|
*/
|
|
|
|
export function splitOpen(): ThunkResult<void> {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
// Clone left state to become the right state
|
|
|
|
const leftState = getState().explore.left;
|
|
|
|
const itemState = {
|
|
|
|
...leftState,
|
|
|
|
queryTransactions: [],
|
|
|
|
initialQueries: leftState.modifiedQueries.slice(),
|
|
|
|
};
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.SplitOpen, payload: { itemState } });
|
2019-01-15 12:52:53 -06:00
|
|
|
dispatch(stateSave());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves Explore state to URL using the `left` and `right` parameters.
|
|
|
|
* If split view is not active, `right` will not be set.
|
|
|
|
*/
|
2019-01-12 16:22:28 -06:00
|
|
|
export function stateSave() {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const { left, right, split } = getState().explore;
|
|
|
|
const urlStates: { [index: string]: string } = {};
|
|
|
|
const leftUrlState: ExploreUrlState = {
|
|
|
|
datasource: left.datasourceInstance.name,
|
|
|
|
queries: left.modifiedQueries.map(clearQueryKeys),
|
|
|
|
range: left.range,
|
|
|
|
};
|
|
|
|
urlStates.left = serializeStateToUrlParam(leftUrlState, true);
|
|
|
|
if (split) {
|
|
|
|
const rightUrlState: ExploreUrlState = {
|
|
|
|
datasource: right.datasourceInstance.name,
|
|
|
|
queries: right.modifiedQueries.map(clearQueryKeys),
|
|
|
|
range: right.range,
|
|
|
|
};
|
|
|
|
urlStates.right = serializeStateToUrlParam(rightUrlState, true);
|
|
|
|
}
|
|
|
|
dispatch(updateLocation({ query: urlStates }));
|
|
|
|
};
|
|
|
|
}
|
2019-01-15 12:52:53 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand/collapse the graph result viewer. When collapsed, graph queries won't be run.
|
|
|
|
*/
|
|
|
|
export function toggleGraph(exploreId: ExploreId): ThunkResult<void> {
|
|
|
|
return (dispatch, getState) => {
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.ToggleGraph, payload: { exploreId } });
|
2019-01-15 12:52:53 -06:00
|
|
|
if (getState().explore[exploreId].showingGraph) {
|
|
|
|
dispatch(runQueries(exploreId));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand/collapse the logs result viewer. When collapsed, log queries won't be run.
|
|
|
|
*/
|
|
|
|
export function toggleLogs(exploreId: ExploreId): ThunkResult<void> {
|
|
|
|
return (dispatch, getState) => {
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.ToggleLogs, payload: { exploreId } });
|
2019-01-15 12:52:53 -06:00
|
|
|
if (getState().explore[exploreId].showingLogs) {
|
|
|
|
dispatch(runQueries(exploreId));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expand/collapse the table result viewer. When collapsed, table queries won't be run.
|
|
|
|
*/
|
|
|
|
export function toggleTable(exploreId: ExploreId): ThunkResult<void> {
|
|
|
|
return (dispatch, getState) => {
|
2019-01-16 03:21:11 -06:00
|
|
|
dispatch({ type: ActionTypes.ToggleTable, payload: { exploreId } });
|
2019-01-15 12:52:53 -06:00
|
|
|
if (getState().explore[exploreId].showingTable) {
|
|
|
|
dispatch(runQueries(exploreId));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2019-01-28 06:27:56 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets state for explore.
|
|
|
|
*/
|
|
|
|
export function resetExplore(): ThunkResult<void> {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch({ type: ActionTypes.ResetExplore, payload: {} });
|
|
|
|
};
|
|
|
|
}
|