123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- import { PrometheusDatasource } from './datasource';
- import { getQueryHints, SUM_HINT_THRESHOLD_COUNT } from './query_hints';
- describe('getQueryHints()', () => {
- it('returns no hints for no series', () => {
- expect(getQueryHints('', [])).toEqual([]);
- });
- it('returns no hints for empty series', () => {
- expect(getQueryHints('', [{ datapoints: [] }])).toEqual([]);
- });
- it('returns a rate hint for a counter metric', () => {
- const series = [
- {
- datapoints: [
- [23, 1000],
- [24, 1001],
- ],
- },
- ];
- const hints = getQueryHints('metric_total', series);
- expect(hints!.length).toBe(1);
- expect(hints![0]).toMatchObject({
- label: 'Selected metric looks like a counter.',
- fix: {
- action: {
- type: 'ADD_RATE',
- query: 'metric_total',
- },
- },
- });
- });
- it('returns a certain rate hint for a counter metric', () => {
- const series = [
- {
- datapoints: [
- [23, 1000],
- [24, 1001],
- ],
- },
- ];
- const mock: unknown = { languageProvider: { metricsMetadata: { foo: { type: 'counter' } } } };
- const datasource = mock as PrometheusDatasource;
- let hints = getQueryHints('foo', series, datasource);
- expect(hints!.length).toBe(1);
- expect(hints![0]).toMatchObject({
- label: 'Selected metric is a counter.',
- fix: {
- action: {
- type: 'ADD_RATE',
- query: 'foo',
- },
- },
- });
- // Test substring match not triggering hint
- hints = getQueryHints('foo_foo', series, datasource);
- expect(hints).toEqual([]);
- });
- it('returns no rate hint for a counter metric that already has a rate', () => {
- const series = [
- {
- datapoints: [
- [23, 1000],
- [24, 1001],
- ],
- },
- ];
- const hints = getQueryHints('rate(metric_total[1m])', series);
- expect(hints).toEqual([]);
- });
- it('returns no rate hint for a counter metric that already has an increase', () => {
- const series = [
- {
- datapoints: [
- [23, 1000],
- [24, 1001],
- ],
- },
- ];
- const hints = getQueryHints('increase(metric_total[1m])', series);
- expect(hints).toEqual([]);
- });
- it('returns a rate hint with action for a counter metric with labels', () => {
- const series = [
- {
- datapoints: [
- [23, 1000],
- [24, 1001],
- ],
- },
- ];
- const hints = getQueryHints('metric_total{job="grafana"}', series);
- expect(hints!.length).toBe(1);
- expect(hints![0].label).toContain('Selected metric looks like a counter');
- expect(hints![0].fix).toBeDefined();
- });
- it('returns a rate hint w/o action for a complex counter metric', () => {
- const series = [
- {
- datapoints: [
- [23, 1000],
- [24, 1001],
- ],
- },
- ];
- const hints = getQueryHints('sum(metric_total)', series);
- expect(hints!.length).toBe(1);
- expect(hints![0].label).toContain('rate()');
- expect(hints![0].fix).toBeUndefined();
- });
- it('returns a histogram hint for a bucket series', () => {
- const series = [{ datapoints: [[23, 1000]] }];
- const hints = getQueryHints('metric_bucket', series);
- expect(hints!.length).toBe(1);
- expect(hints![0]).toMatchObject({
- label: 'Selected metric has buckets.',
- fix: {
- action: {
- type: 'ADD_HISTOGRAM_QUANTILE',
- query: 'metric_bucket',
- },
- },
- });
- });
- it('returns a histogram hint with action for a bucket with labels', () => {
- const series = [
- {
- datapoints: [
- [23, 1000],
- [24, 1001],
- ],
- },
- ];
- const hints = getQueryHints('metric_bucket{job="grafana"}', series);
- expect(hints!.length).toBe(1);
- expect(hints![0].label).toContain('Selected metric has buckets.');
- expect(hints![0].fix).toBeDefined();
- });
- 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({
- type: 'ADD_SUM',
- label: 'Many time series results returned.',
- fix: {
- label: 'Consider aggregating with sum().',
- action: {
- type: 'ADD_SUM',
- query: 'metric',
- preventSubmit: true,
- },
- },
- });
- });
- });
|