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-detailed-state.ts
Views: 687
1
/*
2
Get detailed state for a compute server.
3
4
This can get just the state for a given component.
5
6
One application of this is that the file system sync daemon
7
can check for the error message to be cleared.
8
*/
9
10
import getProjectOrAccountId from "lib/account/get-account";
11
import { getDetailedState } from "@cocalc/server/compute/set-detailed-state";
12
import getParams from "lib/api/get-params";
13
import isCollaborator from "@cocalc/server/projects/is-collaborator";
14
15
import { apiRoute, apiRouteOperation } from "lib/api";
16
import {
17
GetDetailedServerStateInputSchema,
18
GetDetailedServerStateOutputSchema,
19
} from "lib/api/schema/compute/get-detailed-state";
20
21
22
async function handle(req, res) {
23
try {
24
res.json(await get(req));
25
} catch (err) {
26
res.json({ error: `${err.message}` });
27
return;
28
}
29
}
30
31
async function get(req) {
32
// This is a bit complicated because it can be used by a project api key,
33
// in which case project_id must not be passed in, or it can be auth'd
34
// by a normal api key or account, in which case project_id must be passed in.
35
const project_or_account_id = await getProjectOrAccountId(req);
36
if (!project_or_account_id) {
37
throw Error("invalid auth");
38
}
39
const { id, name, project_id: project_id0 } = getParams(req);
40
41
let project_id;
42
if (!project_id0) {
43
project_id = project_or_account_id;
44
} else {
45
if (
46
!(await isCollaborator({
47
account_id: project_or_account_id,
48
project_id: project_id0,
49
}))
50
) {
51
throw Error("must be a collaborator on project with compute server");
52
}
53
project_id = project_id0;
54
}
55
56
return await getDetailedState({
57
project_id,
58
id,
59
name,
60
});
61
}
62
63
export default apiRoute({
64
getDetailedServerState: apiRouteOperation({
65
method: "POST",
66
openApiOperation: {
67
tags: ["Compute"]
68
},
69
})
70
.input({
71
contentType: "application/json",
72
body: GetDetailedServerStateInputSchema,
73
})
74
.outputs([
75
{
76
status: 200,
77
contentType: "text/plain",
78
body: GetDetailedServerStateOutputSchema,
79
},
80
])
81
.handler(handle),
82
});
83
84