Skip to content

Commit

Permalink
feat: 将 canyon-report 抽离出来
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Oct 17, 2023
1 parent 5816a5c commit 9f9498a
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/app-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"graphql": "^16.8.0",
"graphql-query-complexity": "^0.12.0",
"graphql-subscriptions": "^2.0.0",
"istanbul-lib-coverage": "^3.2.0",
"luxon": "^3.4.2",
"mockjs": "^1.1.0",
"mongodb": "^6.1.0",
Expand Down
25 changes: 25 additions & 0 deletions packages/app-backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,21 @@ type DiffLine {
new_lines: [Float!]!
}

type Statistics2 {
total: Float!
covered: Float!
skipped: Float!
pct: Float!
}

type CoverageSummary {
path: String!
statements: Statistics2!
lines: Statistics2!
functions: Statistics2!
branches: Statistics2!
}

type GitlabFileInfo {
"""文件内容"""
content: String!
Expand Down Expand Up @@ -251,6 +266,16 @@ type Query {
"""Commit Sha"""
commit_sha: String!
): [DiffLine!]!
retrieveRepoCoverageSummary(
"""仓库ID"""
repo_id: ID!

"""Commit Sha"""
commit_sha: String!

"""上报 ID"""
report_id: String!
): [CoverageSummary!]!

"""获取文件内容"""
fileInfo(repo_id: ID!, filepath: String!, commit_sha: String!): GitlabFileInfo!
Expand Down
29 changes: 29 additions & 0 deletions packages/app-backend/src/coverage/coverage.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CoverageRepo } from './models/coverage-repo.model';
import { CoverageOverview } from './models/coverage-overview.model';
import { CoverageBaseInfo } from './models/coverage-base-info.model';
import { DiffLine } from './models/newdiff.model';
import { CoverageSummary } from './models/coverage-summary';

@Resolver(() => 'Coverage')
export class CoverageResolver {
Expand Down Expand Up @@ -150,4 +151,32 @@ export class CoverageResolver {
): Promise<DiffLine[]> {
return this.teamEnvironmentsService.newdiff(repo_id, commit_sha);
}

@Query(() => [CoverageSummary], {})
retrieveRepoCoverageSummary(
@Args({
name: 'repo_id',
type: () => ID,
description: '仓库ID',
})
repo_id: string,
@Args({
name: 'commit_sha',
type: () => String,
description: 'Commit Sha',
})
commit_sha: string,
@Args({
name: 'report_id',
type: () => String,
description: '上报 ID',
})
report_id: string,
): Promise<CoverageSummary[]> {
return this.teamEnvironmentsService.retrieveRepoCoverageSummary(
repo_id,
commit_sha,
report_id,
);
}
}
54 changes: 54 additions & 0 deletions packages/app-backend/src/coverage/coverage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,33 @@ import { decompressedData } from '../utils/zstd';
import CanyonUtil from 'canyon-util';
import { CoverageDocument } from './schema/coverage.schema';
import * as dayjs from 'dayjs';
import * as libCoverage from 'istanbul-lib-coverage';
function dange(d) {
return d.reduce((p, c, index) => {
return {
...p,
[index]: c,
};
}, {});
}

// 还原 istanbul 的数据结构
function reductionCoverage(coverage) {
return coverage.reduce((p, c) => {
return {
...p,
[c.path]: {
statementMap: dange(c.statementMap),
fnMap: dange(c.fnMap),
branchMap: dange(c.branchMap),
s: dange(c.s),
f: dange(c.f),
b: dange(c.b),
path: c.path,
},
};
}, {});
}
function calculateSct(sct: { covered: number; total: number }) {
if (sct.total === 0) {
return '0%';
Expand All @@ -22,6 +48,18 @@ function calculateSctNum(sct: { covered: number; total: number }) {
}
return Math.floor((sct.covered / sct.total) * 100);
}
const genSummary = (coverage: any, rootPath: string) => {
const summary: any = {};
const m = libCoverage.createCoverageMap(coverage);
m.files().forEach(function (f) {
const fc = m.fileCoverageFor(f),
s = fc.toSummary();
summary[f.replace(rootPath, '')] = s;
});
console.log(JSON.parse(JSON.stringify(summary)));
return JSON.parse(JSON.stringify(summary));
};

@Injectable()
export class CoverageService {
constructor(
Expand Down Expand Up @@ -307,6 +345,22 @@ export class CoverageService {
});
}

async retrieveRepoCoverageSummary(repo_id, commit_sha, report_id) {
const covObject = await this.getCoverageDataFromDB(
repo_id,
commit_sha,
report_id,
);
const istanbulCoverage = genSummary(reductionCoverage(covObject), '');
return Object.keys(istanbulCoverage).reduce((p, c) => {
p.push({
path: c,
...istanbulCoverage[c],
});
return p;
}, []);
}

private async getCoverageDataFromDB(repoID, commit_sha, report_id) {
const { relation_id } = await this.prisma.coverage.findFirst({
where: {
Expand Down
13 changes: 13 additions & 0 deletions packages/app-backend/src/coverage/models/common.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Field, ObjectType } from '@nestjs/graphql';

@ObjectType()
export class Statistics2 {
@Field(() => Number)
total: number;
@Field(() => Number)
covered: number;
@Field(() => Number)
skipped: number;
@Field(() => Number)
pct: number;
}
16 changes: 16 additions & 0 deletions packages/app-backend/src/coverage/models/coverage-summary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { Statistics2 } from './common.model';

@ObjectType()
export class CoverageSummary {
@Field(() => String)
path: string;
@Field(() => Statistics2)
statements: Statistics2;
@Field(() => Statistics2)
lines: Statistics2;
@Field(() => Statistics2)
functions: Statistics2;
@Field(() => Statistics2)
branches: Statistics2;
}
2 changes: 2 additions & 0 deletions packages/app-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
"ahooks": "^3.7.8",
"antd": "^5.8.4",
"axios": "^1.5.0",
"canyon-report": "^0.2.2",
"dayjs": "^1.11.9",
"echarts": "^5.4.3",
"echarts-for-react": "^3.0.2",
"graphql": "^16.8.0",
"highlight.js": "^11.8.0",
"i18next": "^23.4.6",
"istanbul-lib-coverage": "^3.2.0",
"monaco-editor": "^0.43.0",
"path-to-regexp": "^6.2.1",
"query-string": "^8.1.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/app-ui/src/helpers/backend/gen/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const documents = {
"query GetRepo($repo_id: ID!) {\n getRepo(repo_id: $repo_id) {\n id\n name\n path_with_namespace\n description\n report_times\n }\n}": types.GetRepoDocument,
"query GetTreeSummary($repo_id: ID!, $commit_sha: String!, $report_id: String!, $mode: String!) {\n treeSummary(\n repo_id: $repo_id\n commit_sha: $commit_sha\n report_id: $report_id\n mode: $mode\n )\n}": types.GetTreeSummaryDocument,
"query Me {\n me {\n id\n username\n password\n nickname\n avatar\n th_refresh_token\n th_access_token\n email\n th_id\n role\n created_at\n }\n}": types.MeDocument,
"query RetrieveRepoCoverageSummary($repo_id: ID!, $commit_sha: String!, $report_id: String!) {\n retrieveRepoCoverageSummary(\n repo_id: $repo_id\n commit_sha: $commit_sha\n report_id: $report_id\n ) {\n path\n statements {\n total\n covered\n skipped\n pct\n }\n lines {\n total\n covered\n skipped\n pct\n }\n functions {\n total\n covered\n skipped\n pct\n }\n branches {\n total\n covered\n skipped\n pct\n }\n }\n}": types.RetrieveRepoCoverageSummaryDocument,
};

/**
Expand Down Expand Up @@ -74,6 +75,10 @@ export function graphql(source: "query GetTreeSummary($repo_id: ID!, $commit_sha
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(source: "query Me {\n me {\n id\n username\n password\n nickname\n avatar\n th_refresh_token\n th_access_token\n email\n th_id\n role\n created_at\n }\n}"): (typeof documents)["query Me {\n me {\n id\n username\n password\n nickname\n avatar\n th_refresh_token\n th_access_token\n email\n th_id\n role\n created_at\n }\n}"];
/**
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
*/
export function graphql(source: "query RetrieveRepoCoverageSummary($repo_id: ID!, $commit_sha: String!, $report_id: String!) {\n retrieveRepoCoverageSummary(\n repo_id: $repo_id\n commit_sha: $commit_sha\n report_id: $report_id\n ) {\n path\n statements {\n total\n covered\n skipped\n pct\n }\n lines {\n total\n covered\n skipped\n pct\n }\n functions {\n total\n covered\n skipped\n pct\n }\n branches {\n total\n covered\n skipped\n pct\n }\n }\n}"): (typeof documents)["query RetrieveRepoCoverageSummary($repo_id: ID!, $commit_sha: String!, $report_id: String!) {\n retrieveRepoCoverageSummary(\n repo_id: $repo_id\n commit_sha: $commit_sha\n report_id: $report_id\n ) {\n path\n statements {\n total\n covered\n skipped\n pct\n }\n lines {\n total\n covered\n skipped\n pct\n }\n functions {\n total\n covered\n skipped\n pct\n }\n branches {\n total\n covered\n skipped\n pct\n }\n }\n}"];

export function graphql(source: string) {
return (documents as any)[source] ?? {};
Expand Down
Loading

0 comments on commit 9f9498a

Please sign in to comment.