mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Prometheus: Pass scoped vars when creating PromLink query (#40525)
This commit is contained in:
parent
4e1cf7dea7
commit
4a3be0f845
@ -5,9 +5,17 @@ import { PromQuery } from '../types';
|
|||||||
import { PrometheusDatasource } from '../datasource';
|
import { PrometheusDatasource } from '../datasource';
|
||||||
import PromLink from './PromLink';
|
import PromLink from './PromLink';
|
||||||
|
|
||||||
|
jest.mock('@grafana/data', () => ({
|
||||||
|
...(jest.requireActual('@grafana/data') as any),
|
||||||
|
rangeUtil: {
|
||||||
|
intervalToSeconds: jest.fn(() => 15),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
const getPanelData = (panelDataOverrides?: Partial<PanelData>) => {
|
const getPanelData = (panelDataOverrides?: Partial<PanelData>) => {
|
||||||
const panelData = {
|
const panelData = {
|
||||||
request: {
|
request: {
|
||||||
|
scopedVars: [{ __interval: { text: '15s', value: '15s' } }],
|
||||||
targets: [
|
targets: [
|
||||||
{ refId: 'A', datasource: 'prom1' },
|
{ refId: 'A', datasource: 'prom1' },
|
||||||
{ refId: 'B', datasource: 'prom2' },
|
{ refId: 'B', datasource: 'prom2' },
|
||||||
@ -30,6 +38,7 @@ const getDataSource = (datasourceOverrides?: Partial<PrometheusDatasource>) => {
|
|||||||
getPrometheusTime: () => 123,
|
getPrometheusTime: () => 123,
|
||||||
createQuery: () => ({ expr: 'up', step: 15 }),
|
createQuery: () => ({ expr: 'up', step: 15 }),
|
||||||
directUrl: 'prom1',
|
directUrl: 'prom1',
|
||||||
|
getRateIntervalScopedVariable: jest.fn(() => ({ __rate_interval: { text: '60s', value: '60s' } })),
|
||||||
};
|
};
|
||||||
|
|
||||||
return (Object.assign(datasource, datasourceOverrides) as unknown) as PrometheusDatasource;
|
return (Object.assign(datasource, datasourceOverrides) as unknown) as PrometheusDatasource;
|
||||||
|
@ -3,7 +3,7 @@ import React, { FC, useEffect, useState, memo } from 'react';
|
|||||||
|
|
||||||
import { PrometheusDatasource } from '../datasource';
|
import { PrometheusDatasource } from '../datasource';
|
||||||
import { PromQuery } from '../types';
|
import { PromQuery } from '../types';
|
||||||
import { DataQueryRequest, PanelData, textUtil } from '@grafana/data';
|
import { DataQueryRequest, PanelData, ScopedVars, textUtil, rangeUtil } from '@grafana/data';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
datasource: PrometheusDatasource;
|
datasource: PrometheusDatasource;
|
||||||
@ -22,7 +22,7 @@ const PromLink: FC<Props> = ({ panelData, query, datasource }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const {
|
||||||
request: { range, interval },
|
request: { range, interval, scopedVars },
|
||||||
} = panelData;
|
} = panelData;
|
||||||
|
|
||||||
const start = datasource.getPrometheusTime(range.from, false);
|
const start = datasource.getPrometheusTime(range.from, false);
|
||||||
@ -30,8 +30,18 @@ const PromLink: FC<Props> = ({ panelData, query, datasource }) => {
|
|||||||
const rangeDiff = Math.ceil(end - start);
|
const rangeDiff = Math.ceil(end - start);
|
||||||
const endTime = range.to.utc().format('YYYY-MM-DD HH:mm');
|
const endTime = range.to.utc().format('YYYY-MM-DD HH:mm');
|
||||||
|
|
||||||
|
const enrichedScopedVars: ScopedVars = {
|
||||||
|
...scopedVars,
|
||||||
|
// As we support $__rate_interval variable in min step, we need add it to scopedVars
|
||||||
|
...datasource.getRateIntervalScopedVariable(
|
||||||
|
rangeUtil.intervalToSeconds(interval),
|
||||||
|
rangeUtil.intervalToSeconds(datasource.interval)
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
interval,
|
interval,
|
||||||
|
scopedVars: enrichedScopedVars,
|
||||||
} as DataQueryRequest<PromQuery>;
|
} as DataQueryRequest<PromQuery>;
|
||||||
|
|
||||||
const queryOptions = datasource.createQuery(query, options, start, end);
|
const queryOptions = datasource.createQuery(query, options, start, end);
|
||||||
|
Loading…
Reference in New Issue
Block a user