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}