2020-07-22 14:09:27 -04:00
|
|
|
import { getSeriesLabel, makeDataSeries } from '~/helpers/monitor_helper';
|
2019-04-05 05:14:13 -04:00
|
|
|
|
|
|
|
describe('monitor helper', () => {
|
|
|
|
const defaultConfig = { default: true, name: 'default name' };
|
|
|
|
const name = 'data name';
|
|
|
|
const series = [[1, 1], [2, 2], [3, 3]];
|
2020-07-22 14:09:27 -04:00
|
|
|
|
|
|
|
describe('getSeriesLabel', () => {
|
|
|
|
const metricAttributes = { __name__: 'up', app: 'prometheus' };
|
|
|
|
|
|
|
|
it('gets a single attribute label', () => {
|
|
|
|
expect(getSeriesLabel('app', metricAttributes)).toBe('app: prometheus');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets a templated label', () => {
|
|
|
|
expect(getSeriesLabel('{{__name__}}', metricAttributes)).toBe('up');
|
|
|
|
expect(getSeriesLabel('{{app}}', metricAttributes)).toBe('prometheus');
|
|
|
|
expect(getSeriesLabel('{{missing}}', metricAttributes)).toBe('{{missing}}');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets a multiple label', () => {
|
|
|
|
expect(getSeriesLabel(null, metricAttributes)).toBe('__name__: up, app: prometheus');
|
|
|
|
expect(getSeriesLabel('', metricAttributes)).toBe('__name__: up, app: prometheus');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('gets a simple label', () => {
|
|
|
|
expect(getSeriesLabel('A label', {})).toBe('A label');
|
|
|
|
});
|
|
|
|
});
|
2019-04-05 05:14:13 -04:00
|
|
|
|
|
|
|
describe('makeDataSeries', () => {
|
2020-07-22 14:09:27 -04:00
|
|
|
const data = ({ metric = { default_name: name }, values = series } = {}) => [
|
|
|
|
{ metric, values },
|
|
|
|
];
|
|
|
|
|
2019-04-05 05:14:13 -04:00
|
|
|
const expectedDataSeries = [
|
|
|
|
{
|
|
|
|
...defaultConfig,
|
|
|
|
data: series,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
it('converts query results to data series', () => {
|
2020-07-22 14:09:27 -04:00
|
|
|
expect(makeDataSeries(data({ metric: {} }), defaultConfig)).toEqual(expectedDataSeries);
|
2019-04-05 05:14:13 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns an empty array if no query results exist', () => {
|
2020-07-22 14:09:27 -04:00
|
|
|
expect(makeDataSeries([], defaultConfig)).toEqual([]);
|
2019-04-05 05:14:13 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles multi-series query results', () => {
|
|
|
|
const expectedData = { ...expectedDataSeries[0], name: 'default name: data name' };
|
|
|
|
|
2020-07-22 14:09:27 -04:00
|
|
|
expect(makeDataSeries([...data(), ...data()], defaultConfig)).toEqual([
|
2019-04-05 05:14:13 -04:00
|
|
|
expectedData,
|
|
|
|
expectedData,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2019-11-04 07:06:19 -05:00
|
|
|
it('updates series name from templates', () => {
|
|
|
|
const config = {
|
|
|
|
...defaultConfig,
|
|
|
|
name: '{{cmd}}',
|
|
|
|
};
|
|
|
|
|
2020-07-22 14:09:27 -04:00
|
|
|
const [result] = makeDataSeries([{ metric: { cmd: 'brpop' }, values: series }], config);
|
2019-11-04 07:06:19 -05:00
|
|
|
|
|
|
|
expect(result.name).toEqual('brpop');
|
|
|
|
});
|
|
|
|
|
2020-04-09 11:09:29 -04:00
|
|
|
it('supports a multi metric label template expression', () => {
|
|
|
|
const config = {
|
|
|
|
...defaultConfig,
|
|
|
|
name: '',
|
|
|
|
};
|
|
|
|
|
2020-07-22 14:09:27 -04:00
|
|
|
const [result] = makeDataSeries(
|
2020-04-09 11:09:29 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
metric: {
|
|
|
|
backend: 'HA Server',
|
|
|
|
frontend: 'BA Server',
|
|
|
|
app: 'prometheus',
|
|
|
|
instance: 'k8 cluster 1',
|
|
|
|
},
|
|
|
|
values: series,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result.name).toBe(
|
|
|
|
'backend: HA Server, frontend: BA Server, app: prometheus, instance: k8 cluster 1',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-11-04 07:06:19 -05:00
|
|
|
it('supports space-padded template expressions', () => {
|
|
|
|
const config = {
|
|
|
|
...defaultConfig,
|
|
|
|
name: 'backend: {{ backend }}',
|
|
|
|
};
|
|
|
|
|
2020-07-22 14:09:27 -04:00
|
|
|
const [result] = makeDataSeries(
|
2019-11-04 07:06:19 -05:00
|
|
|
[{ metric: { backend: 'HA Server' }, values: series }],
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result.name).toEqual('backend: HA Server');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports repeated template variables', () => {
|
|
|
|
const config = { ...defaultConfig, name: '{{cmd}}, {{cmd}}' };
|
|
|
|
|
2020-07-22 14:09:27 -04:00
|
|
|
const [result] = makeDataSeries([{ metric: { cmd: 'brpop' }, values: series }], config);
|
2019-11-04 07:06:19 -05:00
|
|
|
|
|
|
|
expect(result.name).toEqual('brpop, brpop');
|
|
|
|
});
|
|
|
|
|
2019-11-15 07:06:12 -05:00
|
|
|
it('supports hyphenated template variables', () => {
|
|
|
|
const config = { ...defaultConfig, name: 'expired - {{ test-attribute }}' };
|
|
|
|
|
2020-07-22 14:09:27 -04:00
|
|
|
const [result] = makeDataSeries(
|
2019-11-15 07:06:12 -05:00
|
|
|
[{ metric: { 'test-attribute': 'test-attribute-value' }, values: series }],
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result.name).toEqual('expired - test-attribute-value');
|
|
|
|
});
|
|
|
|
|
2019-11-04 07:06:19 -05:00
|
|
|
it('updates multiple series names from templates', () => {
|
|
|
|
const config = {
|
|
|
|
...defaultConfig,
|
|
|
|
name: '{{job}}: {{cmd}}',
|
|
|
|
};
|
|
|
|
|
2020-07-22 14:09:27 -04:00
|
|
|
const [result] = makeDataSeries(
|
2019-11-04 07:06:19 -05:00
|
|
|
[{ metric: { cmd: 'brpop', job: 'redis' }, values: series }],
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result.name).toEqual('redis: brpop');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates name for each series', () => {
|
|
|
|
const config = {
|
|
|
|
...defaultConfig,
|
|
|
|
name: '{{cmd}}',
|
|
|
|
};
|
|
|
|
|
2020-07-22 14:09:27 -04:00
|
|
|
const [firstSeries, secondSeries] = makeDataSeries(
|
2019-11-04 07:06:19 -05:00
|
|
|
[
|
|
|
|
{ metric: { cmd: 'brpop' }, values: series },
|
|
|
|
{ metric: { cmd: 'zrangebyscore' }, values: series },
|
|
|
|
],
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(firstSeries.name).toEqual('brpop');
|
|
|
|
expect(secondSeries.name).toEqual('zrangebyscore');
|
|
|
|
});
|
2019-04-05 05:14:13 -04:00
|
|
|
});
|
|
|
|
});
|