feat: add test notify

This commit is contained in:
moonrailgun 2024-10-20 16:58:23 +08:00
parent b4ab20ad32
commit 4e3fd9db64
2 changed files with 37 additions and 0 deletions

View File

@ -61,6 +61,10 @@ export const MonitorInfo: React.FC<MonitorInfoProps> = React.memo((props) => {
onSuccess: defaultSuccessHandler, onSuccess: defaultSuccessHandler,
onError: defaultErrorHandler, onError: defaultErrorHandler,
}); });
const testNotifyScriptMutation = trpc.monitor.testNotifyScript.useMutation({
onSuccess: defaultSuccessHandler,
onError: defaultErrorHandler,
});
const trpcUtils = trpc.useContext(); const trpcUtils = trpc.useContext();
@ -229,6 +233,15 @@ export const MonitorInfo: React.FC<MonitorInfoProps> = React.memo((props) => {
label: t('Show Badge'), label: t('Show Badge'),
onClick: () => setShowBadge(true), onClick: () => setShowBadge(true),
}, },
{
key: 'testNotify',
label: t('Test Notify'),
onClick: () =>
testNotifyScriptMutation.mutateAsync({
workspaceId,
monitorId,
}),
},
{ {
type: 'divider', type: 'divider',
}, },

View File

@ -33,6 +33,7 @@ import {
monitorPublicInfoSchema, monitorPublicInfoSchema,
} from '../../model/_schema/monitor.js'; } from '../../model/_schema/monitor.js';
import { monitorPageManager } from '../../model/monitor/page/manager.js'; import { monitorPageManager } from '../../model/monitor/page/manager.js';
import { token } from '../../model/notification/token/index.js';
export const monitorRouter = router({ export const monitorRouter = router({
all: workspaceProcedure all: workspaceProcedure
@ -204,6 +205,29 @@ export const monitorRouter = router({
usage: res.usage, usage: res.usage,
}; };
}), }),
testNotifyScript: workspaceAdminProcedure
.input(
z.object({
monitorId: z.string(),
})
)
.output(z.void())
.mutation(async ({ input }) => {
const { monitorId } = input;
const runner = monitorManager.getRunner(monitorId);
if (!runner) {
throw new Error('This monitor is not running or not existed.');
}
if (runner.monitor.notifications.length === 0) {
throw new Error('This monitor not has any notifications.');
}
await runner.notify('Test title', [
token.paragraph('Test content'),
token.paragraph(`Send from monitor: ${runner.monitor.name}`),
]);
}),
data: workspaceProcedure data: workspaceProcedure
.meta( .meta(
buildMonitorOpenapi({ buildMonitorOpenapi({