Skip to content

Commit

Permalink
feat: 概览接口添加状态
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Zhang committed Dec 27, 2023
1 parent 07873ea commit f1157a8
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 100 deletions.
26 changes: 26 additions & 0 deletions packages/canyon-backend/src/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const emptyStatistics = {
newlines: {
total: 0,
covered: 0,
skipped: 0,
pct: 0,
},
lines: {
total: 0,
covered: 0,
skipped: 0,
pct: 0,
},
functions: {
total: 0,
covered: 0,
skipped: 0,
pct: 0,
},
branches: {
total: 0,
covered: 0,
skipped: 0,
pct: 0,
},
};
14 changes: 9 additions & 5 deletions packages/canyon-backend/src/coverage/coverage.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ export class CoverageController {
reportId?: string;
report_id?: string;
reportID?: string;
commitSha?: string;
commit_sha?: string;
sha?: string;
},
) {
return this.triggerAggCoverageService.invoke({
reportID: params.reportId || params.report_id || params.reportID,
commitSha: params.commitSha || params.commit_sha || null,
sha: params.sha,
});
}

Expand Down Expand Up @@ -91,12 +90,17 @@ export class CoverageController {
commitsha?: string;
commitSha?: string;
commit_sha?: string;
sha?: string;
},
) {
return this.retrieveCoverageTreeSummaryService.invoke({
reportID: params.reportId || params.report_id || params.reportID || null,
commitSha:
params.commitsha || params.commitSha || params.commit_sha || null,
sha:
params.sha ||
params.commitsha ||
params.commitSha ||
params.commit_sha ||
null,
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,82 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../prisma/prisma.service';
import { getProjectByID } from '../../adapter/gitlab.adapter';
import { calculateCoverageOverviewByConditionFilter } from '../../utils/summary';
import { removeNullKeys } from '../../utils/utils';
import { emptyStatistics } from '../../constant';

@Injectable()
export class RetrieveCoverageTreeSummaryService {
constructor(private readonly prisma: PrismaService) {}

async invoke(params) {
async invoke(params: { reportID?: string; sha?: string }) {
const redirectUri = process.env.REDIRECT_URI;
const { commitSha } = await this.prisma.summary.findFirst({
const summaryFindFirst = await this.prisma.summary.findFirst({
where: removeNullKeys({
reportID: params.reportID,
commitSha: params.commitSha,
covType: 'all',
commitSha: params.sha,
covType: 'agg',
}),
});

const cv = await this.prisma.coverage.findFirst({
const commitSha = summaryFindFirst?.commitSha;

if (!commitSha) {
const noCommitShaWithCoverage = await this.prisma.coverage.findFirst({
where: removeNullKeys({
reportID: params.reportID,
commitSha: params.sha,
}),
});
return {
status: 'pending',
reportIDs: [],
sha: noCommitShaWithCoverage?.commitSha || '',
statistics: emptyStatistics,
};
}

const coverage = await this.prisma.coverage.findFirst({
where: {
commitSha,
},
});
const repoGitlabInfo = await getProjectByID(
cv.projectID,
'gdr7c7GAPjZ2JXWAZKXH',
);

const summarys = await this.prisma.summary.findMany({
where: {
commitSha,
covType: 'all',
},
});
// 这边加一下状态,只能根据commit查,并且必须是所有关联的reportID都是success才返回

const tasks = await this.prisma.task.findMany({
where: {
commitSha,
projectID: coverage.projectID,
},
});
function getStatus(tasks) {
const state = tasks.every((task) => task.status === 'success');
if (state) {
return 'success';
} else if (
tasks.some(
(task) => task.status === 'running' || task.status === 'notstarted',
)
) {
return 'pending';
}
return 'fail';
}
return {
status: getStatus(tasks),
reportIDs: tasks
.filter(({ status }) => status)
.map(({ reportID }) => reportID),
reportUrl: `${(redirectUri || '').replace('/login', '')}/project/${
cv.projectID
coverage.projectID
}/commits/${commitSha}`,
commitSha: commitSha,
sha: commitSha,
statistics: calculateCoverageOverviewByConditionFilter(summarys),
repo: repoGitlabInfo,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,102 +9,113 @@ function checkTasksStatus(tasks) {
@Injectable()
export class TriggerAggCoverageService {
constructor(private readonly prisma: PrismaService) {}
// 传sha返回一个数组
// 传reportID返回一个对象
async invoke(params: { reportID?: string; sha?: string }) {
if (params.sha) {
const reportIDs = await this.prisma.coverage.findMany({
where: {
commitSha: params.sha,
covType: 'normal',
},
distinct: ['reportID'],
select: {
reportID: true,
},
});

async invoke(params) {
const { commitSha } = params;

const reportIDs = await this.prisma.coverage.findMany({
const results = [];
for (let i = 0; i < reportIDs.length; i++) {
const result = await this.createAggTaskByReportID(
reportIDs[i].reportID,
);
results.push(result);
}
return results;
} else if (params.reportID) {
return await this.createAggTaskByReportID(params.reportID);
} else {
return {
msg: '参数错误',
data: [],
code: 0,
reportID: params.reportID,
};
}
}
async createAggTaskByReportID(reportID) {
const tasks = await this.prisma.task.findMany({
where: {
commitSha,
reportID,
},
distinct: ['reportID'],
select: {
reportID: true,
});
const coverages = await this.prisma.coverage.findMany({
where: {
reportID: reportID,
},
});

const arr = [];
for (let i = 0; i < reportIDs.length; i++) {
const s = await ffffffff.call(this, { reportID: reportIDs[i].reportID });
arr.push(s);
if (coverages.length === 0) {
return {
msg: '未查询到reportId',
data: [],
code: 0,
reportID: reportID,
};
}

return arr;
// 如果有聚合在跑,就提示在聚合
if (tasks.length > 0 && checkTasksStatus(tasks)) {
return {
msg: '报告聚合中',
data: [],
code: -1,
reportID: reportID,
};
} 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);
}

// ********** 重要 **********
// 关键
// ********** 重要 **********

async function ffffffff() {
const { reportID } = params;
const tasks = await this.prisma.task.findMany({
const commitsAssociatedWithReport = await this.prisma.coverage.findMany({
where: {
reportID,
},
});
const coverages = await this.prisma.coverage.findMany({
where: {
reportID: reportID,
distinct: ['commitSha'], // 使用 distinct 来确保返回唯一的 commitSha
select: {
commitSha: true,
projectID: true, // 添加 projectID 到返回结果中
},
});
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: commitSha,
projectID: projectID,
result: {},
},
);
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: commitSha,
projectID: projectID,
result: {},
},
});
}

return {
msg: '聚合中',
data: [],
code: 1,
};
});
}

return {
msg: '聚合中',
data: [],
code: 1,
reportID: reportID,
};
}
}
}

0 comments on commit f1157a8

Please sign in to comment.