Skip to content

Commit 37d930a

Browse files
committed
added new operation status
1 parent 4c65a21 commit 37d930a

File tree

4 files changed

+47
-22
lines changed

4 files changed

+47
-22
lines changed

src/services/monitor-analytic/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { apiCall } from "../core";
22
import { HttpMethod } from "../core/types";
3+
import { getAndWaitForOperationStatusCompleted } from "../operation";
4+
import type { OperationStatus } from "../operation/types";
35

46
/**
57
* Get server bandwidth usage or disk size per application.
@@ -23,15 +25,20 @@ export function getServerSummary(
2325
/**
2426
* Get server usage.
2527
* @param {number} serverId Numeric id of the server.
26-
* @returns {Promise<{ status: boolean, operation_id: string }>} A promise resolving with the operation id.
28+
* @returns {Promise<OperationStatus>} A promise resolving with the operation id.
2729
*/
28-
export function getServerUsage(
30+
export async function getServerUsage(
2931
serverId: number
30-
): Promise<{ status: boolean; operation_id: string }> {
32+
): Promise<OperationStatus> {
3133
const data = {
3234
server_id: serverId,
3335
};
34-
return apiCall("/server/analytics/serverUsage", HttpMethod.GET, data);
36+
const req = await apiCall(
37+
"/server/analytics/serverUsage",
38+
HttpMethod.GET,
39+
data
40+
);
41+
return await getAndWaitForOperationStatusCompleted(req.operation_id);
3542
}
3643

3744
/**

src/services/operation/index.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1+
import { sleep } from "../../utils";
12
import { apiCall } from "../core";
23
import { HttpMethod } from "../core/types";
3-
4-
// Define an interface for the operation status response
5-
interface OperationStatus {
6-
id: string;
7-
type: string;
8-
server_id: string;
9-
estimated_time_remaining: string;
10-
frontend_step_number: string;
11-
status: string;
12-
is_completed: string;
13-
message: string;
14-
app_id: string;
15-
}
16-
17-
interface GetOperationStatusResponse {
18-
operation: OperationStatus;
19-
}
4+
import type { GetOperationStatusResponse, OperationStatus } from "./types";
205

216
/**
227
* Gets the status of an operation that is running in the background.
@@ -40,8 +25,22 @@ interface GetOperationStatusResponse {
4025
* }
4126
* ```
4227
*/
43-
export function getOperationStatus(id: number): Promise<OperationStatus> {
28+
export function getOperationStatus(id: string): Promise<OperationStatus> {
4429
return apiCall(`/operation/${id}`, HttpMethod.GET).then(
4530
(response: GetOperationStatusResponse) => response.operation
4631
);
4732
}
33+
34+
export async function getAndWaitForOperationStatusCompleted(
35+
operationId: string
36+
) {
37+
let operationStatus = await getOperationStatus(operationId);
38+
while (operationStatus.is_completed !== "1") {
39+
const waitTime = operationStatus.estimated_time_remaining
40+
? parseInt(operationStatus.estimated_time_remaining) * 60000
41+
: 5000;
42+
await sleep(waitTime);
43+
operationStatus = await getOperationStatus(operationId);
44+
}
45+
return operationStatus;
46+
}

src/services/operation/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Define an interface for the operation status response
2+
export interface OperationStatus {
3+
id: string;
4+
type: string;
5+
server_id: string;
6+
estimated_time_remaining: string;
7+
frontend_step_number: string;
8+
status: string;
9+
is_completed: string;
10+
parameters?: string;
11+
message: string;
12+
app_id: string;
13+
}
14+
15+
export interface GetOperationStatusResponse {
16+
operation: OperationStatus;
17+
}

src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const sleep = (ms: number) =>
2+
new Promise((resolve) => setTimeout(resolve, ms));

0 commit comments

Comments
 (0)