Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangtao25 committed Apr 12, 2024
1 parent ef68dab commit c2d3150
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 93 deletions.
2 changes: 1 addition & 1 deletion packages/canyon-backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ model Coverage {
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(3)
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(3)
@@map("coverage_0407")
@@map("coverage")
}

model CoverageData {
Expand Down
3 changes: 3 additions & 0 deletions packages/canyon-backend/src/coverage/coveragedisk.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export class CoveragediskEntity {
@Column()
projectID: string;

@Column()
reportID: string;

@Column()
sha: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export class CoverageData {

@Prop({ type: Object })
coverage: CoverageType;

@Prop()
v: string;

@Prop()
createdAt: Date;
}

export const CoverageDataSchema = SchemaFactory.createForClass(CoverageData);
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import {
mergeCoverageMap,
} from '@canyon/data';
import { removeNullKeys } from '../../utils/utils';
import { validateObject } from '../../utils/coverage';
// import { validateObject } from '../../utils/coverage';
import { CoverageDataAdapterService } from './coverage-data-adapter.service';
import { PullChangeCodeAndInsertDbService } from './pull-change-code-and-insert-db.service';
import { logger } from '../../logger';
import { CoveragediskService } from './coveragedisk.service';
// import tr from '@canyon/report/src/Report/components/Tr';

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

Expand Down Expand Up @@ -38,14 +39,25 @@ export class ConsumerCoverageService {

if (lockAcquired) {
try {
const time1 = new Date().valueOf();
await this.consume(queueDataToBeConsumed, 'agg');
console.log(
queueDataToBeConsumed.reportID + '-agg',
new Date().valueOf() - time1,
);
const time2 = new Date().valueOf();
await this.consume(queueDataToBeConsumed, 'all');
console.log(
queueDataToBeConsumed.reportID + '-all',
new Date().valueOf() - time2,
);
// 执行任务
} finally {
await this.releaseLock(lockName);
}
} else {
await sleep(3000);
await this.coveragediskService.pushQueue(queueDataToBeConsumed);
await sleep(1000);
// 锁已被其他实例持有,无法获取锁
}
} else {
Expand All @@ -68,8 +80,11 @@ export class ConsumerCoverageService {
}),
});
// 拉取变更代码
const time3 = new Date().valueOf();
await this.pullChangeCode(queueDataToBeConsumed);
console.log(`拉取变更代码耗时:${new Date().valueOf() - time3}`);
// 判断是否需要拉取变更代码,对比sha和compareTarget
const time4 = new Date().valueOf();
const codechanges =
queueDataToBeConsumed.sha === queueDataToBeConsumed.compareTarget
? []
Expand All @@ -79,14 +94,18 @@ export class ConsumerCoverageService {
compareTarget: queueDataToBeConsumed.compareTarget,
},
});
console.log(`查询变更代码耗时:${new Date().valueOf() - time4}`);
if (coverage) {
const time5 = new Date().valueOf();
const cov = await this.coverageDataAdapterService.retrieve(
coverage.relationID,
);
const newcoverage = mergeCoverageMap(
queueDataToBeConsumed.coverage,
validateObject(cov),
);
console.log(`查询覆盖率文件耗时:${new Date().valueOf() - time5}`);

const time6 = new Date().valueOf();
const newcoverage = mergeCoverageMap(queueDataToBeConsumed.coverage, cov);
console.log(`合并覆盖率文件耗时:${new Date().valueOf() - time6}`);

await this.prisma.coverage.update({
where: {
id: coverage.id,
Expand All @@ -100,10 +119,13 @@ export class ConsumerCoverageService {
compareTarget: queueDataToBeConsumed.compareTarget,
},
}); // 更新时间
return this.coverageDataAdapterService.update(
const time7 = new Date().valueOf();
const r = await this.coverageDataAdapterService.update(
coverage.relationID,
newcoverage,
);
console.log(`更新覆盖率文件耗时:${new Date().valueOf() - time7}`);
return r;
} else {
// 创建新的agg
const newAgg = await this.prisma.coverage.create({
Expand Down Expand Up @@ -168,66 +190,55 @@ export class ConsumerCoverageService {
async acquireLock(lockName: string, lockTimeout: number): Promise<boolean> {
const now = new Date();
const expirationTime = new Date(now.getTime() + lockTimeout);

try {
await this.prisma.$transaction(async (prisma) => {
// 查询锁
const existingLock = await prisma.distributedlock.findUnique({
where: {
lockName,
},
});
// 查询锁
const existingLock = await this.prisma.distributedlock.findUnique({
where: {
lockName,
},
});

if (existingLock) {
// 锁已存在,检查是否已过期或者已被释放
if (!existingLock.isLocked || existingLock.lockExpiration < now) {
// 锁未被持有或者已过期,尝试更新锁
await prisma.distributedlock.update({
where: {
lockName,
},
data: {
isLocked: true,
lockTimestamp: now,
lockExpiration: expirationTime,
},
});
return true; // 锁获取成功
} else {
// 锁被其他实例持有且未过期
return false; // 锁获取失败
}
} else {
// 锁不存在,创建新锁
await prisma.distributedlock.create({
data: {
if (existingLock) {
// 锁已存在,检查是否已过期或者已被释放
if (existingLock.lockExpiration < now) {
// 锁未被持有或者已过期,尝试更新锁
await this.prisma.distributedlock.update({
where: {
lockName,
isLocked: true,
},
data: {
lockTimestamp: now,
lockExpiration: expirationTime,
},
});
return true; // 锁获取成功
} else {
// 锁被其他实例持有且未过期
return false; // 锁获取失败
}
});

return true; // 事务成功执行,锁获取成功
} else {
// 锁不存在,创建新锁
await this.prisma.distributedlock.create({
data: {
lockName,
isLocked: true,
lockTimestamp: now,
lockExpiration: expirationTime,
},
});
return true; // 锁获取成功
}
} catch (error) {
console.error('Error acquiring lock:', error);
return false; // 锁获取失败
}
}

async releaseLock(lockName: string): Promise<void> {
await this.prisma.distributedlock.update({
await this.prisma.distributedlock.delete({
where: {
lockName,
},
data: {
isLocked: false,
lockTimestamp: null,
lockExpiration: null,
},
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { InjectModel } from '@nestjs/mongoose';
import { CoverageData } from '../schemas/coverage-data.schema';
import { Model } from 'mongoose';
import * as mongoose from 'mongoose';
import { splitJSONIntoQuarters, validateObject } from '../../utils/coverage';
// import { splitJSONIntoQuarters, validateObject } from '../../utils/coverage';
import { compressedData, decompressedData } from '../../utils/zstd';
@Injectable()
export class CoverageDataAdapterService {
constructor(
Expand All @@ -14,53 +15,33 @@ export class CoverageDataAdapterService {
) {}
// crud
// 1/4 格式化,防止空key
create(coverage, coverageID) {
// 创建新的coverage
const coverage8 = splitJSONIntoQuarters(coverage);
return Promise.all(
coverage8.map((item) =>
this.coverageDataModel.create({
coverage: item,
coverageID: coverageID,
}),
),
).then((res) => {
return res.reduce((acc, cur, index) => {
return {
...acc,
[index]: String(cur._id),
};
}, {});
});
async create(coverage, coverageID) {
return this.coverageDataModel
.create({
coverage: await compressedData(JSON.stringify(coverage)),
coverageID,
v: '20240412',
createdAt: new Date(),
})
.then((r) => String(r._id));
}
update(relationID, coverage) {
// 更新coverage
const coverage8 = splitJSONIntoQuarters(coverage);
return Promise.all(
Object.values(relationID).map((id: string, index) =>
this.coverageDataModel.findByIdAndUpdate(
new mongoose.Types.ObjectId(id),
{
coverage: coverage8[index],
},
),
),
async update(relationID, coverage) {
return this.coverageDataModel.updateOne(
{
_id: new mongoose.Types.ObjectId(relationID),
},
{
coverage: await compressedData(JSON.stringify(coverage)),
createdAt: new Date(),
},
);
}
retrieve(relationID) {
return Promise.all(
Object.values(relationID).map((id: string) =>
this.coverageDataModel.findById(new mongoose.Types.ObjectId(id)),
),
)
.then((res) => {
return res.reduce((acc, cur) => {
return {
...acc,
...(cur.coverage || {}),
};
}, {});
return this.coverageDataModel
.findOne({
_id: new mongoose.Types.ObjectId(relationID),
})
.then((res) => validateObject(res));
.then((r) => decompressedData(r.coverage))
.then((r) => JSON.parse(r));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class CoveragediskService {
pid: String(process.pid),
projectID: data.projectID,
sha: data.sha,
reportID: data.reportID,
data: JSON.stringify(data),
createdAt: new Date(),
});
Expand All @@ -32,6 +33,7 @@ export class CoveragediskService {
projectID: true,
sha: true,
data: true,
reportID: true,
},
});
if (!old) {
Expand All @@ -42,6 +44,7 @@ export class CoveragediskService {
projectID: old.projectID,
sha: old.sha,
pid: String(process.pid),
reportID: old.reportID,
},
select: {
id: true,
Expand Down

0 comments on commit c2d3150

Please sign in to comment.