DataSource: Allow data source plugins to set query default values (#49581)

* make it possible to set default query

* set default query in cloudwatch ds

* remove cloudwatch example code

* apply feedback

* Update public/app/features/explore/state/query.ts

Co-authored-by: Giordano Ricci <me@giordanoricci.com>

* Update public/app/features/query/components/QueryGroup.tsx

Co-authored-by: Giordano Ricci <me@giordanoricci.com>

* add alpha tag to method definiton

Co-authored-by: Giordano Ricci <me@giordanoricci.com>
This commit is contained in:
Erik Sundell 2022-06-16 22:22:05 +02:00 committed by GitHub
parent 4a749e68a8
commit 1b51cd2043
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 4 deletions

View File

@ -345,6 +345,12 @@ abstract class DataSourceApi<
| StandardVariableSupport<DataSourceApi<TQuery, TOptions>> | StandardVariableSupport<DataSourceApi<TQuery, TOptions>>
| CustomVariableSupport<DataSourceApi<TQuery, TOptions>> | CustomVariableSupport<DataSourceApi<TQuery, TOptions>>
| DataSourceVariableSupport<DataSourceApi<TQuery, TOptions>>; | DataSourceVariableSupport<DataSourceApi<TQuery, TOptions>>;
/*
* Optionally, use this method to set default values for a query
* @alpha -- experimental
*/
getDefaultQuery?(app: CoreApp): Partial<TQuery>;
} }
export interface MetadataInspectorProps< export interface MetadataInspectorProps<

View File

@ -158,8 +158,8 @@ export class Explore extends React.PureComponent<Props, ExploreState> {
}; };
onClickAddQueryRowButton = () => { onClickAddQueryRowButton = () => {
const { exploreId, queryKeys } = this.props; const { exploreId, queryKeys, datasourceInstance } = this.props;
this.props.addQueryRow(exploreId, queryKeys.length); this.props.addQueryRow(exploreId, queryKeys.length, datasourceInstance);
}; };
onMakeAbsoluteTime = () => { onMakeAbsoluteTime = () => {

View File

@ -5,6 +5,7 @@ import { mergeMap, throttleTime } from 'rxjs/operators';
import { import {
AbsoluteTimeRange, AbsoluteTimeRange,
CoreApp,
DataQuery, DataQuery,
DataQueryErrorType, DataQueryErrorType,
DataQueryResponse, DataQueryResponse,
@ -213,10 +214,17 @@ export const clearCacheAction = createAction<ClearCachePayload>('explore/clearCa
/** /**
* Adds a query row after the row with the given index. * Adds a query row after the row with the given index.
*/ */
export function addQueryRow(exploreId: ExploreId, index: number): ThunkResult<void> { export function addQueryRow(
exploreId: ExploreId,
index: number,
datasource: DataSourceApi | undefined | null
): ThunkResult<void> {
return (dispatch, getState) => { return (dispatch, getState) => {
const queries = getState().explore[exploreId]!.queries; const queries = getState().explore[exploreId]!.queries;
const query = generateEmptyQuery(queries, index); const query = {
...datasource?.getDefaultQuery?.(CoreApp.Explore),
...generateEmptyQuery(queries, index),
};
dispatch(addQueryRowAction({ exploreId, index, query })); dispatch(addQueryRowAction({ exploreId, index, query }));
}; };

View File

@ -3,6 +3,7 @@ import React, { PureComponent } from 'react';
import { Unsubscribable } from 'rxjs'; import { Unsubscribable } from 'rxjs';
import { import {
CoreApp,
DataQuery, DataQuery,
DataSourceApi, DataSourceApi,
DataSourceInstanceSettings, DataSourceInstanceSettings,
@ -137,6 +138,7 @@ export class QueryGroup extends PureComponent<Props, State> {
const ds = !dsSettings?.meta.mixed ? dsSettings : defaultDataSource; const ds = !dsSettings?.meta.mixed ? dsSettings : defaultDataSource;
return { return {
...this.state.dataSource?.getDefaultQuery?.(CoreApp.PanelEditor),
datasource: { uid: ds?.uid, type: ds?.type }, datasource: { uid: ds?.uid, type: ds?.type },
}; };
} }