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:
@@ -35,6 +35,11 @@ export abstract class VariableSupportBase<
|
|||||||
TOptions extends DataSourceJsonData = DataSourceOptionsType<DSType>
|
TOptions extends DataSourceJsonData = DataSourceOptionsType<DSType>
|
||||||
> {
|
> {
|
||||||
abstract getType(): VariableSupportType;
|
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 userEvent from '@testing-library/user-event';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import { DataSourceApi } from '@grafana/data';
|
import { DataSourceApi, VariableSupportType } from '@grafana/data';
|
||||||
import { mockDataSource } from 'app/features/alerting/unified/mocks';
|
import { mockDataSource } from 'app/features/alerting/unified/mocks';
|
||||||
import { DataSourceType } from 'app/features/alerting/unified/utils/datasource';
|
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', () => {
|
describe('when the user changes', () => {
|
||||||
it.each`
|
it.each`
|
||||||
fieldName | propName | expectedArgs
|
fieldName | propName | expectedArgs
|
||||||
|
|||||||
@@ -134,10 +134,20 @@ export class QueryVariableEditorUnConnected extends PureComponent<Props, State>
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const query = variable.query;
|
|
||||||
const datasource = extended.dataSource;
|
const datasource = extended.dataSource;
|
||||||
const VariableQueryEditor = extended.VariableQueryEditor;
|
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)) {
|
if (isLegacyQueryEditor(VariableQueryEditor, datasource)) {
|
||||||
return (
|
return (
|
||||||
<Field label="Query">
|
<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'> = {
|
export const DEFAULT_METRICS_QUERY: Omit<CloudWatchMetricsQuery, 'refId'> = {
|
||||||
queryMode: 'Metrics',
|
queryMode: 'Metrics',
|
||||||
@@ -29,3 +37,8 @@ export const getDefaultLogsQuery = (
|
|||||||
logGroupNames: legacyDefaultLogGroups,
|
logGroupNames: legacyDefaultLogGroups,
|
||||||
logGroups: defaultLogGroups ?? [],
|
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 { ALL_ACCOUNTS_OPTION } from './components/Account';
|
||||||
import { VariableQueryEditor } from './components/VariableQueryEditor/VariableQueryEditor';
|
import { VariableQueryEditor } from './components/VariableQueryEditor/VariableQueryEditor';
|
||||||
import { CloudWatchDatasource } from './datasource';
|
import { CloudWatchDatasource } from './datasource';
|
||||||
|
import { DEFAULT_VARIABLE_QUERY } from './defaultQueries';
|
||||||
import { migrateVariableQuery } from './migrations/variableQueryMigrations';
|
import { migrateVariableQuery } from './migrations/variableQueryMigrations';
|
||||||
import { ResourcesAPI } from './resources/ResourcesAPI';
|
import { ResourcesAPI } from './resources/ResourcesAPI';
|
||||||
import { standardStatistics } from './standardStatistics';
|
import { standardStatistics } from './standardStatistics';
|
||||||
@@ -158,6 +159,10 @@ export class CloudWatchVariableSupport extends CustomVariableSupport<CloudWatchD
|
|||||||
return metricFindOptions.length ? [this.allMetricFindValue, ...metricFindOptions] : [];
|
return metricFindOptions.length ? [this.allMetricFindValue, ...metricFindOptions] : [];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getDefaultQuery(): Partial<VariableQuery> {
|
||||||
|
return DEFAULT_VARIABLE_QUERY;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function selectableValueToMetricFindOption({ label, value }: SelectableValue<string>): MetricFindValue {
|
function selectableValueToMetricFindOption({ label, value }: SelectableValue<string>): MetricFindValue {
|
||||||
|
|||||||
Reference in New Issue
Block a user