might be unstable for versioned data

This commit is contained in:
Nate Kelley 2025-04-15 22:01:21 -06:00
parent 3726915e43
commit 090f7877b7
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
7 changed files with 39 additions and 15 deletions

View File

@ -50,14 +50,15 @@ export const useGetMetricVersionNumber = ({
? versionNumberQueryParam || versionNumberPathParam ? versionNumberQueryParam || versionNumberPathParam
: undefined; : undefined;
const versionNumber = useMemo(() => { const versionNumberX = useMemo(() => {
if (versionNumberProp === null) return undefined; if (versionNumberProp === null) return undefined;
return versionNumberProp || versionNumberFromParams return (
? parseInt(versionNumberFromParams!) versionNumberProp ??
: undefined; (versionNumberFromParams ? parseInt(versionNumberFromParams!) : undefined)
);
}, [versionNumberProp, versionNumberFromParams]); }, [versionNumberProp, versionNumberFromParams]);
return versionNumber; return versionNumberX;
}; };
/** /**
@ -81,9 +82,7 @@ export const useGetMetric = <TData = IBusterMetric>(
const versionNumber = useGetMetricVersionNumber({ versionNumber: versionNumberProp }); const versionNumber = useGetMetricVersionNumber({ versionNumber: versionNumberProp });
const options = useMemo(() => { const options = metricsQueryKeys.metricsGetMetric(id!, versionNumber);
return metricsQueryKeys.metricsGetMetric(id!, versionNumber);
}, [id, versionNumber]);
const queryFn = useMemoizedFn(async () => { const queryFn = useMemoizedFn(async () => {
const result = await getMetric({ id: id!, password, version_number: versionNumber }); const result = await getMetric({ id: id!, password, version_number: versionNumber });
@ -92,6 +91,7 @@ export const useGetMetric = <TData = IBusterMetric>(
const isLatestVersion = const isLatestVersion =
updatedMetric.version_number === last(updatedMetric.versions)?.version_number; updatedMetric.version_number === last(updatedMetric.versions)?.version_number;
if (isLatestVersion) setOriginalMetric(updatedMetric); if (isLatestVersion) setOriginalMetric(updatedMetric);
if (!versionNumber && result?.version_number) { if (!versionNumber && result?.version_number) {
@ -247,6 +247,7 @@ export const useSaveMetric = (params?: { updateOnSave?: boolean }) => {
); );
const newMetric = upgradeMetricToIMetric(data, oldMetric || null); const newMetric = upgradeMetricToIMetric(data, oldMetric || null);
if (updateOnSave && data) { if (updateOnSave && data) {
//We need to update BOTH the versioned and the non-versioned metric
queryClient.setQueryData( queryClient.setQueryData(
metricsQueryKeys.metricsGetMetric(data.id, data.version_number).queryKey, metricsQueryKeys.metricsGetMetric(data.id, data.version_number).queryKey,
newMetric newMetric
@ -375,6 +376,7 @@ export const useShareMetric = () => {
metricsQueryKeys.metricsGetMetric(data.id, data.version_number).queryKey metricsQueryKeys.metricsGetMetric(data.id, data.version_number).queryKey
); );
const upgradedMetric = upgradeMetricToIMetric(data, oldMetric || null); const upgradedMetric = upgradeMetricToIMetric(data, oldMetric || null);
queryClient.setQueryData( queryClient.setQueryData(
metricsQueryKeys.metricsGetMetric(data.id, data.version_number).queryKey, metricsQueryKeys.metricsGetMetric(data.id, data.version_number).queryKey,
upgradedMetric upgradedMetric

View File

@ -170,7 +170,7 @@ const useListMetricVersions = ({
const metricVersionNumber = useChatLayoutContextSelector((x) => x.metricVersionNumber); const metricVersionNumber = useChatLayoutContextSelector((x) => x.metricVersionNumber);
const { data: metric, isFetched } = useGetMetric( const { data: metric } = useGetMetric(
{ {
id: type === 'metric' ? assetId : undefined id: type === 'metric' ? assetId : undefined
}, },

View File

@ -17,7 +17,10 @@ export const PERSISTED_QUERIES = [
queryKeys.metricsGetList().queryKey, queryKeys.metricsGetList().queryKey,
queryKeys.collectionsGetList().queryKey, queryKeys.collectionsGetList().queryKey,
queryKeys.termsGetList.queryKey, queryKeys.termsGetList.queryKey,
queryKeys.datasetsListQueryOptions().queryKey queryKeys.datasetsListQueryOptions().queryKey,
queryKeys.permissionGroupList.queryKey,
queryKeys.datasourceGetList.queryKey,
queryKeys.datasetGroupsList.queryKey
].map(hashKey); ].map(hashKey);
export const PERMANENT_QUERIES = [queryKeys.getCurrencies.queryKey].map(hashKey); export const PERMANENT_QUERIES = [queryKeys.getCurrencies.queryKey].map(hashKey);

View File

@ -1,23 +1,30 @@
import { useQueryClient } from '@tanstack/react-query'; import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useOriginalMetricStore } from './useOriginalMetricStore'; import { useOriginalMetricStore } from './useOriginalMetricStore';
import { useMemoizedFn } from '@/hooks'; import { useMemoizedFn } from '@/hooks';
import { metricsQueryKeys } from '@/api/query_keys/metric'; import { metricsQueryKeys } from '@/api/query_keys/metric';
import { useGetMetric } from '@/api/buster_rest/metrics'; import { useGetMetric } from '@/api/buster_rest/metrics';
import { compareObjectsByKeys } from '@/lib/objects'; import { compareObjectsByKeys } from '@/lib/objects';
import { useMemo } from 'react'; import { useMemo } from 'react';
import last from 'lodash/last';
export const useIsMetricChanged = ({ metricId }: { metricId: string }) => { export const useIsMetricChanged = ({ metricId }: { metricId: string }) => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const originalMetric = useOriginalMetricStore((x) => x.getOriginalMetric(metricId)); const originalMetric = useOriginalMetricStore((x) => x.getOriginalMetric(metricId));
const { data: currentMetric, refetch: refetchCurrentMetric } = useGetMetric( const { data: latestVersionNumber } = useGetMetric(
{ id: metricId }, { id: metricId },
{ select: (x) => last(x.versions)?.version_number }
);
const { data: currentMetric, refetch: refetchCurrentMetric } = useGetMetric(
{ id: metricId, versionNumber: latestVersionNumber },
{ {
select: (x) => ({ select: (x) => ({
name: x.name, name: x.name,
description: x.description, description: x.description,
chart_config: x.chart_config, chart_config: x.chart_config,
file: x.file file: x.file,
updated_at: x.updated_at
}) })
} }
); );

View File

@ -20,7 +20,6 @@ export const useIsMetricReadOnly = ({
} = useGetMetric( } = useGetMetric(
{ id: metricId }, { id: metricId },
{ {
enabled: false,
select: (x) => ({ select: (x) => ({
permission: x.permission, permission: x.permission,
versions: x.versions, versions: x.versions,

View File

@ -33,7 +33,6 @@ export const getChangedTopLevelMessageValues = (
'sql', 'sql',
'file' 'file'
]); ]);
return changes; return changes;
}; };

View File

@ -47,11 +47,25 @@ export const compareObjectsByKeys = <K extends string>(
if (typeof val1 === 'object' && typeof val2 === 'object') { if (typeof val1 === 'object' && typeof val2 === 'object') {
const itWasEqual = isEqual(val1, val2) || isEqual(JSON.stringify(val1), JSON.stringify(val2)); const itWasEqual = isEqual(val1, val2) || isEqual(JSON.stringify(val1), JSON.stringify(val2));
// if (!itWasEqual) {
// console.log('--------------NESTED KEYS NOT EQUAL------------------');
// console.log('KEY', key);
// console.log('ORIGINAL', val1);
// console.log('NEW', val2);
// }
return itWasEqual; return itWasEqual;
} }
const itWasEqual = isEqual(val1, val2); const itWasEqual = isEqual(val1, val2);
// if (!itWasEqual) {
// console.log('--------------KEYS NOT EQUAL------------------');
// console.log('KEY', key);
// console.log('ORIGINAL', val1);
// console.log('NEW', val2);
// }
return itWasEqual; return itWasEqual;
}); });
}; };