From 8f90d3f8ff55468a77a6c3001e78848621a81819 Mon Sep 17 00:00:00 2001 From: whyour Date: Wed, 2 Nov 2022 00:02:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E4=BD=8D=E7=BD=AE=E8=AE=A1=E7=AE=97=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/api/env.ts | 1 - back/data/env.ts | 5 ++- back/loaders/initData.ts | 4 +-- back/loaders/initTask.ts | 1 - back/services/cronView.ts | 4 +-- back/services/dependence.ts | 1 - back/services/env.ts | 72 +++++++++++++++++++++++++------------ back/services/system.ts | 1 - back/services/user.ts | 1 - package.json | 2 +- src/pages/log/index.tsx | 3 +- src/pages/script/index.tsx | 2 +- 12 files changed, 61 insertions(+), 36 deletions(-) diff --git a/back/api/env.ts b/back/api/env.ts index a54aea1c..c05e8c60 100644 --- a/back/api/env.ts +++ b/back/api/env.ts @@ -109,7 +109,6 @@ export default (app: Router) => { }), }), async (req: Request<{ id: number }>, res: Response, next: NextFunction) => { - const logger: Logger = Container.get('logger'); try { const envService = Container.get(EnvService); const data = await envService.move(req.params.id, req.body); diff --git a/back/data/env.ts b/back/data/env.ts index 946689c6..82503f4d 100644 --- a/back/data/env.ts +++ b/back/data/env.ts @@ -26,7 +26,10 @@ export enum EnvStatus { 'disabled', } -export const initEnvPosition = 9999999999; +export const maxPosition = 9000000000000000; +export const initPosition = 4500000000000000; +export const stepPosition = 10000000; +export const minPosition = 100; interface EnvInstance extends Model, Env {} export const EnvModel = sequelize.define('Env', { diff --git a/back/loaders/initData.ts b/back/loaders/initData.ts index 00b99025..a3973884 100644 --- a/back/loaders/initData.ts +++ b/back/loaders/initData.ts @@ -4,7 +4,7 @@ import { Container } from 'typedi'; import { Crontab, CrontabModel, CrontabStatus } from '../data/cron'; import CronService from '../services/cron'; import EnvService from '../services/env'; -import _ from 'lodash'; +import groupBy from 'lodash/groupBy'; import { DependenceModel } from '../data/dependence'; import { Op } from 'sequelize'; import config from '../config'; @@ -26,7 +26,7 @@ export default async () => { order: [['type', 'DESC']], raw: true, }).then(async (docs) => { - const groups = _.groupBy(docs, 'type'); + const groups = groupBy(docs, 'type'); const keys = Object.keys(groups).sort((a, b) => parseInt(b) - parseInt(a)); for (const key of keys) { const group = groups[key]; diff --git a/back/loaders/initTask.ts b/back/loaders/initTask.ts index 9036191f..c185b916 100644 --- a/back/loaders/initTask.ts +++ b/back/loaders/initTask.ts @@ -1,5 +1,4 @@ import { Container } from 'typedi'; -import _ from 'lodash'; import SystemService from '../services/system'; import ScheduleService from '../services/schedule'; import SubscriptionService from '../services/subscription'; diff --git a/back/services/cronView.ts b/back/services/cronView.ts index bab86cd6..766aa780 100644 --- a/back/services/cronView.ts +++ b/back/services/cronView.ts @@ -1,14 +1,14 @@ import { Service, Inject } from 'typedi'; import winston from 'winston'; import { CrontabView, CrontabViewModel } from '../data/cronView'; -import { initEnvPosition } from '../data/env'; +import { initPosition } from '../data/env'; @Service() export default class CronViewService { constructor(@Inject('logger') private logger: winston.Logger) {} public async create(payload: CrontabView): Promise { - let position = initEnvPosition; + let position = initPosition; const views = await this.list(); if (views && views.length > 0 && views[views.length - 1].position) { position = views[views.length - 1].position as number; diff --git a/back/services/dependence.ts b/back/services/dependence.ts index 24922ade..dfeb5bc2 100644 --- a/back/services/dependence.ts +++ b/back/services/dependence.ts @@ -9,7 +9,6 @@ import { unInstallDependenceCommandTypes, DependenceModel, } from '../data/dependence'; -import _ from 'lodash'; import { spawn } from 'child_process'; import SockService from './sock'; import { Op } from 'sequelize'; diff --git a/back/services/env.ts b/back/services/env.ts index 52c77f0a..73db2491 100644 --- a/back/services/env.ts +++ b/back/services/env.ts @@ -1,10 +1,17 @@ import { Service, Inject } from 'typedi'; import winston from 'winston'; -import { getFileContentByName } from '../config/util'; import config from '../config'; import * as fs from 'fs'; -import { Env, EnvModel, EnvStatus, initEnvPosition } from '../data/env'; -import _ from 'lodash'; +import { + Env, + EnvModel, + EnvStatus, + initPosition, + maxPosition, + minPosition, + stepPosition, +} from '../data/env'; +import groupBy from 'lodash/groupBy'; import { Op } from 'sequelize'; @Service() @@ -13,17 +20,22 @@ export default class EnvService { public async create(payloads: Env[]): Promise { const envs = await this.envs(); - let position = initEnvPosition; - if (envs && envs.length > 0 && envs[envs.length - 1].position) { - position = envs[envs.length - 1].position as number; + let position = initPosition; + if ( + envs && + envs.length > 0 && + typeof envs[envs.length - 1].position === 'number' + ) { + position = envs[envs.length - 1].position!; } const tabs = payloads.map((x) => { - position = position / 2; + position = position - stepPosition; const tab = new Env({ ...x, position }); return tab; }); const docs = await this.insert(tabs); await this.set_envs(); + await this.checkPosition(tabs[tabs.length - 1].position!); return docs; } @@ -67,25 +79,40 @@ export default class EnvService { const envs = await this.envs(); if (toIndex === 0 || toIndex === envs.length - 1) { targetPosition = isUpward - ? envs[0].position! * 2 - : envs[toIndex].position! / 2; + ? envs[0].position! + stepPosition + : envs[toIndex].position! - stepPosition; } else { targetPosition = isUpward ? (envs[toIndex].position! + envs[toIndex - 1].position!) / 2 : (envs[toIndex].position! + envs[toIndex + 1].position!) / 2; } + const newDoc = await this.update({ id, - position: targetPosition, + position: this.getPrecisionPosition(targetPosition), }); + + await this.checkPosition(targetPosition); return newDoc; } - public async envs( - searchText: string = '', - sort: any = { position: -1 }, - query: any = {}, - ): Promise { + private async checkPosition(position: number) { + const precisionPosition = parseFloat(position.toPrecision(16)); + if (precisionPosition < minPosition || precisionPosition > maxPosition) { + const envs = await this.envs(); + let position = initPosition; + for (const env of envs) { + position = position - stepPosition; + await this.updateDb({ ...env, position }); + } + } + } + + private getPrecisionPosition(position: number): number { + return parseFloat(position.toPrecision(16)); + } + + public async envs(searchText: string = '', query: any = {}): Promise { let condition = { ...query }; if (searchText) { const encodeText = encodeURIComponent(searchText); @@ -155,12 +182,11 @@ export default class EnvService { } public async set_envs() { - const envs = await this.envs( - '', - { position: -1 }, - { name: { [Op.not]: null }, status: EnvStatus.normal }, - ); - const groups = _.groupBy(envs, 'name'); + const envs = await this.envs('', { + name: { [Op.not]: null }, + status: EnvStatus.normal, + }); + const groups = groupBy(envs, 'name'); let env_string = ''; for (const key in groups) { if (Object.prototype.hasOwnProperty.call(groups, key)) { @@ -168,8 +194,8 @@ export default class EnvService { // 忽略不符合bash要求的环境变量名称 if (/^[a-zA-Z_][0-9a-zA-Z_]*$/.test(key)) { - let value = _(group) - .map('value') + let value = group + .map((x) => x.value) .join('&') .replace(/(\\)[^n]/g, '\\\\') .replace(/(\\$)/, '\\\\') diff --git a/back/services/system.ts b/back/services/system.ts index afcc2dc5..47e8418b 100644 --- a/back/services/system.ts +++ b/back/services/system.ts @@ -2,7 +2,6 @@ import { Service, Inject } from 'typedi'; import winston from 'winston'; import config from '../config'; import * as fs from 'fs'; -import _ from 'lodash'; import { AuthDataType, AuthInfo, AuthModel, LoginStatus } from '../data/auth'; import { NotificationInfo } from '../data/notify'; import NotificationService from './notify'; diff --git a/back/services/user.ts b/back/services/user.ts index 303abf94..3eac0b76 100644 --- a/back/services/user.ts +++ b/back/services/user.ts @@ -3,7 +3,6 @@ import winston from 'winston'; import { createRandomString, getNetIp, getPlatform } from '../config/util'; import config from '../config'; import * as fs from 'fs'; -import _ from 'lodash'; import jwt from 'jsonwebtoken'; import { authenticator } from '@otplib/preset-default'; import { AuthDataType, AuthInfo, AuthModel, LoginStatus } from '../data/auth'; diff --git a/package.json b/package.json index f782cab2..4d94e88b 100644 --- a/package.json +++ b/package.json @@ -97,7 +97,7 @@ "@types/express": "^4.17.13", "@types/express-jwt": "^6.0.4", "@types/jsonwebtoken": "^8.5.8", - "@types/lodash": "^4.14.179", + "@types/lodash": "^4.14.185", "@types/multer": "^1.4.7", "@types/nedb": "^1.8.12", "@types/node": "^17.0.21", diff --git a/src/pages/log/index.tsx b/src/pages/log/index.tsx index 4d60a467..44c81916 100644 --- a/src/pages/log/index.tsx +++ b/src/pages/log/index.tsx @@ -21,7 +21,8 @@ import { useOutletContext } from '@umijs/max'; import { SharedContext } from '@/layouts'; import { DeleteOutlined } from '@ant-design/icons'; import { depthFirstSearch } from '@/utils'; -import { debounce, uniq } from 'lodash'; +import debounce from 'lodash/groupBy'; +import uniq from 'lodash/uniq'; import useFilterTreeData from '@/hooks/useFilterTreeData'; const { Text } = Typography; diff --git a/src/pages/script/index.tsx b/src/pages/script/index.tsx index 1974fcca..4c054714 100644 --- a/src/pages/script/index.tsx +++ b/src/pages/script/index.tsx @@ -38,7 +38,7 @@ import { parse } from 'query-string'; import { depthFirstSearch } from '@/utils'; import { SharedContext } from '@/layouts'; import useFilterTreeData from '@/hooks/useFilterTreeData'; -import { uniq } from 'lodash'; +import uniq from 'lodash/uniq'; const { Text } = Typography;