feat: add survey count and feed event count

This commit is contained in:
moonrailgun 2024-08-04 19:44:35 +08:00
parent 0a0a27549a
commit f1496429d3
6 changed files with 103 additions and 11 deletions

View File

@ -3,7 +3,6 @@ import { createFileRoute } from '@tanstack/react-router';
import { useTranslation } from '@i18next-toolkit/react';
import { CommonWrapper } from '@/components/CommonWrapper';
import { ScrollArea } from '@/components/ui/scroll-area';
import { Statistic } from 'antd';
import { useMemo } from 'react';
import { trpc } from '../../api/trpc';
import { useCurrentWorkspaceId } from '../../store/user';
@ -45,7 +44,7 @@ function PageComponent() {
</div>
</CardHeader>
<CardContent>
<div className="flex gap-2">
<div className="grid grid-cols-2 gap-2 sm:grid-cols-3">
<Card className="flex-1">
<CardHeader className="text-muted-foreground">
{t('Website Accepted Count')}
@ -72,6 +71,24 @@ function PageComponent() {
{formatNumber(data?.monitorExecutionCount ?? 0)}
</CardContent>
</Card>
<Card className="flex-1">
<CardHeader className="text-muted-foreground">
{t('Survey Count')}
</CardHeader>
<CardContent>
{formatNumber(data?.surveyCount ?? 0)}
</CardContent>
</Card>
<Card className="flex-1">
<CardHeader className="text-muted-foreground">
{t('Feed Event Count')}
</CardHeader>
<CardContent>
{formatNumber(data?.feedEventCount ?? 0)}
</CardContent>
</Card>
</div>
</CardContent>
</Card>

View File

@ -81,9 +81,14 @@ async function statDailyUsage() {
const end = dayjs().startOf('day').toDate();
const date = dayjs().subtract(1, 'day').toDate();
const [websiteAcceptCountRes, websiteEventCountRes, monitorExecutionCount] =
await Promise.all([
await prisma.$queryRaw<WebsiteEventCountSqlReturn>`
const [
websiteAcceptCountRes,
websiteEventCountRes,
monitorExecutionCount,
surveyCount,
feedEventCount,
] = await Promise.all([
await prisma.$queryRaw<WebsiteEventCountSqlReturn>`
SELECT
w.id AS workspace_id,
COALESCE(COUNT(we.id), 0) AS count
@ -99,7 +104,7 @@ async function statDailyUsage() {
w.id, w.name
ORDER BY
w.id`,
await prisma.$queryRaw<WebsiteEventCountSqlReturn>`
await prisma.$queryRaw<WebsiteEventCountSqlReturn>`
SELECT
w.id AS workspace_id,
COALESCE(COUNT(we.id), 0) AS count
@ -116,7 +121,7 @@ async function statDailyUsage() {
w.id, w.name
ORDER BY
w.id`,
await prisma.$queryRaw<WebsiteEventCountSqlReturn>`
await prisma.$queryRaw<WebsiteEventCountSqlReturn>`
SELECT
w.id AS workspace_id,
COALESCE(COUNT(md.id), 0) AS count
@ -132,7 +137,39 @@ async function statDailyUsage() {
w.id
ORDER BY
w.id`,
]);
await prisma.$queryRaw<WebsiteEventCountSqlReturn>`
SELECT
w.id AS workspace_id,
COALESCE(COUNT(sr.id), 0) AS count
FROM
"Workspace" w
LEFT JOIN
"Survey" s ON w.id = s."workspaceId"
LEFT JOIN
"SurveyResult" sr ON s.id = sr."surveyId"
AND sr."createdAt" >= ${start}
AND sr."createdAt" < ${end}
GROUP BY
w.id
ORDER BY
w.id`,
await prisma.$queryRaw<WebsiteEventCountSqlReturn>`
SELECT
w.id AS workspace_id,
COALESCE(COUNT(fe.id), 0) AS count
FROM
"Workspace" w
LEFT JOIN
"FeedChannel" fc ON w.id = fc."workspaceId"
LEFT JOIN
"FeedEvent" fe ON fc.id = fe."channelId"
AND fe."createdAt" >= ${start}
AND fe."createdAt" < ${end}
GROUP BY
w.id
ORDER BY
w.id`,
]);
const map: Map<string, Prisma.WorkspaceDailyUsageCreateManyInput> = new Map();
@ -141,6 +178,8 @@ async function statDailyUsage() {
websiteAcceptedCount: 0,
websiteEventCount: 0,
monitorExecutionCount: 0,
surveyCount: 0,
feedEventCount: 0,
date,
};
@ -174,6 +213,26 @@ async function statDailyUsage() {
});
});
surveyCount.forEach((item) => {
const workspaceId = item.workspace_id;
map.set(workspaceId, {
...blank,
...map.get(workspaceId),
workspaceId,
surveyCount: Number(item.count),
});
});
feedEventCount.forEach((item) => {
const workspaceId = item.workspace_id;
map.set(workspaceId, {
...blank,
...map.get(workspaceId),
workspaceId,
feedEventCount: Number(item.count),
});
});
await prisma.workspaceDailyUsage.createMany({
data: Array.from(map.values()),
skipDuplicates: true,

View File

@ -0,0 +1,6 @@
-- AlterTable
ALTER TABLE "WorkspaceDailyUsage" ADD COLUMN "feedEventCount" INTEGER NOT NULL DEFAULT 0,
ADD COLUMN "surveyCount" INTEGER NOT NULL DEFAULT 0,
ALTER COLUMN "websiteAcceptedCount" SET DEFAULT 0,
ALTER COLUMN "websiteEventCount" SET DEFAULT 0,
ALTER COLUMN "monitorExecutionCount" SET DEFAULT 0;

View File

@ -421,9 +421,11 @@ model WorkspaceDailyUsage {
id String @id @unique @default(cuid()) @db.VarChar(30)
workspaceId String @db.VarChar(30)
date DateTime @db.Date // calc every day
websiteAcceptedCount Int // website accept any request count
websiteEventCount Int // website accept event request count
monitorExecutionCount Int // monitor exec number count
websiteAcceptedCount Int @default(0) // website accept any request count
websiteEventCount Int @default(0) // website accept event request count
monitorExecutionCount Int @default(0) // monitor exec number count
surveyCount Int @default(0) // survey receive count
feedEventCount Int @default(0) // feed event receive count
createdAt DateTime @default(now()) @db.Timestamptz(6)
workspace Workspace @relation(fields: [workspaceId], references: [id], onUpdate: Cascade, onDelete: Cascade)

View File

@ -9,6 +9,8 @@ export const WorkspaceDailyUsageModelSchema = z.object({
websiteAcceptedCount: z.number().int(),
websiteEventCount: z.number().int(),
monitorExecutionCount: z.number().int(),
surveyCount: z.number().int(),
feedEventCount: z.number().int(),
createdAt: z.date(),
})

View File

@ -24,6 +24,8 @@ export const billingRouter = router({
websiteAcceptedCount: z.number(),
websiteEventCount: z.number(),
monitorExecutionCount: z.number(),
surveyCount: z.number(),
feedEventCount: z.number(),
})
)
.query(async ({ input }) => {
@ -41,6 +43,8 @@ export const billingRouter = router({
websiteAcceptedCount: true,
websiteEventCount: true,
monitorExecutionCount: true,
surveyCount: true,
feedEventCount: true,
},
});
@ -48,6 +52,8 @@ export const billingRouter = router({
websiteAcceptedCount: res._sum.websiteAcceptedCount ?? 0,
websiteEventCount: res._sum.websiteEventCount ?? 0,
monitorExecutionCount: res._sum.monitorExecutionCount ?? 0,
surveyCount: res._sum.surveyCount ?? 0,
feedEventCount: res._sum.feedEventCount ?? 0,
};
}),
});