fix: 优化后台体验与导出日志交互
This commit is contained in:
@@ -1,7 +1,29 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { Controller, Get, Query } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiPropertyOptional, ApiTags } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsInt, IsOptional, IsString, Max, Min } from 'class-validator';
|
||||
import { DashboardService } from './dashboard.service';
|
||||
|
||||
class DashboardQueryDto {
|
||||
@ApiPropertyOptional({ description: '近 N 日', default: 7 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
@Max(365)
|
||||
days?: number;
|
||||
|
||||
@ApiPropertyOptional({ description: '开始时间' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
startTime?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: '结束时间' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
endTime?: string;
|
||||
}
|
||||
|
||||
@ApiTags('数据看板')
|
||||
@ApiBearerAuth()
|
||||
@Controller('dashboard')
|
||||
@@ -10,7 +32,7 @@ export class DashboardController {
|
||||
|
||||
@Get('overview')
|
||||
@ApiOperation({ summary: '看板总览' })
|
||||
overview() {
|
||||
return this.dashboardService.overview();
|
||||
overview(@Query() query: DashboardQueryDto) {
|
||||
return this.dashboardService.overview(query);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,29 @@ import dayjs from 'dayjs';
|
||||
import { QueryResult, TraceCodeStatus } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
export interface DashboardOverviewQuery {
|
||||
days?: number;
|
||||
startTime?: string;
|
||||
endTime?: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class DashboardService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async overview() {
|
||||
private resolveRange(query: DashboardOverviewQuery) {
|
||||
const end = query.endTime ? dayjs(query.endTime) : dayjs().endOf('day');
|
||||
const days = Math.min(Math.max(Number(query.days || 7), 1), 365);
|
||||
const start = query.startTime ? dayjs(query.startTime) : end.startOf('day').subtract(days - 1, 'day');
|
||||
return {
|
||||
start: start.isValid() ? start : dayjs().startOf('day').subtract(6, 'day'),
|
||||
end: end.isValid() ? end : dayjs().endOf('day'),
|
||||
};
|
||||
}
|
||||
|
||||
async overview(query: DashboardOverviewQuery = {}) {
|
||||
const todayStart = dayjs().startOf('day').toDate();
|
||||
const range = this.resolveRange(query);
|
||||
|
||||
const [
|
||||
totalArtworks,
|
||||
@@ -35,16 +52,17 @@ export class DashboardService {
|
||||
totalQueries,
|
||||
todayQueries,
|
||||
abnormalQueries,
|
||||
trend: await this.trend(7),
|
||||
topArtworks: await this.topArtworks(5),
|
||||
trend: await this.trend(range.start.toDate(), range.end.toDate()),
|
||||
topArtworks: await this.topArtworks(5, range.start.toDate(), range.end.toDate()),
|
||||
};
|
||||
}
|
||||
|
||||
// 近 n 天查询趋势
|
||||
async trend(days: number) {
|
||||
const start = dayjs().startOf('day').subtract(days - 1, 'day');
|
||||
async trend(startDate: Date, endDate: Date) {
|
||||
const start = dayjs(startDate).startOf('day');
|
||||
const end = dayjs(endDate).endOf('day');
|
||||
const days = Math.min(Math.max(end.diff(start, 'day') + 1, 1), 365);
|
||||
const logs = await this.prisma.queryLog.findMany({
|
||||
where: { queriedAt: { gte: start.toDate() } },
|
||||
where: { queriedAt: { gte: start.toDate(), lte: end.toDate() } },
|
||||
select: { queriedAt: true },
|
||||
});
|
||||
const buckets: Record<string, number> = {};
|
||||
@@ -58,15 +76,16 @@ export class DashboardService {
|
||||
return Object.entries(buckets).map(([date, count]) => ({ date, count }));
|
||||
}
|
||||
|
||||
// 高频查询作品榜:按款聚合(该款所有码查询次数之和)
|
||||
async topArtworks(limit: number) {
|
||||
const grouped = await this.prisma.traceCode.groupBy({
|
||||
// 高频查询作品榜:按选定时间范围内的查询日志聚合。
|
||||
async topArtworks(limit: number, startDate: Date, endDate: Date) {
|
||||
const grouped = await this.prisma.queryLog.groupBy({
|
||||
by: ['artworkId'],
|
||||
_sum: { queryCount: true },
|
||||
orderBy: { _sum: { queryCount: 'desc' } },
|
||||
where: { artworkId: { not: null }, queriedAt: { gte: startDate, lte: endDate } },
|
||||
_count: { _all: true },
|
||||
orderBy: { _count: { artworkId: 'desc' } },
|
||||
take: limit,
|
||||
});
|
||||
const ids = grouped.map((g) => g.artworkId);
|
||||
const ids = grouped.map((g) => g.artworkId).filter((id): id is number => id !== null);
|
||||
if (!ids.length) return [];
|
||||
const artworks = await this.prisma.artwork.findMany({
|
||||
where: { id: { in: ids } },
|
||||
@@ -75,9 +94,9 @@ export class DashboardService {
|
||||
const map = new Map(artworks.map((a) => [a.id, a]));
|
||||
return grouped.map((g) => ({
|
||||
artworkId: g.artworkId,
|
||||
title: map.get(g.artworkId)?.title ?? '-',
|
||||
archiveNo: map.get(g.artworkId)?.archiveNo ?? '',
|
||||
queryCount: g._sum.queryCount ?? 0,
|
||||
title: map.get(g.artworkId!)?.title ?? '-',
|
||||
archiveNo: map.get(g.artworkId!)?.archiveNo ?? '',
|
||||
queryCount: g._count._all ?? 0,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user