feat: add monitor order with updatedAt

This commit is contained in:
moonrailgun 2023-12-29 21:15:32 +08:00
parent d9d234551a
commit 625374cafe
4 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Monitor" ADD COLUMN "updatedAt" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP;

View File

@ -261,6 +261,7 @@ model Monitor {
/// [CommonPayload] /// [CommonPayload]
payload Json @db.Json payload Json @db.Json
createdAt DateTime @default(now()) @db.Timestamptz(6) createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @default(now()) @updatedAt @db.Timestamptz(6)
workspace Workspace @relation(fields: [workspaceId], references: [id]) workspace Workspace @relation(fields: [workspaceId], references: [id])

View File

@ -20,6 +20,7 @@ export const MonitorModelSchema = z.object({
*/ */
payload: jsonSchema, payload: jsonSchema,
createdAt: z.date(), createdAt: z.date(),
updatedAt: z.date(),
}) })
export interface CompleteMonitor extends z.infer<typeof MonitorModelSchema> { export interface CompleteMonitor extends z.infer<typeof MonitorModelSchema> {

View File

@ -45,6 +45,9 @@ export const monitorRouter = router({
}, },
}, },
}, },
orderBy: {
updatedAt: 'desc',
},
}); });
return monitors as MonitorInfoWithNotificationIds[]; return monitors as MonitorInfoWithNotificationIds[];
@ -426,6 +429,60 @@ export const monitorRouter = router({
return list; return list;
}), }),
clearEvents: workspaceOwnerProcedure
.meta(
buildMonitorOpenapi({
method: 'DELETE',
path: '/clearEvents',
})
)
.input(
z.object({
monitorId: z.string().cuid2(),
})
)
.output(z.number())
.mutation(async ({ input }) => {
const { workspaceId, monitorId } = input;
const { count } = await prisma.monitorEvent.deleteMany({
where: {
monitor: {
id: monitorId,
workspaceId,
},
},
});
return count;
}),
clearData: workspaceOwnerProcedure
.meta(
buildMonitorOpenapi({
method: 'DELETE',
path: '/clearData',
})
)
.input(
z.object({
monitorId: z.string().cuid2(),
})
)
.output(z.number())
.mutation(async ({ input }) => {
const { workspaceId, monitorId } = input;
const { count } = await prisma.monitorData.deleteMany({
where: {
monitor: {
id: monitorId,
workspaceId,
},
},
});
return count;
}),
getStatus: workspaceProcedure getStatus: workspaceProcedure
.meta( .meta(
buildMonitorOpenapi({ buildMonitorOpenapi({