mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Prometheus: Run Explore both queries trough backend (#39462)
* Prometheus: Run Explore both queries trough backend
* Refactor, simplify
* Set default values for query type selector
* Run multiple queries as one query trough backend
* Remove trailing newlines
* Pass utcOffset
* Remove trailing comma
* Add meta to frames only at 1 place
* Set exemplars to falsee if they are disabled
* Revert "Set exemplars to falsee if they are disabled"
This reverts commit e7b697c9f3.
This commit is contained in:
@@ -18,6 +18,10 @@ export const PromExploreQueryEditor: FC<Props> = (props: Props) => {
|
||||
if (query.exemplar === undefined) {
|
||||
onChange({ ...query, exemplar: true });
|
||||
}
|
||||
|
||||
if (!query.instant && !query.range) {
|
||||
onChange({ ...query, instant: true, range: true });
|
||||
}
|
||||
}, [onChange, query]);
|
||||
|
||||
function onChangeQueryStep(value: string) {
|
||||
|
||||
@@ -289,36 +289,18 @@ export class PrometheusDatasource extends DataSourceWithBackend<PromQuery, PromO
|
||||
};
|
||||
};
|
||||
|
||||
prepareOptionsV2 = (options: DataQueryRequest<PromQuery>) => {
|
||||
const targets = options.targets.map((target) => {
|
||||
//This is currently only preparing options for Explore queries where we know the format of data we want to receive
|
||||
if (target.instant) {
|
||||
return { ...target, instant: true, range: false, format: 'table' };
|
||||
}
|
||||
return {
|
||||
...target,
|
||||
instant: false,
|
||||
range: true,
|
||||
format: 'time_series',
|
||||
utcOffsetSec: this.timeSrv.timeRange().to.utcOffset() * 60,
|
||||
};
|
||||
});
|
||||
|
||||
return { ...options, targets };
|
||||
};
|
||||
|
||||
query(options: DataQueryRequest<PromQuery>): Observable<DataQueryResponse> {
|
||||
// WIP - currently we want to run trough backend only if all queries are explore + range/instant queries
|
||||
const shouldRunBackendQuery =
|
||||
this.access === 'proxy' &&
|
||||
options.app === CoreApp.Explore &&
|
||||
!options.targets.some((query) => query.exemplar) &&
|
||||
// When running both queries, run through proxy
|
||||
!options.targets.some((query) => query.instant && query.range);
|
||||
this.access === 'proxy' && options.app === CoreApp.Explore && !options.targets.some((query) => query.exemplar);
|
||||
|
||||
if (shouldRunBackendQuery) {
|
||||
const newOptions = this.prepareOptionsV2(options);
|
||||
return super.query(newOptions).pipe(map((response) => transformV2(response, newOptions)));
|
||||
const targets = options.targets.map((target) => ({
|
||||
...target,
|
||||
// We need to pass utcOffsetSec to backend to calculate aligned range
|
||||
utcOffsetSec: this.timeSrv.timeRange().to.utcOffset() * 60,
|
||||
}));
|
||||
return super.query({ ...options, targets }).pipe(map((response) => transformV2(response, options)));
|
||||
// Run queries trough browser/proxy
|
||||
} else {
|
||||
const start = this.getPrometheusTime(options.range.from, false);
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
DataQueryResponse,
|
||||
DataQueryRequest,
|
||||
PreferredVisualisationType,
|
||||
CoreApp,
|
||||
} from '@grafana/data';
|
||||
import { FetchResponse, getDataSourceSrv, getTemplateSrv } from '@grafana/runtime';
|
||||
import { partition } from 'lodash';
|
||||
@@ -41,12 +42,25 @@ interface TimeAndValue {
|
||||
[TIME_SERIES_VALUE_FIELD_NAME]: number;
|
||||
}
|
||||
|
||||
const isTableResult = (dataFrame: DataFrame, options: DataQueryRequest<PromQuery>): boolean => {
|
||||
// We want to process instant results in Explore as table
|
||||
if ((options.app === CoreApp.Explore && dataFrame.meta?.custom?.queryType) === 'instant') {
|
||||
return true;
|
||||
}
|
||||
|
||||
// We want to process all dataFrames with target.format === 'table' as table
|
||||
const target = options.targets.find((target) => target.refId === dataFrame.refId);
|
||||
if (target?.format === 'table') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
// V2 result trasnformer used to transform query results from queries that were run trough prometheus backend
|
||||
export function transformV2(response: DataQueryResponse, options: DataQueryRequest<PromQuery>) {
|
||||
// Get refIds that have table format as we need to process those to table reuslts
|
||||
const tableRefIds = options.targets.filter((target) => target.format === 'table').map((target) => target.refId);
|
||||
const [tableResults, otherResults]: [DataFrame[], DataFrame[]] = partition(response.data, (dataFrame) =>
|
||||
dataFrame.refId ? tableRefIds.includes(dataFrame.refId) : false
|
||||
isTableResult(dataFrame, options)
|
||||
);
|
||||
|
||||
// For table results, we need to transform data frames to table data frames
|
||||
|
||||
Reference in New Issue
Block a user