From 61bb8bd82f50e005d2b1b5da7edcb43961066c04 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Fri, 29 Sep 2023 17:12:06 +0800 Subject: [PATCH] feat: add monitor model and api --- prisma/schema.prisma | 29 +++++++++++++--- src/client/api/trpc.ts | 4 +++ src/server/trpc/index.ts | 2 ++ src/server/trpc/routers/monitor.ts | 55 ++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 src/server/trpc/routers/monitor.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 6631f0e..e35b721 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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[] +} diff --git a/src/client/api/trpc.ts b/src/client/api/trpc.ts index c78f0c4..6080fe7 100644 --- a/src/client/api/trpc.ts +++ b/src/client/api/trpc.ts @@ -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(); +export type RouterInput = inferRouterInputs; +export type RouterOutput = inferRouterOutputs; + export const trpcClient = trpc.createClient({ links: [ httpBatchLink({ diff --git a/src/server/trpc/index.ts b/src/server/trpc/index.ts index 1a23390..d3b5287 100644 --- a/src/server/trpc/index.ts +++ b/src/server/trpc/index.ts @@ -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; diff --git a/src/server/trpc/routers/monitor.ts b/src/server/trpc/routers/monitor.ts new file mode 100644 index 0000000..84209f7 --- /dev/null +++ b/src/server/trpc/routers/monitor.ts @@ -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; + }), +});