grafana/public/app/features/correlations/utils.ts
Piotr Jamróz ae927eab73
Glue: Enrich query results data frames in Explore with correlations to generate static links from correlations (#56295)
* Attach static generic data link to data frames in Explore

* WIP

* Always load correlations config when the query is run

This will be moved to Wrapper.tsx and called only once Explore is mounted

* remove comment

* Load the config when Explore is loaded

* Clean up

* Check for feature toggle, simplify cod

* Simplify the code

* Remove unused code

* Fix types

* Add a test for attaching links

* Revert package.json changes

* Display title provided in the correlation label

* Add missing mocks

* Fix tests

* Merge branch 'main' into ifrost/integration/attach-generic-data-link

# Conflicts:
#	public/app/features/explore/Wrapper.tsx
#	public/app/features/explore/state/main.ts

* Remove redundant async calls

* Do not block Wrapper before correlations are loaded (only delay the query)

* Test showing results after correlations are loaded

* Post-merge fix

* Use more consistent naming

* Avoid null assertions

Co-authored-by: Elfo404 <me@giordanoricci.com>
2022-10-11 14:40:55 +02:00

50 lines
1.7 KiB
TypeScript

import { DataFrame } from '@grafana/data';
import { CorrelationData } from './useCorrelations';
type DataFrameRefIdToDataSourceUid = Record<string, string>;
/**
* Creates data links from provided CorrelationData object
*
* @param dataFrames list of data frames to be processed
* @param correlations list of of possible correlations that can be applied
* @param dataFrameRefIdToDataSourceUid a map that for provided refId references corresponding data source ui
*/
export const attachCorrelationsToDataFrames = (
dataFrames: DataFrame[],
correlations: CorrelationData[],
dataFrameRefIdToDataSourceUid: DataFrameRefIdToDataSourceUid
): DataFrame[] => {
dataFrames.forEach((dataFrame) => {
const frameRefId = dataFrame.refId;
if (!frameRefId) {
return;
}
const dataSourceUid = dataFrameRefIdToDataSourceUid[frameRefId];
const sourceCorrelations = correlations.filter((correlation) => correlation.source.uid === dataSourceUid);
decorateDataFrameWithInternalDataLinks(dataFrame, sourceCorrelations);
});
return dataFrames;
};
const decorateDataFrameWithInternalDataLinks = (dataFrame: DataFrame, correlations: CorrelationData[]) => {
dataFrame.fields.forEach((field) => {
correlations.map((correlation) => {
if (correlation.config?.field === field.name) {
field.config.links = field.config.links || [];
field.config.links.push({
internal: {
query: correlation.config?.target,
datasourceUid: correlation.target.uid,
datasourceName: correlation.target.name,
},
url: '',
title: correlation.label || correlation.target.name,
});
}
});
});
};