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[] { ...@@ -110,10 +110,9 @@ export function determineQueryHints(series: any[], datasource?: any): any[] {
// Check for monotony // Check for monotony
const datapoints: [number, number][] = s.datapoints; const datapoints: [number, number][] = s.datapoints;
const simpleMetric = query.trim().match(/^\w+$/); if (datapoints.length > 1) {
if (simpleMetric && datapoints.length > 1) {
let increasing = false; let increasing = false;
const monotonic = datapoints.every((dp, index) => { const monotonic = datapoints.filter(dp => dp[0] !== null).every((dp, index) => {
if (index === 0) { if (index === 0) {
return true; return true;
} }
...@@ -122,18 +121,25 @@ export function determineQueryHints(series: any[], datasource?: any): any[] { ...@@ -122,18 +121,25 @@ export function determineQueryHints(series: any[], datasource?: any): any[] {
return dp[0] >= datapoints[index - 1][0]; return dp[0] >= datapoints[index - 1][0];
}); });
if (increasing && monotonic) { if (increasing && monotonic) {
const label = 'Time series is monotonously increasing.'; const simpleMetric = query.trim().match(/^\w+$/);
return { let label = 'Time series is monotonously increasing.';
label, let fix;
index, if (simpleMetric) {
fix: { fix = {
label: 'Fix by adding rate().', label: 'Fix by adding rate().',
action: { action: {
type: 'ADD_RATE', type: 'ADD_RATE',
query, query,
index, index,
}, },
}, };
} else {
label = `${label} Try applying a rate() function.`;
}
return {
label,
index,
fix,
}; };
} }
} }
......
...@@ -213,6 +213,30 @@ describe('PrometheusDatasource', () => { ...@@ -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', () => { it('returns a histogram hint for a bucket series', () => {
const series = [{ datapoints: [[23, 1000]], query: 'metric_bucket', responseIndex: 0 }]; const series = [{ datapoints: [[23, 1000]], query: 'metric_bucket', responseIndex: 0 }];
const hints = determineQueryHints(series); 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