-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db12199
commit e047f8a
Showing
19 changed files
with
743 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/canyon-backend/src/coverage/services/list-agg-status.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '../../prisma/prisma.service'; | ||
function checkTasksStatus(tasks) { | ||
return tasks.some((task) => ['running', 'notstarted'].includes(task.status)); | ||
} | ||
function checkTasksStatusSuccess(tasks) { | ||
return tasks.every((task) => task.status === 'success'); | ||
} | ||
@Injectable() | ||
export class ListAggStatusService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
async invoke(params) { | ||
const tasks = await this.prisma.task.findMany({ | ||
where: { | ||
reportID: params.reportID, | ||
}, | ||
}); | ||
if (tasks.length === 0) { | ||
return { | ||
code: 0, | ||
msg: '未查询到reportId', | ||
data: [], | ||
}; | ||
} else if (checkTasksStatus(tasks)) { | ||
return { | ||
code: 1, | ||
msg: '聚合中', | ||
data: [], | ||
}; | ||
} else if (checkTasksStatusSuccess(tasks)) { | ||
return { | ||
code: 2, | ||
msg: '聚合完成', | ||
}; | ||
} else { | ||
return { | ||
code: 3, | ||
msg: '聚合失败', | ||
}; | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
packages/canyon-backend/src/coverage/services/trigger-agg-coverage.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '../../prisma/prisma.service'; | ||
import axios from 'axios'; | ||
|
||
function checkTasksStatus(tasks) { | ||
return tasks.some((task) => { | ||
return ['running', 'notstarted'].includes(task.status); | ||
}); | ||
} | ||
@Injectable() | ||
export class TriggerAggCoverageService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
|
||
async invoke(params) { | ||
const { reportID } = params; | ||
const tasks = await this.prisma.task.findMany({ | ||
where: { | ||
reportID, | ||
}, | ||
}); | ||
const coverages = await this.prisma.coverage.findMany({ | ||
where: { | ||
reportID: reportID, | ||
}, | ||
}); | ||
if (coverages.length === 0) { | ||
return { | ||
msg: '未查询到reportId', | ||
data: [], | ||
code: 0, | ||
}; | ||
} | ||
|
||
// 如果有聚合在跑,就提示在聚合 | ||
if (tasks.length > 0 && checkTasksStatus(tasks)) { | ||
return { | ||
msg: '报告聚合中', | ||
data: [], | ||
code: -1, | ||
}; | ||
} else { | ||
// 如果这个 reportID聚合完成了,就删除旧的聚合任务,创建新的聚合任务 | ||
for (let i = 0; i < tasks.length; i++) { | ||
await this.prisma.task.deleteMany({ | ||
where: { | ||
id: tasks[i].id, | ||
}, | ||
}); | ||
console.log('删除旧的聚合任务' + tasks[i].id); | ||
} | ||
|
||
// ********** 重要 ********** | ||
// 关键 | ||
// ********** 重要 ********** | ||
|
||
const commitsAssociatedWithReport = await this.prisma.coverage.findMany({ | ||
where: { | ||
reportID, | ||
}, | ||
distinct: ['commitSha'], // 使用 distinct 来确保返回唯一的 commitSha | ||
select: { | ||
commitSha: true, | ||
projectID: true, // 添加 projectID 到返回结果中 | ||
}, | ||
}); | ||
for (let i = 0; i < commitsAssociatedWithReport.length; i++) { | ||
const { commitSha, projectID } = commitsAssociatedWithReport[i]; | ||
await this.prisma.task.create({ | ||
data: { | ||
name: `Coverage Agg Task ${reportID},commitSha:${commitSha},projectID:${projectID}`, | ||
status: 'notstarted', | ||
reportID, | ||
commitSha: '', | ||
projectID: '', | ||
result: {}, | ||
}, | ||
}); | ||
} | ||
|
||
return { | ||
msg: '聚合中', | ||
data: [], | ||
code: 1, | ||
}; | ||
} | ||
} | ||
} |
Empty file.
97 changes: 97 additions & 0 deletions
97
packages/canyon-backend/src/tasks/services/aggregation-coverage.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { PrismaService } from '../../prisma/prisma.service'; | ||
import { | ||
createNewCoverageData, | ||
getSpecificCoverageData, | ||
} from '../../adapter/coverage-data.adapter'; | ||
import { genSummaryMapByCoverageMap, mergeCoverage } from '@canyon/data'; | ||
|
||
export class AggregationCoverageService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
async invoke(covType, commitSha, reportID) { | ||
const coverages = await this.prisma.coverage.findMany({ | ||
where: { | ||
covType: covType === 'agg' ? 'normal' : 'agg', | ||
reportID: covType === 'agg' ? reportID : null, | ||
commitSha, | ||
}, | ||
}); | ||
|
||
let mainCov = {}; | ||
|
||
for (let i = 0; i < coverages.length; i++) { | ||
const singleCov = await getSpecificCoverageData(coverages[i].relationID); | ||
mainCov = mergeCoverage(mainCov, singleCov); | ||
} | ||
|
||
const mainCovCoverageData = await createNewCoverageData(mainCov); | ||
|
||
// 删除老的 | ||
await this.prisma.coverage.deleteMany({ | ||
where: { | ||
reportID: covType === 'agg' ? reportID : null, | ||
covType: covType, | ||
commitSha, | ||
}, | ||
}); | ||
|
||
// 只有是agg的时候要删除就的agg的summary | ||
if (covType === 'agg') { | ||
await this.prisma.summary.deleteMany({ | ||
where: { | ||
reportID: reportID, | ||
}, | ||
}); | ||
} | ||
|
||
const { compareTarget, projectID, reporter } = | ||
await this.prisma.coverage.findFirst({ | ||
where: { | ||
commitSha, | ||
covType: 'normal', | ||
}, | ||
orderBy: { | ||
createdAt: 'desc', // 按照 createdAt 字段的降序排列(最新的在前面) | ||
}, | ||
}); | ||
|
||
const codechanges = await this.prisma.codechange.findMany({ | ||
where: { | ||
commitSha, | ||
compareTarget, | ||
}, | ||
}); | ||
|
||
const coverageSummaryMap = genSummaryMapByCoverageMap(mainCov, codechanges); | ||
for (const coverageSummaryMapKey in coverageSummaryMap) { | ||
// 落库数据 | ||
const { total, skipped, covered } = coverageSummaryMap[ | ||
coverageSummaryMapKey | ||
] as any; | ||
console.log(coverageSummaryMapKey, total, skipped, covered); | ||
await this.prisma.summary.create({ | ||
data: { | ||
reportID: reportID, | ||
metricType: coverageSummaryMapKey, | ||
commitSha: commitSha, | ||
total, | ||
skipped, | ||
covered, | ||
}, | ||
}); | ||
} | ||
|
||
await this.prisma.coverage.create({ | ||
data: { | ||
compareTarget, | ||
commitSha, | ||
reportID: covType === 'agg' ? reportID : '', | ||
projectID, | ||
relationID: mainCovCoverageData.insertedId, | ||
covType: covType, | ||
reporter: reporter, | ||
key: '', | ||
instrumentCwd: '', | ||
}, | ||
}); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/canyon-backend/src/tasks/services/cleaning-up-outdated-data.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Cron } from '@nestjs/schedule'; | ||
import { PrismaService } from '../../prisma/prisma.service'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class CleaningUpOutdatedDataService { | ||
constructor(private readonly prisma: PrismaService) {} | ||
// 每过一天清理一下coverage集合,删除超过一周的normal数据 | ||
@Cron('30 2 * * *') // 每天凌晨2点半执行 | ||
async cleaningUpOutdatedData() { | ||
const coverageDeleteManyRes = await this.prisma.coverage.deleteMany({ | ||
where: { | ||
covType: 'normal', | ||
createdAt: { | ||
lt: new Date(new Date().valueOf() - 7 * 24 * 60 * 60 * 1000), | ||
}, | ||
}, | ||
}); | ||
console.log(coverageDeleteManyRes, 'coverageDeleteManyRes'); | ||
} | ||
} |
Oops, something went wrong.