Commit 0dd2bc89 by Zoltán Bedi Committed by GitHub

Prometheus: Fix prom links in mixed mode (#26244)

* Prometheus: Fix prom links in mixed mode

* Modify PromLink with code style changes
parent b1e48f42
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 = () => (
<div>
<PromLink
datasource={
{ getPrometheusTime: () => 123, createQuery: () => ({ expr: 'up', step: 15 }), directUrl: 'prom1' } as any
}
panelData={getPanelData() as any}
query={{} as any}
/>
<PromLink
datasource={
{ getPrometheusTime: () => 123, createQuery: () => ({ expr: 'up', step: 15 }), directUrl: 'prom2' } as any
}
panelData={getPanelData() as any}
query={{} as any}
/>
</div>
);
const wrapper = mount(<Comp />);
// Trigger componentDidUpdate
wrapper.setProps('s');
await Promise.resolve();
expect(
wrapper
.find('a')
.first()
.getDOMNode<HTMLAnchorElement>().href
).toMatch('prom1');
expect(
wrapper
.find('a')
.last()
.getDOMNode<HTMLAnchorElement>().href
).toMatch('prom2');
});
});
...@@ -4,7 +4,6 @@ import React, { Component } from 'react'; ...@@ -4,7 +4,6 @@ import React, { Component } from 'react';
import { PrometheusDatasource } from '../datasource'; import { PrometheusDatasource } from '../datasource';
import { PromQuery } from '../types'; import { PromQuery } from '../types';
import { DataQueryRequest, PanelData } from '@grafana/data'; import { DataQueryRequest, PanelData } from '@grafana/data';
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
interface Props { interface Props {
datasource: PrometheusDatasource; datasource: PrometheusDatasource;
...@@ -29,19 +28,13 @@ export default class PromLink extends Component<Props, State> { ...@@ -29,19 +28,13 @@ export default class PromLink extends Component<Props, State> {
} }
async getExternalLink(panelData: PanelData): Promise<string> { async getExternalLink(panelData: PanelData): Promise<string> {
const { query } = this.props; const { query, datasource } = this.props;
const { request } = panelData; const { request } = panelData;
if (!request) { if (!request) {
return ''; 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 range = request.range;
const start = datasource.getPrometheusTime(range.from, false); const start = datasource.getPrometheusTime(range.from, false);
const end = datasource.getPrometheusTime(range.to, true); const end = datasource.getPrometheusTime(range.to, true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment