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