Commit dc608483 by Marcus Efraimsson Committed by GitHub

Merge pull request #12876 from grafana/davkal/explore-rate-hinting

Explore: still show rate hint if query is complex
parents c970e1e4 75db4d8e
......@@ -110,10 +110,9 @@ export function determineQueryHints(series: any[], datasource?: any): any[] {
// Check for monotony
const datapoints: [number, number][] = s.datapoints;
const simpleMetric = query.trim().match(/^\w+$/);
if (simpleMetric && datapoints.length > 1) {
if (datapoints.length > 1) {
let increasing = false;
const monotonic = datapoints.every((dp, index) => {
const monotonic = datapoints.filter(dp => dp[0] !== null).every((dp, index) => {
if (index === 0) {
return true;
}
......@@ -122,18 +121,25 @@ export function determineQueryHints(series: any[], datasource?: any): any[] {
return dp[0] >= datapoints[index - 1][0];
});
if (increasing && monotonic) {
const label = 'Time series is monotonously increasing.';
return {
label,
index,
fix: {
const simpleMetric = query.trim().match(/^\w+$/);
let label = 'Time series is monotonously increasing.';
let fix;
if (simpleMetric) {
fix = {
label: 'Fix by adding rate().',
action: {
type: 'ADD_RATE',
query,
index,
},
},
};
} else {
label = `${label} Try applying a rate() function.`;
}
return {
label,
index,
fix,
};
}
}
......
......@@ -213,6 +213,30 @@ describe('PrometheusDatasource', () => {
});
});
it('returns a rate hint w/o action for a complex monotonously increasing series', () => {
const series = [{ datapoints: [[23, 1000], [24, 1001]], query: 'sum(metric)', responseIndex: 0 }];
const hints = determineQueryHints(series);
expect(hints.length).toBe(1);
expect(hints[0].label).toContain('rate()');
expect(hints[0].fix).toBeUndefined();
});
it('returns a rate hint for a monotonously increasing series with missing data', () => {
const series = [{ datapoints: [[23, 1000], [null, 1001], [24, 1002]], query: 'metric', responseIndex: 0 }];
const hints = determineQueryHints(series);
expect(hints.length).toBe(1);
expect(hints[0]).toMatchObject({
label: 'Time series is monotonously increasing.',
index: 0,
fix: {
action: {
type: 'ADD_RATE',
query: 'metric',
},
},
});
});
it('returns a histogram hint for a bucket series', () => {
const series = [{ datapoints: [[23, 1000]], query: 'metric_bucket', responseIndex: 0 }];
const hints = determineQueryHints(series);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment