diff --git a/pkg/registry/apps/correlations/legacy_storage.go b/pkg/registry/apps/correlations/legacy_storage.go index 02ab19bd2d2..23967e12807 100644 --- a/pkg/registry/apps/correlations/legacy_storage.go +++ b/pkg/registry/apps/correlations/legacy_storage.go @@ -65,7 +65,7 @@ func (s *legacyStorage) List(ctx context.Context, options *internalversion.ListO if options.FieldSelector != nil { for _, r := range options.FieldSelector.Requirements() { switch r.Field { - case "metadata.name": + case "spec.datasource.name": switch r.Operator { case selection.Equals, selection.DoubleEquals: uids = []string{r.Value} diff --git a/public/app/features/correlations/CorrelationsPage.tsx b/public/app/features/correlations/CorrelationsPage.tsx index 762cc0aa4cd..1a0568a4540 100644 --- a/public/app/features/correlations/CorrelationsPage.tsx +++ b/public/app/features/correlations/CorrelationsPage.tsx @@ -5,7 +5,7 @@ import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { Status } from '@grafana/api-clients/rtkq/correlations/v0alpha1'; import { DataSourceInstanceSettings, GrafanaTheme2 } from '@grafana/data'; import { Trans, t } from '@grafana/i18n'; -import { CorrelationData, CorrelationsData, isFetchError, reportInteraction } from '@grafana/runtime'; +import { CorrelationData, CorrelationsData, FetchError, isFetchError, reportInteraction } from '@grafana/runtime'; import { Badge, Button, @@ -31,7 +31,7 @@ import { EmptyCorrelationsCTA } from './components/EmptyCorrelationsCTA'; import type { Correlation, GetCorrelationsParams, RemoveCorrelationParams } from './types'; type CorrelationsPageProps = { - fetchCorrelations: (params: GetCorrelationsParams) => Promise; + fetchCorrelations: (params: GetCorrelationsParams) => Promise | CorrelationsData; correlations?: CorrelationsData; isLoading: boolean; changePageFn?: (page: number) => void; @@ -41,7 +41,7 @@ type CorrelationsPageProps = { } | Status >; - error?: Error; + error?: Error | FetchError; hasNextPage?: boolean; }; diff --git a/public/app/features/correlations/CorrelationsPageWrapper.tsx b/public/app/features/correlations/CorrelationsPageWrapper.tsx index 32a81e4ec1e..f511252be60 100644 --- a/public/app/features/correlations/CorrelationsPageWrapper.tsx +++ b/public/app/features/correlations/CorrelationsPageWrapper.tsx @@ -1,10 +1,11 @@ -import { useRef, useState } from 'react'; +import { useState } from 'react'; +import { handleRequestError } from '@grafana/api-clients'; import { useDeleteCorrelationMutation } from '@grafana/api-clients/rtkq/correlations/v0alpha1'; -import { config, CorrelationsData } from '@grafana/runtime'; +import { config } from '@grafana/runtime'; import CorrelationsPage from './CorrelationsPage'; -import { RemoveCorrelationParams } from './types'; +import { GetCorrelationsParams, RemoveCorrelationParams } from './types'; import { useCorrelations } from './useCorrelations'; import { useCorrelationsK8s } from './useCorrelationsK8s'; @@ -23,19 +24,17 @@ export function CorrelationsPageLegacy() { function CorrelationsPageAppPlatform() { const [page, setPage] = useState(1); - let totalItems = useRef(0); const limit = 100; - const { currentData, isLoading, error, remainingItems, doesContinue } = useCorrelationsK8s(limit, page); + const { currentData, isLoading, error, doesContinue } = useCorrelationsK8s(limit, page); const [deleteCorrelation] = useDeleteCorrelationMutation(); - if (page === 1) { - totalItems.current = remainingItems; - } // we cant do a straight refetch, we have to pass in new pages if necessary - const enhRefetch = (): Promise => { - return new Promise(() => currentData); + const enhRefetch = (params: GetCorrelationsParams) => { + return { correlations: currentData, page: params.page, limit, totalCount: 0 }; }; + const fmtedError = error ? handleRequestError(error) : undefined; + return ( { const deleteData = deleteCorrelation({ name: params.uid }); return deleteData.unwrap(); diff --git a/public/app/features/correlations/useCorrelationsK8s.ts b/public/app/features/correlations/useCorrelationsK8s.ts index a6e9d2c8df5..f78a972ba0e 100644 --- a/public/app/features/correlations/useCorrelationsK8s.ts +++ b/public/app/features/correlations/useCorrelationsK8s.ts @@ -1,49 +1,54 @@ +import { handleRequestError } from '@grafana/api-clients'; import { Correlation as CorrelationK8s, useListCorrelationQuery, } from '@grafana/api-clients/rtkq/correlations/v0alpha1'; +import { SupportedTransformationType } from '@grafana/data'; import { CorrelationData, CorrelationExternal, CorrelationQuery } from '@grafana/runtime'; import { toEnrichedCorrelationData } from './useCorrelations'; export const toEnrichedCorrelationDataK8s = (item: CorrelationK8s): CorrelationData | undefined => { - if (item.metadata.name !== undefined) { - const baseCor = { - uid: item.metadata.name, - sourceUID: item.spec.source.name, //todo - label: item.spec.label, - description: item.spec.description, - provisioned: false, // todo, - }; + const baseCor = { + uid: item.metadata.name!, + sourceUID: item.spec.source.name, //todo + label: item.spec.label, + description: item.spec.description, + provisioned: false, // todo + }; - if (item.spec.type === 'external') { - const extCorr: CorrelationExternal = { - ...baseCor, - type: 'external', - config: { - field: item.spec.config.field, - target: { - url: item.spec.config?.target?.url || '', - }, - transformations: [], // todo fix + const transformationsFmt = item.spec.config.transformations?.map((trans) => { + return { + ...trans, + type: trans.type === 'regex' ? SupportedTransformationType.Regex : SupportedTransformationType.Logfmt, + }; + }); + + if (item.spec.type === 'external') { + const extCorr: CorrelationExternal = { + ...baseCor, + type: 'external', + config: { + field: item.spec.config.field, + target: { + url: item.spec.config?.target?.url || '', }, - }; - return toEnrichedCorrelationData(extCorr); - } else { - const queryCorr: CorrelationQuery = { - ...baseCor, - type: 'query', - targetUID: item.spec.target?.name || '', // todo - config: { - field: item.spec.config.field, - target: item.spec.config.target, - transformations: [], // todo fix - }, - }; - return toEnrichedCorrelationData(queryCorr); - } + transformations: transformationsFmt, + }, + }; + return toEnrichedCorrelationData(extCorr); } else { - return undefined; + const queryCorr: CorrelationQuery = { + ...baseCor, + type: 'query', + targetUID: item.spec.target?.name || '', // todo + config: { + field: item.spec.config.field, + target: item.spec.config.target, + transformations: transformationsFmt, + }, + }; + return toEnrichedCorrelationData(queryCorr); } }; @@ -60,13 +65,13 @@ export const useCorrelationsK8s = (limit = 100, page: number) => { const enrichedCorrelations = currentData !== undefined - ? pagedData.map((item) => toEnrichedCorrelationDataK8s(item)).filter((i) => i !== undefined) + ? pagedData + .filter((i) => i.metadata.name !== undefined) + .map((item) => toEnrichedCorrelationDataK8s(item)) + .filter((i) => i !== undefined) : []; - let fmtedError = undefined; - if (error !== undefined) { - fmtedError = new Error(error?.toString() ?? 'useListCorrelationQuery error'); - } + const fmtedError = error ? handleRequestError(error) : undefined; return { currentData: enrichedCorrelations,