2020-10-14 17:03:14 -05:00
|
|
|
import { Field, LinkModel, TimeRange, mapInternalLinkToExplore } from '@grafana/data';
|
2020-06-30 07:51:04 -05:00
|
|
|
import { getLinkSrv } from '../../panel/panellinks/link_srv';
|
2020-12-01 12:10:23 -06:00
|
|
|
import { getTemplateSrv } from '@grafana/runtime';
|
2020-11-09 07:48:24 -06:00
|
|
|
import { splitOpen } from '../state/main';
|
2020-04-22 12:42:28 -05:00
|
|
|
|
|
|
|
/**
|
2020-05-12 10:20:00 -05:00
|
|
|
* Get links from the field of a dataframe and in addition check if there is associated
|
2020-04-22 12:42:28 -05:00
|
|
|
* metadata with datasource in which case we will add onClick to open the link in new split window. This assumes
|
|
|
|
* that we just supply datasource name and field value and Explore split window will know how to render that
|
|
|
|
* appropriately. This is for example used for transition from log with traceId to trace datasource to show that
|
|
|
|
* trace.
|
|
|
|
*/
|
2020-06-30 07:51:04 -05:00
|
|
|
export const getFieldLinksForExplore = (
|
2020-04-22 12:42:28 -05:00
|
|
|
field: Field,
|
|
|
|
rowIndex: number,
|
|
|
|
splitOpenFn: typeof splitOpen,
|
|
|
|
range: TimeRange
|
2020-06-30 07:51:04 -05:00
|
|
|
): Array<LinkModel<Field>> => {
|
|
|
|
const scopedVars: any = {};
|
|
|
|
scopedVars['__value'] = {
|
|
|
|
value: {
|
|
|
|
raw: field.values.get(rowIndex),
|
|
|
|
},
|
|
|
|
text: 'Raw value',
|
|
|
|
};
|
2020-04-23 13:47:54 -05:00
|
|
|
|
2020-06-30 07:51:04 -05:00
|
|
|
return field.config.links
|
|
|
|
? field.config.links.map(link => {
|
|
|
|
if (!link.internal) {
|
|
|
|
const linkModel = getLinkSrv().getDataLinkUIModel(link, scopedVars, field);
|
|
|
|
if (!linkModel.title) {
|
|
|
|
linkModel.title = getTitleFromHref(linkModel.href);
|
|
|
|
}
|
|
|
|
return linkModel;
|
|
|
|
} else {
|
2020-12-01 12:10:23 -06:00
|
|
|
return mapInternalLinkToExplore({
|
|
|
|
link,
|
|
|
|
internalLink: link.internal,
|
|
|
|
scopedVars: scopedVars,
|
|
|
|
range,
|
|
|
|
field,
|
2020-06-30 07:51:04 -05:00
|
|
|
onClickFn: splitOpenFn,
|
|
|
|
replaceVariables: getTemplateSrv().replace.bind(getTemplateSrv()),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: [];
|
|
|
|
};
|
2020-04-22 12:42:28 -05:00
|
|
|
|
2020-06-30 07:51:04 -05:00
|
|
|
function getTitleFromHref(href: string): string {
|
|
|
|
// The URL constructor needs the url to have protocol
|
|
|
|
if (href.indexOf('://') < 0) {
|
|
|
|
// Doesn't really matter what protocol we use.
|
|
|
|
href = `http://${href}`;
|
|
|
|
}
|
|
|
|
let title;
|
|
|
|
try {
|
|
|
|
const parsedUrl = new URL(href);
|
|
|
|
title = parsedUrl.hostname;
|
|
|
|
} catch (_e) {
|
|
|
|
// Should be good enough fallback, user probably did not input valid url.
|
|
|
|
title = href;
|
|
|
|
}
|
|
|
|
return title;
|
2020-04-22 12:42:28 -05:00
|
|
|
}
|