From f9984a76fb91745777fdb7cb506d931da1b35847 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Wed, 2 Oct 2024 21:58:27 +0800 Subject: [PATCH] feat: add monitor summary function --- src/server/model/monitor/index.ts | 28 ++++++++++++++++++++++++++++ src/server/model/website.ts | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/server/model/monitor/index.ts b/src/server/model/monitor/index.ts index cc609a7..f63af05 100644 --- a/src/server/model/monitor/index.ts +++ b/src/server/model/monitor/index.ts @@ -67,3 +67,31 @@ export function getMonitorRecentData( }) .then((arr) => arr.reverse()); } + +export function getMonitorSummaryWithDay( + monitorId: string, + beforeDay: number = 30 +) { + interface MonitorSummaryItem { + day: string; + total_count: number; + up_count: number; + up_rate: number; + } + + return prisma.$queryRaw` + SELECT + DATE("createdAt") AS day, + COUNT(1) AS total_count, + SUM(CASE WHEN "value" >= 0 THEN 1 ELSE 0 END) AS up_count, + (SUM(CASE WHEN "value" >= 0 THEN 1 ELSE 0 END) * 100.0 / COUNT(1)) AS up_rate + FROM + "MonitorData" + WHERE + "monitorId" = ${monitorId} AND + "createdAt" >= CURRENT_DATE - INTERVAL '${beforeDay} days' + GROUP BY + DATE("createdAt") + ORDER BY + day;`; +} diff --git a/src/server/model/website.ts b/src/server/model/website.ts index 0b72c80..5b58c90 100644 --- a/src/server/model/website.ts +++ b/src/server/model/website.ts @@ -333,7 +333,7 @@ export async function getWebsitePageviewMetrics( } return prisma.$queryRaw` - select ${Prisma.sql([`"${column}"`])} x, count(*) y + select ${Prisma.sql([`"${column}"`])} x, count(*) y from "WebsiteEvent" ${joinSession} where "WebsiteEvent"."websiteId" = ${websiteId}