mirror of https://github.com/buster-so/buster.git
shared handler
This commit is contained in:
parent
1025371a69
commit
1ee690b0eb
|
@ -158,8 +158,14 @@ export const prefetchGetDashboard = async (
|
|||
shouldInitializeMetrics: true,
|
||||
});
|
||||
};
|
||||
await queryClient.prefetchQuery({
|
||||
...dashboardQueryKeys.dashboardGetDashboard(id, chosenVersionNumber),
|
||||
queryFn,
|
||||
});
|
||||
|
||||
const queryKey = dashboardQueryKeys.dashboardGetDashboard(id, chosenVersionNumber)?.queryKey;
|
||||
const existingData = queryClient.getQueryData(queryKey);
|
||||
if (!existingData) {
|
||||
await queryClient.prefetchQuery({
|
||||
...dashboardQueryKeys.dashboardGetDashboard(id, chosenVersionNumber),
|
||||
queryFn,
|
||||
});
|
||||
}
|
||||
return existingData || queryClient.getQueryData(queryKey);
|
||||
};
|
||||
|
|
|
@ -169,21 +169,6 @@ export const useGetMetricData = <TData = BusterMetricDataExtended>(
|
|||
});
|
||||
};
|
||||
|
||||
export const prefetchGetMetric = async (
|
||||
{ id, version_number }: { id: string; version_number: number | undefined },
|
||||
queryClient: QueryClient
|
||||
) => {
|
||||
const options = metricsQueryKeys.metricsGetMetric(id, version_number || 'LATEST');
|
||||
const existingData = queryClient.getQueryData(options.queryKey);
|
||||
if (!existingData) {
|
||||
await queryClient.prefetchQuery({
|
||||
...options,
|
||||
queryFn: () => getMetric({ id, version_number }),
|
||||
});
|
||||
}
|
||||
return existingData;
|
||||
};
|
||||
|
||||
export const prefetchGetMetricDataClient = async (
|
||||
{ id, version_number }: { id: string; version_number: number },
|
||||
queryClient: QueryClient
|
||||
|
|
|
@ -8,14 +8,21 @@ export const prefetchGetMetric = async (
|
|||
queryClientProp?: QueryClient
|
||||
) => {
|
||||
const queryClient = queryClientProp || new QueryClient();
|
||||
const queryKey = metricsQueryKeys.metricsGetMetric(
|
||||
params.id,
|
||||
params.version_number || 'LATEST'
|
||||
)?.queryKey;
|
||||
const existingData = queryClient.getQueryData(queryKey);
|
||||
|
||||
await queryClient.prefetchQuery({
|
||||
...metricsQueryKeys.metricsGetMetric(params.id, params.version_number || 'LATEST'),
|
||||
queryFn: async () => {
|
||||
const result = await getMetric(params);
|
||||
return upgradeMetricToIMetric(result, null);
|
||||
},
|
||||
});
|
||||
if (!existingData) {
|
||||
await queryClient.prefetchQuery({
|
||||
...metricsQueryKeys.metricsGetMetric(params.id, params.version_number || 'LATEST'),
|
||||
queryFn: async () => {
|
||||
const result = await getMetric(params);
|
||||
return upgradeMetricToIMetric(result, null);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return queryClient;
|
||||
return existingData || queryClient.getQueryData(queryKey);
|
||||
};
|
||||
|
|
|
@ -25,13 +25,12 @@ import type {
|
|||
import { mainApi, mainApiV2 } from '../instances';
|
||||
|
||||
export const getMetric = async (params: GetMetricRequest): Promise<GetMetricResponse> => {
|
||||
console.log('getMetric1!!!!');
|
||||
return mainApi
|
||||
.get<GetMetricResponse>(`/metric_files/${params.id}`, {
|
||||
params,
|
||||
})
|
||||
.then((res) => {
|
||||
console.log('getMetric res');
|
||||
console.log('getMetric res', typeof window !== 'undefined');
|
||||
return res.data;
|
||||
});
|
||||
};
|
||||
|
|
|
@ -75,17 +75,21 @@ export const prefetchGetReportsListClient = async (
|
|||
|
||||
export const prefetchGetReport = async (
|
||||
reportId: string,
|
||||
version_number: number | undefined,
|
||||
queryClientProp?: QueryClient
|
||||
report_version_number: number | undefined,
|
||||
queryClient: QueryClient
|
||||
) => {
|
||||
const queryClient = queryClientProp || new QueryClient();
|
||||
const version_number = report_version_number || 'LATEST';
|
||||
|
||||
await queryClient.prefetchQuery({
|
||||
...reportsQueryKeys.reportsGetReport(reportId, version_number || 'LATEST'),
|
||||
queryFn: () => getReportById(reportId),
|
||||
});
|
||||
const queryKey = reportsQueryKeys.reportsGetReport(reportId, version_number)?.queryKey;
|
||||
const existingData = queryClient.getQueryData(queryKey);
|
||||
if (!existingData) {
|
||||
await queryClient.prefetchQuery({
|
||||
...reportsQueryKeys.reportsGetReport(reportId, version_number || 'LATEST'),
|
||||
queryFn: () => getReportById(reportId),
|
||||
});
|
||||
}
|
||||
|
||||
return queryClient;
|
||||
return existingData || queryClient.getQueryData(queryKey);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
import type { AssetType } from '@buster/server-shared/assets';
|
||||
import type { QueryClient } from '@tanstack/react-query';
|
||||
import { z } from 'zod';
|
||||
import { prefetchGetMetric } from '@/api/buster_rest/metrics';
|
||||
|
||||
export const validateSearch = z.object({
|
||||
metric_version_number: z.coerce.number().optional(),
|
||||
});
|
||||
|
||||
export const loader = async ({
|
||||
params: { metricId },
|
||||
context: { queryClient },
|
||||
deps: { metric_version_number },
|
||||
}: {
|
||||
params: { metricId: string };
|
||||
deps: { metric_version_number?: number };
|
||||
context: { queryClient: QueryClient };
|
||||
}): Promise<{ title: string | undefined }> => {
|
||||
const data = await prefetchGetMetric(
|
||||
{ id: metricId, version_number: metric_version_number },
|
||||
queryClient
|
||||
);
|
||||
return {
|
||||
title: data?.name,
|
||||
};
|
||||
};
|
||||
|
||||
export const staticData = {
|
||||
assetType: 'metric' as AssetType,
|
||||
};
|
||||
|
||||
export const head = ({ loaderData }: { loaderData?: { title: string | undefined } } = {}) => ({
|
||||
meta: [
|
||||
{ title: loaderData?.title || 'Metric' },
|
||||
{ name: 'description', content: 'View detailed metric analysis and insights' },
|
||||
{ name: 'og:title', content: 'Metric' },
|
||||
{ name: 'og:description', content: 'View detailed metric analysis and insights' },
|
||||
],
|
||||
});
|
|
@ -0,0 +1,34 @@
|
|||
import type { QueryClient } from '@tanstack/react-query';
|
||||
import { prefetchGetDashboard } from '@/api/buster_rest/dashboards';
|
||||
import { prefetchGetMetric } from '@/api/buster_rest/metrics';
|
||||
import { prefetchGetReport } from '@/api/buster_rest/reports';
|
||||
|
||||
export const createDashboardLoader = async ({
|
||||
params: { dashboardId },
|
||||
context: { queryClient },
|
||||
deps: { dashboard_version_number },
|
||||
}: {
|
||||
params: { dashboardId: string };
|
||||
deps: { dashboard_version_number?: number };
|
||||
context: { queryClient: QueryClient };
|
||||
}): Promise<{ title: string | undefined }> => {
|
||||
const data = await prefetchGetDashboard(dashboardId, dashboard_version_number, queryClient);
|
||||
return {
|
||||
title: data?.dashboard?.name,
|
||||
};
|
||||
};
|
||||
|
||||
export const createReportLoader = async ({
|
||||
params: { reportId },
|
||||
context: { queryClient },
|
||||
deps: { report_version_number },
|
||||
}: {
|
||||
params: { reportId: string };
|
||||
deps: { report_version_number?: number };
|
||||
context: { queryClient: QueryClient };
|
||||
}): Promise<{ title: string | undefined }> => {
|
||||
const data = await prefetchGetReport(reportId, report_version_number, queryClient);
|
||||
return {
|
||||
title: data?.name,
|
||||
};
|
||||
};
|
|
@ -6,12 +6,16 @@ import {
|
|||
useParams,
|
||||
useSearch,
|
||||
} from '@tanstack/react-router';
|
||||
import { z } from 'zod';
|
||||
import { getTitle as getAssetTitle } from '@/api/buster_rest/title';
|
||||
import { AppAssetCheckLayout, type AppAssetCheckLayoutProps } from '@/layouts/AppAssetCheckLayout';
|
||||
import { getAssetIdAndVersionNumber } from '@/layouts/AppAssetCheckLayout/preloadAsset';
|
||||
|
||||
export const Route = createFileRoute('/app/_app/_asset')({
|
||||
component: RouteComponent,
|
||||
loaderDeps: ({ search }) => {
|
||||
return search;
|
||||
},
|
||||
context: () => ({
|
||||
getAssetTitle,
|
||||
}),
|
||||
|
|
|
@ -1,36 +1,9 @@
|
|||
import type { AssetType } from '@buster/server-shared/assets';
|
||||
import { createFileRoute, Link } from '@tanstack/react-router';
|
||||
import { z } from 'zod';
|
||||
import { prefetchGetMetric } from '@/api/buster_rest/metrics/getMetricQueryRequests';
|
||||
|
||||
const searchParamsSchema = z.object({
|
||||
metric_version_number: z.coerce.number().optional(),
|
||||
});
|
||||
import { createFileRoute } from '@tanstack/react-router';
|
||||
import * as metricServerContext from '@/context/BusterAssets/metricServerAssetContext';
|
||||
|
||||
export const Route = createFileRoute('/app/_app/_asset/metrics/$metricId')({
|
||||
loader: async ({ params, context }) => {
|
||||
const title = await context.getAssetTitle({
|
||||
assetId: params.metricId,
|
||||
assetType: 'metric',
|
||||
});
|
||||
await prefetchGetMetric({ id: params.metricId, version_number: 1 }, context.queryClient);
|
||||
return {
|
||||
title,
|
||||
};
|
||||
},
|
||||
staticData: {
|
||||
assetType: 'metric' as Extract<AssetType, 'metric'>,
|
||||
},
|
||||
validateSearch: searchParamsSchema,
|
||||
head: ({ loaderData }) => ({
|
||||
meta: [
|
||||
{ title: loaderData?.title || 'Metric' },
|
||||
{ name: 'description', content: 'View detailed metric analysis and insights' },
|
||||
{ name: 'og:title', content: 'Metric' },
|
||||
{ name: 'og:description', content: 'View detailed metric analysis and insights' },
|
||||
],
|
||||
}),
|
||||
component: RouteComponent,
|
||||
...metricServerContext,
|
||||
});
|
||||
|
||||
function RouteComponent() {
|
||||
|
|
|
@ -11,16 +11,4 @@ export const Route = createFileRoute('/app/_app/metrics/')({
|
|||
],
|
||||
}),
|
||||
component: MetricListContainer,
|
||||
loader: async ({ context }) => {
|
||||
console.log('metrics index loader');
|
||||
return {
|
||||
swag: true,
|
||||
};
|
||||
},
|
||||
beforeLoad: ({ context }) => {
|
||||
console.log('metrics index beforeLoad');
|
||||
return {
|
||||
swagger: true,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue