mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Fix typescript strict null errors from 674 -> 565 (#26057)
* Chore: Fix typescript strict null errors * Added new limit * Fixed ts issue * fixed tests * trying to fix type inference * Fixing more ts errors * Revert tsconfig option * Fix * Fixed code * More fixes * fix tests * Updated snapshot
This commit is contained in:
@@ -7,7 +7,7 @@ import { PrometheusDatasource } from './datasource';
|
||||
*/
|
||||
export const SUM_HINT_THRESHOLD_COUNT = 20;
|
||||
|
||||
export function getQueryHints(query: string, series?: any[], datasource?: PrometheusDatasource): QueryHint[] | null {
|
||||
export function getQueryHints(query: string, series?: any[], datasource?: PrometheusDatasource): QueryHint[] {
|
||||
const hints = [];
|
||||
|
||||
// ..._bucket metric needs a histogram_quantile()
|
||||
@@ -32,27 +32,32 @@ export function getQueryHints(query: string, series?: any[], datasource?: Promet
|
||||
// Use metric metadata for exact types
|
||||
const nameMatch = query.match(/\b(\w+_(total|sum|count))\b/);
|
||||
let counterNameMetric = nameMatch ? nameMatch[1] : '';
|
||||
const metricsMetadata = datasource?.languageProvider?.metricsMetadata;
|
||||
const metricsMetadata = datasource?.languageProvider?.metricsMetadata ?? {};
|
||||
const metricMetadataKeys = Object.keys(metricsMetadata);
|
||||
let certain = false;
|
||||
if (_.size(metricsMetadata) > 0) {
|
||||
counterNameMetric = Object.keys(metricsMetadata).find(metricName => {
|
||||
// Only considering first type information, could be non-deterministic
|
||||
const metadata = metricsMetadata[metricName][0];
|
||||
if (metadata.type.toLowerCase() === 'counter') {
|
||||
const metricRegex = new RegExp(`\\b${metricName}\\b`);
|
||||
if (query.match(metricRegex)) {
|
||||
certain = true;
|
||||
return true;
|
||||
|
||||
if (metricMetadataKeys.length > 0) {
|
||||
counterNameMetric =
|
||||
metricMetadataKeys.find(metricName => {
|
||||
// Only considering first type information, could be non-deterministic
|
||||
const metadata = metricsMetadata[metricName][0];
|
||||
if (metadata.type.toLowerCase() === 'counter') {
|
||||
const metricRegex = new RegExp(`\\b${metricName}\\b`);
|
||||
if (query.match(metricRegex)) {
|
||||
certain = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return false;
|
||||
}) ?? '';
|
||||
}
|
||||
|
||||
if (counterNameMetric) {
|
||||
const simpleMetric = query.trim().match(/^\w+$/);
|
||||
const verb = certain ? 'is' : 'looks like';
|
||||
let label = `Metric ${counterNameMetric} ${verb} a counter.`;
|
||||
let fix: QueryFix;
|
||||
let fix: QueryFix | undefined;
|
||||
|
||||
if (simpleMetric) {
|
||||
fix = {
|
||||
label: 'Fix by adding rate().',
|
||||
@@ -60,10 +65,11 @@ export function getQueryHints(query: string, series?: any[], datasource?: Promet
|
||||
type: 'ADD_RATE',
|
||||
query,
|
||||
},
|
||||
} as QueryFix;
|
||||
};
|
||||
} else {
|
||||
label = `${label} Try applying a rate() function.`;
|
||||
}
|
||||
|
||||
hints.push({
|
||||
type: 'APPLY_RATE',
|
||||
label,
|
||||
@@ -119,5 +125,5 @@ export function getQueryHints(query: string, series?: any[], datasource?: Promet
|
||||
}
|
||||
}
|
||||
|
||||
return hints.length > 0 ? hints : null;
|
||||
return hints;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user