feat: 实现出库导入、区块链存证、查询日志、看板与系统设置
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import dayjs from 'dayjs';
|
||||
import { QueryResult, TraceCodeStatus } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class DashboardService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async overview() {
|
||||
const todayStart = dayjs().startOf('day').toDate();
|
||||
|
||||
const [
|
||||
totalArtworks,
|
||||
boundCodes,
|
||||
activatedCodes,
|
||||
totalQueries,
|
||||
todayQueries,
|
||||
abnormalQueries,
|
||||
] = await this.prisma.$transaction([
|
||||
this.prisma.artwork.count({ where: { deletedAt: null } }),
|
||||
this.prisma.traceCode.count({ where: { artworkId: { not: null } } }),
|
||||
this.prisma.traceCode.count({ where: { status: TraceCodeStatus.ACTIVE } }),
|
||||
this.prisma.queryLog.count(),
|
||||
this.prisma.queryLog.count({ where: { queriedAt: { gte: todayStart } } }),
|
||||
this.prisma.queryLog.count({
|
||||
where: { queryResult: { in: [QueryResult.INVALID, QueryResult.DISABLED, QueryResult.ABNORMAL] } },
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
totalArtworks,
|
||||
boundCodes,
|
||||
activatedCodes,
|
||||
totalQueries,
|
||||
todayQueries,
|
||||
abnormalQueries,
|
||||
trend: await this.trend(7),
|
||||
topArtworks: await this.topArtworks(5),
|
||||
};
|
||||
}
|
||||
|
||||
// 近 n 天查询趋势
|
||||
async trend(days: number) {
|
||||
const start = dayjs().startOf('day').subtract(days - 1, 'day');
|
||||
const logs = await this.prisma.queryLog.findMany({
|
||||
where: { queriedAt: { gte: start.toDate() } },
|
||||
select: { queriedAt: true },
|
||||
});
|
||||
const buckets: Record<string, number> = {};
|
||||
for (let i = 0; i < days; i++) {
|
||||
buckets[start.add(i, 'day').format('MM-DD')] = 0;
|
||||
}
|
||||
for (const l of logs) {
|
||||
const k = dayjs(l.queriedAt).format('MM-DD');
|
||||
if (k in buckets) buckets[k] += 1;
|
||||
}
|
||||
return Object.entries(buckets).map(([date, count]) => ({ date, count }));
|
||||
}
|
||||
|
||||
// 高频查询作品榜
|
||||
async topArtworks(limit: number) {
|
||||
const codes = await this.prisma.traceCode.findMany({
|
||||
where: { artworkId: { not: null }, queryCount: { gt: 0 } },
|
||||
orderBy: { queryCount: 'desc' },
|
||||
take: limit,
|
||||
include: { artwork: { select: { id: true, title: true } } },
|
||||
});
|
||||
return codes.map((c) => ({
|
||||
artworkId: c.artworkId,
|
||||
title: c.artwork?.title ?? '-',
|
||||
shortCode: c.shortCode,
|
||||
queryCount: c.queryCount,
|
||||
}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user