From 0dd2bc895307e87780481e838eb933850be98daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Bedi?= Date: Mon, 13 Jul 2020 13:14:50 +0200 Subject: [PATCH] Prometheus: Fix prom links in mixed mode (#26244) * Prometheus: Fix prom links in mixed mode * Modify PromLink with code style changes --- .../prometheus/components/PromLink.test.tsx | 58 +++++++++++++++++++ .../prometheus/components/PromLink.tsx | 9 +-- 2 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 public/app/plugins/datasource/prometheus/components/PromLink.test.tsx diff --git a/public/app/plugins/datasource/prometheus/components/PromLink.test.tsx b/public/app/plugins/datasource/prometheus/components/PromLink.test.tsx new file mode 100644 index 00000000000..0206173211d --- /dev/null +++ b/public/app/plugins/datasource/prometheus/components/PromLink.test.tsx @@ -0,0 +1,58 @@ +import React from 'react'; +import { mount } from 'enzyme'; +import PromLink from './PromLink'; + +const getPanelData = () => ({ + request: { + targets: [ + { refId: 'A', datasource: 'prom1' }, + { refId: 'B', datasource: 'prom2' }, + ], + range: { + to: { + utc: () => ({ + format: jest.fn(), + }), + }, + }, + }, +}); +describe('PromLink component', () => { + it('should show different link when there are 2 components with the same panel data', async () => { + const Comp = () => ( +
+ 123, createQuery: () => ({ expr: 'up', step: 15 }), directUrl: 'prom1' } as any + } + panelData={getPanelData() as any} + query={{} as any} + /> + 123, createQuery: () => ({ expr: 'up', step: 15 }), directUrl: 'prom2' } as any + } + panelData={getPanelData() as any} + query={{} as any} + /> +
+ ); + const wrapper = mount(); + // Trigger componentDidUpdate + wrapper.setProps('s'); + await Promise.resolve(); + + expect( + wrapper + .find('a') + .first() + .getDOMNode().href + ).toMatch('prom1'); + expect( + wrapper + .find('a') + .last() + .getDOMNode().href + ).toMatch('prom2'); + }); +}); diff --git a/public/app/plugins/datasource/prometheus/components/PromLink.tsx b/public/app/plugins/datasource/prometheus/components/PromLink.tsx index 9668281814f..917a128422e 100644 --- a/public/app/plugins/datasource/prometheus/components/PromLink.tsx +++ b/public/app/plugins/datasource/prometheus/components/PromLink.tsx @@ -4,7 +4,6 @@ import React, { Component } from 'react'; import { PrometheusDatasource } from '../datasource'; import { PromQuery } from '../types'; import { DataQueryRequest, PanelData } from '@grafana/data'; -import { getDatasourceSrv } from 'app/features/plugins/datasource_srv'; interface Props { datasource: PrometheusDatasource; @@ -29,19 +28,13 @@ export default class PromLink extends Component { } async getExternalLink(panelData: PanelData): Promise { - const { query } = this.props; + const { query, datasource } = this.props; const { request } = panelData; if (!request) { return ''; } - const target = request.targets.length > 0 ? request.targets[0] : ({ datasource: null } as any); - const datasourceName = target.datasource; - const datasource: PrometheusDatasource = datasourceName - ? (((await getDatasourceSrv().get(datasourceName)) as any) as PrometheusDatasource) - : (this.props.datasource as PrometheusDatasource); - const range = request.range; const start = datasource.getPrometheusTime(range.from, false); const end = datasource.getPrometheusTime(range.to, true);