diff --git a/src/components/Dashboard/Tiles/Apps/AppPing.tsx b/src/components/Dashboard/Tiles/Apps/AppPing.tsx index 5af4b0938..a0ce9f9a5 100644 --- a/src/components/Dashboard/Tiles/Apps/AppPing.tsx +++ b/src/components/Dashboard/Tiles/Apps/AppPing.tsx @@ -1,10 +1,10 @@ import { Indicator, Tooltip } from '@mantine/core'; import Consola from 'consola'; -import { motion } from 'framer-motion'; import { useTranslation } from 'next-i18next'; +import { api } from '~/utils/api'; + import { useConfigContext } from '../../../../config/provider'; import { AppType } from '../../../../types/app'; -import { api } from '~/utils/api'; interface AppPingProps { app: AppType; @@ -16,60 +16,47 @@ export const AppPing = ({ app }: AppPingProps) => { const active = (config?.settings.customization.layout.enabledPing && app.network.enabledStatusChecker) ?? false; - const { data, isLoading, error } = usePingQuery(app, active); - - const isOnline = data?.state === 'online'; + const { data, isLoading, isFetching, isSuccess } = api.app.ping.useQuery(app.id, { + retry: false, + enabled: active, + select: (data) => { + const isOk = getIsOk(app, data.status); + Consola.info(`Ping ${app.name} (${app.url}) ${data.status} ${isOk}`); + return { + status: data.status, + state: isOk ? ('online' as const) : ('down' as const), + statusText: data.statusText, + }; + }, + }); if (!active) return null; return ( - +
- +
); }; -const usePingQuery = (app: AppType, isEnabled: boolean) => - api.app.ping.useQuery( - { - url: app.url, - }, - { - enabled: isEnabled, - select: (data) => { - const statusCode = data.status; - const isOk = getIsOk(app, statusCode); - return { - status: statusCode, - state: isOk ? ('online' as const) : ('down' as const), - }; - }, - } - ); - -const getIsOk = (app: AppType, status: number) => { +export const getIsOk = (app: AppType, status: number) => { if (app.network.okStatus === undefined || app.network.statusCodes.length >= 1) { Consola.log('Using new status codes'); return app.network.statusCodes.includes(status.toString()); diff --git a/src/server/api/routers/app.ts b/src/server/api/routers/app.ts index 184e4989e..2fc3dd6c8 100644 --- a/src/server/api/routers/app.ts +++ b/src/server/api/routers/app.ts @@ -1,46 +1,58 @@ -import { z } from 'zod'; -import axios, { AxiosError } from 'axios'; -import https from 'https'; -import Consola from 'consola'; import { TRPCError } from '@trpc/server'; +import axios, { AxiosError } from 'axios'; +import Consola from 'consola'; +import { getCookie } from 'cookies-next'; +import https from 'https'; +import { z } from 'zod'; +import { getIsOk } from '~/components/Dashboard/Tiles/Apps/AppPing'; +import { getConfig } from '~/tools/config/getConfig'; +import { AppType } from '~/types/app'; + import { createTRPCRouter, publicProcedure } from '../trpc'; export const appRouter = createTRPCRouter({ - ping: publicProcedure - .input( - z.object({ - url: z.string(), - }) - ) - .query(async ({ input }) => { - const agent = new https.Agent({ rejectUnauthorized: false }); - const res = await axios - .get(input.url, { httpsAgent: agent, timeout: 2000 }) - .then((response) => ({ - status: response.status, - statusText: response.statusText, - })) - .catch((error: AxiosError) => { - if (error.response) { - Consola.warn(`Unexpected response: ${error.message}`); + ping: publicProcedure.input(z.string()).query(async ({ input }) => { + const agent = new https.Agent({ rejectUnauthorized: false }); + const configName = getCookie('config-name'); + const config = getConfig(configName?.toString() ?? 'default'); + const app = config.apps.find((app) => app.id === input); + const url = app?.url; + if (url === undefined || !app) { + throw new TRPCError({ + code: 'NOT_FOUND', + message: 'App or url not found', + }); + } + const res = await axios + .get(url, { httpsAgent: agent, timeout: 2000 }) + .then((response) => ({ + status: response.status, + statusText: response.statusText, + })) + .catch((error: AxiosError) => { + if (error.response) { + if (getIsOk(app as AppType, error.response.status)) { return { + state: 'offline', status: error.response.status, statusText: error.response.statusText, }; } - if (error.code === 'ECONNABORTED') { - throw new TRPCError({ - code: 'TIMEOUT', - message: 'Request Timeout', - }); - } - - Consola.error(`Unexpected error: ${error.message}`); + } + if (error.code === 'ECONNABORTED') { throw new TRPCError({ - code: 'INTERNAL_SERVER_ERROR', - message: 'Internal Server Error', + code: 'TIMEOUT', + message: 'Request Timeout', }); + } + + Consola.error(`Unexpected response: ${error.message}`); + throw new TRPCError({ + code: 'INTERNAL_SERVER_ERROR', + cause: app.id, + message: error.message, }); - return res; - }), + }); + return res; + }), });