feat: add monitor model and api
This commit is contained in:
parent
6d1c4e7d17
commit
61bb8bd82f
@ -31,6 +31,7 @@ model Workspace {
|
|||||||
users WorkspacesOnUsers[]
|
users WorkspacesOnUsers[]
|
||||||
websites Website[]
|
websites Website[]
|
||||||
notifications Notification[]
|
notifications Notification[]
|
||||||
|
monitors Monitor[]
|
||||||
|
|
||||||
// for user currentWorkspace
|
// for user currentWorkspace
|
||||||
selectedUsers User[]
|
selectedUsers User[]
|
||||||
@ -208,14 +209,32 @@ model TelemetryEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model Notification {
|
model Notification {
|
||||||
id String @id() @default(uuid()) @db.Uuid
|
id String @id() @default(uuid()) @db.Uuid
|
||||||
workspaceId String @db.Uuid
|
workspaceId String @db.Uuid
|
||||||
name String @db.VarChar(100)
|
name String @db.VarChar(100)
|
||||||
type String @db.VarChar(100)
|
type String @db.VarChar(100)
|
||||||
payload Json @db.Json
|
payload Json @db.Json
|
||||||
createdAt DateTime? @default(now()) @db.Timestamptz(6)
|
createdAt DateTime? @default(now()) @db.Timestamptz(6)
|
||||||
|
|
||||||
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
||||||
|
|
||||||
|
monitors Monitor[]
|
||||||
|
|
||||||
@@index([workspaceId])
|
@@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[]
|
||||||
|
}
|
||||||
|
@ -3,9 +3,13 @@ import type { AppRouter } from '../../server/trpc';
|
|||||||
import { httpBatchLink, TRPCClientErrorLike } from '@trpc/client';
|
import { httpBatchLink, TRPCClientErrorLike } from '@trpc/client';
|
||||||
import { getJWT } from './auth';
|
import { getJWT } from './auth';
|
||||||
import { message } from 'antd';
|
import { message } from 'antd';
|
||||||
|
import { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
|
||||||
|
|
||||||
export const trpc = createTRPCReact<AppRouter>();
|
export const trpc = createTRPCReact<AppRouter>();
|
||||||
|
|
||||||
|
export type RouterInput = inferRouterInputs<AppRouter>;
|
||||||
|
export type RouterOutput = inferRouterOutputs<AppRouter>;
|
||||||
|
|
||||||
export const trpcClient = trpc.createClient({
|
export const trpcClient = trpc.createClient({
|
||||||
links: [
|
links: [
|
||||||
httpBatchLink({
|
httpBatchLink({
|
||||||
|
@ -2,10 +2,12 @@ import * as trpcExpress from '@trpc/server/adapters/express';
|
|||||||
import { createContext, router } from './trpc';
|
import { createContext, router } from './trpc';
|
||||||
import { notificationRouter } from './routers/notification';
|
import { notificationRouter } from './routers/notification';
|
||||||
import { websiteRouter } from './routers/website';
|
import { websiteRouter } from './routers/website';
|
||||||
|
import { monitorRouter } from './routers/monitor';
|
||||||
|
|
||||||
const appRouter = router({
|
const appRouter = router({
|
||||||
website: websiteRouter,
|
website: websiteRouter,
|
||||||
notification: notificationRouter,
|
notification: notificationRouter,
|
||||||
|
monitor: monitorRouter,
|
||||||
});
|
});
|
||||||
|
|
||||||
export type AppRouter = typeof appRouter;
|
export type AppRouter = typeof appRouter;
|
||||||
|
55
src/server/trpc/routers/monitor.ts
Normal file
55
src/server/trpc/routers/monitor.ts
Normal 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;
|
||||||
|
}),
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user