From 67f8d17b2bb0e38960f6cc06730648036df77b4b Mon Sep 17 00:00:00 2001 From: akaster99 Date: Mon, 18 Sep 2023 17:08:54 +0900 Subject: [PATCH 1/4] feat: modelSkeleton --- src/ainize.ts | 4 ++ src/controllers/modelController.ts | 65 ++++++++++++++++++++++++++++ src/model.ts | 68 ++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 src/controllers/modelController.ts create mode 100644 src/model.ts diff --git a/src/ainize.ts b/src/ainize.ts index 53cb3bb..206f5cc 100644 --- a/src/ainize.ts +++ b/src/ainize.ts @@ -9,6 +9,7 @@ import DepositService from "./modules/service/depositService"; import UseService from "./modules/service/useService"; import Service from "./modules/service"; import Admin from "./modules/admin"; +import Model from "./model"; export default class Ainize { private cache: NodeCache; ain: Ain; @@ -33,6 +34,9 @@ export default class Ainize { this.admin = new Admin(this, depositService, useService); } + model(modelName: string) { + return new Model(modelName); + } test() { console.log("test"); } diff --git a/src/controllers/modelController.ts b/src/controllers/modelController.ts new file mode 100644 index 0000000..89f288c --- /dev/null +++ b/src/controllers/modelController.ts @@ -0,0 +1,65 @@ +export default class ModelController { + instance: ModelController | undefined; + constructor() { + if(this.instance) return this.instance; + this.instance = this; + } + + //TODO(woojae): implement this + async isRunning(modelName: string) { + return await true; + } + + //TODO(woojae): implement this + async getInformation(modelName: string) { + return await true; + } + + //TODO(woojae): implement this + async calculateCost(modelName: string, requestData: string) { + return await 0.3; + } + + //TODO(woojae): implement this + async chargeCredit(modelName: string, amount: number) { + return await true; + } + + //TODO(woojae): implement this + async withdrawCredit(modelName: string, amount: number) { + return await true; + } + + //TODO(woojae): implement this + async getCreditBalance(modelName: string) { + return await 0.3; + } + + //TODO(woojae): implement this + async getCreditHistory(modelName: string) { + return await true; + } + + //TODO(woojae): implement this + async use(modelName: string, requestData: string) { + return await true; + } + + //TODO(woojae): implement this + //NOTE(woojae): need admin + async run(modelName: string) { + return await true; + } + + //TODO(woojae): implement this + //NOTE:(woojae): need admin + async stop(modelName: string) { + return await true; + } + + //TODO:(woojae): implement this + //NOTE:(woojae): need admin + async changeModelInfo(modelName: string, config: any) { + return await true; + } +} \ No newline at end of file diff --git a/src/model.ts b/src/model.ts new file mode 100644 index 0000000..6bd4dea --- /dev/null +++ b/src/model.ts @@ -0,0 +1,68 @@ +import ModelController from "./controllers/modelController"; + +export default class Model { + modelName: string; + modelController: ModelController; + + constructor(modelName: string) { + this.modelName = modelName; + this.modelController = new ModelController(); + } + //TODO(woojae): login not Required + async isRunning() { + return await this.modelController.isRunning(this.modelName); + } + + //TODO(woojae): login not Required + async getInformation() { + return await this.modelController.getInformation(this.modelName); + } + + //TODO(woojae): login not Required + async calculateCost (requestData: string) { + return await this.modelController.calculateCost(this.modelName, requestData); + } + + async chargeCredit(amount: number) { + return await this.modelController.chargeCredit(this.modelName, amount); + } + + async withdrawCredit(amount: number) { + return await this.modelController.withdrawCredit(this.modelName, amount); + } + + async getCreditBalance() { + return await this.modelController.getCreditBalance(this.modelName); + } + + async getCreditHistory() { + return await this.modelController.getCreditHistory(this.modelName); + } + + async use(requestData: string) { + return await this.modelController.use(this.modelName, requestData); + } + + //NOTE(woojae): need admin + async run() { + await this.isAdmin(); + return await this.modelController.run(this.modelName); + } + + //NOTE(woojae): need admin + async stop() { + await this.isAdmin(); + return await this.modelController.stop(this.modelName); + } + + //NOTE(woojae): need admin + async changeModelInfo(config: any) { + await this.isAdmin(); + return await this.modelController.changeModelInfo(this.modelName, config); + } + + //TODO(woojae): implement this + private async isAdmin() { + return true; + } +} From 71cca96a71c523a944d2bd93d52c9a93f2ccb571 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Mon, 18 Sep 2023 17:29:50 +0900 Subject: [PATCH 2/4] refactor: private static instance --- src/controllers/modelController.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/controllers/modelController.ts b/src/controllers/modelController.ts index 89f288c..c49168b 100644 --- a/src/controllers/modelController.ts +++ b/src/controllers/modelController.ts @@ -1,8 +1,8 @@ export default class ModelController { - instance: ModelController | undefined; + private static instance: ModelController | undefined; constructor() { - if(this.instance) return this.instance; - this.instance = this; + if(ModelController.instance) return ModelController.instance; + ModelController.instance = this; } //TODO(woojae): implement this From fbbfdc8a6fea1f3b41b8971692c04718ae2fc531 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Mon, 18 Sep 2023 17:39:04 +0900 Subject: [PATCH 3/4] refactor: constructor to static --- src/controllers/modelController.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/controllers/modelController.ts b/src/controllers/modelController.ts index c49168b..8c946b0 100644 --- a/src/controllers/modelController.ts +++ b/src/controllers/modelController.ts @@ -1,8 +1,10 @@ export default class ModelController { private static instance: ModelController | undefined; - constructor() { - if(ModelController.instance) return ModelController.instance; - ModelController.instance = this; + static getInstance() { + if(!ModelController.instance){ + ModelController.instance = new ModelController(); + } + return ModelController.instance; } //TODO(woojae): implement this From ea30ec3d9d8e042c7aaa0569540d34b49f9b31c6 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Mon, 18 Sep 2023 17:39:28 +0900 Subject: [PATCH 4/4] refactor: modelController --- src/model.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/model.ts b/src/model.ts index 6bd4dea..6280a6a 100644 --- a/src/model.ts +++ b/src/model.ts @@ -6,7 +6,7 @@ export default class Model { constructor(modelName: string) { this.modelName = modelName; - this.modelController = new ModelController(); + this.modelController = ModelController.getInstance(); } //TODO(woojae): login not Required async isRunning() {