From 19e7ed516bae41ac80ca9e167f548ae7a0dabfa9 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 6 Jan 2024 22:07:58 +0800 Subject: [PATCH] refactor: extract some monitor logic into model --- src/server/model/monitor/index.ts | 58 ++++++++++++++++++++++++++++++ src/server/trpc/routers/monitor.ts | 51 ++++++++------------------ 2 files changed, 73 insertions(+), 36 deletions(-) diff --git a/src/server/model/monitor/index.ts b/src/server/model/monitor/index.ts index 856928d..c115ca2 100644 --- a/src/server/model/monitor/index.ts +++ b/src/server/model/monitor/index.ts @@ -1,3 +1,61 @@ +import { MonitorPublicInfoSchema } from '../../../types'; +import { prisma } from '../_client'; import { MonitorManager } from './manager'; export const monitorManager = new MonitorManager(); + +export async function getMonitorPublicInfos(monitorIds: string[]) { + const res = await prisma.monitor.findMany({ + where: { + id: { + in: monitorIds, + }, + }, + }); + + return res.map((item) => MonitorPublicInfoSchema.parse(item)); +} + +export function getMonitorData( + workspaceId: string, + monitorId: string, + startDate: Date, + endDate: Date +) { + return prisma.monitorData.findMany({ + where: { + monitor: { + id: monitorId, + workspaceId, + }, + createdAt: { + gte: startDate, + lte: endDate, + }, + }, + select: { + value: true, + createdAt: true, + }, + }); +} + +export function getMonitorRecentData( + workspaceId: string, + monitorId: string, + take: number +) { + return prisma.monitorData.findMany({ + where: { + monitor: { + id: monitorId, + workspaceId, + }, + }, + take: -take, + select: { + value: true, + createdAt: true, + }, + }); +} diff --git a/src/server/trpc/routers/monitor.ts b/src/server/trpc/routers/monitor.ts index be70cee..d5babac 100644 --- a/src/server/trpc/routers/monitor.ts +++ b/src/server/trpc/routers/monitor.ts @@ -7,7 +7,12 @@ import { } from '../trpc'; import { prisma } from '../../model/_client'; import { z } from 'zod'; -import { monitorManager } from '../../model/monitor'; +import { + getMonitorData, + getMonitorPublicInfos, + getMonitorRecentData, + monitorManager, +} from '../../model/monitor'; import { MonitorInfoWithNotificationIds, MonitorPublicInfoSchema, @@ -101,15 +106,8 @@ export const monitorRouter = router({ .output(z.array(MonitorPublicInfoSchema)) .query(async ({ input }) => { const { monitorIds } = input; - const res = await prisma.monitor.findMany({ - where: { - id: { - in: monitorIds, - }, - }, - }); - return res.map((item) => MonitorPublicInfoSchema.parse(item)); + return getMonitorPublicInfos(monitorIds); }), upsert: workspaceOwnerProcedure .meta( @@ -220,22 +218,12 @@ export const monitorRouter = router({ .query(async ({ input }) => { const { monitorId, workspaceId, startAt, endAt } = input; - return prisma.monitorData.findMany({ - where: { - monitor: { - id: monitorId, - workspaceId, - }, - createdAt: { - gte: new Date(startAt), - lte: new Date(endAt), - }, - }, - select: { - value: true, - createdAt: true, - }, - }); + return getMonitorData( + workspaceId, + monitorId, + new Date(startAt), + new Date(endAt) + ); }), changeActive: workspaceOwnerProcedure .meta( @@ -312,18 +300,9 @@ export const monitorRouter = router({ ) ) .query(async ({ input }) => { - const { monitorId, take } = input; + const { workspaceId, monitorId, take } = input; - return prisma.monitorData.findMany({ - where: { - monitorId, - }, - take: -take, - select: { - value: true, - createdAt: true, - }, - }); + return getMonitorRecentData(workspaceId, monitorId, take); }), dataMetrics: workspaceProcedure .meta(