Explore: Make getFieldLinksForExplore more reusable (#30134)

* Change signature of getFieldLinksForExplore

* Add comment

* Update public/app/features/explore/utils/links.ts

Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>

Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>
This commit is contained in:
Andrej Ocenas
2021-01-08 13:29:34 +01:00
committed by GitHub
parent cf1c01dd8b
commit e4607a4a19
5 changed files with 45 additions and 20 deletions

View File

@@ -102,7 +102,7 @@ export class LogsContainer extends PureComponent<LogsContainerProps> {
};
getFieldLinks = (field: Field, rowIndex: number) => {
return getFieldLinksForExplore(field, rowIndex, this.props.splitOpen, this.props.range);
return getFieldLinksForExplore({ field, rowIndex, splitOpenFn: this.props.splitOpen, range: this.props.range });
};
render() {

View File

@@ -48,7 +48,7 @@ export class TableContainer extends PureComponent<TableContainerProps> {
// differently and sidestep this getLinks API on a dataframe
for (const field of tableResult.fields) {
field.getLinks = (config: ValueLinkConfig) => {
return getFieldLinksForExplore(field, config.valueRowIndex!, splitOpen, range);
return getFieldLinksForExplore({ field, rowIndex: config.valueRowIndex!, splitOpenFn: splitOpen, range });
};
}
}

View File

@@ -17,7 +17,7 @@ describe('getFieldLinksForExplore', () => {
title: 'external',
url: 'http://regionalhost',
});
const links = getFieldLinksForExplore(field, 0, jest.fn(), range);
const links = getFieldLinksForExplore({ field, rowIndex: 0, splitOpenFn: jest.fn(), range });
expect(links[0].href).toBe('http://regionalhost');
expect(links[0].title).toBe('external');
@@ -28,7 +28,7 @@ describe('getFieldLinksForExplore', () => {
title: '',
url: 'http://regionalhost',
});
const links = getFieldLinksForExplore(field, 0, jest.fn(), range);
const links = getFieldLinksForExplore({ field, rowIndex: 0, splitOpenFn: jest.fn(), range });
expect(links[0].href).toBe('http://regionalhost');
expect(links[0].title).toBe('regionalhost');
@@ -45,7 +45,7 @@ describe('getFieldLinksForExplore', () => {
},
});
const splitfn = jest.fn();
const links = getFieldLinksForExplore(field, 0, splitfn, range);
const links = getFieldLinksForExplore({ field, rowIndex: 0, splitOpenFn: splitfn, range });
expect(links[0].href).toBe(
'/explore?left={"range":{"from":"now-1h","to":"now"},"datasource":"test_ds","queries":[{"query":"query_1"}]}'

View File

@@ -1,6 +1,15 @@
import { Field, LinkModel, TimeRange, mapInternalLinkToExplore, InterpolateFunction } from '@grafana/data';
import {
Field,
LinkModel,
TimeRange,
mapInternalLinkToExplore,
InterpolateFunction,
ScopedVars,
DataFrame,
getFieldDisplayValuesProxy,
} from '@grafana/data';
import { getLinkSrv } from '../../panel/panellinks/link_srv';
import { getTemplateSrv } from '@grafana/runtime';
import { config, getTemplateSrv } from '@grafana/runtime';
import { splitOpen } from '../state/main';
/**
@@ -10,13 +19,16 @@ import { splitOpen } from '../state/main';
* appropriately. This is for example used for transition from log with traceId to trace datasource to show that
* trace.
*/
export const getFieldLinksForExplore = (
field: Field,
rowIndex: number,
splitOpenFn: typeof splitOpen,
range: TimeRange
): Array<LinkModel<Field>> => {
const scopedVars: any = {};
export const getFieldLinksForExplore = (options: {
field: Field;
rowIndex: number;
splitOpenFn?: typeof splitOpen;
range: TimeRange;
vars?: ScopedVars;
dataFrame?: DataFrame;
}): Array<LinkModel<Field>> => {
const { field, vars, splitOpenFn, range, rowIndex, dataFrame } = options;
const scopedVars: any = { ...(vars || {}) };
scopedVars['__value'] = {
value: {
raw: field.values.get(rowIndex),
@@ -24,6 +36,20 @@ export const getFieldLinksForExplore = (
text: 'Raw value',
};
// If we have a dataFrame we can allow referencing other columns and their values in the interpolation.
if (dataFrame) {
scopedVars['__data'] = {
value: {
name: dataFrame.name,
refId: dataFrame.refId,
fields: getFieldDisplayValuesProxy(dataFrame, rowIndex, {
theme: config.theme,
}),
},
text: 'Data',
};
}
return field.config.links
? field.config.links.map(link => {
if (!link.internal) {