Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Nov 10, 2023
1 parent c5727e6 commit e4e6b0d
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Warnings:
- You are about to drop the column `path` on the `summary` table. All the data in the column will be lost.
- Added the required column `report_id` to the `summary` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "summary" DROP COLUMN "path",
ADD COLUMN "report_id" TEXT NOT NULL;

-- CreateTable
CREATE TABLE "codechange" (
"id" SERIAL NOT NULL,
"project_id" TEXT NOT NULL,
"compare_target" TEXT NOT NULL,
"commit_sha" TEXT NOT NULL,
"path" TEXT NOT NULL,
"additions" INTEGER[],
"deletions" INTEGER[],
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "codechange_pkey" PRIMARY KEY ("id")
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Warnings:
- You are about to drop the column `type` on the `summary` table. All the data in the column will be lost.
- Added the required column `metric_type` to the `summary` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "summary" DROP COLUMN "type",
ADD COLUMN "metric_type" TEXT NOT NULL;
5 changes: 3 additions & 2 deletions packages/canyon-backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ model Project {
@@map("project")
}

// 只存 cov type 是 agg 的
model Summary {
id Int @id @default(autoincrement())
total Int // 总数
covered Int // 覆盖数
skipped Int // 跳过数
type String // 类型
metricType String @map("metric_type")
path String // 路径
reportID String @map("report_id")
commitSha String @map("commit_sha")
Expand Down
2 changes: 1 addition & 1 deletion packages/canyon-backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type Log {
commitSha: String!

"""上报ID"""
reportId: String!
reportID: String!

"""创建时间"""
createdAt: DateTime!
Expand Down
54 changes: 54 additions & 0 deletions packages/canyon-backend/src/adapter/gitlab.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@ interface FileInfo {
content: string;
}

interface Commit {
id: string;
short_id: string;
created_at: string;
parent_ids: string[];
title: string;
message: string;
author_name: string;
author_email: string;
authored_date: string;
committer_name: string;
committer_email: string;
committed_date: string;
web_url: string;
ci_reports: any[]; // 你可以根据实际数据结构定义适当的类型
stats: {
additions: number;
deletions: number;
total: number;
};
status: string;
project_id: number;
last_pipeline: {
id: number;
project_id: number;
sha: string;
ref: string;
status: string;
created_at: string;
updated_at: string;
web_url: string;
};
}

const { GITLAB_URL } = process.env;

export const getFileInfo = async (
Expand All @@ -39,3 +73,23 @@ export const getFileInfo = async (
)
.then(({ data }) => data);
};

export const getCommits = async (
{ projectID, commitShas }: { projectID: string; commitShas: string[] },
token: string,
) => {
return await Promise.all(
commitShas.map((commitSha) =>
axios
.get<Commit>(
`${GITLAB_URL}/api/v4/projects/${projectID}/repository/commits/${commitSha}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
},
)
.then(({ data }) => data),
),
);
};
3 changes: 2 additions & 1 deletion packages/canyon-backend/src/coverage/coverage.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { PrismaModule } from 'src/prisma/prisma.module';
import { CoverageController } from './coverage.controller';
import { CoverageResolver } from './coverage.resolver';
import { CoverageService } from './services/coverage.service';
import { ProjectOverviewService } from './services/project-overview.service';

@Module({
imports: [PrismaModule],
controllers: [CoverageController],
providers: [CoverageResolver, CoverageService],
providers: [CoverageResolver, CoverageService, ProjectOverviewService],
exports: [],
})
export class CoverageModule {}
5 changes: 3 additions & 2 deletions packages/canyon-backend/src/coverage/coverage.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { AuthUser } from '../types/AuthUser';
import { ProjectOverview } from './models/project-overview.model';
import { CoverageSummary } from './models/coverage-summary';
import { GitlabFileInfo } from './models/gitlab-file-info.model';
import { ProjectOverviewService } from './services/project-overview.service';
@Resolver(() => 'Coverage')
export class CoverageResolver {
constructor(
// private readonly projectService: ProjectService,
private readonly projectOverviewService: ProjectOverviewService,
private readonly coverageService: CoverageService,
) {}
@Query(() => [Project], {
Expand All @@ -30,7 +31,7 @@ export class CoverageResolver {
@GqlUser() user: AuthUser,
@Args('projectID', { type: () => String }) projectID: string,
): Promise<ProjectOverview> {
return this.coverageService.getProjectOverview(projectID);
return this.projectOverviewService.invoke(projectID, user);
}

@Query(() => [CoverageSummary], {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Log {
@Field(() => String, {
description: '上报ID',
})
reportId: string;
reportID: string;
@Field(() => Date, {
description: '创建时间',
})
Expand Down
Empty file.
Empty file.
86 changes: 0 additions & 86 deletions packages/canyon-backend/src/coverage/services/coverage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,91 +47,6 @@ export class CoverageService {
return projects;
}

async getProjectOverview(projectID): Promise<ProjectOverview> {
console.log('111');
const covTypeAggcoverages = await this.prisma.coverage.findMany({
where: {
projectID: projectID,
covType: 'agg',
},
orderBy: {
createdAt: 'desc',
},
});

const covTypeAllCoverages = await this.prisma.coverage.findMany({
where: {
projectID: projectID,
covType: 'all',
},
orderBy: {
createdAt: 'desc',
},
});

const aggregatedReports: any = covTypeAggcoverages.reduce((acc, report) => {
const commitSha = report.commitSha;
const commit = { web_url: '', message: '' };
if (!acc[commitSha]) {
acc[commitSha] = {
statistics: {
statements: [],
functions: [],
branches: [],
lines: [],
},
commitSha,
webUrl: commit?.web_url || '???',
message: commit?.message || '???',
lastReportTime: new Date(),
times: 1,
logs: [],
};
} else {
acc[commitSha].logs.push([]);
acc[commitSha].times += 1;
}

return acc;
}, {});
console.log(Object.values(aggregatedReports));
return {
compartmentData: [
{
label: 'totalTimes',
value: String(covTypeAggcoverages.length),
},
{
label: 'averageCoverage',
value: (100).toFixed(0) + '%',
},
{
label: 'lastReportTime',
value: dayjs(new Date()).format('MM-DD HH:mm'),
},
{
label: 'lastCommitCoverage',
value: '100',
},
],
chartData: [
{
commitSha: '123',
newLines: 12,
newFiles: 12,
allFiles: 12,
},
{
commitSha: '123',
newLines: 12,
newFiles: 12,
allFiles: 12,
},
],
records: Object.values(aggregatedReports),
};
}

async getCoverageSummaryMap(
commitSha: string,
reportID: string,
Expand Down Expand Up @@ -182,7 +97,6 @@ export class CoverageService {

// 私有方法
private async getCoverageDataFromAdapter(commitSha, reportID) {
console.log(commitSha, reportID)
const { relationID } = await this.prisma.coverage.findFirst({
where: {
commitSha: commitSha,
Expand Down
Empty file.
Loading

0 comments on commit e4e6b0d

Please sign in to comment.