Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: model run and stop #59

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor) admin. ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다!

}
}
}
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();
}
}