CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/pages/api/v2/compute/get-network-usage.ts
Views: 687
1
/*
2
Get network usage by a specific server during a particular period of time.
3
*/
4
5
import getAccountId from "lib/account/get-account";
6
import { getNetworkUsage } from "@cocalc/server/compute/control";
7
import getParams from "lib/api/get-params";
8
import { getServer } from "@cocalc/server/compute/get-servers";
9
10
import { apiRoute, apiRouteOperation } from "lib/api";
11
import {
12
GetComputeServerNetworkUsageInputSchema,
13
GetComputeServerNetworkUsageOutputSchema,
14
} from "lib/api/schema/compute/get-network-usage";
15
16
17
async function handle(req, res) {
18
try {
19
res.json(await get(req));
20
} catch (err) {
21
res.json({ error: `${err.message}` });
22
return;
23
}
24
}
25
26
async function get(req) {
27
const account_id = await getAccountId(req);
28
if (!account_id) {
29
throw Error("must be signed in");
30
}
31
const { id, start, end } = getParams(req);
32
if (!start) {
33
throw Error("must specify start");
34
}
35
if (!end) {
36
throw Error("must specify end");
37
}
38
const server = await getServer({ account_id, id });
39
return await getNetworkUsage({
40
server,
41
start: new Date(start),
42
end: new Date(end),
43
});
44
}
45
46
export default apiRoute({
47
getNetworkUsage: apiRouteOperation({
48
method: "POST",
49
openApiOperation: {
50
tags: ["Compute"]
51
},
52
})
53
.input({
54
contentType: "application/json",
55
body: GetComputeServerNetworkUsageInputSchema,
56
})
57
.outputs([
58
{
59
status: 200,
60
contentType: "application/json",
61
body: GetComputeServerNetworkUsageOutputSchema,
62
},
63
])
64
.handler(handle),
65
});
66
67