mirror of https://github.com/grafana/grafana
parent
2e02a8c855
commit
fbed57ab43
@ -1,100 +1,92 @@ |
|||||||
import _ from 'lodash'; |
import _ from 'lodash'; |
||||||
|
|
||||||
export function getQueryHints(series: any[], datasource?: any): any[] { |
export function getQueryHints(query: string, series?: any[], datasource?: any): any[] { |
||||||
const hints = series.map((s, i) => { |
const hints = []; |
||||||
const query: string = s.query; |
|
||||||
const index: number = s.responseIndex; |
|
||||||
if (query === undefined || index === undefined) { |
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
// ..._bucket metric needs a histogram_quantile()
|
// ..._bucket metric needs a histogram_quantile()
|
||||||
const histogramMetric = query.trim().match(/^\w+_bucket$/); |
const histogramMetric = query.trim().match(/^\w+_bucket$/); |
||||||
if (histogramMetric) { |
if (histogramMetric) { |
||||||
const label = 'Time series has buckets, you probably wanted a histogram.'; |
const label = 'Time series has buckets, you probably wanted a histogram.'; |
||||||
return { |
hints.push({ |
||||||
index, |
type: 'HISTOGRAM_QUANTILE', |
||||||
label, |
label, |
||||||
fix: { |
fix: { |
||||||
label: 'Fix by adding histogram_quantile().', |
label: 'Fix by adding histogram_quantile().', |
||||||
action: { |
action: { |
||||||
type: 'ADD_HISTOGRAM_QUANTILE', |
type: 'ADD_HISTOGRAM_QUANTILE', |
||||||
query, |
query, |
||||||
index, |
|
||||||
}, |
|
||||||
}, |
}, |
||||||
}; |
}, |
||||||
} |
}); |
||||||
|
} |
||||||
|
|
||||||
// Check for monotony
|
// Check for monotony on series (table results are being ignored here)
|
||||||
const datapoints: number[][] = s.datapoints; |
if (series && series.length > 0) { |
||||||
if (query.indexOf('rate(') === -1 && datapoints.length > 1) { |
series.forEach(s => { |
||||||
let increasing = false; |
const datapoints: number[][] = s.datapoints; |
||||||
const nonNullData = datapoints.filter(dp => dp[0] !== null); |
if (query.indexOf('rate(') === -1 && datapoints.length > 1) { |
||||||
const monotonic = nonNullData.every((dp, index) => { |
let increasing = false; |
||||||
if (index === 0) { |
const nonNullData = datapoints.filter(dp => dp[0] !== null); |
||||||
return true; |
const monotonic = nonNullData.every((dp, index) => { |
||||||
} |
if (index === 0) { |
||||||
increasing = increasing || dp[0] > nonNullData[index - 1][0]; |
return true; |
||||||
// monotonic?
|
} |
||||||
return dp[0] >= nonNullData[index - 1][0]; |
increasing = increasing || dp[0] > nonNullData[index - 1][0]; |
||||||
}); |
// monotonic?
|
||||||
if (increasing && monotonic) { |
return dp[0] >= nonNullData[index - 1][0]; |
||||||
const simpleMetric = query.trim().match(/^\w+$/); |
}); |
||||||
let label = 'Time series is monotonously increasing.'; |
if (increasing && monotonic) { |
||||||
let fix; |
const simpleMetric = query.trim().match(/^\w+$/); |
||||||
if (simpleMetric) { |
let label = 'Time series is monotonously increasing.'; |
||||||
fix = { |
let fix; |
||||||
label: 'Fix by adding rate().', |
if (simpleMetric) { |
||||||
action: { |
fix = { |
||||||
type: 'ADD_RATE', |
label: 'Fix by adding rate().', |
||||||
query, |
action: { |
||||||
index, |
type: 'ADD_RATE', |
||||||
}, |
query, |
||||||
}; |
}, |
||||||
} else { |
}; |
||||||
label = `${label} Try applying a rate() function.`; |
} else { |
||||||
|
label = `${label} Try applying a rate() function.`; |
||||||
|
} |
||||||
|
hints.push({ |
||||||
|
type: 'APPLY_RATE', |
||||||
|
label, |
||||||
|
fix, |
||||||
|
}); |
||||||
} |
} |
||||||
return { |
|
||||||
label, |
|
||||||
index, |
|
||||||
fix, |
|
||||||
}; |
|
||||||
} |
} |
||||||
} |
}); |
||||||
|
} |
||||||
|
|
||||||
// Check for recording rules expansion
|
// Check for recording rules expansion
|
||||||
if (datasource && datasource.ruleMappings) { |
if (datasource && datasource.ruleMappings) { |
||||||
const mapping = datasource.ruleMappings; |
const mapping = datasource.ruleMappings; |
||||||
const mappingForQuery = Object.keys(mapping).reduce((acc, ruleName) => { |
const mappingForQuery = Object.keys(mapping).reduce((acc, ruleName) => { |
||||||
if (query.search(ruleName) > -1) { |
if (query.search(ruleName) > -1) { |
||||||
return { |
|
||||||
...acc, |
|
||||||
[ruleName]: mapping[ruleName], |
|
||||||
}; |
|
||||||
} |
|
||||||
return acc; |
|
||||||
}, {}); |
|
||||||
if (_.size(mappingForQuery) > 0) { |
|
||||||
const label = 'Query contains recording rules.'; |
|
||||||
return { |
return { |
||||||
label, |
...acc, |
||||||
index, |
[ruleName]: mapping[ruleName], |
||||||
fix: { |
|
||||||
label: 'Expand rules', |
|
||||||
action: { |
|
||||||
type: 'EXPAND_RULES', |
|
||||||
query, |
|
||||||
index, |
|
||||||
mapping: mappingForQuery, |
|
||||||
}, |
|
||||||
}, |
|
||||||
}; |
}; |
||||||
} |
} |
||||||
|
return acc; |
||||||
|
}, {}); |
||||||
|
if (_.size(mappingForQuery) > 0) { |
||||||
|
const label = 'Query contains recording rules.'; |
||||||
|
hints.push({ |
||||||
|
type: 'EXPAND_RULES', |
||||||
|
label, |
||||||
|
fix: { |
||||||
|
label: 'Expand rules', |
||||||
|
action: { |
||||||
|
type: 'EXPAND_RULES', |
||||||
|
query, |
||||||
|
mapping: mappingForQuery, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}); |
||||||
} |
} |
||||||
|
} |
||||||
// No hint found
|
return hints.length > 0 ? hints : null; |
||||||
return null; |
|
||||||
}); |
|
||||||
return hints; |
|
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue