From 54ec11818df5f4e3a979c4407646b1e998b8506e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Allen=20Zhang=20=28=E5=BC=A0=E6=B6=9B=29?= Date: Tue, 20 Feb 2024 18:59:44 +0800 Subject: [PATCH] feat: agg task --- .../services/consumer-coverage.service.ts | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/canyon-backend/src/tasks/services/consumer-coverage.service.ts b/packages/canyon-backend/src/tasks/services/consumer-coverage.service.ts index b92013d4..6a83ad6f 100755 --- a/packages/canyon-backend/src/tasks/services/consumer-coverage.service.ts +++ b/packages/canyon-backend/src/tasks/services/consumer-coverage.service.ts @@ -288,15 +288,37 @@ export class ConsumerCoverageService { } // 查询agg类型的数据,如果有就更新,没有就插入 - const updateCoverage = await this.prisma.coverage.findFirst({ + const updateCoverages = await this.prisma.coverage.findMany({ where: removeNullKeys({ covType: covType, sha: normalCoverage.sha, reportID: covType === 'agg' ? normalCoverage.reportID : null, }), + orderBy: { + createdAt: 'asc', //升序排,保留最老的数据 + }, }); - if (updateCoverage) { + if (updateCoverages.length > 0) { + // 更新最老的那个数据 + const updateCoverage = updateCoverages[0]; + if (updateCoverages.length > 1) { + logger({ + type: 'error', + title: '覆盖率数据错误', + message: `agg类型的数据有多个`, + }); + await this.prisma.coverage.deleteMany({ + where: { + id: { + in: updateCoverages + .filter((_, index) => index !== 0) // 除了第一个 + .map((item) => String(item.id)), + }, + }, + }); + } + await this.prisma.coverage.update({ where: { id: String(updateCoverage.id),