mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
4a749e68a8
commit
1b51cd2043
@ -345,6 +345,12 @@ abstract class DataSourceApi<
|
||||
| StandardVariableSupport<DataSourceApi<TQuery, TOptions>>
|
||||
| CustomVariableSupport<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<
|
||||
|
@ -158,8 +158,8 @@ export class Explore extends React.PureComponent<Props, ExploreState> {
|
||||
};
|
||||
|
||||
onClickAddQueryRowButton = () => {
|
||||
const { exploreId, queryKeys } = this.props;
|
||||
this.props.addQueryRow(exploreId, queryKeys.length);
|
||||
const { exploreId, queryKeys, datasourceInstance } = this.props;
|
||||
this.props.addQueryRow(exploreId, queryKeys.length, datasourceInstance);
|
||||
};
|
||||
|
||||
onMakeAbsoluteTime = () => {
|
||||
|
@ -5,6 +5,7 @@ import { mergeMap, throttleTime } from 'rxjs/operators';
|
||||
|
||||
import {
|
||||
AbsoluteTimeRange,
|
||||
CoreApp,
|
||||
DataQuery,
|
||||
DataQueryErrorType,
|
||||
DataQueryResponse,
|
||||
@ -213,10 +214,17 @@ export const clearCacheAction = createAction<ClearCachePayload>('explore/clearCa
|
||||
/**
|
||||
* 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) => {
|
||||
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 }));
|
||||
};
|
||||
|
@ -3,6 +3,7 @@ import React, { PureComponent } from 'react';
|
||||
import { Unsubscribable } from 'rxjs';
|
||||
|
||||
import {
|
||||
CoreApp,
|
||||
DataQuery,
|
||||
DataSourceApi,
|
||||
DataSourceInstanceSettings,
|
||||
@ -137,6 +138,7 @@ export class QueryGroup extends PureComponent<Props, State> {
|
||||
const ds = !dsSettings?.meta.mixed ? dsSettings : defaultDataSource;
|
||||
|
||||
return {
|
||||
...this.state.dataSource?.getDefaultQuery?.(CoreApp.PanelEditor),
|
||||
datasource: { uid: ds?.uid, type: ds?.type },
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user