feat: global instance filter

This commit is contained in:
unitwk 2023-01-29 17:29:40 +08:00
parent a5608a45a3
commit 349ca381e3
4 changed files with 20 additions and 6 deletions

View File

@ -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);

View File

@ -54,9 +54,11 @@ routerApp.on("instance/select", (ctx, data) => {
// keyword condition query
const queryWrapper = InstanceSubsystem.getQueryMapWrapper();
let result = queryWrapper.select<Instance>((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<Instance>(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({

View File

@ -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++;
});

View File

@ -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<Instance>();
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();