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/frontend/compute/log-entry.tsx
Views: 687
1
import type { ComputeServerEvent } from "@cocalc/util/compute/log";
2
import { useTypedRedux } from "@cocalc/frontend/app-framework";
3
import { capitalize, plural } from "@cocalc/util/misc";
4
import { STATE_INFO } from "@cocalc/util/db-schema/compute-servers";
5
import { Icon } from "@cocalc/frontend/components";
6
import ComputeServerTag from "@cocalc/frontend/compute/server-tag";
7
8
export default function LogEntry({
9
project_id,
10
event,
11
hideTitle,
12
}: {
13
project_id: string;
14
event: ComputeServerEvent;
15
hideTitle?: boolean;
16
}) {
17
const computeServers = useTypedRedux({ project_id }, "compute_servers");
18
const title = computeServers?.getIn([`${event.server_id}`, "title"]);
19
if (title == null) {
20
return null;
21
}
22
const cs = hideTitle ? <></> : <>Compute Server "{title}" - </>;
23
const tag = (
24
<ComputeServerTag
25
id={event.server_id}
26
style={{ float: "right", maxWidth: "125px" }}
27
/>
28
);
29
30
switch (event.action) {
31
case "error":
32
return (
33
<>
34
{cs} <Error error={event.error} />
35
{tag}
36
</>
37
);
38
case "state":
39
if (!STATE_INFO[event.state]) {
40
return null;
41
}
42
const { color, icon } = STATE_INFO[event.state];
43
return (
44
<>
45
<span style={{ color }}>
46
<Icon name={icon} /> {capitalize(event.state)}
47
</span>{" "}
48
{cs}
49
{tag}
50
</>
51
);
52
case "configuration":
53
return (
54
<>
55
{cs} Configuration{" "}
56
{plural(Object.keys(event.changes).length, "change")} -{" "}
57
{changeString(event.changes)}
58
{tag}
59
</>
60
);
61
case "automatic-shutdown":
62
return (
63
<>
64
{cs} - Automatic {capitalize(event.automatic_shutdown?.action ?? "Stop")}{" "}
65
{tag}
66
</>
67
);
68
default:
69
return (
70
<>
71
{cs} {JSON.stringify(event)}
72
{tag}
73
</>
74
);
75
}
76
}
77
78
function changeString(changes) {
79
let v: string[] = [];
80
for (const key in changes) {
81
const { from, to } = changes[key];
82
v.push(`${key}: ${JSON.stringify(from)} → ${JSON.stringify(to)}`);
83
}
84
if (v.length == 0) {
85
return "(no change)";
86
}
87
return v.join("; ");
88
}
89
90
export function Error({ error, style }: { error; style? }) {
91
return (
92
<div
93
style={{
94
border: "0px 5px",
95
display: "inline-block",
96
color: "white",
97
background: "darkred",
98
padding: "1px 5px",
99
borderRadius: "3px",
100
...style,
101
}}
102
>
103
{error}
104
</div>
105
);
106
}
107
108