Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dashboard data #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/api/src/api.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import apiConfig from './config';
import { WxkfApiController } from './wxkf.controller';
import { HistoryController } from './history.controller';
import { BotConfigApiController } from './bot-config.controller';
import { HomeApiController } from './home.controller';

@Module({
imports: [
Expand All @@ -20,6 +21,6 @@ import { BotConfigApiController } from './bot-config.controller';
PrismaModule,
ConfigModule.forFeature(apiConfig),
],
controllers: [WxkfApiController, HistoryController, BotConfigApiController],
controllers: [WxkfApiController, HistoryController, BotConfigApiController, HomeApiController],
})
export class ApiModule {}
63 changes: 63 additions & 0 deletions packages/api/src/home.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Controller, Get, Logger, Query, Post, Body } from '@nestjs/common';
import { Prisma, PrismaService, RasaServer, Route, RouteType } from '@senses-chat/operator-database';

@Controller('/api/home')
export class HomeApiController {
private readonly logger = new Logger(HomeApiController.name);

constructor(private readonly prisma: PrismaService) {}

@Get('/dashboard')
async getDashboardData(): Promise<{
visitor: number;
visitorBot: number;
visitorKf: number;
visitorRate: number;
}> {
const today = new Date();
const yesterday = new Date(+new Date() - 1000 * 60 * 60 * 24);
const todayEvents = await this.prisma.eventStorage.findMany({
where: {
createdAt: {
gte: new Date(`${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()} 00:00:00`),
lte: new Date(`${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()} 23:59:59`),
},
},
distinct: ['aggregateId'],
});
const yesterdayEvents = await this.prisma.eventStorage.findMany({
where: {
createdAt: {
gte: new Date(`${yesterday.getFullYear()}-${yesterday.getMonth() + 1}-${yesterday.getDate()} 00:00:00`),
lte: new Date(`${yesterday.getFullYear()}-${yesterday.getMonth() + 1}-${yesterday.getDate()} 23:59:59`),
},
},
distinct: ['aggregateId'],
});
const redirectKfEventsCount = await this.prisma.$queryRaw(
Prisma.sql`
SELECT count(distinct "aggregateId") FROM "EventStorage"
WHERE "eventData"::json->'message'->'content'->'metadata'->'servicer_userid' is not null
AND "createdAt" BETWEEN CURRENT_DATE AND CURRENT_DATE + interval '1 day';
`
);
const todayCount = todayEvents.length;
const yesterdayCount = yesterdayEvents.length;
let rate = 0;
if (todayCount !== yesterdayCount) {
if (todayCount === 0) {
rate = -100;
} else if (yesterdayCount === 0) {
rate = 100;
} else {
rate = todayCount > yesterdayCount ? Math.floor((todayCount / yesterdayCount - 1) * 100) : -Math.floor((1 - todayCount / yesterdayCount) * 100);
}
}
return {
visitor: todayCount,
visitorBot: todayCount,
visitorKf: redirectKfEventsCount?.[0]?.count || 0,
visitorRate: rate,
};
}
}
21 changes: 15 additions & 6 deletions packages/ui/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ import { SessionData } from 'utils/schema';
const { Title, Paragraph } = Typography;

export default function IndexPage(props: { body: string }) {
const { data: dashboardData }: SWRResponse<{
visitor: number;
visitorBot: number;
visitorKf: number;
visitorRate: number;
}, Error> = useSWR(
url(`/api/home/dashboard`),
fetcher,
);
const { data }: SWRResponse<SessionData[], Error> = useSWR(
url(`/api/history/sessions`),
fetcher,
Expand Down Expand Up @@ -94,26 +103,26 @@ export default function IndexPage(props: { body: string }) {
<Row gutter={24}>
<Col span={8}>
<Card>
<Statistic title={<span className='flex items-center'><UserOutlined className='mr-1' />今日访客</span>} valueStyle={{ fontSize: '40px', textAlign: 'center' }} value={0} />
<Statistic title={<span className='flex items-center'><UserOutlined className='mr-1' />今日访客</span>} valueStyle={{ fontSize: '40px', textAlign: 'center' }} value={dashboardData?.visitor} />
<Card.Meta
className='text-right'
description={<Tag color="success">{'+0%'}</Tag>}
description={<Tag color={dashboardData?.visitorRate >= 0 ? 'success' : 'error'}>{`${dashboardData?.visitorRate > 0 ? '+' : ''}${dashboardData?.visitorRate}%`}</Tag>}
/>
</Card>
</Col>
<Col span={8}>
<Card>
<Statistic title={<span className='flex items-center'><RobotOutlined className='mr-1' />智能客服接待</span>} valueStyle={{ fontSize: '40px', textAlign: 'center' }} value={0} />
<Statistic title={<span className='flex items-center'><RobotOutlined className='mr-1' />智能客服接待</span>} valueStyle={{ fontSize: '40px', textAlign: 'center' }} value={dashboardData?.visitorBot} />
<Card.Meta
description={<p className='text-right'>{'占比:0%'}</p>}
description={<p className='text-right'>{`占比:${(dashboardData?.visitorBot / dashboardData?.visitor * 100).toFixed(2)}%`}</p>}
/>
</Card>
</Col>
<Col span={8}>
<Card>
<Statistic title={<span className='flex items-center'><CustomerServiceOutlined className='mr-1' />人工接待</span>} valueStyle={{ fontSize: '40px', textAlign: 'center' }} value={0} />
<Statistic title={<span className='flex items-center'><CustomerServiceOutlined className='mr-1' />人工接待</span>} valueStyle={{ fontSize: '40px', textAlign: 'center' }} value={dashboardData?.visitorKf} />
<Card.Meta
description={<p className='text-right'>{'占比:0%'}</p>}
description={<p className='text-right'>{`占比:${(dashboardData?.visitorKf / dashboardData?.visitor * 100).toFixed(2)}%`}</p>}
/>
</Card>
</Col>
Expand Down