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/cloud-filesystem/api.ts
Views: 687
1
import api from "@cocalc/frontend/client/api";
2
import LRU from "lru-cache";
3
import type {
4
CreateCloudFilesystem,
5
CloudFilesystem,
6
EditCloudFilesystem,
7
} from "@cocalc/util/db-schema/cloud-filesystems";
8
import {
9
CHANGE_MOUNTED,
10
CHANGE_UNMOUNTED,
11
} from "@cocalc/util/db-schema/cloud-filesystems";
12
13
export async function createCloudFilesystem(
14
opts: CreateCloudFilesystem,
15
): Promise<number> {
16
return await api("compute/cloud-filesystem/create", opts);
17
}
18
19
export async function deleteCloudFilesystem({
20
id,
21
lock,
22
}: {
23
id: number;
24
lock: string;
25
}): Promise<void> {
26
await api("compute/cloud-filesystem/delete", { id, lock });
27
}
28
29
export async function editCloudFilesystem(
30
opts: EditCloudFilesystem,
31
): Promise<void> {
32
for (const field in opts) {
33
if (field == "id") {
34
continue;
35
}
36
if (!CHANGE_MOUNTED.has(field) && !CHANGE_UNMOUNTED.has(field)) {
37
throw Error(`invalid field '${field}' for edit`);
38
}
39
}
40
return await api("compute/cloud-filesystem/edit", opts);
41
}
42
43
const cloudFilesystemCache = new LRU<string, CloudFilesystem[]>({
44
max: 100,
45
ttl: 1000 * 30,
46
});
47
48
export async function getCloudFilesystems(
49
// give no options to get all file systems that YOU own across all your projects
50
opts: {
51
// id = specific on
52
id?: number;
53
// project_id = all in a given project (owned by anybody)
54
project_id?: string;
55
// if true, use cache and potentially return stale data (good for repeated calls in a list, etc.)
56
cache?: boolean;
57
} = {},
58
): Promise<CloudFilesystem[]> {
59
const key = `${opts.id}${opts.project_id}`;
60
if (opts.cache && cloudFilesystemCache.has(key)) {
61
return cloudFilesystemCache.get(key)!;
62
}
63
const filesystems = await api("compute/cloud-filesystem/get", {
64
id: opts.id,
65
project_id: opts.project_id,
66
});
67
// always save in cache
68
cloudFilesystemCache.set(key, filesystems);
69
return filesystems;
70
}
71
72
export async function getMetrics({
73
id,
74
limit,
75
offset,
76
}: {
77
id: number;
78
limit?: number;
79
offset?: number;
80
}) {
81
return await api("compute/cloud-filesystem/get-metrics", {
82
cloud_filesystem_id: id,
83
limit,
84
offset,
85
});
86
}
87
88