feat: add monitor summary function

This commit is contained in:
moonrailgun 2024-10-02 21:58:27 +08:00
parent f1513fe3f7
commit bbb8d88116
2 changed files with 29 additions and 1 deletions

View File

@ -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<MonitorSummaryItem[]>`
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;`;
}

View File

@ -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}