🏗️ Migrate dashdot storage to tRPC

This commit is contained in:
Meier Lukas
2023-06-10 13:26:33 +02:00
parent 05e01286d4
commit b1adcf673f
4 changed files with 30 additions and 31 deletions

View File

@@ -42,4 +42,26 @@ export const dashDotRouter = createTRPCRouter({
}); });
return response.data; return response.data;
}), }),
storage: publicProcedure
.input(
z.object({
url: dashDotUrlSchema.transform(removeLeadingSlash),
})
)
.output(z.array(z.number()))
.query(async ({ input }) => {
const response = await axios.get(`${input.url}/load/storage`).catch((error) => {
if (error.response.status === 404) {
throw new TRPCError({
code: 'NOT_FOUND',
message: 'Unable to find specified dash-dot',
});
}
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
});
});
return response.data;
}),
}); });

View File

@@ -1,20 +1,18 @@
import { Group, Stack, Text } from '@mantine/core'; import { Group, Stack, Text } from '@mantine/core';
import { useQuery } from '@tanstack/react-query';
import axios from 'axios';
import { useTranslation } from 'next-i18next'; import { useTranslation } from 'next-i18next';
import { useConfigContext } from '../../config/provider'; import { api } from '~/utils/api';
import { bytes } from '../../tools/bytesHelper'; import { bytes } from '../../tools/bytesHelper';
import { percentage } from '../../tools/shared/math/percentage.tool'; import { percentage } from '../../tools/shared/math/percentage.tool';
import { DashDotInfo } from './DashDotCompactNetwork'; import { DashDotInfo } from './DashDotCompactNetwork';
interface DashDotCompactStorageProps { interface DashDotCompactStorageProps {
info: DashDotInfo; info: DashDotInfo;
widgetId: string; url: string;
} }
export const DashDotCompactStorage = ({ info, widgetId }: DashDotCompactStorageProps) => { export const DashDotCompactStorage = ({ info, url }: DashDotCompactStorageProps) => {
const { t } = useTranslation('modules/dashdot'); const { t } = useTranslation('modules/dashdot');
const { data: storageLoad } = useDashDotStorage(widgetId); const { data: storageLoad } = useDashDotStorage(url);
const totalUsed = calculateTotalLayoutSize({ const totalUsed = calculateTotalLayoutSize({
layout: storageLoad ?? [], layout: storageLoad ?? [],
@@ -55,25 +53,7 @@ interface CalculateTotalLayoutSizeProps<TLayoutItem> {
key?: keyof TLayoutItem; key?: keyof TLayoutItem;
} }
const useDashDotStorage = (widgetId: string) => { const useDashDotStorage = (url: string) =>
const { name: configName, config } = useConfigContext(); api.dashDot.storage.useQuery({
url,
return useQuery({
queryKey: [
'dashdot/storage',
{
configName,
url: config?.widgets.find((x) => x.type === 'dashdot')?.properties.url,
widgetId,
},
],
queryFn: () => fetchDashDotStorageLoad(configName, widgetId),
}); });
};
async function fetchDashDotStorageLoad(configName: string | undefined, widgetId: string) {
if (!configName) throw new Error('configName is undefined');
return (await (
await axios.get('/api/modules/dashdot/storage', { params: { configName, widgetId } })
).data) as number[];
}

View File

@@ -11,7 +11,6 @@ interface DashDotGraphProps {
dashDotUrl: string; dashDotUrl: string;
usePercentages: boolean; usePercentages: boolean;
info: DashDotInfo; info: DashDotInfo;
widgetId: string;
} }
export const DashDotGraph = ({ export const DashDotGraph = ({
@@ -22,13 +21,12 @@ export const DashDotGraph = ({
dashDotUrl, dashDotUrl,
usePercentages, usePercentages,
info, info,
widgetId,
}: DashDotGraphProps) => { }: DashDotGraphProps) => {
const { t } = useTranslation('modules/dashdot'); const { t } = useTranslation('modules/dashdot');
const { classes } = useStyles(); const { classes } = useStyles();
if (graph === 'storage' && isCompact) { if (graph === 'storage' && isCompact) {
return <DashDotCompactStorage info={info} widgetId={widgetId} />; return <DashDotCompactStorage info={info} url={dashDotUrl} />;
} }
if (graph === 'network' && isCompact) { if (graph === 'network' && isCompact) {

View File

@@ -198,7 +198,6 @@ function DashDotTile({ widget }: DashDotTileProps) {
isCompact={g.subValues.compactView ?? false} isCompact={g.subValues.compactView ?? false}
multiView={g.subValues.multiView ?? false} multiView={g.subValues.multiView ?? false}
usePercentages={usePercentages} usePercentages={usePercentages}
widgetId={widget.id}
/> />
</Grid.Col> </Grid.Col>
))} ))}