mirror of
https://github.com/certd/certd.git
synced 2026-04-24 04:17:25 +08:00
perf: 优化首页统计数据,饼图替换成证书数量统计
This commit is contained in:
@@ -4,6 +4,7 @@ import { UserService } from '../../../modules/sys/authority/service/user-service
|
||||
import { RoleService } from '../../../modules/sys/authority/service/role-service.js';
|
||||
import { PipelineService } from '../../../modules/pipeline/service/pipeline-service.js';
|
||||
import { HistoryService } from '../../../modules/pipeline/service/history-service.js';
|
||||
import { CertInfoService } from '../../../modules/monitor/index.js';
|
||||
|
||||
export type ChartItem = {
|
||||
name: string;
|
||||
@@ -12,7 +13,17 @@ export type ChartItem = {
|
||||
export type UserStatisticCount = {
|
||||
pipelineCount?: number;
|
||||
pipelineStatusCount?: ChartItem[];
|
||||
pipelineEnableCount?: {
|
||||
enabled: number;
|
||||
disabled: number;
|
||||
};
|
||||
historyCountPerDay: ChartItem[];
|
||||
certCount?: {
|
||||
total: number;
|
||||
expired: number;
|
||||
expiring: number;
|
||||
notExpired: number;
|
||||
};
|
||||
expiringList: any[];
|
||||
};
|
||||
/**
|
||||
@@ -30,15 +41,25 @@ export class StatisticController extends BaseController {
|
||||
@Inject()
|
||||
historyService: HistoryService;
|
||||
|
||||
@Inject()
|
||||
certInfoService: CertInfoService;
|
||||
|
||||
@Post('/count', { summary: Constants.per.authOnly })
|
||||
public async count() {
|
||||
const pipelineCount = await this.pipelineService.count({ userId: this.getUserId() });
|
||||
const pipelineStatusCount = await this.pipelineService.statusCount({ userId: this.getUserId() });
|
||||
const pipelineEnableCount = await this.pipelineService.enableCount({ userId: this.getUserId() });
|
||||
|
||||
const historyCount = await this.historyService.countPerDay({ userId: this.getUserId(), days: 7 });
|
||||
const expiringList = await this.pipelineService.latestExpiringList({ userId: this.getUserId(), count: 5 });
|
||||
|
||||
const certCount = await this.certInfoService.count({ userId: this.getUserId() });
|
||||
|
||||
const count: UserStatisticCount = {
|
||||
pipelineCount,
|
||||
pipelineStatusCount,
|
||||
pipelineEnableCount,
|
||||
certCount,
|
||||
historyCountPerDay: historyCount,
|
||||
expiringList,
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Provide, Scope, ScopeEnum } from "@midwayjs/core";
|
||||
import { BaseService, CodeException, Constants, PageReq } from "@certd/lib-server";
|
||||
import { InjectEntityModel } from "@midwayjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import { Between, IsNull, LessThan, Not, Repository } from "typeorm";
|
||||
import { CertInfoEntity } from "../entity/cert-info.js";
|
||||
import { utils } from "@certd/basic";
|
||||
import { CertInfo, CertReader } from "@certd/plugin-cert";
|
||||
@@ -181,6 +181,36 @@ export class CertInfoService extends BaseService<CertInfoEntity> {
|
||||
pipelineId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async count({ userId }: { userId: number }) {
|
||||
const total = await this.repository.count({
|
||||
where: {
|
||||
userId,
|
||||
expiresTime: Not(IsNull()),
|
||||
},
|
||||
});
|
||||
|
||||
const expired = await this.repository.count({
|
||||
where: {
|
||||
userId,
|
||||
expiresTime: LessThan(new Date().getTime()),
|
||||
},
|
||||
});
|
||||
|
||||
const expiring = await this.repository.count({
|
||||
where: {
|
||||
userId,
|
||||
expiresTime: Between(new Date().getTime(), new Date().getTime() + 15 * 24 * 60 * 60 * 1000),
|
||||
},
|
||||
});
|
||||
|
||||
const notExpired = total - expired - expiring;
|
||||
return {
|
||||
total,
|
||||
expired,
|
||||
expiring,
|
||||
notExpired,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -771,6 +771,26 @@ export class PipelineService extends BaseService<PipelineEntity> {
|
||||
return statusCount;
|
||||
}
|
||||
|
||||
async enableCount(param: { userId?: any } = {}) {
|
||||
const statusCount = await this.repository
|
||||
.createQueryBuilder()
|
||||
.select("disabled")
|
||||
.addSelect("count(1)", "count")
|
||||
.where({
|
||||
userId: param.userId
|
||||
})
|
||||
.groupBy("disabled")
|
||||
.getRawMany();
|
||||
const result = {
|
||||
enabled: 0,
|
||||
disabled: 0,
|
||||
};
|
||||
for (const item of statusCount) {
|
||||
result[item.disabled ? "disabled" : "enabled"] = parseInt(item.count);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async latestExpiringList({ userId }: any) {
|
||||
let list = await this.repository.find({
|
||||
select: {
|
||||
|
||||
Reference in New Issue
Block a user