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 diff --git a/src/controllers/modelController.ts b/src/controllers/modelController.ts index 51982a9..972da19 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(); @@ -55,28 +55,26 @@ export default class ModelController { return this.ain.sendTransaction(txBody); } - //TODO(woojae): implement this async withdrawCredit(modelName: string, amount: number) { 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 +88,29 @@ 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; + //TODO(woojae): implement this. + async run(modelName: string): Promise { + await true; } - //TODO(woojae): implement this - //NOTE:(woojae): need admin - async stop(modelName: string) { - return await true; + //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) { - 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 6bfd921..7ca5f31 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,54 +9,94 @@ 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); } - //TODO(woojae): login not Required + /** + * Get model information. not implemented yet. + * @returns {string} Model information. + */ async getInformation() { return await this.modelController.getInformation(this.modelName); } - //TODO(woojae): login not Required + /** + * Calculate estimated cost for given request data. + * @param {string} rerquestData string data for request to model. + * @returns {number} Estimated cost. + */ 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); } - //NOTE(woojae): need admin + /** + * Change status of AI model container to Running. Need to be admin. Not implemented yet. + */ async run() { await this.isAdmin(); return await this.modelController.run(this.modelName); } - //NOTE(woojae): need admin + /** + * Change status of AI model container to Stopped. Need to be admin. Not implemented yet. + */ async stop() { await this.isAdmin(); return await this.modelController.stop(this.modelName); } - //NOTE(woojae): need admin + /** + * Change model configuration. Need to be admin. Not implemented yet. + * @param {any} config Configuration to change. Not implemented yet. + */ async changeModelInfo(config: any) { await this.isAdmin(); return await this.modelController.changeModelInfo(this.modelName, config); 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;