@grafana/data#dateMath TypeScript Examples
The following examples show how to use
@grafana/data#dateMath.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DashNavTimeControls.tsx From grafana-chinese with Apache License 2.0 | 6 votes |
onChangeTimePicker = (timeRange: TimeRange) => {
const { dashboard } = this.props;
const panel = dashboard.timepicker;
const hasDelay = panel.nowDelay && timeRange.raw.to === 'now';
const adjustedFrom = dateMath.isMathString(timeRange.raw.from) ? timeRange.raw.from : timeRange.from;
const adjustedTo = dateMath.isMathString(timeRange.raw.to) ? timeRange.raw.to : timeRange.to;
const nextRange = {
from: adjustedFrom,
to: hasDelay ? 'now-' + panel.nowDelay : adjustedTo,
};
getTimeSrv().setTime(nextRange);
};
Example #2
Source File: datasource.ts From grafana-chinese with Apache License 2.0 | 6 votes |
translateTime(date: any, roundUp: any, timezone: any) {
if (_.isString(date)) {
if (date === 'now') {
return 'now';
} else if (date.indexOf('now-') >= 0 && date.indexOf('/') === -1) {
date = date.substring(3);
date = date.replace('m', 'min');
date = date.replace('M', 'mon');
return date;
}
date = dateMath.parse(date, roundUp, timezone);
}
// graphite' s from filter is exclusive
// here we step back one minute in order
// to guarantee that we get all the data that
// exists for the specified range
if (roundUp) {
if (date.get('s')) {
date.add(1, 's');
}
} else if (roundUp === false) {
if (date.get('s')) {
date.subtract(1, 's');
}
}
return date.unix();
}
Example #3
Source File: datasource.ts From grafana-chinese with Apache License 2.0 | 6 votes |
getInfluxTime(date: any, roundUp: any, timezone: any) {
if (_.isString(date)) {
if (date === 'now') {
return 'now()';
}
const parts = /^now-(\d+)([dhms])$/.exec(date);
if (parts) {
const amount = parseInt(parts[1], 10);
const unit = parts[2];
return 'now() - ' + amount + unit;
}
date = dateMath.parse(date, roundUp, timezone);
}
return date.valueOf() + 'ms';
}
Example #4
Source File: TimeSrv.ts From grafana-chinese with Apache License 2.0 | 6 votes |
timeRange(): TimeRange {
// make copies if they are moment (do not want to return out internal moment, because they are mutable!)
const raw = {
from: isDateTime(this.time.from) ? dateTime(this.time.from) : this.time.from,
to: isDateTime(this.time.to) ? dateTime(this.time.to) : this.time.to,
};
const timezone: TimeZone = this.dashboard ? this.dashboard.getTimezone() : undefined;
return {
from: dateMath.parse(raw.from, false, timezone),
to: dateMath.parse(raw.to, true, timezone),
raw: raw,
};
}
Example #5
Source File: DashboardLoaderSrv.ts From grafana-chinese with Apache License 2.0 | 6 votes |
_executeScript(result: any) {
const services = {
dashboardSrv: this.dashboardSrv,
datasourceSrv: this.datasourceSrv,
};
const scriptFunc = new Function(
'ARGS',
'kbn',
'dateMath',
'_',
'moment',
'window',
'document',
'$',
'jQuery',
'services',
result.data
);
const scriptResult = scriptFunc(this.$routeParams, kbn, dateMath, _, moment, window, document, $, $, services);
// Handle async dashboard scripts
if (_.isFunction(scriptResult)) {
return new Promise(resolve => {
scriptResult((dashboard: any) => {
this.$timeout(() => {
resolve({ data: dashboard });
});
});
});
}
return { data: scriptResult };
}
Example #6
Source File: explore.ts From grafana-chinese with Apache License 2.0 | 6 votes |
getTimeRangeFromUrl = (range: RawTimeRange, timeZone: TimeZone): TimeRange => {
const raw = {
from: parseRawTime(range.from),
to: parseRawTime(range.to),
};
return {
from: dateMath.parse(raw.from, false, timeZone as any),
to: dateMath.parse(raw.to, true, timeZone as any),
raw,
};
}
Example #7
Source File: module.ts From grafana-chinese with Apache License 2.0 | 6 votes |
getStateChanges() {
const params: any = {
limit: this.panel.limit,
type: 'alert',
newState: this.panel.stateFilter,
};
if (this.panel.onlyAlertsOnDashboard) {
params.dashboardId = this.dashboard.id;
}
params.from = dateMath.parse(this.dashboard.time.from).unix() * 1000;
params.to = dateMath.parse(this.dashboard.time.to).unix() * 1000;
return promiseToDigest(this.$scope)(
getBackendSrv()
.get('/api/annotations', params, `alert-list-get-state-changes-${this.panel.id}`)
.then(data => {
this.alertHistory = _.map(data, al => {
al.time = this.dashboard.formatDate(al.time, 'MMM D, YYYY HH:mm:ss');
al.stateModel = alertDef.getStateDisplayModel(al.newState);
al.info = alertDef.getAlertAnnotationInfo(al);
return al;
});
this.noAlertsMessage = this.alertHistory.length === 0 ? 'No alerts in current time range' : '';
return this.alertHistory;
})
);
}
Example #8
Source File: time.ts From grafana-chinese with Apache License 2.0 | 6 votes |
stringToDateTimeType = (value: string | DateTime, roundUp?: boolean, timeZone?: TimeZone): DateTime => {
if (isDateTime(value)) {
return value;
}
if (value.indexOf('now') !== -1) {
if (!dateMath.isValid(value)) {
return dateTime();
}
const parsed = dateMath.parse(value, roundUp, timeZone);
return parsed || dateTime();
}
return dateTimeForTimeZone(timeZone, value, TIME_FORMAT);
}
Example #9
Source File: mapper.ts From grafana-chinese with Apache License 2.0 | 6 votes |
stringToDateTime = (value: string | DateTime, roundUp?: boolean, timeZone?: TimeZone): DateTime => {
if (isDateTime(value)) {
if (timeZone === 'utc') {
return value.utc();
}
return value;
}
if (value.indexOf('now') !== -1) {
if (!dateMath.isValid(value)) {
return dateTime();
}
const parsed = dateMath.parse(value, roundUp, timeZone);
return parsed || dateTime();
}
return dateTimeForTimeZone(timeZone, value, TIME_FORMAT);
}
Example #10
Source File: mapper.ts From grafana-chinese with Apache License 2.0 | 6 votes |
mapStringsToTimeRange = (from: string, to: string, roundup?: boolean, timeZone?: TimeZone): TimeRange => {
const fromDate = stringToDateTimeType(from, roundup, timeZone);
const toDate = stringToDateTimeType(to, roundup, timeZone);
if (dateMath.isMathString(from) || dateMath.isMathString(to)) {
return {
from: fromDate,
to: toDate,
raw: {
from,
to,
},
};
}
return {
from: fromDate,
to: toDate,
raw: {
from: fromDate,
to: toDate,
},
};
}
Example #11
Source File: helpers.ts From grafana-chinese with Apache License 2.0 | 6 votes |
timeRange(parse: boolean) {
if (parse === false) {
return this.time;
}
return {
from: dateMath.parse(this.time.from, false),
to: dateMath.parse(this.time.to, true),
};
}
Example #12
Source File: BarGaugePanel.test.tsx From grafana-chinese with Apache License 2.0 | 5 votes |
function createTimeRange(): TimeRange {
return {
from: dateMath.parse('now-6h') || dateTime(),
to: dateMath.parse('now') || dateTime(),
raw: { from: 'now-6h', to: 'now' },
};
}
Example #13
Source File: plugin_loader.ts From grafana-chinese with Apache License 2.0 | 5 votes |
exposeToPlugin('app/core/utils/datemath', dateMath);
Example #14
Source File: query_ctrl.ts From grafana-chinese with Apache License 2.0 | 5 votes |
addPoint() {
this.target.points = this.target.points || [];
this.newPointTime = dateMath.parse(this.newPointTime);
this.target.points.push([this.newPointValue, this.newPointTime.valueOf()]);
this.target.points = _.sortBy(this.target.points, p => p[1]);
this.refresh();
}
Example #15
Source File: datasource.ts From grafana-chinese with Apache License 2.0 | 5 votes |
getPrometheusTime(date: string | DateTime, roundUp: boolean) {
if (typeof date === 'string') {
date = dateMath.parse(date, roundUp);
}
return Math.ceil(date.valueOf() / 1000);
}
Example #16
Source File: datasource.ts From grafana-chinese with Apache License 2.0 | 5 votes |
convertToTSDBTime(date: any, roundUp: any, timezone: any) {
if (date === 'now') {
return null;
}
date = dateMath.parse(date, roundUp, timezone);
return date.valueOf();
}
Example #17
Source File: datasource.ts From grafana-chinese with Apache License 2.0 | 5 votes |
getTime(date: string | DateTime, roundUp: boolean) {
if (typeof date === 'string') {
date = dateMath.parse(date, roundUp);
}
return Math.ceil(date.valueOf() * 1e6);
}
Example #18
Source File: datasource.ts From grafana-chinese with Apache License 2.0 | 5 votes |
convertToCloudWatchTime(date: any, roundUp: any) {
if (_.isString(date)) {
date = dateMath.parse(date, roundUp);
}
return Math.round(date.valueOf() / 1000);
}
Example #19
Source File: TimePicker.tsx From grafana-chinese with Apache License 2.0 | 5 votes |
formattedRange = (value: TimeRange, isUTC: boolean) => {
const adjustedTimeRange = {
to: dateMath.isMathString(value.raw.to) ? value.raw.to : adjustedTime(value.to, isUTC),
from: dateMath.isMathString(value.raw.from) ? value.raw.from : adjustedTime(value.from, isUTC),
};
return rangeUtil.describeTimeRange(adjustedTimeRange);
}
Example #20
Source File: panel.ts From grafana-chinese with Apache License 2.0 | 5 votes |
export function applyPanelTimeOverrides(panel: PanelModel, timeRange: TimeRange): TimeOverrideResult {
const newTimeData = {
timeInfo: '',
timeRange: timeRange,
};
if (panel.timeFrom) {
const timeFromInterpolated = templateSrv.replace(panel.timeFrom, panel.scopedVars);
const timeFromInfo = rangeUtil.describeTextRange(timeFromInterpolated);
if (timeFromInfo.invalid) {
newTimeData.timeInfo = 'invalid time override';
return newTimeData;
}
if (_isString(timeRange.raw.from)) {
const timeFromDate = dateMath.parse(timeFromInfo.from);
newTimeData.timeInfo = timeFromInfo.display;
newTimeData.timeRange = {
from: timeFromDate,
to: dateMath.parse(timeFromInfo.to),
raw: {
from: timeFromInfo.from,
to: timeFromInfo.to,
},
};
}
}
if (panel.timeShift) {
const timeShiftInterpolated = templateSrv.replace(panel.timeShift, panel.scopedVars);
const timeShiftInfo = rangeUtil.describeTextRange(timeShiftInterpolated);
if (timeShiftInfo.invalid) {
newTimeData.timeInfo = 'invalid timeshift';
return newTimeData;
}
const timeShift = '-' + timeShiftInterpolated;
newTimeData.timeInfo += ' timeshift ' + timeShift;
const from = dateMath.parseDateMath(timeShift, newTimeData.timeRange.from, false);
const to = dateMath.parseDateMath(timeShift, newTimeData.timeRange.to, true);
newTimeData.timeRange = {
from,
to,
raw: {
from,
to,
},
};
}
if (panel.hideTimeOverride) {
newTimeData.timeInfo = '';
}
return newTimeData;
}
Example #21
Source File: runRequest.ts From grafana-chinese with Apache License 2.0 | 5 votes |
/*
* This function should handle composing a PanelData from multiple responses
*/
export function processResponsePacket(packet: DataQueryResponse, state: RunningQueryState): RunningQueryState {
const request = state.panelData.request;
const packets: MapOfResponsePackets = {
...state.packets,
};
packets[packet.key || 'A'] = packet;
let loadingState = packet.state || LoadingState.Done;
let error: DataQueryError | undefined = undefined;
// Update the time range
const range = { ...request.range };
const timeRange = isString(range.raw.from)
? {
from: dateMath.parse(range.raw.from, false),
to: dateMath.parse(range.raw.to, true),
raw: range.raw,
}
: range;
const combinedData = flatten(
lodashMap(packets, (packet: DataQueryResponse) => {
if (packet.error) {
loadingState = LoadingState.Error;
error = packet.error;
}
return packet.data;
})
);
const panelData = {
state: loadingState,
series: combinedData,
error,
request,
timeRange,
};
return { packets, panelData };
}
Example #22
Source File: explore.ts From grafana-chinese with Apache License 2.0 | 5 votes |
getTimeRange = (timeZone: TimeZone, rawRange: RawTimeRange): TimeRange => {
return {
from: dateMath.parse(rawRange.from, false, timeZone as any),
to: dateMath.parse(rawRange.to, true, timeZone as any),
raw: rawRange,
};
}
Example #23
Source File: time.ts From grafana-chinese with Apache License 2.0 | 5 votes |
isValidTimeString = (text: string) => dateMath.isValid(text)
Example #24
Source File: TimeRangeForm.tsx From grafana-chinese with Apache License 2.0 | 5 votes |
function isValid(value: string, roundup?: boolean, timeZone?: TimeZone): boolean {
if (dateMath.isMathString(value)) {
return isValidTimeString(value);
}
const parsed = stringToDateTimeType(value, roundup, timeZone);
return parsed.isValid();
}
Example #25
Source File: datasource.test.ts From grafana-chinese with Apache License 2.0 | 4 votes |
describe('ElasticDatasource', function(this: any) {
const datasourceRequestMock = jest.spyOn(backendSrv, 'datasourceRequest');
beforeEach(() => {
jest.clearAllMocks();
});
const $rootScope = {
$on: jest.fn(),
appEvent: jest.fn(),
};
const templateSrv: any = {
replace: jest.fn(text => {
if (text.startsWith('$')) {
return `resolvedVariable`;
} else {
return text;
}
}),
getAdhocFilters: jest.fn(() => []),
};
const timeSrv: any = createTimeSrv('now-1h');
const ctx = {
$rootScope,
} as any;
function createTimeSrv(from: string) {
const srv: any = {
time: { from: from, to: 'now' },
};
srv.timeRange = jest.fn(() => {
return {
from: dateMath.parse(srv.time.from, false),
to: dateMath.parse(srv.time.to, true),
};
});
srv.setTime = jest.fn(time => {
srv.time = time;
});
return srv;
}
function createDatasource(instanceSettings: DataSourceInstanceSettings<ElasticsearchOptions>) {
createDatasourceWithTime(instanceSettings, timeSrv as TimeSrv);
}
function createDatasourceWithTime(
instanceSettings: DataSourceInstanceSettings<ElasticsearchOptions>,
timeSrv: TimeSrv
) {
instanceSettings.jsonData = instanceSettings.jsonData || ({} as ElasticsearchOptions);
ctx.ds = new ElasticDatasource(instanceSettings, templateSrv as TemplateSrv, timeSrv);
}
describe('When testing datasource with index pattern', () => {
beforeEach(() => {
createDatasource({
url: 'http://es.com',
database: '[asd-]YYYY.MM.DD',
jsonData: { interval: 'Daily', esVersion: 2 } as ElasticsearchOptions,
} as DataSourceInstanceSettings<ElasticsearchOptions>);
});
it('should translate index pattern to current day', () => {
let requestOptions: any;
datasourceRequestMock.mockImplementation(options => {
requestOptions = options;
return Promise.resolve({ data: {} });
});
ctx.ds.testDatasource();
const today = toUtc().format('YYYY.MM.DD');
expect(requestOptions.url).toBe('http://es.com/asd-' + today + '/_mapping');
});
});
describe('When issuing metric query with interval pattern', () => {
let requestOptions: any, parts: any, header: any, query: any, result: any;
beforeEach(async () => {
createDatasource({
url: 'http://es.com',
database: '[asd-]YYYY.MM.DD',
jsonData: { interval: 'Daily', esVersion: 2 } as ElasticsearchOptions,
} as DataSourceInstanceSettings<ElasticsearchOptions>);
datasourceRequestMock.mockImplementation(options => {
requestOptions = options;
return Promise.resolve({
data: {
responses: [
{
aggregations: {
'1': {
buckets: [
{
doc_count: 10,
key: 1000,
},
],
},
},
},
],
},
});
});
query = {
range: {
from: toUtc([2015, 4, 30, 10]),
to: toUtc([2015, 5, 1, 10]),
},
targets: [
{
alias: '$varAlias',
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '1' }],
metrics: [{ type: 'count', id: '1' }],
query: 'escape\\:test',
},
],
};
result = await ctx.ds.query(query);
parts = requestOptions.data.split('\n');
header = angular.fromJson(parts[0]);
});
it('should translate index pattern to current day', () => {
expect(header.index).toEqual(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
});
it('should not resolve the variable in the original alias field in the query', () => {
expect(query.targets[0].alias).toEqual('$varAlias');
});
it('should resolve the alias variable for the alias/target in the result', () => {
expect(result.data[0].target).toEqual('resolvedVariable');
});
it('should json escape lucene query', () => {
const body = angular.fromJson(parts[1]);
expect(body.query.bool.filter[1].query_string.query).toBe('escape\\:test');
});
});
describe('When issuing logs query with interval pattern', () => {
async function setupDataSource(jsonData?: Partial<ElasticsearchOptions>) {
createDatasource({
url: 'http://es.com',
database: 'mock-index',
jsonData: {
interval: 'Daily',
esVersion: 2,
timeField: '@timestamp',
...(jsonData || {}),
} as ElasticsearchOptions,
} as DataSourceInstanceSettings<ElasticsearchOptions>);
datasourceRequestMock.mockImplementation(options => {
return Promise.resolve(logsResponse);
});
const query = {
range: {
from: toUtc([2015, 4, 30, 10]),
to: toUtc([2019, 7, 1, 10]),
},
targets: [
{
alias: '$varAlias',
refId: 'A',
bucketAggs: [{ type: 'date_histogram', settings: { interval: 'auto' }, id: '2' }],
metrics: [{ type: 'count', id: '1' }],
query: 'escape\\:test',
interval: '10s',
isLogsQuery: true,
timeField: '@timestamp',
},
],
};
const queryBuilderSpy = jest.spyOn(ctx.ds.queryBuilder, 'getLogsQuery');
const response = await ctx.ds.query(query);
return { queryBuilderSpy, response };
}
it('should call getLogsQuery()', async () => {
const { queryBuilderSpy } = await setupDataSource();
expect(queryBuilderSpy).toHaveBeenCalled();
});
it('should enhance fields with links', async () => {
const { response } = await setupDataSource({
dataLinks: [
{
field: 'host',
url: 'http://localhost:3000/${__value.raw}',
},
],
});
// 1 for logs and 1 for counts.
expect(response.data.length).toBe(2);
const links = response.data[0].fields.find((field: Field) => field.name === 'host').config.links;
expect(links.length).toBe(1);
expect(links[0].url).toBe('http://localhost:3000/${__value.raw}');
});
});
describe('When issuing document query', () => {
let requestOptions: any, parts: any, header: any;
beforeEach(() => {
createDatasource({
url: 'http://es.com',
database: 'test',
jsonData: { esVersion: 2 } as ElasticsearchOptions,
} as DataSourceInstanceSettings<ElasticsearchOptions>);
datasourceRequestMock.mockImplementation(options => {
requestOptions = options;
return Promise.resolve({ data: { responses: [] } });
});
ctx.ds.query({
range: {
from: dateTime([2015, 4, 30, 10]),
to: dateTime([2015, 5, 1, 10]),
},
targets: [
{
bucketAggs: [],
metrics: [{ type: 'raw_document' }],
query: 'test',
},
],
});
parts = requestOptions.data.split('\n');
header = angular.fromJson(parts[0]);
});
it('should set search type to query_then_fetch', () => {
expect(header.search_type).toEqual('query_then_fetch');
});
it('should set size', () => {
const body = angular.fromJson(parts[1]);
expect(body.size).toBe(500);
});
});
describe('When getting fields', () => {
beforeEach(() => {
createDatasource({
url: 'http://es.com',
database: 'metricbeat',
jsonData: { esVersion: 50 } as ElasticsearchOptions,
} as DataSourceInstanceSettings<ElasticsearchOptions>);
datasourceRequestMock.mockImplementation(options => {
return Promise.resolve({
data: {
metricbeat: {
mappings: {
metricsets: {
_all: {},
properties: {
'@timestamp': { type: 'date' },
beat: {
properties: {
name: {
fields: { raw: { type: 'keyword' } },
type: 'string',
},
hostname: { type: 'string' },
},
},
system: {
properties: {
cpu: {
properties: {
system: { type: 'float' },
user: { type: 'float' },
},
},
process: {
properties: {
cpu: {
properties: {
total: { type: 'float' },
},
},
name: { type: 'string' },
},
},
},
},
},
},
},
},
},
});
});
});
it('should return nested fields', async () => {
const fieldObjects = await ctx.ds.getFields({
find: 'fields',
query: '*',
});
const fields = _.map(fieldObjects, 'text');
expect(fields).toEqual([
'@timestamp',
'beat.name.raw',
'beat.name',
'beat.hostname',
'system.cpu.system',
'system.cpu.user',
'system.process.cpu.total',
'system.process.name',
]);
});
it('should return number fields', async () => {
const fieldObjects = await ctx.ds.getFields({
find: 'fields',
query: '*',
type: 'number',
});
const fields = _.map(fieldObjects, 'text');
expect(fields).toEqual(['system.cpu.system', 'system.cpu.user', 'system.process.cpu.total']);
});
it('should return date fields', async () => {
const fieldObjects = await ctx.ds.getFields({
find: 'fields',
query: '*',
type: 'date',
});
const fields = _.map(fieldObjects, 'text');
expect(fields).toEqual(['@timestamp']);
});
});
describe('When getting field mappings on indices with gaps', () => {
const twoWeekTimeSrv: any = createTimeSrv('now-2w');
const basicResponse = {
data: {
metricbeat: {
mappings: {
metricsets: {
_all: {},
properties: {
'@timestamp': { type: 'date' },
beat: {
properties: {
hostname: { type: 'string' },
},
},
},
},
},
},
},
};
const alternateResponse = {
data: {
metricbeat: {
mappings: {
metricsets: {
_all: {},
properties: {
'@timestamp': { type: 'date' },
},
},
},
},
},
};
beforeEach(() => {
createDatasourceWithTime(
{
url: 'http://es.com',
database: '[asd-]YYYY.MM.DD',
jsonData: { interval: 'Daily', esVersion: 50 } as ElasticsearchOptions,
} as DataSourceInstanceSettings<ElasticsearchOptions>,
twoWeekTimeSrv
);
});
it('should return fields of the newest available index', async () => {
const twoDaysBefore = toUtc()
.subtract(2, 'day')
.format('YYYY.MM.DD');
const threeDaysBefore = toUtc()
.subtract(3, 'day')
.format('YYYY.MM.DD');
datasourceRequestMock.mockImplementation(options => {
if (options.url === `http://es.com/asd-${twoDaysBefore}/_mapping`) {
return Promise.resolve(basicResponse);
} else if (options.url === `http://es.com/asd-${threeDaysBefore}/_mapping`) {
return Promise.resolve(alternateResponse);
}
return Promise.reject({ status: 404 });
});
const fieldObjects = await ctx.ds.getFields({
find: 'fields',
query: '*',
});
const fields = _.map(fieldObjects, 'text');
expect(fields).toEqual(['@timestamp', 'beat.hostname']);
});
it('should not retry when ES is down', async () => {
const twoDaysBefore = toUtc()
.subtract(2, 'day')
.format('YYYY.MM.DD');
datasourceRequestMock.mockImplementation(options => {
if (options.url === `http://es.com/asd-${twoDaysBefore}/_mapping`) {
return Promise.resolve(basicResponse);
}
return Promise.reject({ status: 500 });
});
expect.assertions(2);
try {
await ctx.ds.getFields({
find: 'fields',
query: '*',
});
} catch (e) {
expect(e).toStrictEqual({ status: 500 });
expect(datasourceRequestMock).toBeCalledTimes(1);
}
});
it('should not retry more than 7 indices', async () => {
datasourceRequestMock.mockImplementation(() => {
return Promise.reject({ status: 404 });
});
expect.assertions(2);
try {
await ctx.ds.getFields({
find: 'fields',
query: '*',
});
} catch (e) {
expect(e).toStrictEqual({ status: 404 });
expect(datasourceRequestMock).toBeCalledTimes(7);
}
});
});
describe('When getting fields from ES 7.0', () => {
beforeEach(() => {
createDatasource({
url: 'http://es.com',
database: 'genuine.es7._mapping.response',
jsonData: { esVersion: 70 } as ElasticsearchOptions,
} as DataSourceInstanceSettings<ElasticsearchOptions>);
datasourceRequestMock.mockImplementation(options => {
return Promise.resolve({
data: {
'genuine.es7._mapping.response': {
mappings: {
properties: {
'@timestamp_millis': {
type: 'date',
format: 'epoch_millis',
},
classification_terms: {
type: 'keyword',
},
domains: {
type: 'keyword',
},
ip_address: {
type: 'ip',
},
justification_blob: {
properties: {
criterion: {
type: 'text',
fields: {
keyword: {
type: 'keyword',
ignore_above: 256,
},
},
},
overall_vote_score: {
type: 'float',
},
shallow: {
properties: {
jsi: {
properties: {
sdb: {
properties: {
dsel2: {
properties: {
'bootlegged-gille': {
properties: {
botness: {
type: 'float',
},
general_algorithm_score: {
type: 'float',
},
},
},
'uncombed-boris': {
properties: {
botness: {
type: 'float',
},
general_algorithm_score: {
type: 'float',
},
},
},
},
},
},
},
},
},
},
},
},
},
overall_vote_score: {
type: 'float',
},
ua_terms_long: {
type: 'keyword',
},
ua_terms_short: {
type: 'keyword',
},
},
},
},
},
});
});
});
it('should return nested fields', async () => {
const fieldObjects = await ctx.ds.getFields({
find: 'fields',
query: '*',
});
const fields = _.map(fieldObjects, 'text');
expect(fields).toEqual([
'@timestamp_millis',
'classification_terms',
'domains',
'ip_address',
'justification_blob.criterion.keyword',
'justification_blob.criterion',
'justification_blob.overall_vote_score',
'justification_blob.shallow.jsi.sdb.dsel2.bootlegged-gille.botness',
'justification_blob.shallow.jsi.sdb.dsel2.bootlegged-gille.general_algorithm_score',
'justification_blob.shallow.jsi.sdb.dsel2.uncombed-boris.botness',
'justification_blob.shallow.jsi.sdb.dsel2.uncombed-boris.general_algorithm_score',
'overall_vote_score',
'ua_terms_long',
'ua_terms_short',
]);
});
it('should return number fields', async () => {
const fieldObjects = await ctx.ds.getFields({
find: 'fields',
query: '*',
type: 'number',
});
const fields = _.map(fieldObjects, 'text');
expect(fields).toEqual([
'justification_blob.overall_vote_score',
'justification_blob.shallow.jsi.sdb.dsel2.bootlegged-gille.botness',
'justification_blob.shallow.jsi.sdb.dsel2.bootlegged-gille.general_algorithm_score',
'justification_blob.shallow.jsi.sdb.dsel2.uncombed-boris.botness',
'justification_blob.shallow.jsi.sdb.dsel2.uncombed-boris.general_algorithm_score',
'overall_vote_score',
]);
});
it('should return date fields', async () => {
const fieldObjects = await ctx.ds.getFields({
find: 'fields',
query: '*',
type: 'date',
});
const fields = _.map(fieldObjects, 'text');
expect(fields).toEqual(['@timestamp_millis']);
});
});
describe('When issuing aggregation query on es5.x', () => {
let requestOptions: any, parts: any, header: any;
beforeEach(() => {
createDatasource({
url: 'http://es.com',
database: 'test',
jsonData: { esVersion: 5 } as ElasticsearchOptions,
} as DataSourceInstanceSettings<ElasticsearchOptions>);
datasourceRequestMock.mockImplementation(options => {
requestOptions = options;
return Promise.resolve({ data: { responses: [] } });
});
ctx.ds.query({
range: {
from: dateTime([2015, 4, 30, 10]),
to: dateTime([2015, 5, 1, 10]),
},
targets: [
{
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '2' }],
metrics: [{ type: 'count' }],
query: 'test',
},
],
});
parts = requestOptions.data.split('\n');
header = angular.fromJson(parts[0]);
});
it('should not set search type to count', () => {
expect(header.search_type).not.toEqual('count');
});
it('should set size to 0', () => {
const body = angular.fromJson(parts[1]);
expect(body.size).toBe(0);
});
});
describe('When issuing metricFind query on es5.x', () => {
let requestOptions: any, parts, header: any, body: any, results: any;
beforeEach(() => {
createDatasource({
url: 'http://es.com',
database: 'test',
jsonData: { esVersion: 5 } as ElasticsearchOptions,
} as DataSourceInstanceSettings<ElasticsearchOptions>);
datasourceRequestMock.mockImplementation(options => {
requestOptions = options;
return Promise.resolve({
data: {
responses: [
{
aggregations: {
'1': {
buckets: [
{ doc_count: 1, key: 'test' },
{
doc_count: 2,
key: 'test2',
key_as_string: 'test2_as_string',
},
],
},
},
},
],
},
});
});
ctx.ds.metricFindQuery('{"find": "terms", "field": "test"}').then((res: any) => {
results = res;
});
parts = requestOptions.data.split('\n');
header = angular.fromJson(parts[0]);
body = angular.fromJson(parts[1]);
});
it('should get results', () => {
expect(results.length).toEqual(2);
});
it('should use key or key_as_string', () => {
expect(results[0].text).toEqual('test');
expect(results[1].text).toEqual('test2_as_string');
});
it('should not set search type to count', () => {
expect(header.search_type).not.toEqual('count');
});
it('should set size to 0', () => {
expect(body.size).toBe(0);
});
it('should not set terms aggregation size to 0', () => {
expect(body['aggs']['1']['terms'].size).not.toBe(0);
});
});
describe('query', () => {
it('should replace range as integer not string', () => {
const dataSource = new ElasticDatasource(
{
url: 'http://es.com',
database: '[asd-]YYYY.MM.DD',
jsonData: {
interval: 'Daily',
esVersion: 2,
timeField: '@time',
},
} as DataSourceInstanceSettings<ElasticsearchOptions>,
templateSrv as TemplateSrv,
timeSrv as TimeSrv
);
(dataSource as any).post = jest.fn(() => Promise.resolve({ responses: [] }));
dataSource.query(createElasticQuery());
const query = ((dataSource as any).post as jest.Mock).mock.calls[0][1];
expect(typeof JSON.parse(query.split('\n')[1]).query.bool.filter[0].range['@time'].gte).toBe('number');
});
});
});
Example #26
Source File: datasource.test.ts From grafana-chinese with Apache License 2.0 | 4 votes |
describe('CloudWatchDatasource', () => {
const datasourceRequestMock = jest.spyOn(backendSrv, 'datasourceRequest');
const instanceSettings = {
jsonData: { defaultRegion: 'us-east-1' },
name: 'TestDatasource',
} as DataSourceInstanceSettings;
const templateSrv = new TemplateSrv();
const start = 1483196400 * 1000;
const defaultTimeRange = { from: new Date(start), to: new Date(start + 3600 * 1000) };
const timeSrv = {
time: { from: 'now-1h', to: 'now' },
timeRange: () => {
return {
from: dateMath.parse(timeSrv.time.from, false),
to: dateMath.parse(timeSrv.time.to, true),
};
},
} as TimeSrv;
const ctx = {
templateSrv,
} as any;
beforeEach(() => {
ctx.ds = new CloudWatchDatasource(instanceSettings, templateSrv, timeSrv);
jest.clearAllMocks();
});
describe('When performing CloudWatch query', () => {
let requestParams: { queries: CloudWatchQuery[] };
const query = {
range: defaultTimeRange,
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
expression: '',
refId: 'A',
region: 'us-east-1',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: {
InstanceId: 'i-12345678',
},
statistics: ['Average'],
period: '300',
},
],
};
const response: any = {
timings: [null],
results: {
A: {
error: '',
refId: 'A',
meta: { gmdMeta: [] },
series: [
{
name: 'CPUUtilization_Average',
points: [
[1, 1483228800000],
[2, 1483229100000],
[5, 1483229700000],
],
tags: {
InstanceId: 'i-12345678',
},
},
],
},
},
};
beforeEach(() => {
datasourceRequestMock.mockImplementation(params => {
requestParams = params.data;
return Promise.resolve({ data: response });
});
});
it('should generate the correct query', done => {
ctx.ds.query(query).then(() => {
const params = requestParams.queries[0];
expect(params.namespace).toBe(query.targets[0].namespace);
expect(params.metricName).toBe(query.targets[0].metricName);
expect(params.dimensions['InstanceId']).toStrictEqual(['i-12345678']);
expect(params.statistics).toEqual(query.targets[0].statistics);
expect(params.period).toBe(query.targets[0].period);
done();
});
});
it('should generate the correct query with interval variable', done => {
templateSrv.init([
new CustomVariable(
{
name: 'period',
current: {
value: '10m',
},
multi: false,
},
{} as any
),
]);
const query = {
range: defaultTimeRange,
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
refId: 'A',
region: 'us-east-1',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: {
InstanceId: 'i-12345678',
},
statistics: ['Average'],
period: '[[period]]',
},
],
};
ctx.ds.query(query).then(() => {
const params = requestParams.queries[0];
expect(params.period).toBe('600');
done();
});
});
it.each(['pNN.NN', 'p9', 'p99.', 'p99.999'])('should cancel query for invalid extended statistics (%s)', stat => {
const query = {
range: defaultTimeRange,
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
refId: 'A',
region: 'us-east-1',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: {
InstanceId: 'i-12345678',
},
statistics: [stat],
period: '60s',
},
],
};
expect(ctx.ds.query.bind(ctx.ds, query)).toThrow(/Invalid extended statistics/);
});
it('should return series list', done => {
ctx.ds.query(query).then((result: any) => {
expect(result.data[0].name).toBe(response.results.A.series[0].name);
expect(result.data[0].fields[0].values.buffer[0]).toBe(response.results.A.series[0].points[0][0]);
done();
});
});
describe('a correct cloudwatch url should be built for each time series in the response', () => {
beforeEach(() => {
datasourceRequestMock.mockImplementation(params => {
requestParams = params.data;
return Promise.resolve({ data: response });
});
});
it('should be built correctly if theres one search expressions returned in meta for a given query row', done => {
response.results['A'].meta.gmdMeta = [{ Expression: `REMOVE_EMPTY(SEARCH('some expression'))`, Period: '300' }];
ctx.ds.query(query).then((result: any) => {
expect(result.data[0].name).toBe(response.results.A.series[0].name);
expect(result.data[0].fields[0].config.links[0].title).toBe('View in CloudWatch console');
expect(decodeURIComponent(result.data[0].fields[0].config.links[0].url)).toContain(
`region=us-east-1#metricsV2:graph={"view":"timeSeries","stacked":false,"title":"A","start":"2016-12-31T15:00:00.000Z","end":"2016-12-31T16:00:00.000Z","region":"us-east-1","metrics":[{"expression":"REMOVE_EMPTY(SEARCH(\'some expression\'))"}]}`
);
done();
});
});
it('should be built correctly if theres two search expressions returned in meta for a given query row', done => {
response.results['A'].meta.gmdMeta = [
{ Expression: `REMOVE_EMPTY(SEARCH('first expression'))` },
{ Expression: `REMOVE_EMPTY(SEARCH('second expression'))` },
];
ctx.ds.query(query).then((result: any) => {
expect(result.data[0].name).toBe(response.results.A.series[0].name);
expect(result.data[0].fields[0].config.links[0].title).toBe('View in CloudWatch console');
expect(decodeURIComponent(result.data[0].fields[0].config.links[0].url)).toContain(
`region=us-east-1#metricsV2:graph={"view":"timeSeries","stacked":false,"title":"A","start":"2016-12-31T15:00:00.000Z","end":"2016-12-31T16:00:00.000Z","region":"us-east-1","metrics":[{"expression":"REMOVE_EMPTY(SEARCH(\'first expression\'))"},{"expression":"REMOVE_EMPTY(SEARCH(\'second expression\'))"}]}`
);
done();
});
});
it('should be built correctly if the query is a metric stat query', done => {
response.results['A'].meta.gmdMeta = [{ Period: '300' }];
ctx.ds.query(query).then((result: any) => {
expect(result.data[0].name).toBe(response.results.A.series[0].name);
expect(result.data[0].fields[0].config.links[0].title).toBe('View in CloudWatch console');
expect(decodeURIComponent(result.data[0].fields[0].config.links[0].url)).toContain(
`region=us-east-1#metricsV2:graph={\"view\":\"timeSeries\",\"stacked\":false,\"title\":\"A\",\"start\":\"2016-12-31T15:00:00.000Z\",\"end\":\"2016-12-31T16:00:00.000Z\",\"region\":\"us-east-1\",\"metrics\":[[\"AWS/EC2\",\"CPUUtilization\",\"InstanceId\",\"i-12345678\",{\"stat\":\"Average\",\"period\":\"300\"}]]}`
);
done();
});
});
it('should not be added at all if query is a math expression', done => {
query.targets[0].expression = 'a * 2';
response.results['A'].meta.searchExpressions = [];
ctx.ds.query(query).then((result: any) => {
expect(result.data[0].fields[0].config.links).toBeUndefined();
done();
});
});
});
describe('and throttling exception is thrown', () => {
const partialQuery = {
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: {
InstanceId: 'i-12345678',
},
statistics: ['Average'],
period: '300',
expression: '',
};
const query = {
range: defaultTimeRange,
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{ ...partialQuery, refId: 'A', region: 'us-east-1' },
{ ...partialQuery, refId: 'B', region: 'us-east-2' },
{ ...partialQuery, refId: 'C', region: 'us-east-1' },
{ ...partialQuery, refId: 'D', region: 'us-east-2' },
{ ...partialQuery, refId: 'E', region: 'eu-north-1' },
],
};
const backendErrorResponse = {
data: {
message: 'Throttling: exception',
results: {
A: {
error: 'Throttling: exception',
refId: 'A',
meta: {},
},
B: {
error: 'Throttling: exception',
refId: 'B',
meta: {},
},
C: {
error: 'Throttling: exception',
refId: 'C',
meta: {},
},
D: {
error: 'Throttling: exception',
refId: 'D',
meta: {},
},
E: {
error: 'Throttling: exception',
refId: 'E',
meta: {},
},
},
},
};
beforeEach(() => {
redux.setStore({
dispatch: jest.fn(),
} as any);
datasourceRequestMock.mockImplementation(() => {
return Promise.reject(backendErrorResponse);
});
});
it('should display one alert error message per region+datasource combination', done => {
const memoizedDebounceSpy = jest.spyOn(ctx.ds, 'debouncedAlert');
ctx.ds.query(query).catch(() => {
expect(memoizedDebounceSpy).toHaveBeenCalledWith('TestDatasource', 'us-east-1');
expect(memoizedDebounceSpy).toHaveBeenCalledWith('TestDatasource', 'us-east-2');
expect(memoizedDebounceSpy).toHaveBeenCalledWith('TestDatasource', 'eu-north-1');
expect(memoizedDebounceSpy).toBeCalledTimes(3);
done();
});
});
});
describe('when regions query is used', () => {
beforeEach(() => {
datasourceRequestMock.mockImplementation(() => {
return Promise.resolve({});
});
ctx.ds = new CloudWatchDatasource(instanceSettings, templateSrv, timeSrv);
ctx.ds.doMetricQueryRequest = jest.fn(() => []);
});
describe('and region param is left out', () => {
it('should use the default region', done => {
ctx.ds.metricFindQuery('metrics(testNamespace)').then(() => {
expect(ctx.ds.doMetricQueryRequest).toHaveBeenCalledWith('metrics', {
namespace: 'testNamespace',
region: instanceSettings.jsonData.defaultRegion,
});
done();
});
});
});
describe('and region param is defined by user', () => {
it('should use the user defined region', done => {
ctx.ds.metricFindQuery('metrics(testNamespace2, custom-region)').then(() => {
expect(ctx.ds.doMetricQueryRequest).toHaveBeenCalledWith('metrics', {
namespace: 'testNamespace2',
region: 'custom-region',
});
done();
});
});
});
});
});
describe('When query region is "default"', () => {
it('should return the datasource region if empty or "default"', () => {
const defaultRegion = instanceSettings.jsonData.defaultRegion;
expect(ctx.ds.getActualRegion()).toBe(defaultRegion);
expect(ctx.ds.getActualRegion('')).toBe(defaultRegion);
expect(ctx.ds.getActualRegion('default')).toBe(defaultRegion);
});
it('should return the specified region if specified', () => {
expect(ctx.ds.getActualRegion('some-fake-region-1')).toBe('some-fake-region-1');
});
let requestParams: { queries: CloudWatchQuery[] };
beforeEach(() => {
ctx.ds.performTimeSeriesQuery = jest.fn(request => {
requestParams = request;
return Promise.resolve({ data: {} });
});
});
it('should query for the datasource region if empty or "default"', done => {
const query = {
range: defaultTimeRange,
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
refId: 'A',
region: 'default',
namespace: 'AWS/EC2',
metricName: 'CPUUtilization',
dimensions: {
InstanceId: 'i-12345678',
},
statistics: ['Average'],
period: '300s',
},
],
};
ctx.ds.query(query).then((result: any) => {
expect(requestParams.queries[0].region).toBe(instanceSettings.jsonData.defaultRegion);
done();
});
});
});
describe('When performing CloudWatch query for extended statistics', () => {
const query = {
range: defaultTimeRange,
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
refId: 'A',
region: 'us-east-1',
namespace: 'AWS/ApplicationELB',
metricName: 'TargetResponseTime',
dimensions: {
LoadBalancer: 'lb',
TargetGroup: 'tg',
},
statistics: ['p90.00'],
period: '300s',
},
],
};
const response: any = {
timings: [null],
results: {
A: {
error: '',
refId: 'A',
meta: {
gmdMeta: [
{
Period: 300,
},
],
},
series: [
{
name: 'TargetResponseTime_p90.00',
points: [
[1, 1483228800000],
[2, 1483229100000],
[5, 1483229700000],
],
tags: {
LoadBalancer: 'lb',
TargetGroup: 'tg',
},
},
],
},
},
};
beforeEach(() => {
datasourceRequestMock.mockImplementation(params => {
return Promise.resolve({ data: response });
});
});
it('should return series list', done => {
ctx.ds.query(query).then((result: any) => {
expect(result.data[0].name).toBe(response.results.A.series[0].name);
expect(result.data[0].fields[0].values.buffer[0]).toBe(response.results.A.series[0].points[0][0]);
done();
});
});
});
describe('When performing CloudWatch query with template variables', () => {
let requestParams: { queries: CloudWatchQuery[] };
beforeEach(() => {
templateSrv.init([
new CustomVariable(
{
name: 'var1',
current: {
value: 'var1-foo',
},
multi: false,
},
{} as any
),
new CustomVariable(
{
name: 'var2',
current: {
value: 'var2-foo',
},
multi: false,
},
{} as any
),
new CustomVariable(
{
name: 'var3',
options: [
{ selected: true, value: 'var3-foo' },
{ selected: false, value: 'var3-bar' },
{ selected: true, value: 'var3-baz' },
],
current: {
value: ['var3-foo', 'var3-baz'],
},
multi: true,
},
{} as any
),
new CustomVariable(
{
name: 'var4',
options: [
{ selected: true, value: 'var4-foo' },
{ selected: false, value: 'var4-bar' },
{ selected: true, value: 'var4-baz' },
],
current: {
value: ['var4-foo', 'var4-baz'],
},
multi: true,
},
{} as any
),
]);
datasourceRequestMock.mockImplementation(params => {
requestParams = params.data;
return Promise.resolve({ data: {} });
});
});
it('should generate the correct query for single template variable', done => {
const query = {
range: defaultTimeRange,
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
refId: 'A',
region: 'us-east-1',
namespace: 'TestNamespace',
metricName: 'TestMetricName',
dimensions: {
dim2: '[[var2]]',
},
statistics: ['Average'],
period: '300s',
},
],
};
ctx.ds.query(query).then(() => {
expect(requestParams.queries[0].dimensions['dim2']).toStrictEqual(['var2-foo']);
done();
});
});
it('should generate the correct query in the case of one multilple template variables', done => {
const query = {
range: defaultTimeRange,
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
refId: 'A',
region: 'us-east-1',
namespace: 'TestNamespace',
metricName: 'TestMetricName',
dimensions: {
dim1: '[[var1]]',
dim2: '[[var2]]',
dim3: '[[var3]]',
},
statistics: ['Average'],
period: '300s',
},
],
scopedVars: {
var1: { selected: true, value: 'var1-foo' },
var2: { selected: true, value: 'var2-foo' },
},
};
ctx.ds.query(query).then(() => {
expect(requestParams.queries[0].dimensions['dim1']).toStrictEqual(['var1-foo']);
expect(requestParams.queries[0].dimensions['dim2']).toStrictEqual(['var2-foo']);
expect(requestParams.queries[0].dimensions['dim3']).toStrictEqual(['var3-foo', 'var3-baz']);
done();
});
});
it('should generate the correct query in the case of multilple multi template variables', done => {
const query = {
range: defaultTimeRange,
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
refId: 'A',
region: 'us-east-1',
namespace: 'TestNamespace',
metricName: 'TestMetricName',
dimensions: {
dim1: '[[var1]]',
dim3: '[[var3]]',
dim4: '[[var4]]',
},
statistics: ['Average'],
period: '300s',
},
],
};
ctx.ds.query(query).then(() => {
expect(requestParams.queries[0].dimensions['dim1']).toStrictEqual(['var1-foo']);
expect(requestParams.queries[0].dimensions['dim3']).toStrictEqual(['var3-foo', 'var3-baz']);
expect(requestParams.queries[0].dimensions['dim4']).toStrictEqual(['var4-foo', 'var4-baz']);
done();
});
});
it('should generate the correct query for multilple template variables, lack scopedVars', done => {
const query = {
range: defaultTimeRange,
rangeRaw: { from: 1483228800, to: 1483232400 },
targets: [
{
refId: 'A',
region: 'us-east-1',
namespace: 'TestNamespace',
metricName: 'TestMetricName',
dimensions: {
dim1: '[[var1]]',
dim2: '[[var2]]',
dim3: '[[var3]]',
},
statistics: ['Average'],
period: '300',
},
],
scopedVars: {
var1: { selected: true, value: 'var1-foo' },
},
};
ctx.ds.query(query).then(() => {
expect(requestParams.queries[0].dimensions['dim1']).toStrictEqual(['var1-foo']);
expect(requestParams.queries[0].dimensions['dim2']).toStrictEqual(['var2-foo']);
expect(requestParams.queries[0].dimensions['dim3']).toStrictEqual(['var3-foo', 'var3-baz']);
done();
});
});
});
function describeMetricFindQuery(query: any, func: any) {
describe('metricFindQuery ' + query, () => {
const scenario: any = {};
scenario.setup = async (setupCallback: any) => {
beforeEach(async () => {
await setupCallback();
datasourceRequestMock.mockImplementation(args => {
scenario.request = args.data;
return Promise.resolve({ data: scenario.requestResponse });
});
ctx.ds.metricFindQuery(query).then((args: any) => {
scenario.result = args;
});
});
};
func(scenario);
});
}
describeMetricFindQuery('regions()', async (scenario: any) => {
await scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [{ rows: [['us-east-1', 'us-east-1']] }],
},
},
};
});
it('should call __GetRegions and return result', () => {
expect(scenario.result[0].text).toContain('us-east-1');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('regions');
});
});
describeMetricFindQuery('namespaces()', async (scenario: any) => {
await scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [{ rows: [['AWS/EC2', 'AWS/EC2']] }],
},
},
};
});
it('should call __GetNamespaces and return result', () => {
expect(scenario.result[0].text).toContain('AWS/EC2');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('namespaces');
});
});
describeMetricFindQuery('metrics(AWS/EC2, us-east-2)', async (scenario: any) => {
await scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [{ rows: [['CPUUtilization', 'CPUUtilization']] }],
},
},
};
});
it('should call __GetMetrics and return result', () => {
expect(scenario.result[0].text).toBe('CPUUtilization');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('metrics');
});
});
describeMetricFindQuery('dimension_keys(AWS/EC2)', async (scenario: any) => {
await scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [{ rows: [['InstanceId', 'InstanceId']] }],
},
},
};
});
it('should call __GetDimensions and return result', () => {
console.log({ a: scenario.requestResponse.results });
expect(scenario.result[0].text).toBe('InstanceId');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('dimension_keys');
});
});
describeMetricFindQuery('dimension_values(us-east-1,AWS/EC2,CPUUtilization,InstanceId)', async (scenario: any) => {
await scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [{ rows: [['i-12345678', 'i-12345678']] }],
},
},
};
});
it('should call __ListMetrics and return result', () => {
expect(scenario.result[0].text).toContain('i-12345678');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('dimension_values');
});
});
describeMetricFindQuery('dimension_values(default,AWS/EC2,CPUUtilization,InstanceId)', async (scenario: any) => {
await scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [{ rows: [['i-12345678', 'i-12345678']] }],
},
},
};
});
it('should call __ListMetrics and return result', () => {
expect(scenario.result[0].text).toContain('i-12345678');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('dimension_values');
});
});
describeMetricFindQuery(
'resource_arns(default,ec2:instance,{"environment":["production"]})',
async (scenario: any) => {
await scenario.setup(() => {
scenario.requestResponse = {
results: {
metricFindQuery: {
tables: [
{
rows: [
[
'arn:aws:ec2:us-east-1:123456789012:instance/i-12345678901234567',
'arn:aws:ec2:us-east-1:123456789012:instance/i-76543210987654321',
],
],
},
],
},
},
};
});
it('should call __ListMetrics and return result', () => {
expect(scenario.result[0].text).toContain('arn:aws:ec2:us-east-1:123456789012:instance/i-12345678901234567');
expect(scenario.request.queries[0].type).toBe('metricFindQuery');
expect(scenario.request.queries[0].subtype).toBe('resource_arns');
});
}
);
});