grafana/public/app/plugins/datasource/loki/components/stats.ts
Gareth Dawson 0072b2cca4
Loki: Use better query validation before requesting stats (#70800)
* fix: use correct validate funtion

* rename isValidQuery to isQueryWithError

* lets pretend i didnt break things again

* Loki validation: add missing comments and refactor validation function

* isValidQuery: add unit test

* Loki datasource: add tests for getQueryStats

* Remove isValidQuery function

* UnwrapParamEditor: interpolate query before testing validity

* Stats: trim queries when evaluating change

---------

Co-authored-by: Matias Chomicki <matyax@gmail.com>
2023-06-29 16:50:17 +01:00

55 lines
1.3 KiB
TypeScript

import { DateTime, isDateTime, TimeRange } from '@grafana/data';
import { LokiDatasource } from '../datasource';
import { QueryStats } from '../types';
export async function getStats(datasource: LokiDatasource, query: string): Promise<QueryStats | null> {
if (!query) {
return null;
}
const response = await datasource.getQueryStats(query);
if (!response) {
return null;
}
return Object.values(response).every((v) => v === 0) ? null : response;
}
/**
* This function compares two time values. If the first is absolute, it compares them using `DateTime.isSame`.
*
* @param {(DateTime | string)} time1
* @param {(DateTime | string | undefined)} time2
*/
function compareTime(time1: DateTime | string, time2: DateTime | string | undefined) {
const isAbsolute = isDateTime(time1);
if (isAbsolute) {
return time1.isSame(time2);
}
return time1 === time2;
}
export function shouldUpdateStats(
query: string,
prevQuery: string | undefined,
timerange: TimeRange,
prevTimerange: TimeRange | undefined
): boolean {
if (prevQuery === undefined || query.trim() !== prevQuery.trim()) {
return true;
}
if (
compareTime(timerange.raw.from, prevTimerange?.raw.from) &&
compareTime(timerange.raw.to, prevTimerange?.raw.to)
) {
return false;
}
return true;
}