Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Sep 11, 2023
1 parent 3b9c4d4 commit a275756
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 129 deletions.
3 changes: 3 additions & 0 deletions packages/app-backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@ type Query {
"""topNodes"""
topNodes: [NodeStatus!]!

"""db 同步"""
db: [String!]!

"""获取所有Coverage列表"""
listCOVs: [Coverage!]!

Expand Down
7 changes: 7 additions & 0 deletions packages/app-backend/src/coverage/coverage.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export class CoverageResolver {
private readonly pubsub: PubSubService,
) {}

@Query(() => [String], {
description: 'db 同步',
})
db(): Promise<string[]> {
return this.teamEnvironmentsService.db();
}

@Query(() => [Coverage], {
description: '获取所有Coverage列表',
})
Expand Down
59 changes: 57 additions & 2 deletions packages/app-backend/src/coverage/coverage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,66 @@ import { CoverageDataDocument } from './schema/coverage-data.schema';
import mongoose, { Model } from 'mongoose';
import { decompressedData } from '../utils/zstd';
import CanyonUtil from 'canyon-util';
import { CoverageDocument } from './schema/coverage.schema';
const userData = [];
const repoMap = [];
@Injectable()
export class CoverageService {
constructor(
private readonly prisma: PrismaService,
private readonly pubsub: PubSubService,
@Inject('MONGODB_CONNECTION_CoverageRepository')
private coverageModel: Model<CoverageDocument>,
@Inject('MONGODB_CONNECTION_CoverageDataRepository')
private coverageDataModel: Model<CoverageDataDocument>,
) {}

async db(): Promise<string[]> {
const allCovs0 = await this.coverageModel
.find({ covType: 'all' })
.then((r) => r.map((i) => String(i._id)));

const allCovs1 = await this.coverageModel
.find({ covType: 'agg' })
.then((r) => r.map((i) => String(i._id)));

const allCovs = [...allCovs0, ...allCovs1];

for (let i = 0; i < allCovs.length; i++) {
const s = await this.coverageModel.findOne({ _id: allCovs[i] });

if (s.reportId && repoMap.find((i) => i._id.$oid === s.repoId)) {
const cUser = await this.prisma.user.findFirst({
where: {
username: userData.find((i) => i._id.$oid === s.reporter).username,
},
});

await this.prisma.coverage
.create({
data: {
key: s.key || '',
commitSha: s.commitSha,
repoId: repoMap.find((i) => i._id.$oid === s.repoId).thRepoId,
instrumentCwd: s.instrumentCwd || '',
reporter: cUser.id + '',
reportId: s.reportId,
covType: s.covType,
covAggStatus: s.covAggStatus,
relationId: s.relationId,
createdAt: s.createdAt,
statistics: s.statistics as any,
},
})
.then((r) => {
console.log(r.commitSha);
});
}
}

// console.log(s);
return [''];
}
async findAllImages(): Promise<Coverage[]> {
// @ts-ignore
return this.prisma.coverage.findMany({});
Expand Down Expand Up @@ -44,9 +96,12 @@ export class CoverageService {
const c = await this.prisma.commit.findMany({
where: { projectId: Number(repoId) },
});
// console.log(c)
const aggregatedReports: any = reports.reduce((acc, report) => {
const commitSha = report.commitSha;
const cObj = c.find((c) => c.id === commitSha);
const cObj = c.find((c) => {
return c.id === commitSha;
}) || { message: '' };
if (!acc[commitSha]) {
acc[commitSha] = {
commitSha,
Expand Down Expand Up @@ -181,7 +236,7 @@ export class CoverageService {
const covObject = await this.getCoverageDataFromDB(
repoID,
commitsha,
reportID,
'all',
);
// 由于需要在后端根据路径获取指定文件的覆盖率,所以需要解压缩
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Connection as MongoConnection } from 'mongoose';
import { CoverageDataSchema } from '../schema/coverage-data.schema';

import { CoverageSchema } from '../schema/coverage.schema';

const coverageDataSchemaName = 'canyon_v5_coverage_data';
const coverageSchemaName = 'canyon_v5_coverage';
export const coverageProviders = [
{
provide: 'MONGODB_CONNECTION_CoverageDataRepository',
Expand All @@ -14,4 +15,10 @@ export const coverageProviders = [
),
inject: ['MONGODB_CONNECTION'],
},
{
provide: 'MONGODB_CONNECTION_CoverageRepository',
useFactory: (connection: MongoConnection) =>
connection.model('coverage_model', CoverageSchema, coverageSchemaName),
inject: ['MONGODB_CONNECTION'],
},
];
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Prop, Schema, SchemaFactory} from '@nestjs/mongoose';
import {Document} from 'mongoose';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type CoverageDataDocument = CoverageData & Document;

Expand Down
43 changes: 43 additions & 0 deletions packages/app-backend/src/coverage/schema/coverage.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type CoverageDocument = Coverage & Document;

class Statistics {
statements: { total: null; covered: null; skipped: null };
functions: { total: null; covered: null; skipped: null };
lines: { total: null; covered: null; skipped: null };
branches: { total: null; covered: null; skipped: null };
}

@Schema()
export class Coverage {
@Prop()
key: string;
// zstd压缩过的数据
@Prop()
commitSha: string;
@Prop()
repoId: string;
@Prop()
instrumentCwd: string;
@Prop()
reporter: string;
@Prop()
reportId: string;
@Prop()
covType: string;
// covType normal、agg(以reportId)、all(以commit为维度)
@Prop()
covAggStatus: string;
@Prop()
statistics: Statistics;
@Prop()
relationId: string;
@Prop({
default: () => new Date(),
})
createdAt: Date;
}

export const CoverageSchema = SchemaFactory.createForClass(Coverage);
Loading

0 comments on commit a275756

Please sign in to comment.