mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
CloudMonitor: Fix to hide queries when used in conjunction with Expressions (#53537)
* Correctly pass all original query parameters on interpolation * Add datasource tests - Update props on mock query - Add mock instance settings - Add util function to generate template variables - Add datasource test file - Correct setting of templateSrv - Update betterer results * Simplify test and pass templateSrv appropriately * Fix lint issue * Simplify test and add type that supports parital nested objects * Update test and remove unneeded util function * Rename to avoid duplicate mock
This commit is contained in:
parent
2b5d12310a
commit
d4f382892d
@ -0,0 +1,25 @@
|
||||
import { DataSourceInstanceSettings, DataSourcePluginMeta } from '@grafana/data';
|
||||
import { GoogleAuthType } from '@grafana/google-sdk';
|
||||
|
||||
import { CloudMonitoringOptions } from '../types';
|
||||
|
||||
export const createMockInstanceSetttings = (
|
||||
overrides?: Partial<DataSourceInstanceSettings<CloudMonitoringOptions>>
|
||||
): DataSourceInstanceSettings<CloudMonitoringOptions> => ({
|
||||
url: '/ds/1',
|
||||
id: 1,
|
||||
uid: 'abc',
|
||||
type: 'stackdriver',
|
||||
access: 'proxy',
|
||||
meta: {} as DataSourcePluginMeta,
|
||||
name: 'stackdriver',
|
||||
|
||||
jsonData: {
|
||||
authenticationType: GoogleAuthType.JWT,
|
||||
defaultProject: 'test-project',
|
||||
gceDefaultProject: 'test-project',
|
||||
clientEmail: 'test-email@test.com',
|
||||
tokenUri: 'https://oauth2.googleapis.com/token',
|
||||
},
|
||||
...overrides,
|
||||
});
|
@ -1,5 +1,9 @@
|
||||
import { AlignmentTypes, CloudMonitoringQuery, EditorMode, MetricQuery, QueryType, SLOQuery } from '../types';
|
||||
|
||||
type Subset<K> = {
|
||||
[attr in keyof K]?: K[attr] extends object ? Subset<K[attr]> : K[attr];
|
||||
};
|
||||
|
||||
export const createMockMetricQuery: (overrides?: Partial<MetricQuery>) => MetricQuery = (
|
||||
overrides?: Partial<MetricQuery>
|
||||
) => {
|
||||
@ -9,6 +13,9 @@ export const createMockMetricQuery: (overrides?: Partial<MetricQuery>) => Metric
|
||||
crossSeriesReducer: 'REDUCE_NONE',
|
||||
query: '',
|
||||
projectName: 'cloud-monitoring-default-project',
|
||||
filters: [],
|
||||
groupBys: [],
|
||||
view: 'FULL',
|
||||
...overrides,
|
||||
};
|
||||
};
|
||||
@ -28,12 +35,17 @@ export const createMockSLOQuery: (overrides?: Partial<SLOQuery>) => SLOQuery = (
|
||||
};
|
||||
};
|
||||
|
||||
export const createMockQuery: (overrides?: Partial<CloudMonitoringQuery>) => CloudMonitoringQuery = (overrides) => {
|
||||
export const createMockQuery: (overrides?: Subset<CloudMonitoringQuery>) => CloudMonitoringQuery = (overrides) => {
|
||||
return {
|
||||
datasource: {
|
||||
type: 'stackdriver',
|
||||
uid: 'abc',
|
||||
},
|
||||
refId: 'cloudMonitoringRefId',
|
||||
queryType: QueryType.METRICS,
|
||||
intervalMs: 0,
|
||||
type: 'timeSeriesQuery',
|
||||
hide: false,
|
||||
...overrides,
|
||||
metricQuery: createMockMetricQuery(overrides?.metricQuery),
|
||||
sloQuery: createMockSLOQuery(overrides?.sloQuery),
|
||||
|
@ -0,0 +1,28 @@
|
||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
||||
|
||||
import { createMockInstanceSetttings } from './__mocks__/cloudMonitoringInstanceSettings';
|
||||
import { createMockQuery } from './__mocks__/cloudMonitoringQuery';
|
||||
import Datasource from './datasource';
|
||||
|
||||
describe('Cloud Monitoring Datasource', () => {
|
||||
describe('interpolateVariablesInQueries', () => {
|
||||
it('should leave a query unchanged if there are no template variables', () => {
|
||||
const mockInstanceSettings = createMockInstanceSetttings();
|
||||
const ds = new Datasource(mockInstanceSettings);
|
||||
const query = createMockQuery();
|
||||
const templateVariablesApplied = ds.interpolateVariablesInQueries([query], {});
|
||||
expect(templateVariablesApplied[0]).toEqual(query);
|
||||
});
|
||||
|
||||
it('should correctly apply template variables', () => {
|
||||
const templateSrv = new TemplateSrv();
|
||||
templateSrv.replace = jest.fn().mockReturnValue('project-variable');
|
||||
const mockInstanceSettings = createMockInstanceSetttings();
|
||||
const ds = new Datasource(mockInstanceSettings, templateSrv);
|
||||
const query = createMockQuery({ metricQuery: { projectName: '$testVar' } });
|
||||
const templatedQuery = ds.interpolateVariablesInQueries([query], {});
|
||||
expect(templatedQuery[0]).toHaveProperty('datasource');
|
||||
expect(templatedQuery[0].metricQuery.projectName).toEqual('project-variable');
|
||||
});
|
||||
});
|
||||
});
|
@ -57,16 +57,12 @@ export default class CloudMonitoringDatasource extends DataSourceWithBackend<
|
||||
return super.query(request);
|
||||
}
|
||||
|
||||
applyTemplateVariables(
|
||||
{ metricQuery, refId, queryType, sloQuery, type = 'timeSeriesQuery' }: CloudMonitoringQuery,
|
||||
scopedVars: ScopedVars
|
||||
): Record<string, any> {
|
||||
applyTemplateVariables(target: CloudMonitoringQuery, scopedVars: ScopedVars): Record<string, any> {
|
||||
const { metricQuery, sloQuery } = target;
|
||||
return {
|
||||
...target,
|
||||
datasource: this.getRef(),
|
||||
refId,
|
||||
intervalMs: this.intervalMs,
|
||||
type,
|
||||
queryType,
|
||||
metricQuery: {
|
||||
...this.interpolateProps(metricQuery, scopedVars),
|
||||
projectName: this.templateSrv.replace(
|
||||
|
@ -7,8 +7,6 @@ import { AGGREGATIONS, ALIGNMENTS, SYSTEM_LABELS } from './constants';
|
||||
import CloudMonitoringDatasource from './datasource';
|
||||
import { AlignmentTypes, CustomMetaData, MetricDescriptor, MetricKind, PreprocessorType, ValueTypes } from './types';
|
||||
|
||||
const templateSrv: TemplateSrv = getTemplateSrv();
|
||||
|
||||
export const extractServicesFromMetricDescriptors = (metricDescriptors: MetricDescriptor[]) =>
|
||||
uniqBy(metricDescriptors, 'service');
|
||||
|
||||
@ -79,6 +77,7 @@ export const getAlignmentPickerData = (
|
||||
perSeriesAligner: string | undefined = AlignmentTypes.ALIGN_MEAN,
|
||||
preprocessor?: PreprocessorType
|
||||
) => {
|
||||
const templateSrv: TemplateSrv = getTemplateSrv();
|
||||
const alignOptions = getAlignmentOptionsByMetric(valueType!, metricKind!, preprocessor!).map((option) => ({
|
||||
...option,
|
||||
label: option.text,
|
||||
|
Loading…
Reference in New Issue
Block a user