From a2757568afdd5ce84d7d7a388181a89fae4e6fc0 Mon Sep 17 00:00:00 2001 From: Allen Zhang <37892968+zhangtao25@users.noreply.github.com> Date: Mon, 11 Sep 2023 11:16:07 +0800 Subject: [PATCH] feat: update --- packages/app-backend/schema.gql | 3 + .../src/coverage/coverage.resolver.ts | 7 + .../src/coverage/coverage.service.ts | 59 ++++- .../coverage/providers/coverage.providers.ts | 9 +- .../coverage/schema/coverage-data.schema.ts | 4 +- .../src/coverage/schema/coverage.schema.ts | 43 ++++ .../app-backend/src/tasks/tasks.service.ts | 204 ++++++++++-------- packages/app-ui/src/pages/coverage/index.tsx | 40 +--- 8 files changed, 240 insertions(+), 129 deletions(-) create mode 100644 packages/app-backend/src/coverage/schema/coverage.schema.ts diff --git a/packages/app-backend/schema.gql b/packages/app-backend/schema.gql index 2e285bed..a8f48d85 100644 --- a/packages/app-backend/schema.gql +++ b/packages/app-backend/schema.gql @@ -395,6 +395,9 @@ type Query { """topNodes""" topNodes: [NodeStatus!]! + """db 同步""" + db: [String!]! + """获取所有Coverage列表""" listCOVs: [Coverage!]! diff --git a/packages/app-backend/src/coverage/coverage.resolver.ts b/packages/app-backend/src/coverage/coverage.resolver.ts index 584e6df8..4d0d921b 100755 --- a/packages/app-backend/src/coverage/coverage.resolver.ts +++ b/packages/app-backend/src/coverage/coverage.resolver.ts @@ -18,6 +18,13 @@ export class CoverageResolver { private readonly pubsub: PubSubService, ) {} + @Query(() => [String], { + description: 'db 同步', + }) + db(): Promise { + return this.teamEnvironmentsService.db(); + } + @Query(() => [Coverage], { description: '获取所有Coverage列表', }) diff --git a/packages/app-backend/src/coverage/coverage.service.ts b/packages/app-backend/src/coverage/coverage.service.ts index 4e8d63d8..316a1279 100755 --- a/packages/app-backend/src/coverage/coverage.service.ts +++ b/packages/app-backend/src/coverage/coverage.service.ts @@ -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, @Inject('MONGODB_CONNECTION_CoverageDataRepository') private coverageDataModel: Model, ) {} + + async db(): Promise { + 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 { // @ts-ignore return this.prisma.coverage.findMany({}); @@ -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, @@ -181,7 +236,7 @@ export class CoverageService { const covObject = await this.getCoverageDataFromDB( repoID, commitsha, - reportID, + 'all', ); // 由于需要在后端根据路径获取指定文件的覆盖率,所以需要解压缩 try { diff --git a/packages/app-backend/src/coverage/providers/coverage.providers.ts b/packages/app-backend/src/coverage/providers/coverage.providers.ts index 29f7f9ec..d4bd57da 100644 --- a/packages/app-backend/src/coverage/providers/coverage.providers.ts +++ b/packages/app-backend/src/coverage/providers/coverage.providers.ts @@ -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', @@ -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'], + }, ]; diff --git a/packages/app-backend/src/coverage/schema/coverage-data.schema.ts b/packages/app-backend/src/coverage/schema/coverage-data.schema.ts index f387ec59..c1f609f7 100644 --- a/packages/app-backend/src/coverage/schema/coverage-data.schema.ts +++ b/packages/app-backend/src/coverage/schema/coverage-data.schema.ts @@ -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; diff --git a/packages/app-backend/src/coverage/schema/coverage.schema.ts b/packages/app-backend/src/coverage/schema/coverage.schema.ts new file mode 100644 index 00000000..050b8c27 --- /dev/null +++ b/packages/app-backend/src/coverage/schema/coverage.schema.ts @@ -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); diff --git a/packages/app-backend/src/tasks/tasks.service.ts b/packages/app-backend/src/tasks/tasks.service.ts index 34fec60e..5ec557f0 100755 --- a/packages/app-backend/src/tasks/tasks.service.ts +++ b/packages/app-backend/src/tasks/tasks.service.ts @@ -4,6 +4,7 @@ import axios from 'axios'; import { GitCommit, Project } from '../t'; import { PrismaService } from '../prisma/prisma.service'; import { PubSubService } from '../pubsub/pubsub.service'; +import * as process from 'process'; @Injectable() export class TasksService { @@ -18,110 +19,125 @@ export class TasksService { this.logger.debug('Called when the second is 45'); } - @Cron(`0 8 * * *`) + // @Cron(`0 8 * * *`) // @Timeout(500) async handleTimeout() { this.logger.debug('Called once after 5 seconds'); - const gitlabBaseUrl = 'http://gitlab.com/'; // 或者你的GitLab实例的URL - const repos = []; - for (let i = 0; i < repos.length; i++) { - const repoData = await axios({ - method: 'get', - url: `${gitlabBaseUrl}/api/v4/projects/${repos[i]}`, - headers: { - Authorization: `Bearer 6bb2887ea5bc21df691ab73f877b056593a798471f1a161e9b2682ed885dc84f`, - }, - }).then((res) => { - return res.data; - }); - - this.prisma.repo - .findFirst({ where: { id: repos[i] } }) - .then(async (r) => { - if (r) { - await this.prisma.repo.update({ - where: { id: repos[i] }, - data: { - name: repoData.name, - pathWithNamespace: repoData.path_with_namespace, - description: repoData.description, - }, - }); - } else { - await this.prisma.repo.create({ - data: { - id: repoData.id, - name: repoData.name, - pathWithNamespace: repoData.path_with_namespace, - description: repoData.description, - reportTimes: 0, - lastTimeReport: new Date(), - }, - }); - } - }); - } + const gitlabBaseUrl = process.env.GITLAB_URL; // 或者你的GitLab实例的URL + // const repos = []; + // for (let i = 0; i < repos.length; i++) { + // const repoData = await axios({ + // method: 'get', + // url: `${gitlabBaseUrl}/api/v4/projects/${repos[i]}`, + // headers: { + // Authorization: `Bearer 6bb2887ea5bc21df691ab73f877b056593a798471f1a161e9b2682ed885dc84f`, + // }, + // }).then((res) => { + // return res.data; + // }); + // + // this.prisma.repo + // .findFirst({ where: { id: repos[i] } }) + // .then(async (r) => { + // if (r) { + // await this.prisma.repo.update({ + // where: { id: repos[i] }, + // data: { + // name: repoData.name, + // pathWithNamespace: repoData.path_with_namespace, + // description: repoData.description, + // }, + // }); + // } else { + // await this.prisma.repo.create({ + // data: { + // id: repoData.id, + // name: repoData.name, + // pathWithNamespace: repoData.path_with_namespace, + // description: repoData.description, + // reportTimes: 0, + // lastTimeReport: new Date(), + // }, + // }); + // } + // }); + // } // 98795 - const commits = []; - for (let i = 0; i < commits.length; i++) { - const commitData = await axios({ - method: 'get', - url: `${gitlabBaseUrl}/api/v4/projects/98795/repository/commits/${commits[i]}`, - headers: { - Authorization: `Bearer 6bb2887ea5bc21df691ab73f877b056593a798471f1a161e9b2682ed885dc84f`, - }, - }).then((res) => { - return res.data; - }); + const alls = await this.prisma.coverage.findMany({ + where: { + covType: 'all', + }, + }); + // const repoID = ''; + // const commits = []; - this.prisma.commit - .findFirst({ where: { id: commits[i] } }) - .then(async (r) => { - if (r) { - await this.prisma.commit.update({ - where: { id: commits[i] }, - data: { - title: commitData.title, - message: commitData.message, - authorName: commitData.author_name, - authorEmail: commitData.author_email, - authoredDate: commitData.authored_date, - committerName: commitData.committer_name, - committerEmail: commitData.committer_email, - committedDate: commitData.committed_date, - webUrl: commitData.web_url, - createdAt: commitData.created_at, - projectId: commitData.project_id, - }, - }); - } else { - await this.prisma.commit.create({ - data: { - id: commitData.id, - title: commitData.title, - message: commitData.message, - authorName: commitData.author_name, - authorEmail: commitData.author_email, - authoredDate: commitData.authored_date, - committerName: commitData.committer_name, - committerEmail: commitData.committer_email, - committedDate: commitData.committed_date, - webUrl: commitData.web_url, - createdAt: commitData.created_at, - projectId: commitData.project_id, - // name: commitData.name, - // pathWithNamespace: commitData.path_with_namespace, - // description: commitData.description, - // reportTimes: 0, - // lastTimeReport: new Date(), - }, - }); - } + for (let i = 0; i < alls.length; i++) { + try { + const repoID = alls[i].repoId; + const commit = alls[i].commitSha; + const commitData = await axios({ + method: 'get', + url: `${gitlabBaseUrl}/api/v4/projects/${repoID}/repository/commits/${commit}`, + headers: { + Authorization: `Bearer 6bb2887ea5bc21df691ab73f877b056593a798471f1a161e9b2682ed885dc84f`, + }, + }).then((res) => { + return res.data; }); + + console.log(repoID, commit); + + this.prisma.commit + .findFirst({ where: { id: commit } }) + .then(async (r) => { + if (r) { + await this.prisma.commit.update({ + where: { id: commit }, + data: { + title: commitData.title, + message: commitData.message, + authorName: commitData.author_name, + authorEmail: commitData.author_email, + authoredDate: commitData.authored_date, + committerName: commitData.committer_name, + committerEmail: commitData.committer_email, + committedDate: commitData.committed_date, + webUrl: commitData.web_url, + createdAt: commitData.created_at, + projectId: commitData.project_id, + }, + }); + } else { + await this.prisma.commit.create({ + data: { + id: commitData.id, + title: commitData.title, + message: commitData.message, + authorName: commitData.author_name, + authorEmail: commitData.author_email, + authoredDate: commitData.authored_date, + committerName: commitData.committer_name, + committerEmail: commitData.committer_email, + committedDate: commitData.committed_date, + webUrl: commitData.web_url, + createdAt: commitData.created_at, + projectId: commitData.project_id, + // name: commitData.name, + // pathWithNamespace: commitData.path_with_namespace, + // description: commitData.description, + // reportTimes: 0, + // lastTimeReport: new Date(), + }, + }); + } + }); + } catch (e) { + console.log(e); + } } } } diff --git a/packages/app-ui/src/pages/coverage/index.tsx b/packages/app-ui/src/pages/coverage/index.tsx index fc51d697..74ed7ce5 100644 --- a/packages/app-ui/src/pages/coverage/index.tsx +++ b/packages/app-ui/src/pages/coverage/index.tsx @@ -1,29 +1,13 @@ -import Icon, { CheckCircleOutlined, CloseCircleOutlined, PlusOutlined } from '@ant-design/icons'; import { useQuery } from '@apollo/client'; -import { css } from '@emotion/react'; -import { Button, Input, Segmented, Space, Table, Tag } from 'antd'; +import { Table } from 'antd'; import { ColumnsType } from 'antd/es/table'; -import { useState } from 'react'; +import { useTranslation } from 'react-i18next'; import { useNavigate } from 'react-router-dom'; -import Stopwatch from '~icons/bi/stopwatch'; -import Branch from '~icons/ion/git-branch-outline'; -import Commit from '~icons/radix-icons/commit'; -import UiwDate from '~icons/uiw/date'; - -// uiw/date/ -// import { -// GetApplicationsDocument, -// ListPipelinesDocument, -// } from '../../../helpers/backend/gen/graphql.ts'; - -import { useTranslation } from 'react-i18next' -import { GetCoverageReposDocument, ListCoVsDocument, Repo } from "../../helpers/backend/gen/graphql.ts"; -// import CoverageOverview from "./[id]/overview.tsx"; +import { GetCoverageReposDocument, Repo } from '@/helpers/backend/gen/graphql.ts'; const AppPipeline = () => { - const { t } = useTranslation() - const [value, setValue] = useState('Map'); - const { data: listPipelinesDocumentData,loading } = useQuery(GetCoverageReposDocument, {}); + const { t } = useTranslation(); + const { data: listPipelinesDocumentData, loading } = useQuery(GetCoverageReposDocument, {}); // 创建一个deployment const nav = useNavigate(); @@ -39,12 +23,12 @@ const AppPipeline = () => { return ( { - nav(`/coverage/${tableListItem.id}/overview`) + nav(`/coverage/${tableListItem.id}/overview`); }} > {_} - ) + ); }, }, { @@ -54,7 +38,6 @@ const AppPipeline = () => { { title: t('lastTimeReport'), dataIndex: 'lastTimeReport', - // valueType: 'dateTime', }, { title: t('operation'), @@ -62,21 +45,18 @@ const AppPipeline = () => { return ( { - nav(`/coverage/${tableListItem.id}/overview`) + nav(`/coverage/${tableListItem.id}/overview`); }} > {t('detail')} - ) + ); }, }, - ] + ]; return (
- record.id}