From 5613a9ffbe8988c333b44197c373e7480c8fa981 Mon Sep 17 00:00:00 2001 From: Kristina Durivage Date: Thu, 13 Nov 2025 12:43:45 -0600 Subject: [PATCH] start create correlation, attempt to fix target type --- apps/correlations/kinds/correlations.cue | 9 +++- .../features/correlations/useCorrelations.ts | 53 ++++++++++++++----- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/apps/correlations/kinds/correlations.cue b/apps/correlations/kinds/correlations.cue index defd0bacc1e..6e458900462 100644 --- a/apps/correlations/kinds/correlations.cue +++ b/apps/correlations/kinds/correlations.cue @@ -38,9 +38,16 @@ but for query correlations, target is the json for populating the query editor, for every datasource right now this is resolving to require an object of objects, like item.spec.config.target.url is expecting an object -instead of a string. how do i fix this? */ +instead of a string. how do i fix this? + +Already tried and unsuccessful TargetSpec: [string]: _ +*/ +TargetSpec: { + [string]: _ +} + TransformationSpec: { type: "regex" | "logfmt" expression: string diff --git a/public/app/features/correlations/useCorrelations.ts b/public/app/features/correlations/useCorrelations.ts index b473973bc21..5f391d26f37 100644 --- a/public/app/features/correlations/useCorrelations.ts +++ b/public/app/features/correlations/useCorrelations.ts @@ -15,6 +15,7 @@ import { CorrelationQuery, } from '@grafana/runtime'; import { useGrafana } from 'app/core/context/GrafanaContext'; +import { dispatch } from 'app/store/store'; import { Correlation, @@ -109,7 +110,7 @@ const toEnrichedCorrelationDataK8s = (item: CorrelationK8s): CorrelationData | u config: { field: item.spec.config.field, target: { - url: item.spec.config.target.url.url || '', // todo this is wrong, fix in spec + url: item.spec.config.target.url || '', // todo this is wrong, fix in spec }, transformations: [], // todo fix }, @@ -184,17 +185,45 @@ export const useCorrelations = () => { ); const [createInfo, create] = useAsyncFn<(params: CreateCorrelationParams) => Promise>( - ({ sourceUID, ...correlation }) => - backend - .post(`/api/datasources/uid/${sourceUID}/correlations`, correlation) - .then((response) => { - const enrichedCorrelation = toEnrichedCorrelationData(response.result); - if (enrichedCorrelation !== undefined) { - return enrichedCorrelation; - } else { - throw new Error('invalid sourceUID'); - } - }), + async ({ sourceUID, ...correlation }) => { + if (config.featureToggles.kubernetesCorrelations) { + // todo, fix once target is fixed + let target; + if (correlation.type === 'external') { + target = { url: { url: correlation.config.target.url } }; + } else { + target = { target: { ...correlation.config.target } }; + } + + const result = await dispatch( + correlationAPIv0alpha1.endpoints.createCorrelation.initiate({ + correlation: { + apiVersion: 'correlations.grafana.app/v0alpha1', + kind: 'Correlations', + metadata: {}, + spec: { + ...correlation, + label: correlation.label ?? '', + source: { name: sourceUID, group: '' }, + config: { ...correlation.config, target: target, transformations: [] }, + }, + }, + }) + ); + return result; + } else { + return backend + .post(`/api/datasources/uid/${sourceUID}/correlations`, correlation) + .then((response) => { + const enrichedCorrelation = toEnrichedCorrelationData(response.result); + if (enrichedCorrelation !== undefined) { + return enrichedCorrelation; + } else { + throw new Error('invalid sourceUID'); + } + }); + } + }, [backend] );