From 349ca381e373b47bb368960f738e24e025e9111e Mon Sep 17 00:00:00 2001 From: unitwk Date: Sun, 29 Jan 2023 17:29:40 +0800 Subject: [PATCH] feat: global instance filter --- src/app.ts | 2 +- src/routers/Instance_router.ts | 6 ++++-- src/routers/info_router.ts | 2 +- src/service/system_instance.ts | 16 ++++++++++++++-- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/app.ts b/src/app.ts index 915a687..133acb9 100755 --- a/src/app.ts +++ b/src/app.ts @@ -88,7 +88,7 @@ initDependent(); // Initialize application instance system try { InstanceSubsystem.loadInstances(); - logger.info($t("app.instanceLoad", { n: InstanceSubsystem.instances.size })); + logger.info($t("app.instanceLoad", { n: InstanceSubsystem.getInstances().length })); } catch (err) { logger.error($t("app.instanceLoadError"), err); process.exit(-1); diff --git a/src/routers/Instance_router.ts b/src/routers/Instance_router.ts index 852601c..b451d6e 100755 --- a/src/routers/Instance_router.ts +++ b/src/routers/Instance_router.ts @@ -54,9 +54,11 @@ routerApp.on("instance/select", (ctx, data) => { // keyword condition query const queryWrapper = InstanceSubsystem.getQueryMapWrapper(); let result = queryWrapper.select((v) => { + if (InstanceSubsystem.isGlobalInstance(v)) return false; if (!v.config.nickname.toLowerCase().includes(condition.instanceName.toLowerCase())) return false; return true; }); + result = result.sort((a, b) => (a.config.nickname > b.config.nickname ? 1 : -1)); // paging function const pageResult = queryWrapper.page(result, page, pageSize); // filter unwanted data @@ -85,7 +87,7 @@ routerApp.on("instance/select", (ctx, data) => { // Get an overview of this daemon instance routerApp.on("instance/overview", (ctx) => { const overview: IInstanceDetail[] = []; - InstanceSubsystem.instances.forEach((instance) => { + InstanceSubsystem.getInstances().forEach((instance) => { overview.push({ instanceUuid: instance.instanceUuid, started: instance.startCount, @@ -102,7 +104,7 @@ routerApp.on("instance/overview", (ctx) => { routerApp.on("instance/section", (ctx, data) => { const instanceUuids = data.instanceUuids as string[]; const overview: IInstanceDetail[] = []; - InstanceSubsystem.instances.forEach((instance) => { + InstanceSubsystem.getInstances().forEach((instance) => { instanceUuids.forEach((targetUuid) => { if (targetUuid === instance.instanceUuid) { overview.push({ diff --git a/src/routers/info_router.ts b/src/routers/info_router.ts index 71473bb..6ba545d 100755 --- a/src/routers/info_router.ts +++ b/src/routers/info_router.ts @@ -18,7 +18,7 @@ routerApp.on("info/overview", async (ctx) => { const daemonVersion = getVersion(); let total = 0; let running = 0; - InstanceSubsystem.instances.forEach((v) => { + InstanceSubsystem.getInstances().forEach((v) => { total++; if (v.status() == Instance.STATUS_RUNNING) running++; }); diff --git a/src/service/system_instance.ts b/src/service/system_instance.ts index 3aacd21..483ea11 100755 --- a/src/service/system_instance.ts +++ b/src/service/system_instance.ts @@ -216,8 +216,7 @@ class InstanceSubsystem extends EventEmitter { logger.info(`Instance ${instance.config.nickname} (${instance.instanceUuid}) is running or busy, and is being forced to end.`); promises.push( instance.execCommand(new KillCommand()).then(() => { - if (instance.config.nickname !== InstanceSubsystem.GLOBAL_INSTANCE) - StorageSubsystem.store("InstanceConfig", instance.instanceUuid, instance.config); + if (!this.isGlobalInstance(instance)) StorageSubsystem.store("InstanceConfig", instance.instanceUuid, instance.config); logger.info(`Instance ${instance.config.nickname} (${instance.instanceUuid}) saved successfully.`); }) ); @@ -225,6 +224,19 @@ class InstanceSubsystem extends EventEmitter { } await Promise.all(promises); } + + getInstances() { + let newArr = new Array(); + this.instances.forEach((instance) => { + if (!this.isGlobalInstance(instance)) newArr.push(instance); + }); + newArr = newArr.sort((a, b) => (a.config.nickname > a.config.nickname ? 1 : -1)); + return newArr; + } + + isGlobalInstance(instance: Instance) { + return instance.instanceUuid === InstanceSubsystem.GLOBAL_INSTANCE_UUID || instance.config.nickname === InstanceSubsystem.GLOBAL_INSTANCE; + } } export default new InstanceSubsystem();