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/compute-servers-table.ts
Views: 687
1
/*
2
Compute servers in a specific project.
3
*/
4
5
import { Table } from "@cocalc/frontend/app-framework/Table";
6
import { redux } from "@cocalc/frontend/app-framework";
7
import { isValidUUID } from "@cocalc/util/misc";
8
import { computeServersEnabled } from "./config";
9
import { delay } from "awaiting";
10
11
const PREFIX = "compute-server-";
12
function projectIdToName(project_id) {
13
return `${PREFIX}${project_id}`;
14
}
15
function nameToProjectId(name) {
16
return name.slice(PREFIX.length);
17
}
18
19
// Create and register compute servers table for a given project,
20
// which gets automatically synchronized with the database, when
21
// changes occur.
22
class ComputeServersTable extends Table {
23
constructor(project_id: string) {
24
if (!isValidUUID(project_id)) {
25
throw Error(`project_id must be a valid uuid but is ${project_id}`);
26
}
27
super(projectIdToName(project_id), redux);
28
this.query = this.query.bind(this);
29
this._change = this._change.bind(this);
30
}
31
32
options() {
33
return [];
34
}
35
36
query() {
37
return {
38
compute_servers: [
39
{
40
project_id: nameToProjectId(this.name),
41
id: null,
42
project_specific_id: null,
43
account_id: null,
44
title: null,
45
color: null,
46
cost_per_hour: null,
47
deleted: null,
48
error: null,
49
state: null,
50
state_changed: null,
51
idle_timeout: null,
52
automatic_shutdown: null,
53
autorestart: null,
54
cloud: null,
55
configuration: null,
56
provisioned_configuration: null,
57
data: null,
58
avatar_image_tiny: null,
59
last_edited: null,
60
purchase_id: null,
61
detailed_state: null,
62
position: null,
63
template: null,
64
},
65
],
66
};
67
}
68
69
_change(table) {
70
const actions = redux.getProjectActions(nameToProjectId(this.name));
71
// Using {compute_servers:table.get()} does NOT work. The id keys are integers,
72
// which leads to problems when converting after an update.
73
actions.setState({ compute_servers: table.get()?.toJS() });
74
}
75
}
76
77
const tables: { [project_id: string]: ComputeServersTable } = {};
78
export async function init(project_id: string) {
79
let enabled = computeServersEnabled();
80
while (enabled === null) {
81
// customize hasn't been loaded yet, so we don't know.
82
await delay(1000);
83
enabled = computeServersEnabled();
84
}
85
86
if (!enabled) {
87
// no need -- would just waste resources
88
return;
89
}
90
if (tables[project_id] != null) {
91
return;
92
}
93
tables[project_id] = new ComputeServersTable(project_id);
94
}
95
96
export function close(project_id: string) {
97
if (tables[project_id] == null) {
98
return;
99
}
100
tables[project_id].close();
101
delete tables[project_id];
102
}
103
104