This commit is contained in:
xiaojunnuo
2024-10-31 22:35:05 +08:00
parent 584378a32b
commit b817cb4a1b
13 changed files with 125 additions and 41 deletions

View File

@@ -103,4 +103,10 @@ export class PipelineController extends CrudController<PipelineService> {
await this.service.cancel(historyId);
return this.ok({});
}
@Post('/count', { summary: Constants.per.authOnly })
async count() {
const count = await this.service.count({ userId: this.getUserId() });
return this.ok({ count });
}
}

View File

@@ -0,0 +1,3 @@
export interface SqlAdapter {
date(columnName: string): string;
}

View File

@@ -0,0 +1,34 @@
import { SqliteAdapter } from './sqlite.js';
import { PostgresqlAdapter } from './postgresql.js';
import { Config, Init, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { SqlAdapter } from './d.js';
@Provide()
@Scope(ScopeEnum.Singleton)
export class DbAdapter implements SqlAdapter {
adapter: SqlAdapter;
@Config('typeorm.dataSource.default.type')
dbType: string;
@Init()
async init() {
if (this.isSqlite()) {
this.adapter = new SqliteAdapter();
} else if (this.isPostgresql()) {
this.adapter = new PostgresqlAdapter();
} else {
throw new Error(`dbType ${this.dbType} not support`);
}
}
isSqlite() {
return this.dbType === 'better-sqlite3';
}
isPostgresql() {
return this.dbType === 'postgres';
}
date(columnName: string) {
return this.adapter.date(columnName);
}
}

View File

@@ -0,0 +1,7 @@
import { SqlAdapter } from './d.js';
export class PostgresqlAdapter implements SqlAdapter {
date(columnName: string) {
return `to_char(${columnName}, 'YYYY-MM-DD')`;
}
}

View File

@@ -0,0 +1,7 @@
import { SqlAdapter } from './d.js';
export class SqliteAdapter implements SqlAdapter {
date(columnName: string) {
return `date(${columnName}, 'localtime')`;
}
}

View File

@@ -6,10 +6,10 @@ import { HistoryEntity } from '../entity/history.js';
import { PipelineEntity } from '../entity/pipeline.js';
import { HistoryDetail } from '../entity/vo/history-detail.js';
import { HistoryLogService } from './history-log-service.js';
import { FileItem, Pipeline, RunnableCollection } from '@certd/pipeline';
import { FileStore } from '@certd/pipeline';
import { logger } from '@certd/pipeline';
import { FileItem, FileStore, logger, Pipeline, RunnableCollection } from '@certd/pipeline';
import dayjs from 'dayjs';
import { DbAdapter } from '../../db/index.js';
/**
* 证书申请
*/
@@ -24,6 +24,9 @@ export class HistoryService extends BaseService<HistoryEntity> {
@Inject()
logService: HistoryLogService;
@Inject()
dbAdapter: DbAdapter;
@Config('certd')
private certdConfig: any;
@@ -178,8 +181,6 @@ export class HistoryService extends BaseService<HistoryEntity> {
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) {
@@ -187,8 +188,8 @@ export class HistoryService extends BaseService<HistoryEntity> {
}
const result = await this.getRepository()
.createQueryBuilder('main')
.select('date(main.createTime) AS date') // 将UNIX时间戳转换为日期
.addSelect('COUNT(*) AS count')
.select(`${this.dbAdapter.date('main.createTime')} AS date`) // 将UNIX时间戳转换为日期
.addSelect('COUNT(1) AS count')
.where(where)
.groupBy('date')
.getRawMany();

View File

@@ -20,6 +20,7 @@ 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';
import { DbAdapter } from '../../db/index.js';
const runningTasks: Map<string | number, Executor> = new Map();
const freeCount = 10;
@@ -60,6 +61,9 @@ export class PipelineService extends BaseService<PipelineEntity> {
@Config('certd')
private certdConfig: any;
@Inject()
dbAdapter: DbAdapter;
//@ts-ignore
getRepository() {
return this.repository;
@@ -522,8 +526,8 @@ export class PipelineService extends BaseService<PipelineEntity> {
const todayEnd = dayjs().endOf('day');
const result = await this.getRepository()
.createQueryBuilder('main')
.select('date(main.createTime) AS date') // 将UNIX时间戳转换为日期
.addSelect('COUNT(*) AS count')
.select(`${this.dbAdapter.date('main.createTime')} AS date`) // 将UNIX时间戳转换为日期
.addSelect('COUNT(1) AS count')
.where({
// 0点
createTime: MoreThan(todayEnd.add(-param.days, 'day').toDate()),

View File

@@ -4,18 +4,15 @@ import { MoreThan, Repository } from 'typeorm';
import { UserEntity } from '../entity/user.js';
import * as _ from 'lodash-es';
import md5 from 'md5';
import { CommonException, FileService } from '@certd/lib-server';
import { BaseService } from '@certd/lib-server';
import { BaseService, CommonException, Constants, FileService, SysInstallInfo, SysSettingsService } from '@certd/lib-server';
import { RoleService } from './role-service.js';
import { PermissionService } from './permission-service.js';
import { UserRoleService } from './user-role-service.js';
import { Constants } from '@certd/lib-server';
import { UserRoleEntity } from '../entity/user-role.js';
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';
import { DbAdapter } from '../../../db/index.js';
/**
* 系统用户
@@ -37,6 +34,8 @@ export class UserService extends BaseService<UserEntity> {
@Inject()
fileService: FileService;
@Inject()
dbAdapter: DbAdapter;
//@ts-ignore
getRepository() {
@@ -260,8 +259,8 @@ export class UserService extends BaseService<UserEntity> {
const todayEnd = dayjs().endOf('day');
const result = await this.getRepository()
.createQueryBuilder('main')
.select('date(main.createTime) AS date') // 将UNIX时间戳转换为日期
.addSelect('COUNT(*) AS count')
.select(`${this.dbAdapter.date('main.createTime')} AS date`) // 将UNIX时间戳转换为日期
.addSelect('COUNT(1) AS count')
.where({
// 0点
createTime: MoreThan(todayEnd.add(-param.days, 'day').toDate()),