Skip to content

Commit

Permalink
chore: repo overall update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Nov 19, 2024
1 parent a95e814 commit 3cfb91b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 97 deletions.
1 change: 1 addition & 0 deletions packages/canyon-collect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@prisma/client": "5.16.1",
"@swc/cli": "0.4.1-nightly.20240914",
"@swc/core": "1.7.40",
"canyon-data": "^0.1.1-alpha.11",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"dotenv": "^16.4.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,34 @@ import {
regularData,
remapCoverage,
} from '../../../utils/coverage';
import { mergeCoverageMap } from 'canyon-data';

@Injectable()
export class CoverageClientService {
constructor(private readonly prisma: PrismaService) {}
async invoke({ sha, projectID, coverage, instrumentCwd }) {
// 1. 检查是否上传map
const coverageMapCount = await this.prisma.coverage.findFirst({
// coverage
const coverageDb = await this.prisma.coverage.findFirst({
where: {
projectID: projectID,
sha: sha,
covType: 'all',
},
});

if (!coverageMapCount) {
if (!coverageDb) {
throw new HttpException('coverage map not found', 400);
}

const oldcoverage =
const coverageObject =
typeof coverage === 'string' ? JSON.parse(coverage) : coverage;

// const sss = regularData(oldcoverage)
// const coverageDbMap = await decompressedData(coverageDb.map)
// .then((r) => r.toString())
// .then((r) => JSON.parse(r));

const coverageMapCount111 = await decompressedData(coverageMapCount.map)
const coverageDbHit = await decompressedData(coverageDb.hit)
.then((r) => r.toString())
.then((r) => JSON.parse(r));
// console.log('oldcoverage', coverageMapCount111);
Expand All @@ -44,93 +48,30 @@ export class CoverageClientService {
// map要存,而且build时候的路径要存,不然回不去********
// ********
const { coverage: formartCOv } = await formatReportObject({
coverage: regularData(oldcoverage),
coverage: regularData(coverageObject),
instrumentCwd: instrumentCwd,
});

const formatCoverage = IstanbulHitMapSchema.parse(formartCOv);
const formatCoverageHit = IstanbulHitMapSchema.parse(formartCOv);

console.log(coverageMapCount111, formatCoverage);
// console.log(coverageMapCount111, formatCoverage);

const finalRes = {};
for (const key in coverageMapCount111) {
console.log(key);
finalRes[key] = {
...coverageMapCount111[key],
...formatCoverage[key],
path: key,
};
}
// ut_coverage,在基础上扩展,source之类的

// invoke.coverage
const formatCoverageStr = JSON.stringify(formatCoverage);
// const size = new TextEncoder().encode(formatCoverageStr).length;
const compressedFormatCoverageStr = await compressedData(formatCoverageStr);

// 先不考虑分布式,直接存储
// this.prisma.coverage.upsert()
// ********* 合并
const mergeCoveHit = mergeCoverageMap(coverageDbHit, formatCoverageHit);
// *********

return remapCoverage(finalRes).then((r) => {
return r;
// update
// mergeCoveHit
const sss = await compressedData(JSON.stringify(mergeCoveHit));
return this.prisma.coverage.updateMany({
where: {
projectID: projectID,
sha: sha,
covType: 'all',
},
data: {
hit: sss,
},
});
}
// async invoke(invoke) {
// // 1. 检查是否上传map
// const coverageMapCount = await this.prisma.coverageMap.count({
// where: {
// projectID: invoke.projectID,
// sha: invoke.sha,
// },
// });
//
// if (coverageMapCount === 0) {
// throw new HttpException('coverage map not found', 400);
// }
//
// const oldcoverage =
// typeof invoke.coverage === 'string'
// ? JSON.parse(invoke.coverage)
// : invoke.coverage;
//
// // 暂时解决方案,需要解决sourceMap问题
// const { coverage } = await formatReportObject({
// coverage: regularData(oldcoverage),
// instrumentCwd: invoke.instrumentCwd,
// });
//
// const formatCoverage = IstanbulHitMapSchema.parse(coverage);
//
// // ut_coverage,在基础上扩展,source之类的
//
// // invoke.coverage
// const formatCoverageStr = JSON.stringify(formatCoverage);
// const size = new TextEncoder().encode(formatCoverageStr).length;
// const compressedFormatCoverageStr = await compressedData(formatCoverageStr);
// return this.prisma.coverageLog
// .create({
// data: {
// projectID: invoke.projectID,
// sha: invoke.sha,
// timing: invoke.timing,
// hit: compressedFormatCoverageStr,
// size: size,
// },
// })
// .then((r) => {
// if (r) {
// return {
// id: r.id,
// size: r.size,
// };
// } else {
// return {
// id: '',
// size: 0,
// };
// }
// });
//
// // return formatCoverage
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { compressedData } from '../../../utils/zstd';
import {
formatReportObject,
regularData,
removeStartEndNull,
resetCoverageData,
} from '../../../utils/coverage';
import { coverageObj } from '../models/coverage.model';
Expand All @@ -26,7 +27,9 @@ export class CoverageMapClientService {
instrumentCwd: instrumentCwd,
});

const formatCoverageMap = IstanbulMapMapSchema.parse(formatedCoverage);
const formatCoverageMap = IstanbulMapMapSchema.parse(
removeStartEndNull(formatedCoverage),
);

// coverage
const formatCoverageStr = JSON.stringify(formatCoverageMap);
Expand Down
2 changes: 1 addition & 1 deletion packages/canyon-collect/src/utils/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function regularData(data: any) {
}

// TODO:在覆盖率map数据上来的时候,有必要做一次过滤,去掉start和end为空的情况。然后再交由zod进行校验,这里需要非常严格的校验。
const removeStartEndNull = (coverage) => {
export const removeStartEndNull = (coverage) => {
const obj = {};
Object.keys(coverage).forEach((key) => {
const item = coverage[key];
Expand Down
3 changes: 3 additions & 0 deletions packages/canyon-collect/src/utils/zstd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export async function compressedData(input) {
}

export async function decompressedData(compressedData) {
if (compressedData.length === 0) {
return '{}';
}
const decompressed = zlib.brotliDecompressSync(compressedData);
return decompressed;
}
21 changes: 12 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3cfb91b

Please sign in to comment.