From 99b6436418d4ef275e30d735ef67695943b3b5a1 Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Wed, 16 Apr 2025 23:05:37 -0600 Subject: [PATCH] metric updates --- .../metrics/getMetricQueryRequests.ts | 21 +++++++++---------- .../buster_rest/metrics/metricQueryHelpers.ts | 9 ++++---- .../metrics/updateMetricQueryRequests.ts | 1 + web/src/api/query_keys/metric.ts | 6 +++--- .../context/Assets/BusterAssetsProvider.tsx | 2 +- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/web/src/api/buster_rest/metrics/getMetricQueryRequests.ts b/web/src/api/buster_rest/metrics/getMetricQueryRequests.ts index 2ca1c50ae..ea70c6aae 100644 --- a/web/src/api/buster_rest/metrics/getMetricQueryRequests.ts +++ b/web/src/api/buster_rest/metrics/getMetricQueryRequests.ts @@ -31,13 +31,13 @@ export const useGetMetric = ( versionNumber: versionNumberProp }); - const initialOptions = metricsQueryKeys.metricsGetMetric(id!, paramVersionNumber || 'INITIAL'); + const initialOptions = metricsQueryKeys.metricsGetMetric(id!, paramVersionNumber || null); - const initialQueryFn = useMemoizedFn(async (version?: number) => { + const initialQueryFn = useMemoizedFn(async (version?: number | null) => { const result = await getMetric({ id: id!, password, - version_number: version + version_number: version === null ? undefined : version }); const updatedMetric = upgradeMetricToIMetric(result, null); const isLatestVersion = @@ -67,10 +67,9 @@ export const useGetMetric = ( }); return useQuery({ - queryKey: metricsQueryKeys.metricsGetMetric(id!, selectedVersionNumber!).queryKey, - enabled: - !!latestVersionNumber && !!selectedVersionNumber && isFetchedInitial && !isErrorInitial, - queryFn: () => initialQueryFn(selectedVersionNumber!), + queryKey: metricsQueryKeys.metricsGetMetric(id!, selectedVersionNumber).queryKey, + enabled: !!latestVersionNumber && isFetchedInitial && !isErrorInitial, + queryFn: () => initialQueryFn(selectedVersionNumber), select: params?.select }); }; @@ -122,7 +121,7 @@ export const useGetMetricData = ( const queryFn = useMemoizedFn(async () => { const result = await getMetricData({ id: id!, - version_number: selectedVersionNumber, + version_number: selectedVersionNumber || undefined, password }); @@ -130,7 +129,7 @@ export const useGetMetricData = ( }); return useQuery({ - ...metricsQueryKeys.metricsGetData(id!, selectedVersionNumber), + ...metricsQueryKeys.metricsGetData(id!, selectedVersionNumber!), queryFn, enabled: () => { return ( @@ -148,7 +147,7 @@ export const useGetMetricData = ( }; export const prefetchGetMetricDataClient = async ( - { id, version_number }: { id: string; version_number: number | undefined }, + { id, version_number }: { id: string; version_number: number }, queryClient: QueryClient ) => { const options = metricsQueryKeys.metricsGetData(id, version_number); @@ -163,7 +162,7 @@ export const prefetchGetMetricDataClient = async ( export const usePrefetchGetMetricDataClient = () => { const queryClient = useQueryClient(); - return useMemoizedFn(({ id, versionNumber }: { id: string; versionNumber: number | undefined }) => + return useMemoizedFn(({ id, versionNumber }: { id: string; versionNumber: number }) => prefetchGetMetricDataClient({ id, version_number: versionNumber }, queryClient) ); }; diff --git a/web/src/api/buster_rest/metrics/metricQueryHelpers.ts b/web/src/api/buster_rest/metrics/metricQueryHelpers.ts index 6f344555a..804cde968 100644 --- a/web/src/api/buster_rest/metrics/metricQueryHelpers.ts +++ b/web/src/api/buster_rest/metrics/metricQueryHelpers.ts @@ -1,6 +1,6 @@ import { useParams, useSearchParams } from 'next/navigation'; import { useMemo } from 'react'; -import { hashKey, Query, QueryFilters, useQueryClient } from '@tanstack/react-query'; +import { Query, useQueryClient } from '@tanstack/react-query'; import { IBusterMetric } from '@/api/asset_interfaces/metric'; import { metricsQueryKeys } from '@/api/query_keys/metric'; import last from 'lodash/last'; @@ -25,7 +25,8 @@ export const useGetMetricVersionNumber = (props?: { versionNumber?: number | nul const latestVersionNumber = useGetLatestMetricVersion({ metricId: metricIdPathParam! }); - const selectedVersionNumber: number = useMemo(() => { + const selectedVersionNumber: number | null = useMemo(() => { + if (versionNumberProp === null) return null; return paramVersionNumber || latestVersionNumber || 0; }, [paramVersionNumber, latestVersionNumber]); @@ -51,7 +52,7 @@ const useGetLatestMetricVersion = ({ metricId }: { metricId: string }) => { const queryClient = useQueryClient(); const memoizedKey = useMemo(() => { - return metricsQueryKeys.metricsGetMetric(metricId, 'INITIAL').queryKey.slice(0, -1); + return metricsQueryKeys.metricsGetMetric(metricId, null).queryKey.slice(0, -1); }, [metricId]); const queries = queryClient.getQueriesData({ @@ -83,7 +84,7 @@ export const useGetLatestMetricVersionNumber = () => { const method = useMemoizedFn((metricId: string) => { const queries = queryClient.getQueriesData({ - queryKey: metricsQueryKeys.metricsGetMetric(metricId, 'INITIAL').queryKey.slice(0, -1), + queryKey: metricsQueryKeys.metricsGetMetric(metricId, null).queryKey.slice(0, -1), predicate: filterMetricPredicate }); diff --git a/web/src/api/buster_rest/metrics/updateMetricQueryRequests.ts b/web/src/api/buster_rest/metrics/updateMetricQueryRequests.ts index 4d1beb867..24764ee20 100644 --- a/web/src/api/buster_rest/metrics/updateMetricQueryRequests.ts +++ b/web/src/api/buster_rest/metrics/updateMetricQueryRequests.ts @@ -62,6 +62,7 @@ export const useSaveMetric = (params?: { updateOnSave?: boolean }) => { const metric = queryClient.getQueryData( metricsQueryKeys.metricsGetMetric(id, selectedVersionNumber).queryKey ); + if (!metric) return; const metricVersionNumber = metric?.version_number; const metricData = queryClient.getQueryData( metricsQueryKeys.metricsGetData(id, metricVersionNumber).queryKey diff --git a/web/src/api/query_keys/metric.ts b/web/src/api/query_keys/metric.ts index 1df897f84..dd04f9113 100644 --- a/web/src/api/query_keys/metric.ts +++ b/web/src/api/query_keys/metric.ts @@ -6,9 +6,9 @@ import type { } from '@/api/asset_interfaces/metric'; import { type listMetrics } from '../buster_rest/metrics'; -export const metricsGetMetric = (metricId: string, version_number: number | 'INITIAL') => { +export const metricsGetMetric = (metricId: string, version_number: number | null) => { return queryOptions({ - queryKey: ['metrics', 'get', metricId, version_number] as const, + queryKey: ['metrics', 'get', metricId, version_number || 'INITIAL'] as const, staleTime: 30 * 60 * 1000 }); }; @@ -22,7 +22,7 @@ export const metricsGetList = ( initialDataUpdatedAt: 0 }); -export const metricsGetData = (id: string, version_number?: number | undefined) => +export const metricsGetData = (id: string, version_number: number) => queryOptions({ queryKey: ['metrics', 'data', id, version_number || 'INITIAL'] as const, staleTime: 3 * 60 * 60 * 1000 // 3 hours, diff --git a/web/src/context/Assets/BusterAssetsProvider.tsx b/web/src/context/Assets/BusterAssetsProvider.tsx index 1f87b23cb..5381cbc81 100644 --- a/web/src/context/Assets/BusterAssetsProvider.tsx +++ b/web/src/context/Assets/BusterAssetsProvider.tsx @@ -26,7 +26,7 @@ const useBusterAssets = () => { const invalidateAssetData = useMemoizedFn(async (assetId: string, type: ShareAssetType) => { if (type === 'metric') { await queryClient.invalidateQueries({ - queryKey: queryKeys.metricsGetMetric(assetId, 'INITIAL').queryKey + queryKey: queryKeys.metricsGetMetric(assetId, null).queryKey }); } else if (type === 'dashboard') { await queryClient.invalidateQueries({