Skip to content

Commit

Permalink
Merge pull request #59 from ainize-team/feat/woojae/modelAdmin
Browse files Browse the repository at this point in the history
feat: model run and stop
  • Loading branch information
akastercomcom authored Sep 22, 2023
2 parents 5726a5d + 8cdefa9 commit c71ee0a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
32 changes: 20 additions & 12 deletions src/controllers/modelController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export default class ModelController {
}

async chargeCredit(modelName: string, amount: number): Promise<string> {
this.isLoggedIn();
this.isRunning(modelName);
const transferKey = Date.now();
const userAddress = this.ain.getAddress();
Expand All @@ -61,21 +60,18 @@ export default class ModelController {
}

async getCreditBalance(modelName: string): Promise<number> {
this.isLoggedIn();
const userAddress = this.ain.getAddress();
const balancePath = Path.app(modelName).balanceOfUser(userAddress);
return await this.ain.getValue(balancePath) as number | 0;
}

async getCreditHistory(modelName: string): Promise<creditHistories> {
this.isLoggedIn();
const userAddress = this.ain.getAddress();
const creditHistoryPath = Path.app(modelName).historyOfUser(userAddress);
return await this.ain.getValue(creditHistoryPath) as creditHistories;
}

async use(modelName: string, requestData: string) : Promise<string> {
this.isLoggedIn();
this.isRunning(modelName);
const result = await new Promise(async (resolve, reject) => {
const requestKey = Date.now();
Expand All @@ -91,28 +87,40 @@ export default class ModelController {
return result as string;
}

//TODO(woojae): implement this.
async run(modelName: string): Promise<void> {
await true;
const statusPath = Path.app(modelName).status();
const statusOp = buildSetOperation("SET_VALUE", statusPath, ContainerStatus.RUNNING);
const txBody = buildTxBody(statusOp);
await this.ain.sendTransaction(txBody);
}

//TODO(woojae): implement this.
async stop(modelName: string): Promise<void> {
await true;
const statusPath = Path.app(modelName).status();
const statusOp = buildSetOperation("SET_VALUE", statusPath, ContainerStatus.STOP);
const txBody = buildTxBody(statusOp);
await this.ain.sendTransaction(txBody);
}

//TODO:(woojae): implement this
async changeModelInfo(modelName: string, config: any): Promise<void> {
await true;
}

private async getDepositAddress(appName: string): Promise<string> {
return (await this.ain.getValue(Path.app(appName).billingConfig())).depositAddress;
private async getDepositAddress(modelName: string): Promise<string> {
return (await this.ain.getValue(Path.app(modelName).billingConfig())).depositAddress;
}

private isLoggedIn(): boolean {
isLoggedIn(): void {
if(!this.ain.getDefaultAccount())
throw new Error('You should login First.');
return true;
}

async isAdmin(modelName: string): Promise<void> {
this.isLoggedIn();
const adminPath = `/manage_app/${modelName}/config/admin`;
const adminList = await this.ain.getValue(adminPath);
if(!adminList[this.ain.getAddress()]) {
throw new Error('You are not admin');
}
}
}
12 changes: 10 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default class Model {
* @returns {string} Transaction hash.
*/
async chargeCredit(amount: number) {
this.isLoggedIn();
return await this.modelController.chargeCredit(this.modelName, amount);
}

Expand All @@ -49,6 +50,7 @@ export default class Model {
* @returns {string} Transaction hash.
*/
async withdrawCredit(amount: number) {
this.isLoggedIn();
return await this.modelController.withdrawCredit(this.modelName, amount);
}

Expand All @@ -57,6 +59,7 @@ export default class Model {
* @returns {number} Amount of credit balance.
*/
async getCreditBalance() {
this.isLoggedIn();
return await this.modelController.getCreditBalance(this.modelName);
}

Expand All @@ -65,6 +68,7 @@ export default class Model {
* @returns {creditHistories} Histories of credit deposit and usage.
*/
async getCreditHistory() {
this.isLoggedIn();
return await this.modelController.getCreditHistory(this.modelName);
}

Expand All @@ -74,6 +78,7 @@ export default class Model {
* @returns {string} Response data from model.
*/
async use(requestData: string) {
this.isLoggedIn();
return await this.modelController.use(this.modelName, requestData);
}

Expand Down Expand Up @@ -102,8 +107,11 @@ export default class Model {
return await this.modelController.changeModelInfo(this.modelName, config);
}

//TODO(woojae): implement this
private async isAdmin() {
return true;
return this.modelController.isAdmin(this.modelName);
}

private isLoggedIn() {
return this.modelController.isLoggedIn();
}
}

0 comments on commit c71ee0a

Please sign in to comment.