grafana/public/app/plugins/datasource/prometheus/specs/query_hints.test.ts

105 lines
3.4 KiB
TypeScript
Raw Normal View History

import { getQueryHints, SUM_HINT_THRESHOLD_COUNT } from '../query_hints';
2018-10-04 08:48:08 -05:00
describe('getQueryHints()', () => {
it('returns no hints for no series', () => {
2018-10-23 08:12:53 -05:00
expect(getQueryHints('', [])).toEqual(null);
2018-10-04 08:48:08 -05:00
});
it('returns no hints for empty series', () => {
2018-10-23 08:12:53 -05:00
expect(getQueryHints('', [{ datapoints: [] }])).toEqual(null);
2018-10-04 08:48:08 -05:00
});
it('returns no hint for a monotonically decreasing series', () => {
2018-10-23 08:12:53 -05:00
const series = [{ datapoints: [[23, 1000], [22, 1001]] }];
const hints = getQueryHints('metric', series);
expect(hints).toEqual(null);
2018-10-04 08:48:08 -05:00
});
it('returns no hint for a flat series', () => {
2018-10-23 08:12:53 -05:00
const series = [{ datapoints: [[null, 1000], [23, 1001], [null, 1002], [23, 1003]] }];
const hints = getQueryHints('metric', series);
expect(hints).toEqual(null);
});
it('returns a rate hint for a monotonically increasing series', () => {
2018-10-23 08:12:53 -05:00
const series = [{ datapoints: [[23, 1000], [24, 1001]] }];
const hints = getQueryHints('metric', series);
expect(hints!.length).toBe(1);
expect(hints![0]).toMatchObject({
label: 'Time series is monotonically increasing.',
2018-10-04 08:48:08 -05:00
fix: {
action: {
type: 'ADD_RATE',
query: 'metric',
},
},
});
});
it('returns no rate hint for a monotonically increasing series that already has a rate', () => {
2018-10-23 08:12:53 -05:00
const series = [{ datapoints: [[23, 1000], [24, 1001]] }];
const hints = getQueryHints('rate(metric[1m])', series);
expect(hints).toEqual(null);
2018-10-04 08:48:08 -05:00
});
it('returns a rate hint w/o action for a complex monotonically increasing series', () => {
2018-10-23 08:12:53 -05:00
const series = [{ datapoints: [[23, 1000], [24, 1001]] }];
const hints = getQueryHints('sum(metric)', series);
expect(hints!.length).toBe(1);
expect(hints![0].label).toContain('rate()');
expect(hints![0].fix).toBeUndefined();
2018-10-04 08:48:08 -05:00
});
it('returns a rate hint for a monotonically increasing series with missing data', () => {
2018-10-23 08:12:53 -05:00
const series = [{ datapoints: [[23, 1000], [null, 1001], [24, 1002]] }];
const hints = getQueryHints('metric', series);
expect(hints!.length).toBe(1);
expect(hints![0]).toMatchObject({
label: 'Time series is monotonically increasing.',
2018-10-04 08:48:08 -05:00
fix: {
action: {
type: 'ADD_RATE',
query: 'metric',
},
},
});
});
it('returns a histogram hint for a bucket series', () => {
2018-10-23 08:12:53 -05:00
const series = [{ datapoints: [[23, 1000]] }];
const hints = getQueryHints('metric_bucket', series);
expect(hints!.length).toBe(1);
expect(hints![0]).toMatchObject({
2018-10-04 08:48:08 -05:00
label: 'Time series has buckets, you probably wanted a histogram.',
fix: {
action: {
type: 'ADD_HISTOGRAM_QUANTILE',
query: 'metric_bucket',
},
},
});
});
it('returns a sum hint when many time series results are returned for a simple metric', () => {
const seriesCount = SUM_HINT_THRESHOLD_COUNT;
const series = Array.from({ length: seriesCount }, _ => ({
datapoints: [[0, 0], [0, 0]],
}));
const hints = getQueryHints('metric', series);
expect(hints!.length).toBe(1);
expect(hints![0]).toMatchObject({
2018-10-28 11:48:17 -05:00
type: 'ADD_SUM',
label: 'Many time series results returned.',
fix: {
2018-10-28 11:48:17 -05:00
label: 'Consider aggregating with sum().',
action: {
type: 'ADD_SUM',
query: 'metric',
2018-10-28 11:48:17 -05:00
preventSubmit: true,
},
},
});
});
2018-10-04 08:48:08 -05:00
});