perf: 管理控制台数据统计

This commit is contained in:
xiaojunnuo
2024-10-31 16:19:35 +08:00
parent 63ec5b5519
commit babd5897ae
14 changed files with 265 additions and 74 deletions

View File

@@ -1,4 +1,4 @@
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
import { Controller, Inject, Post, Provide } from '@midwayjs/core';
import { BaseController, Constants } from '@certd/lib-server';
import { UserService } from '../../modules/sys/authority/service/user-service.js';
import { RoleService } from '../../modules/sys/authority/service/role-service.js';
@@ -12,7 +12,7 @@ export type ChartItem = {
export type UserStatisticCount = {
pipelineCount?: number;
pipelineStatusCount?: ChartItem[];
runningCount: ChartItem[];
historyCountPerDay: ChartItem[];
expiringList: any[];
};
/**
@@ -33,36 +33,15 @@ export class StatisticController extends BaseController {
@Post('/count', { summary: Constants.per.authOnly })
public async count() {
const pipelineCount = await this.pipelineService.count({ userId: this.getUserId() });
let pipelineStatusCount = await this.pipelineService.statusCount({ userId: this.getUserId() });
pipelineStatusCount = pipelineStatusCount.map(item => {
return {
name: item.status,
value: item.count,
};
});
const historyCount = await this.historyService.dayCount({ userId: this.getUserId(), days: 7 });
const runningCount = historyCount.map(item => {
return {
name: item.date,
value: item.count,
};
});
const pipelineStatusCount = await this.pipelineService.statusCount({ 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 count: UserStatisticCount = {
pipelineCount,
pipelineStatusCount,
runningCount,
historyCountPerDay: historyCount,
expiringList,
};
return this.ok(count);
}
@Post('/changePassword', { summary: Constants.per.authOnly })
public async changePassword(@Body(ALL) body: any) {
const userId = this.getUserId();
await this.userService.changePassword(userId, body);
return this.ok({});
}
}

View File

@@ -0,0 +1,51 @@
import { Controller, Inject, Post, Provide } from '@midwayjs/core';
import { BaseController } from '@certd/lib-server';
import { UserService } from '../../../modules/sys/authority/service/user-service.js';
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';
export type ChartItem = {
name: string;
value: number;
};
export type SysStatisticCount = {
userCount: number;
pipelineCount?: number;
historyCountPerDay: ChartItem[];
userRegisterCountPerDay: ChartItem[];
pipelineCreateCountPerDay: ChartItem[];
};
/**
*/
@Provide()
@Controller('/api/sys/statistic/')
export class SysStatisticController extends BaseController {
@Inject()
userService: UserService;
@Inject()
roleService: RoleService;
@Inject()
pipelineService: PipelineService;
@Inject()
historyService: HistoryService;
@Post('/count', { summary: 'sys:settings:view' })
public async count() {
const userCount = await this.userService.count();
const userRegisterCountPerDay = await this.userService.registerCountPerDay({ days: 7 });
const pipelineCreateCountPerDay = await this.pipelineService.createCountPerDay({ days: 7 });
const pipelineCount = await this.pipelineService.count({});
const historyCountPerDay = await this.historyService.countPerDay({ days: 7 });
const count: SysStatisticCount = {
userCount,
userRegisterCountPerDay,
pipelineCount,
pipelineCreateCountPerDay,
historyCountPerDay,
};
return this.ok(count);
}
}

View File

@@ -175,17 +175,21 @@ export class HistoryService extends BaseService<HistoryEntity> {
}
}
async dayCount(param: { days: number; userId: any }) {
async countPerDay(param: { days: number; userId?: any }) {
const todayEnd = dayjs().endOf('day');
const where: any = {
// 0点
// userId: param.userId,
createTime: MoreThan(todayEnd.add(-param.days, 'day').toDate()),
};
if (param.userId > 0) {
where.userId = param.userId;
}
const result = await this.getRepository()
.createQueryBuilder('main')
.select('date(main.createTime) AS date') // 将UNIX时间戳转换为日期
.addSelect('COUNT(*) AS count')
.where({
// 0点
userId: param.userId,
createTime: MoreThan(todayEnd.add(-param.days, 'day').toDate()),
})
.where(where)
.groupBy('date')
.getRawMany();

View File

@@ -1,6 +1,6 @@
import { Config, Inject, Provide, Scope, ScopeEnum, sleep } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { In, Repository } from 'typeorm';
import { In, MoreThan, Repository } from 'typeorm';
import { BaseService, NeedVIPException, PageReq, SysPublicSettings, SysSettingsService } from '@certd/lib-server';
import { PipelineEntity } from '../entity/pipeline.js';
import { PipelineDetail } from '../entity/vo/pipeline-detail.js';
@@ -19,6 +19,7 @@ import { AccessGetter } from './access-getter.js';
import { CnameRecordService } from '../../cname/service/cname-record-service.js';
import { CnameProxyService } from './cname-proxy-service.js';
import { PluginConfigGetter } from '../../plugin/service/plugin-config-getter.js';
import dayjs from 'dayjs';
const runningTasks: Map<string | number, Executor> = new Map();
const freeCount = 10;
@@ -473,7 +474,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
await this.historyLogService.addOrUpdate(logEntity);
}
async count(param: { userId: any }) {
async count(param: { userId?: any }) {
const count = await this.repository.count({
where: {
userId: param.userId,
@@ -482,7 +483,7 @@ export class PipelineService extends BaseService<PipelineEntity> {
return count;
}
async statusCount(param: { userId: any }) {
async statusCount(param: { userId?: any } = {}) {
const statusCount = await this.repository
.createQueryBuilder()
.select('status')
@@ -516,4 +517,20 @@ export class PipelineService extends BaseService<PipelineEntity> {
return list.slice(0, 5);
}
async createCountPerDay(param: { days: number } = { days: 7 }) {
const todayEnd = dayjs().endOf('day');
const result = await this.getRepository()
.createQueryBuilder('main')
.select('date(main.createTime) AS date') // 将UNIX时间戳转换为日期
.addSelect('COUNT(*) AS count')
.where({
// 0点
createTime: MoreThan(todayEnd.add(-param.days, 'day').toDate()),
})
.groupBy('date')
.getRawMany();
return result;
}
}

View File

@@ -1,6 +1,6 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { MoreThan, Repository } from 'typeorm';
import { UserEntity } from '../entity/user.js';
import * as _ from 'lodash-es';
import md5 from 'md5';
@@ -15,6 +15,7 @@ import bcrypt from 'bcryptjs';
import { SysSettingsService } from '@certd/lib-server';
import { SysInstallInfo } from '@certd/lib-server';
import { RandomUtil } from '../../../../utils/random.js';
import dayjs from 'dayjs';
/**
* 系统用户
@@ -245,4 +246,29 @@ export class UserService extends BaseService<UserEntity> {
status,
});
}
async count(param: { userId?: any } = {}) {
const count = await this.repository.count({
where: {
id: param.userId,
},
});
return count;
}
async registerCountPerDay(param: { days: number } = { days: 7 }) {
const todayEnd = dayjs().endOf('day');
const result = await this.getRepository()
.createQueryBuilder('main')
.select('date(main.createTime) AS date') // 将UNIX时间戳转换为日期
.addSelect('COUNT(*) AS count')
.where({
// 0点
createTime: MoreThan(todayEnd.add(-param.days, 'day').toDate()),
})
.groupBy('date')
.getRawMany();
return result;
}
}