mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Datasources: Add support for getDetDefaultQuery in variable editor (#62026)
+Cloudwatch implementation
This commit is contained in:
parent
ae8c61c0b2
commit
a9d44aa795
@ -35,6 +35,11 @@ export abstract class VariableSupportBase<
|
||||
TOptions extends DataSourceJsonData = DataSourceOptionsType<DSType>
|
||||
> {
|
||||
abstract getType(): VariableSupportType;
|
||||
|
||||
/**
|
||||
* Define this method in the config if you want to pre-populate the editor with a default query.
|
||||
*/
|
||||
getDefaultQuery?(): Partial<TQuery>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,7 +2,7 @@ import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import React from 'react';
|
||||
|
||||
import { DataSourceApi } from '@grafana/data';
|
||||
import { DataSourceApi, VariableSupportType } from '@grafana/data';
|
||||
import { mockDataSource } from 'app/features/alerting/unified/mocks';
|
||||
import { DataSourceType } from 'app/features/alerting/unified/utils/datasource';
|
||||
|
||||
@ -64,6 +64,42 @@ describe('QueryVariableEditor', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('when the editor is rendered', () => {
|
||||
const extendedCustom = {
|
||||
extended: {
|
||||
VariableQueryEditor: jest.fn().mockImplementation(LegacyVariableQueryEditor),
|
||||
dataSource: {
|
||||
variables: {
|
||||
getType: () => VariableSupportType.Custom,
|
||||
query: jest.fn(),
|
||||
editor: jest.fn(),
|
||||
},
|
||||
} as unknown as DataSourceApi,
|
||||
},
|
||||
};
|
||||
it('should pass down the query with default values if the datasource config defines it', () => {
|
||||
const extended = { ...extendedCustom };
|
||||
extended.extended.dataSource.variables!.getDefaultQuery = jest
|
||||
.fn()
|
||||
.mockImplementation(() => 'some default query');
|
||||
const { props } = setupTestContext(extended);
|
||||
expect(props.extended?.dataSource?.variables?.getDefaultQuery).toBeDefined();
|
||||
expect(props.extended?.dataSource?.variables?.getDefaultQuery).toHaveBeenCalledTimes(1);
|
||||
expect(props.extended?.VariableQueryEditor).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ query: 'some default query' }),
|
||||
expect.anything()
|
||||
);
|
||||
});
|
||||
it('should not pass down a default query if the datasource config doesnt define it', () => {
|
||||
extendedCustom.extended.dataSource.variables!.getDefaultQuery = undefined;
|
||||
const { props } = setupTestContext(extendedCustom);
|
||||
expect(props.extended?.dataSource?.variables?.getDefaultQuery).not.toBeDefined();
|
||||
expect(props.extended?.VariableQueryEditor).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ query: '' }),
|
||||
expect.anything()
|
||||
);
|
||||
});
|
||||
});
|
||||
describe('when the user changes', () => {
|
||||
it.each`
|
||||
fieldName | propName | expectedArgs
|
||||
|
@ -134,10 +134,20 @@ export class QueryVariableEditorUnConnected extends PureComponent<Props, State>
|
||||
return null;
|
||||
}
|
||||
|
||||
const query = variable.query;
|
||||
const datasource = extended.dataSource;
|
||||
const VariableQueryEditor = extended.VariableQueryEditor;
|
||||
|
||||
let query = variable.query;
|
||||
|
||||
if (typeof query === 'string') {
|
||||
query = query || (datasource.variables?.getDefaultQuery?.() ?? '');
|
||||
} else {
|
||||
query = {
|
||||
...datasource.variables?.getDefaultQuery?.(),
|
||||
...variable.query,
|
||||
};
|
||||
}
|
||||
|
||||
if (isLegacyQueryEditor(VariableQueryEditor, datasource)) {
|
||||
return (
|
||||
<Field label="Query">
|
||||
|
@ -1,4 +1,12 @@
|
||||
import { CloudWatchLogsQuery, CloudWatchMetricsQuery, LogGroup, MetricEditorMode, MetricQueryType } from './types';
|
||||
import {
|
||||
CloudWatchLogsQuery,
|
||||
CloudWatchMetricsQuery,
|
||||
LogGroup,
|
||||
MetricEditorMode,
|
||||
MetricQueryType,
|
||||
VariableQuery,
|
||||
VariableQueryType,
|
||||
} from './types';
|
||||
|
||||
export const DEFAULT_METRICS_QUERY: Omit<CloudWatchMetricsQuery, 'refId'> = {
|
||||
queryMode: 'Metrics',
|
||||
@ -29,3 +37,8 @@ export const getDefaultLogsQuery = (
|
||||
logGroupNames: legacyDefaultLogGroups,
|
||||
logGroups: defaultLogGroups ?? [],
|
||||
});
|
||||
|
||||
export const DEFAULT_VARIABLE_QUERY: Partial<VariableQuery> = {
|
||||
queryType: VariableQueryType.Regions,
|
||||
region: 'default',
|
||||
};
|
||||
|
@ -12,6 +12,7 @@ import {
|
||||
import { ALL_ACCOUNTS_OPTION } from './components/Account';
|
||||
import { VariableQueryEditor } from './components/VariableQueryEditor/VariableQueryEditor';
|
||||
import { CloudWatchDatasource } from './datasource';
|
||||
import { DEFAULT_VARIABLE_QUERY } from './defaultQueries';
|
||||
import { migrateVariableQuery } from './migrations/variableQueryMigrations';
|
||||
import { ResourcesAPI } from './resources/ResourcesAPI';
|
||||
import { standardStatistics } from './standardStatistics';
|
||||
@ -158,6 +159,10 @@ export class CloudWatchVariableSupport extends CustomVariableSupport<CloudWatchD
|
||||
return metricFindOptions.length ? [this.allMetricFindValue, ...metricFindOptions] : [];
|
||||
});
|
||||
}
|
||||
|
||||
getDefaultQuery(): Partial<VariableQuery> {
|
||||
return DEFAULT_VARIABLE_QUERY;
|
||||
}
|
||||
}
|
||||
|
||||
function selectableValueToMetricFindOption({ label, value }: SelectableValue<string>): MetricFindValue {
|
||||
|
Loading…
Reference in New Issue
Block a user