From 75a02371664f77e81c6805b5be2c6f475df65d4b Mon Sep 17 00:00:00 2001 From: akaster99 Date: Fri, 22 Sep 2023 10:52:13 +0900 Subject: [PATCH 1/7] feat: add typedoc --- src/model.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/types/type.ts | 11 +++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/model.ts b/src/model.ts index 6bfd921..e3c0500 100644 --- a/src/model.ts +++ b/src/model.ts @@ -1,4 +1,5 @@ import ModelController from "./controllers/modelController"; +import { creditHistories } from "./types/type"; export default class Model { modelName: string; @@ -8,53 +9,99 @@ export default class Model { this.modelName = modelName; this.modelController = ModelController.getInstance(); } + //TODO(woojae): login not Required + /** + * Check if model is running. It throws error when model is not running. + */ async isRunning() { return await this.modelController.isRunning(this.modelName); } + /** + * Get model information. not implemented yet. + * @returns {string} Model information. + */ //TODO(woojae): login not Required async getInformation() { return await this.modelController.getInformation(this.modelName); } + /** + * Calculate estimated cost for given request data. + * @param {string} rerquestData string data for request to model. + * @returns {number} Estimated cost. + */ //TODO(woojae): login not Required async calculateCost (requestData: string) { return await this.modelController.calculateCost(this.modelName, requestData); } + /** + * Charge credit to model. + * @param {number} amount Amount of credit to charge. + * @returns {string} Transaction hash. + */ async chargeCredit(amount: number) { return await this.modelController.chargeCredit(this.modelName, amount); } + /** + * Withdraw credit from model. + * @param {number} amount Amount of credit to withdraw. + * @returns {string} Transaction hash. + */ async withdrawCredit(amount: number) { return await this.modelController.withdrawCredit(this.modelName, amount); } + /** + * Get credit balance of model. + * @returns {number} Amount of credit balance. + */ async getCreditBalance() { return await this.modelController.getCreditBalance(this.modelName); } + /** + * Get credit history of model. + * @returns {creditHistories} Histories of credit deposit and usage. + */ async getCreditHistory() { return await this.modelController.getCreditHistory(this.modelName); } + /** + * Use model with given request data. + * @param {string} requestData String data for request to model. + * @returns {string} Response data from model. + */ async use(requestData: string) { return await this.modelController.use(this.modelName, requestData); } + /** + * Change status of AI model container to Running. Need to be admin. Not implemented yet. + */ //NOTE(woojae): need admin async run() { await this.isAdmin(); return await this.modelController.run(this.modelName); } + /** + * Change status of AI model container to Stopped. Need to be admin. Not implemented yet. + */ //NOTE(woojae): need admin async stop() { await this.isAdmin(); return await this.modelController.stop(this.modelName); } + /** + * Change model configuration. Need to be admin. Not implemented yet. + * @param {any} config Configuration to change. Not implemented yet. + */ //NOTE(woojae): need admin async changeModelInfo(config: any) { await this.isAdmin(); diff --git a/src/types/type.ts b/src/types/type.ts index c797106..72ed108 100644 --- a/src/types/type.ts +++ b/src/types/type.ts @@ -30,6 +30,17 @@ export enum HISTORY_TYPE { USAGE = "USAGE", } +export type creditHistories = { + [timestamp: string]: creditHistory; +} + +export type creditHistory = { + type: HISTORY_TYPE; + amount: number; + transferKey?: string; + requestTimestamp?: string; +} + export type opResult = { code: number; bandwidth_gas_amount: number; From 76eb1b23677623f8508a38359283aa0f71dfad26 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Fri, 22 Sep 2023 10:55:47 +0900 Subject: [PATCH 2/7] refactor: delete --- src/model.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/model.ts b/src/model.ts index e3c0500..c2b003b 100644 --- a/src/model.ts +++ b/src/model.ts @@ -10,7 +10,6 @@ export default class Model { this.modelController = ModelController.getInstance(); } - //TODO(woojae): login not Required /** * Check if model is running. It throws error when model is not running. */ @@ -22,7 +21,6 @@ export default class Model { * Get model information. not implemented yet. * @returns {string} Model information. */ - //TODO(woojae): login not Required async getInformation() { return await this.modelController.getInformation(this.modelName); } @@ -32,7 +30,6 @@ export default class Model { * @param {string} rerquestData string data for request to model. * @returns {number} Estimated cost. */ - //TODO(woojae): login not Required async calculateCost (requestData: string) { return await this.modelController.calculateCost(this.modelName, requestData); } From a7a065b1102c6422c8ce3bf3d0ae9629c73540ab Mon Sep 17 00:00:00 2001 From: akaster99 Date: Fri, 22 Sep 2023 11:01:54 +0900 Subject: [PATCH 3/7] refactor: add types at modelController --- src/controllers/modelController.ts | 38 +++++++++++++++--------------- src/model.ts | 3 --- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/controllers/modelController.ts b/src/controllers/modelController.ts index 51982a9..24ef5ac 100644 --- a/src/controllers/modelController.ts +++ b/src/controllers/modelController.ts @@ -4,7 +4,7 @@ import { Path } from "../constants"; import { getRequestDepositOp, getTransferOp } from "../utils/operator"; import { buildSetOperation, buildTxBody } from "../utils/builder"; import Handler from "../handlers/handler"; -import { ContainerStatus } from "../types/type"; +import { ContainerStatus, creditHistories } from "../types/type"; export default class ModelController { private static instance: ModelController | undefined; @@ -17,7 +17,7 @@ export default class ModelController { return ModelController.instance; } - async isRunning(modelName: string) { + async isRunning(modelName: string): Promise { const isRunning = await this.ain.getValue(Path.app(modelName).status()); if(isRunning !== ContainerStatus.RUNNING) { throw new Error('Model is not running'); @@ -25,11 +25,11 @@ export default class ModelController { } //TODO(woojae): implement this - async getInformation(modelName: string) { + async getInformation(modelName: string): Promise { return await 'information of model'; } - async calculateCost(modelName: string, requestData: string) { + async calculateCost(modelName: string, requestData: string): Promise { const billingConfig = await this.ain.getValue(Path.app(modelName).billingConfig()); const token = requestData.split(' ').length; let cost = token * billingConfig.costPerToken; @@ -41,7 +41,7 @@ export default class ModelController { return cost; } - async chargeCredit(modelName: string, amount: number) { + async chargeCredit(modelName: string, amount: number): Promise { this.isLoggedIn(); this.isRunning(modelName); const transferKey = Date.now(); @@ -61,22 +61,22 @@ export default class ModelController { return await true; } - async getCreditBalance(modelName: string) { + async getCreditBalance(modelName: string): Promise { this.isLoggedIn(); const userAddress = this.ain.getAddress(); const balancePath = Path.app(modelName).balanceOfUser(userAddress); - return await this.ain.getValue(balancePath) | 0; + return await this.ain.getValue(balancePath) as number | 0; } - async getCreditHistory(modelName: string) { + async getCreditHistory(modelName: string): Promise { this.isLoggedIn(); const userAddress = this.ain.getAddress(); const creditHistoryPath = Path.app(modelName).historyOfUser(userAddress); - return await this.ain.getValue(creditHistoryPath); + return await this.ain.getValue(creditHistoryPath) as creditHistories; } //TODO(woojae): connect with handler - async use(modelName: string, requestData: string) { + async use(modelName: string, requestData: string) : Promise { this.isLoggedIn(); this.isRunning(modelName); const result = await new Promise(async (resolve, reject) => { @@ -90,32 +90,32 @@ export default class ModelController { await this.ain.sendTransaction(txBody); return requestKey; }); - return result; + return result as string; } //TODO(woojae): implement this //NOTE(woojae): need admin - async run(modelName: string) { - return await true; + async run(modelName: string): Promise { + await true; } //TODO(woojae): implement this //NOTE:(woojae): need admin - async stop(modelName: string) { - return await true; + async stop(modelName: string): Promise { + await true; } //TODO:(woojae): implement this //NOTE:(woojae): need admin - async changeModelInfo(modelName: string, config: any) { - return await true; + async changeModelInfo(modelName: string, config: any):Promise { + await true; } - private async getDepositAddress(appName: string) { + private async getDepositAddress(appName: string): Promise { return (await this.ain.getValue(Path.app(appName).billingConfig())).depositAddress; } - private isLoggedIn() { + private isLoggedIn(): boolean { if(!this.ain.getDefaultAccount()) throw new Error('You should login First.'); return true; diff --git a/src/model.ts b/src/model.ts index c2b003b..7ca5f31 100644 --- a/src/model.ts +++ b/src/model.ts @@ -80,7 +80,6 @@ export default class Model { /** * Change status of AI model container to Running. Need to be admin. Not implemented yet. */ - //NOTE(woojae): need admin async run() { await this.isAdmin(); return await this.modelController.run(this.modelName); @@ -89,7 +88,6 @@ export default class Model { /** * Change status of AI model container to Stopped. Need to be admin. Not implemented yet. */ - //NOTE(woojae): need admin async stop() { await this.isAdmin(); return await this.modelController.stop(this.modelName); @@ -99,7 +97,6 @@ export default class Model { * Change model configuration. Need to be admin. Not implemented yet. * @param {any} config Configuration to change. Not implemented yet. */ - //NOTE(woojae): need admin async changeModelInfo(config: any) { await this.isAdmin(); return await this.modelController.changeModelInfo(this.modelName, config); From 347abbb07b796318a75991c6e6af07608623036a Mon Sep 17 00:00:00 2001 From: akaster99 Date: Fri, 22 Sep 2023 11:04:03 +0900 Subject: [PATCH 4/7] .gitignore docs/ --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index c6bba59..c33d004 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,6 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* + +# typedoc +docs/ \ No newline at end of file From 30f19d725228fcace1fbfef921e9161e0568b5d7 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Fri, 22 Sep 2023 11:06:02 +0900 Subject: [PATCH 5/7] refactor: getInformation --- src/controllers/modelController.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/modelController.ts b/src/controllers/modelController.ts index 24ef5ac..8943b0c 100644 --- a/src/controllers/modelController.ts +++ b/src/controllers/modelController.ts @@ -25,7 +25,7 @@ export default class ModelController { } //TODO(woojae): implement this - async getInformation(modelName: string): Promise { + async getInformation(modelName: string): Promise { return await 'information of model'; } From 8db5b1d881f66fee0d9dfd83daf6e67bc8028f4d Mon Sep 17 00:00:00 2001 From: akaster99 Date: Fri, 22 Sep 2023 11:13:21 +0900 Subject: [PATCH 6/7] refactor --- src/controllers/modelController.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/controllers/modelController.ts b/src/controllers/modelController.ts index 8943b0c..6c52782 100644 --- a/src/controllers/modelController.ts +++ b/src/controllers/modelController.ts @@ -75,7 +75,6 @@ export default class ModelController { return await this.ain.getValue(creditHistoryPath) as creditHistories; } - //TODO(woojae): connect with handler async use(modelName: string, requestData: string) : Promise { this.isLoggedIn(); this.isRunning(modelName); @@ -93,21 +92,18 @@ export default class ModelController { return result as string; } - //TODO(woojae): implement this - //NOTE(woojae): need admin + //TODO(woojae): implement this. async run(modelName: string): Promise { await true; } - //TODO(woojae): implement this - //NOTE:(woojae): need admin + //TODO(woojae): implement this. async stop(modelName: string): Promise { await true; } //TODO:(woojae): implement this - //NOTE:(woojae): need admin - async changeModelInfo(modelName: string, config: any):Promise { + async changeModelInfo(modelName: string, config: any): Promise { await true; } From 30aaafab62a64b9ff89752e98dcf5fe50a934885 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Fri, 22 Sep 2023 11:13:43 +0900 Subject: [PATCH 7/7] indent --- src/controllers/modelController.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/controllers/modelController.ts b/src/controllers/modelController.ts index 6c52782..972da19 100644 --- a/src/controllers/modelController.ts +++ b/src/controllers/modelController.ts @@ -55,7 +55,6 @@ export default class ModelController { return this.ain.sendTransaction(txBody); } - //TODO(woojae): implement this async withdrawCredit(modelName: string, amount: number) { return await true;