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/util/consts/site-license.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { sortBy } from "lodash";
7
import { SiteLicenseQuota } from "../types/site-licenses";
8
9
type Keys = NonNullable<SiteLicenseQuota["idle_timeout"]>;
10
11
// Site Licenses related constants
12
// the license timeouts combined with the default quota (either hardcoded or the one from the site_settings)
13
// hence 30 minutes + 30 minutes default result in 30 minutes, and the same for "medium" and "day"
14
// @see DEFAULT_QUOTAS.mintime in src/packages/util/upgrade-spec.js
15
// medium and day imply member hosting
16
export const LicenseIdleTimeouts: {
17
[key in Keys]: {
18
mins: number;
19
label: string;
20
labelShort: string; // same as "label", but shorter
21
priceFactor: number;
22
requireMemberhosting?: boolean; // if true, require member hosting
23
};
24
} = {
25
short: {
26
mins: 30,
27
label: "30 minutes",
28
labelShort: "30min",
29
priceFactor: 1,
30
},
31
medium: { mins: 2 * 60, label: "2 hours", labelShort: "2h", priceFactor: 2 },
32
day: {
33
mins: 24 * 60,
34
label: "1 day",
35
labelShort: "1d",
36
priceFactor: 4,
37
requireMemberhosting: true,
38
},
39
} as const;
40
41
export const LicenseIdleTimeoutsKeysOrdered = sortBy(
42
Object.keys(LicenseIdleTimeouts),
43
(v) => LicenseIdleTimeouts[v].mins
44
) as Readonly<Keys[]>;
45
46
export function requiresMemberhosting(key?: Uptime | string): boolean {
47
if (key == null) return false;
48
if (key == "always_running") return true;
49
return LicenseIdleTimeouts[key]?.requireMemberhosting ?? false;
50
}
51
52
export type Uptime = Keys | "always_running";
53
54
export function untangleUptime(uptime?: Uptime): {
55
always_running: boolean;
56
idle_timeout: Keys;
57
} {
58
if (uptime == null) {
59
// Let's just support this and have it default to short, since obviously
60
// by explicitly allowing uptime? via typescript, we can't even tell there
61
// is a problem from typescript errors. See
62
// https://github.com/sagemathinc/cocalc/issues/6257
63
// for how this bug causes a lot of trouble.
64
return { always_running: false, idle_timeout: "short" };
65
}
66
if (uptime == "always_running") {
67
return { always_running: true, idle_timeout: "day" };
68
}
69
return { always_running: false, idle_timeout: uptime };
70
}
71
72
export function displaySiteLicense(uptime: Uptime) {
73
if (uptime == "always_running") {
74
return "Always Running";
75
} else {
76
return LicenseIdleTimeouts[uptime].label;
77
}
78
}
79
80