feat: add monitor model and api

This commit is contained in:
moonrailgun 2023-09-29 17:12:06 +08:00
parent 6d1c4e7d17
commit 61bb8bd82f
4 changed files with 85 additions and 5 deletions

View File

@ -31,6 +31,7 @@ model Workspace {
users WorkspacesOnUsers[]
websites Website[]
notifications Notification[]
monitors Monitor[]
// for user currentWorkspace
selectedUsers User[]
@ -208,14 +209,32 @@ model TelemetryEvent {
}
model Notification {
id String @id() @default(uuid()) @db.Uuid
workspaceId String @db.Uuid
name String @db.VarChar(100)
type String @db.VarChar(100)
payload Json @db.Json
id String @id() @default(uuid()) @db.Uuid
workspaceId String @db.Uuid
name String @db.VarChar(100)
type String @db.VarChar(100)
payload Json @db.Json
createdAt DateTime? @default(now()) @db.Timestamptz(6)
workspace Workspace @relation(fields: [workspaceId], references: [id])
monitors Monitor[]
@@index([workspaceId])
}
model Monitor {
id String @id() @default(uuid()) @db.Uuid
workspaceId String @db.Uuid
name String @db.VarChar(100)
type String @db.VarChar(100)
active Boolean @default(true) @db.Boolean
interval Int @default(20) @db.Integer
maxRetry Int @default(0) @db.Integer
retryInterval Int @default(0) @db.Integer
payload Json @db.Json
workspace Workspace @relation(fields: [workspaceId], references: [id])
notifications Notification[]
}

View File

@ -3,9 +3,13 @@ import type { AppRouter } from '../../server/trpc';
import { httpBatchLink, TRPCClientErrorLike } from '@trpc/client';
import { getJWT } from './auth';
import { message } from 'antd';
import { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
export const trpc = createTRPCReact<AppRouter>();
export type RouterInput = inferRouterInputs<AppRouter>;
export type RouterOutput = inferRouterOutputs<AppRouter>;
export const trpcClient = trpc.createClient({
links: [
httpBatchLink({

View File

@ -2,10 +2,12 @@ import * as trpcExpress from '@trpc/server/adapters/express';
import { createContext, router } from './trpc';
import { notificationRouter } from './routers/notification';
import { websiteRouter } from './routers/website';
import { monitorRouter } from './routers/monitor';
const appRouter = router({
website: websiteRouter,
notification: notificationRouter,
monitor: monitorRouter,
});
export type AppRouter = typeof appRouter;

View File

@ -0,0 +1,55 @@
import { router, workspaceOwnerProcedure, workspaceProcedure } from '../trpc';
import { prisma } from '../../model/_client';
import { z } from 'zod';
export const monitorRouter = router({
all: workspaceProcedure.query(async ({ input }) => {
const workspaceId = input.workspaceId;
const monitors = await prisma.monitor.findMany({
where: {
workspaceId,
},
});
return monitors;
}),
create: workspaceOwnerProcedure
.input(
z.object({
name: z.string(),
type: z.string(),
active: z.boolean().default(true),
interval: z.number().int().default(20),
maxRetry: z.number().int().default(0),
retryInterval: z.number().int().default(0),
payload: z.object({}).passthrough(),
})
)
.mutation(async ({ input }) => {
const {
workspaceId,
name,
type,
active,
interval,
maxRetry,
retryInterval,
payload,
} = input;
const monitor = await prisma.monitor.create({
data: {
workspaceId,
name,
type,
active,
interval,
maxRetry,
retryInterval,
payload,
},
});
return monitor;
}),
});