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/licenses/purchase/dedicated-price.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// define structure and prices of dedicated resources
7
// no sustained use discounts
8
// "quota" ends up in the license's quota field
9
10
import { AVG_MONTH_DAYS, AVG_YEAR_DAYS } from "@cocalc/util/consts/billing";
11
import { getDedicatedDiskKey, PRICES } from "@cocalc/util/upgrades/dedicated";
12
import type { DedicatedDisk, DedicatedVM } from "@cocalc/util/types/dedicated";
13
import dayjs from "dayjs";
14
15
interface Props {
16
dedicated_vm?: DedicatedVM;
17
dedicated_disk?: DedicatedDisk;
18
start?: Date;
19
end?: Date;
20
subscription: "monthly" | "yearly";
21
}
22
23
function getDuration({ start, end, subscription }): number {
24
if (start != null && end != null) {
25
// length of time in days -- not an integer in general!
26
return dayjs(end).diff(dayjs(start), "day", true);
27
} else if (subscription === "yearly") {
28
return AVG_YEAR_DAYS;
29
} else {
30
return AVG_MONTH_DAYS;
31
}
32
}
33
34
export function dedicatedPrice(info: Props): {
35
price: number;
36
monthly: number;
37
} {
38
const { dedicated_vm, dedicated_disk, subscription } = info;
39
40
const start = info.start ? new Date(info.start) : undefined;
41
const end = info.end ? new Date(info.end) : undefined;
42
43
const duration = getDuration({ start, end, subscription });
44
45
if (!!dedicated_vm) {
46
const info = PRICES.vms[dedicated_vm.machine];
47
if (info == null) {
48
throw new Error(`Dedicated VM "${dedicated_vm}" is not defined.`);
49
}
50
return {
51
price: Math.max(0.01, info.price_day * duration),
52
monthly: info.price_day * AVG_MONTH_DAYS,
53
};
54
} else if (!!dedicated_disk) {
55
//console.log(dedicated_disk);
56
const diskID = getDedicatedDiskKey(dedicated_disk);
57
const info = PRICES.disks[diskID];
58
if (info == null) {
59
throw new Error(`Dedicated Disk "${dedicated_disk}" is not defined.`);
60
}
61
return {
62
price: Math.max(0.01, info.price_day * duration),
63
monthly: info.price_day * AVG_MONTH_DAYS,
64
};
65
} else {
66
throw new Error("Neither VM nor Disk specified!");
67
}
68
}
69
70