diff --git a/apps/admin/src/views/Dashboard.vue b/apps/admin/src/views/Dashboard.vue index 81d76f1..aa843c0 100644 --- a/apps/admin/src/views/Dashboard.vue +++ b/apps/admin/src/views/Dashboard.vue @@ -93,7 +93,7 @@ const stats = computed(() => [ { key: 'activatedCodes', label: t('pages.dashboard.activatedCodes'), icon: CheckmarkCircleOutline, color: '#138a72', open: () => router.push({ path: '/trace-codes', query: { status: 'ACTIVE' } }) }, { key: 'totalQueries', label: t('pages.dashboard.totalQueries'), icon: SearchOutline, color: '#3a7bd5', open: () => router.push('/query-logs') }, { key: 'todayQueries', label: t('pages.dashboard.todayQueries'), icon: TodayOutline, color: '#b9851f', open: () => router.push({ path: '/query-logs', query: { quickRange: 'today' } }) }, - { key: 'abnormalQueries', label: t('pages.dashboard.abnormalQueries'), icon: WarningOutline, color: '#c0392b', open: () => router.push({ path: '/query-logs', query: { result: 'ABNORMAL' } }) }, + { key: 'abnormalQueries', label: t('pages.dashboard.abnormalQueries'), icon: WarningOutline, color: '#c0392b', open: () => router.push({ path: '/query-logs', query: { abnormal: '1' } }) }, ]); const trendOption = computed(() => ({ diff --git a/apps/admin/src/views/QueryLogs.vue b/apps/admin/src/views/QueryLogs.vue index fe12fbe..78c8595 100644 --- a/apps/admin/src/views/QueryLogs.vue +++ b/apps/admin/src/views/QueryLogs.vue @@ -60,7 +60,7 @@ import { useI18n } from 'vue-i18n'; import { NTag, type DataTableColumns } from 'naive-ui'; import { SearchOutline } from '@vicons/ionicons5'; import dayjs from 'dayjs'; -import { QueryResult } from '@artveris/shared'; +import { ABNORMAL_QUERY_RESULTS, QueryResult } from '@artveris/shared'; import { queryLogApi } from '@/api'; import { fmt, statusType } from '@/utils/format'; import { useTablePage } from '@/composables/useTablePage'; @@ -80,7 +80,12 @@ const query = reactive<{ keyword: string; archiveNo: string; batchNo: string; re }); const { pagination, page, pageSize, resetPage, setPageResult } = useTablePage(load); const { sortProps, onSorterChange, sortParams } = useRemoteSort(onSearch); -const resultOptions = Object.values(QueryResult).map((v) => ({ label: t(`common.queryResult.${v}`), value: v })); +// 看板「异常查询」的口径:无效、停用、异常三种一起看 +const ABNORMAL_GROUP = '__abnormal__'; +const resultOptions = [ + ...Object.values(QueryResult).map((v) => ({ label: t(`common.queryResult.${v}`), value: v as string })), + { label: ABNORMAL_QUERY_RESULTS.map((v) => t(`common.queryResult.${v}`)).join(' / '), value: ABNORMAL_GROUP }, +]; const columns: DataTableColumns = [ { title: () => t('pages.traceCode.shortCode'), key: 'shortCode', width: 130, align: 'center', ellipsis: { tooltip: true }, render: (r) => r.shortCode || '-' }, @@ -121,7 +126,8 @@ async function load() { keyword: query.keyword || undefined, archiveNo: query.archiveNo || undefined, batchNo: query.batchNo || undefined, - result: query.result || undefined, + result: query.result && query.result !== ABNORMAL_GROUP ? query.result : undefined, + abnormal: query.result === ABNORMAL_GROUP ? true : undefined, startTime: query.startTime, endTime: query.endTime, page: page.value, @@ -154,7 +160,8 @@ function setResultFilter(value: string | number | null) { } onMounted(() => { - if (typeof route.query.result === 'string') query.result = route.query.result; + if (route.query.abnormal === '1') query.result = ABNORMAL_GROUP; + else if (typeof route.query.result === 'string') query.result = route.query.result; if (route.query.quickRange === 'today') { const start = dayjs().startOf('day'); const end = dayjs().endOf('day'); diff --git a/apps/api/src/dashboard/dashboard.service.ts b/apps/api/src/dashboard/dashboard.service.ts index 7888d92..b2b5a80 100644 --- a/apps/api/src/dashboard/dashboard.service.ts +++ b/apps/api/src/dashboard/dashboard.service.ts @@ -1,6 +1,6 @@ import { Injectable } from '@nestjs/common'; import dayjs from 'dayjs'; -import { QueryResult } from '@artveris/shared'; +import { ABNORMAL_QUERY_RESULTS } from '@artveris/shared'; import { PrismaService } from '../prisma/prisma.service'; export interface DashboardOverviewQuery { @@ -41,9 +41,7 @@ export class DashboardService { this.prisma.traceCode.count({ where: { firstActivatedAt: { not: null } } }), 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] } }, - }), + this.prisma.queryLog.count({ where: { queryResult: { in: [...ABNORMAL_QUERY_RESULTS] } } }), ]); return { diff --git a/apps/api/src/query-log/query-log.controller.spec.ts b/apps/api/src/query-log/query-log.controller.spec.ts new file mode 100644 index 0000000..1a78b2b --- /dev/null +++ b/apps/api/src/query-log/query-log.controller.spec.ts @@ -0,0 +1,33 @@ +import { QueryLogController } from './query-log.controller'; + +function createController() { + const prisma = { + queryLog: { findMany: jest.fn().mockReturnValue('rows'), count: jest.fn().mockReturnValue('total') }, + $transaction: jest.fn().mockResolvedValue([[], 0]), + }; + return { prisma, controller: new QueryLogController(prisma as any) }; +} +const paging = { page: 1, pageSize: 20, skip: 0, take: 20 }; + +describe('QueryLogController result filters', () => { + it('lists invalid, disabled and abnormal queries together for the dashboard figure', async () => { + const { controller, prisma } = createController(); + await controller.list({ ...paging, abnormal: true } as any); + expect(prisma.queryLog.count).toHaveBeenCalledWith({ + where: { queryResult: { in: ['INVALID', 'DISABLED', 'ABNORMAL'] } }, + }); + expect(prisma.queryLog.findMany.mock.calls[0][0].where).toEqual(prisma.queryLog.count.mock.calls[0][0].where); + }); + + it('keeps a single chosen result when both filters are given', async () => { + const { controller, prisma } = createController(); + await controller.list({ ...paging, abnormal: true, result: 'REPEAT' } as any); + expect(prisma.queryLog.count).toHaveBeenCalledWith({ where: { queryResult: 'REPEAT' } }); + }); + + it('does not filter by result when neither is given', async () => { + const { controller, prisma } = createController(); + await controller.list({ ...paging } as any); + expect(prisma.queryLog.count).toHaveBeenCalledWith({ where: {} }); + }); +}); diff --git a/apps/api/src/query-log/query-log.controller.ts b/apps/api/src/query-log/query-log.controller.ts index 2ba2f8e..6b2ad27 100644 --- a/apps/api/src/query-log/query-log.controller.ts +++ b/apps/api/src/query-log/query-log.controller.ts @@ -1,8 +1,9 @@ import { Controller, Get, Query } from '@nestjs/common'; import { ApiBearerAuth, ApiOperation, ApiPropertyOptional, ApiTags } from '@nestjs/swagger'; -import { IsEnum, IsOptional, IsString } from 'class-validator'; +import { Transform } from 'class-transformer'; +import { IsBoolean, IsEnum, IsOptional, IsString } from 'class-validator'; import { Prisma } from '@prisma/client'; -import { QueryResult } from '@artveris/shared'; +import { ABNORMAL_QUERY_RESULTS, QueryResult } from '@artveris/shared'; import { PrismaService } from '../prisma/prisma.service'; import { buildPage } from '../common/utils/pagination.util'; import { PaginationDto } from '../common/dto/pagination.dto'; @@ -29,6 +30,14 @@ class QueryLogQueryDto extends PaginationDto { @IsEnum(QueryResult) result?: QueryResult; + @ApiPropertyOptional({ description: '只看异常查询(无效、停用、异常),与看板「异常查询」同一口径' }) + @IsOptional() + @Transform(({ value }) => + value === undefined || value === null || value === '' ? undefined : value === true || value === 'true', + ) + @IsBoolean() + abnormal?: boolean; + @ApiPropertyOptional({ description: '开始时间' }) @IsOptional() @IsString() @@ -59,6 +68,7 @@ export class QueryLogController { if (query.archiveNo) where.archiveNo = { contains: query.archiveNo }; if (query.batchNo) where.batchNo = { contains: query.batchNo }; if (query.result) where.queryResult = query.result; + else if (query.abnormal) where.queryResult = { in: [...ABNORMAL_QUERY_RESULTS] }; if (query.startTime || query.endTime) { where.queriedAt = {}; if (query.startTime) where.queriedAt.gte = new Date(query.startTime); diff --git a/packages/shared/src/enums.ts b/packages/shared/src/enums.ts index cc518cf..373ef27 100644 --- a/packages/shared/src/enums.ts +++ b/packages/shared/src/enums.ts @@ -69,6 +69,13 @@ export enum QueryResult { ABNORMAL = 'ABNORMAL', } +/** 「异常查询」的统计口径:查到无效码、已停用或状态异常的追溯码都算。看板数字和查询日志筛选共用。 */ +export const ABNORMAL_QUERY_RESULTS: readonly QueryResult[] = [ + QueryResult.INVALID, + QueryResult.DISABLED, + QueryResult.ABNORMAL, +]; + export enum ProofStatus { PENDING = 'PENDING', SUBMITTING = 'SUBMITTING',