2022-04-22 14:33:13 +01:00
|
|
|
import { of } from 'rxjs';
|
|
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
import {
|
|
|
|
|
DataSourceInstanceSettings,
|
|
|
|
|
DataSourcePluginMeta,
|
|
|
|
|
PluginMetaInfo,
|
|
|
|
|
PluginType,
|
|
|
|
|
VariableHide,
|
|
|
|
|
} from '@grafana/data';
|
|
|
|
|
import { getBackendSrv, setBackendSrv } from '@grafana/runtime';
|
|
|
|
|
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
2022-02-28 09:28:27 +01:00
|
|
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
2021-11-11 16:48:35 +01:00
|
|
|
import { initialCustomVariableModelState } from 'app/features/variables/custom/reducer';
|
|
|
|
|
import { CustomVariableModel } from 'app/features/variables/types';
|
2022-02-28 09:28:27 +01:00
|
|
|
|
2021-11-11 16:48:35 +01:00
|
|
|
import { CloudWatchDatasource } from '../datasource';
|
2022-09-19 08:50:41 +02:00
|
|
|
import { CloudWatchJsonData } from '../types';
|
|
|
|
|
|
|
|
|
|
export function setupMockedTemplateService(variables: CustomVariableModel[]) {
|
|
|
|
|
const templateService = new TemplateSrv();
|
|
|
|
|
templateService.init(variables);
|
|
|
|
|
templateService.getVariables = jest.fn().mockReturnValue(variables);
|
|
|
|
|
return templateService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const info: PluginMetaInfo = {
|
|
|
|
|
author: {
|
|
|
|
|
name: '',
|
|
|
|
|
},
|
|
|
|
|
description: '',
|
|
|
|
|
links: [],
|
|
|
|
|
logos: {
|
|
|
|
|
large: '',
|
|
|
|
|
small: '',
|
|
|
|
|
},
|
|
|
|
|
screenshots: [],
|
|
|
|
|
updated: '',
|
|
|
|
|
version: '',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const meta: DataSourcePluginMeta<CloudWatchJsonData> = {
|
|
|
|
|
id: '',
|
|
|
|
|
name: '',
|
|
|
|
|
type: PluginType.datasource,
|
|
|
|
|
info,
|
|
|
|
|
module: '',
|
|
|
|
|
baseUrl: '',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const CloudWatchSettings: DataSourceInstanceSettings<CloudWatchJsonData> = {
|
2023-01-09 16:30:21 +01:00
|
|
|
jsonData: { defaultRegion: 'us-west-1', tracingDatasourceUid: 'xray', logGroups: [] },
|
2022-09-19 08:50:41 +02:00
|
|
|
id: 0,
|
|
|
|
|
uid: '',
|
|
|
|
|
type: '',
|
|
|
|
|
name: 'CloudWatch Test Datasource',
|
|
|
|
|
meta,
|
|
|
|
|
readOnly: false,
|
|
|
|
|
access: 'direct',
|
|
|
|
|
};
|
2021-11-11 16:48:35 +01:00
|
|
|
|
2022-04-29 16:42:59 -04:00
|
|
|
export function setupMockedDataSource({
|
|
|
|
|
variables,
|
|
|
|
|
mockGetVariableName = true,
|
2022-10-19 08:40:03 +02:00
|
|
|
getMock = jest.fn(),
|
2022-12-14 12:21:36 +01:00
|
|
|
customInstanceSettings = CloudWatchSettings,
|
2022-09-19 08:50:41 +02:00
|
|
|
}: {
|
2022-10-19 08:40:03 +02:00
|
|
|
getMock?: jest.Func;
|
2022-09-19 08:50:41 +02:00
|
|
|
variables?: CustomVariableModel[];
|
|
|
|
|
mockGetVariableName?: boolean;
|
2022-12-14 12:21:36 +01:00
|
|
|
customInstanceSettings?: DataSourceInstanceSettings<CloudWatchJsonData>;
|
2022-09-19 08:50:41 +02:00
|
|
|
} = {}) {
|
|
|
|
|
let templateService = new TemplateSrv();
|
2021-11-11 16:48:35 +01:00
|
|
|
if (variables) {
|
2022-09-19 08:50:41 +02:00
|
|
|
templateService = setupMockedTemplateService(variables);
|
2022-04-29 16:42:59 -04:00
|
|
|
if (mockGetVariableName) {
|
2022-11-28 12:39:12 +01:00
|
|
|
templateService.getVariableName = (name: string) => name.replace('$', '');
|
2022-04-29 16:42:59 -04:00
|
|
|
}
|
2021-11-11 16:48:35 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
const timeSrv = getTimeSrv();
|
2022-12-14 12:21:36 +01:00
|
|
|
const datasource = new CloudWatchDatasource(customInstanceSettings, templateService, timeSrv);
|
2022-04-18 20:05:10 +02:00
|
|
|
datasource.getVariables = () => ['test'];
|
2022-09-20 07:50:54 +02:00
|
|
|
datasource.api.getNamespaces = jest.fn().mockResolvedValue([]);
|
|
|
|
|
datasource.api.getRegions = jest.fn().mockResolvedValue([]);
|
2022-10-20 12:53:28 +02:00
|
|
|
datasource.api.getDimensionKeys = jest.fn().mockResolvedValue([]);
|
2022-10-26 15:59:26 +02:00
|
|
|
datasource.api.getMetrics = jest.fn().mockResolvedValue([]);
|
2022-11-28 12:39:12 +01:00
|
|
|
datasource.api.getAccounts = jest.fn().mockResolvedValue([]);
|
2023-01-09 16:30:21 +01:00
|
|
|
datasource.api.describeLogGroups = jest.fn().mockResolvedValue([]);
|
2022-09-19 08:50:41 +02:00
|
|
|
const fetchMock = jest.fn().mockReturnValue(of({}));
|
|
|
|
|
setBackendSrv({
|
|
|
|
|
...getBackendSrv(),
|
|
|
|
|
fetch: fetchMock,
|
2022-10-19 08:40:03 +02:00
|
|
|
get: getMock,
|
2022-09-19 08:50:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { datasource, fetchMock, templateService, timeSrv };
|
2021-11-11 16:48:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const metricVariable: CustomVariableModel = {
|
|
|
|
|
...initialCustomVariableModelState,
|
|
|
|
|
id: 'metric',
|
|
|
|
|
name: 'metric',
|
|
|
|
|
current: { value: 'CPUUtilization', text: 'CPUUtilizationEC2', selected: true },
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'DroppedBytes', text: 'DroppedBytes', selected: false },
|
|
|
|
|
{ value: 'CPUUtilization', text: 'CPUUtilization', selected: false },
|
|
|
|
|
],
|
|
|
|
|
multi: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const namespaceVariable: CustomVariableModel = {
|
|
|
|
|
...initialCustomVariableModelState,
|
|
|
|
|
id: 'namespace',
|
|
|
|
|
name: 'namespace',
|
|
|
|
|
query: 'namespaces()',
|
|
|
|
|
current: { value: 'AWS/EC2', text: 'AWS/EC2', selected: true },
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'AWS/Redshift', text: 'AWS/Redshift', selected: false },
|
|
|
|
|
{ value: 'AWS/EC2', text: 'AWS/EC2', selected: false },
|
|
|
|
|
{ value: 'AWS/MQ', text: 'AWS/MQ', selected: false },
|
|
|
|
|
],
|
|
|
|
|
multi: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const labelsVariable: CustomVariableModel = {
|
|
|
|
|
...initialCustomVariableModelState,
|
|
|
|
|
id: 'labels',
|
|
|
|
|
name: 'labels',
|
|
|
|
|
current: {
|
|
|
|
|
value: ['InstanceId', 'InstanceType'],
|
|
|
|
|
text: ['InstanceId', 'InstanceType'].toString(),
|
|
|
|
|
selected: true,
|
|
|
|
|
},
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'InstanceId', text: 'InstanceId', selected: false },
|
|
|
|
|
{ value: 'InstanceType', text: 'InstanceType', selected: false },
|
|
|
|
|
],
|
|
|
|
|
multi: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const limitVariable: CustomVariableModel = {
|
|
|
|
|
...initialCustomVariableModelState,
|
|
|
|
|
id: 'limit',
|
|
|
|
|
name: 'limit',
|
|
|
|
|
current: {
|
|
|
|
|
value: '100',
|
|
|
|
|
text: '100',
|
|
|
|
|
selected: true,
|
|
|
|
|
},
|
|
|
|
|
options: [
|
|
|
|
|
{ value: '10', text: '10', selected: false },
|
|
|
|
|
{ value: '100', text: '100', selected: false },
|
|
|
|
|
{ value: '1000', text: '1000', selected: false },
|
|
|
|
|
],
|
|
|
|
|
multi: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const aggregationvariable: CustomVariableModel = {
|
|
|
|
|
...initialCustomVariableModelState,
|
|
|
|
|
id: 'aggregation',
|
|
|
|
|
name: 'aggregation',
|
|
|
|
|
current: {
|
|
|
|
|
value: 'AVG',
|
|
|
|
|
text: 'AVG',
|
|
|
|
|
selected: true,
|
|
|
|
|
},
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'AVG', text: 'AVG', selected: false },
|
|
|
|
|
{ value: 'SUM', text: 'SUM', selected: false },
|
|
|
|
|
{ value: 'MIN', text: 'MIN', selected: false },
|
|
|
|
|
],
|
|
|
|
|
multi: false,
|
|
|
|
|
};
|
2022-04-25 10:33:49 -04:00
|
|
|
|
|
|
|
|
export const dimensionVariable: CustomVariableModel = {
|
|
|
|
|
...initialCustomVariableModelState,
|
|
|
|
|
id: 'dimension',
|
|
|
|
|
name: 'dimension',
|
|
|
|
|
current: {
|
|
|
|
|
value: 'env',
|
|
|
|
|
text: 'env',
|
|
|
|
|
selected: true,
|
|
|
|
|
},
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'env', text: 'env', selected: false },
|
|
|
|
|
{ value: 'tag', text: 'tag', selected: false },
|
|
|
|
|
],
|
|
|
|
|
multi: false,
|
|
|
|
|
};
|
2022-06-01 10:23:31 -07:00
|
|
|
|
|
|
|
|
export const logGroupNamesVariable: CustomVariableModel = {
|
|
|
|
|
...initialCustomVariableModelState,
|
|
|
|
|
id: 'groups',
|
|
|
|
|
name: 'groups',
|
|
|
|
|
current: {
|
|
|
|
|
value: ['templatedGroup-1', 'templatedGroup-2'],
|
|
|
|
|
text: ['templatedGroup-1', 'templatedGroup-2'],
|
|
|
|
|
selected: true,
|
|
|
|
|
},
|
|
|
|
|
options: [
|
|
|
|
|
{ value: 'templatedGroup-1', text: 'templatedGroup-1', selected: true },
|
|
|
|
|
{ value: 'templatedGroup-2', text: 'templatedGroup-2', selected: true },
|
|
|
|
|
],
|
|
|
|
|
multi: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const regionVariable: CustomVariableModel = {
|
|
|
|
|
...initialCustomVariableModelState,
|
|
|
|
|
id: 'region',
|
|
|
|
|
name: 'region',
|
|
|
|
|
current: {
|
|
|
|
|
value: 'templatedRegion',
|
|
|
|
|
text: 'templatedRegion',
|
|
|
|
|
selected: true,
|
|
|
|
|
},
|
|
|
|
|
options: [{ value: 'templatedRegion', text: 'templatedRegion', selected: true }],
|
|
|
|
|
multi: false,
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-19 08:50:41 +02:00
|
|
|
export const fieldsVariable: CustomVariableModel = {
|
2022-06-01 10:23:31 -07:00
|
|
|
...initialCustomVariableModelState,
|
|
|
|
|
id: 'fields',
|
|
|
|
|
name: 'fields',
|
|
|
|
|
current: {
|
|
|
|
|
value: 'templatedField',
|
|
|
|
|
text: 'templatedField',
|
|
|
|
|
selected: true,
|
|
|
|
|
},
|
|
|
|
|
options: [{ value: 'templatedField', text: 'templatedField', selected: true }],
|
|
|
|
|
multi: false,
|
|
|
|
|
};
|
2022-09-19 08:50:41 +02:00
|
|
|
|
|
|
|
|
export const periodIntervalVariable: CustomVariableModel = {
|
|
|
|
|
...initialCustomVariableModelState,
|
|
|
|
|
id: 'period',
|
|
|
|
|
name: 'period',
|
|
|
|
|
index: 0,
|
|
|
|
|
current: { value: '10m', text: '10m', selected: true },
|
|
|
|
|
options: [{ value: '10m', text: '10m', selected: true }],
|
|
|
|
|
multi: false,
|
|
|
|
|
includeAll: false,
|
|
|
|
|
query: '',
|
|
|
|
|
hide: VariableHide.dontHide,
|
|
|
|
|
type: 'custom',
|
|
|
|
|
};
|
2022-11-28 12:39:12 +01:00
|
|
|
|
|
|
|
|
export const accountIdVariable: CustomVariableModel = {
|
|
|
|
|
...initialCustomVariableModelState,
|
|
|
|
|
id: 'accountId',
|
|
|
|
|
name: 'accountId',
|
|
|
|
|
current: {
|
|
|
|
|
value: 'templatedaccountId',
|
|
|
|
|
text: 'templatedaccountId',
|
|
|
|
|
selected: true,
|
|
|
|
|
},
|
|
|
|
|
options: [{ value: 'templatedRegion', text: 'templatedRegion', selected: true }],
|
|
|
|
|
multi: false,
|
|
|
|
|
};
|