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/cost.ts
Views: 687
1
/*
2
Compute cost per hour of a configuration in a given state.
3
*/
4
5
import type {
6
State,
7
Configuration,
8
} from "@cocalc/util/db-schema/compute-servers";
9
import { getGoogleCloudPriceData, getHyperstackPriceData } from "./api";
10
import computeGoogleCloudCost from "@cocalc/util/compute/cloud/google-cloud/compute-cost";
11
import computeHyperstackCost from "@cocalc/util/compute/cloud/hyperstack/compute-cost";
12
13
export default async function costPerHour({
14
configuration,
15
state,
16
}: {
17
configuration: Configuration;
18
state: State;
19
}): Promise<number> {
20
if (state == "deprovisioned") {
21
// always a cost of 0 in this state
22
return 0;
23
}
24
if (configuration.cloud == "onprem") {
25
// free for now -- but we will charge, e.g., for bandwidth and management when
26
// this has a management layer
27
return 0;
28
}
29
if (state != "running" && state != "off" && state != "suspended") {
30
throw Error("state must be stable");
31
}
32
if (configuration.cloud == "google-cloud") {
33
const priceData = await getGoogleCloudPriceData();
34
return computeGoogleCloudCost({ configuration, priceData, state });
35
} else if (configuration.cloud == "hyperstack") {
36
const priceData = await getHyperstackPriceData();
37
return computeHyperstackCost({ configuration, priceData, state });
38
} else {
39
throw Error(
40
`cost computation not yet implemented for '${configuration.cloud}'`,
41
);
42
}
43
}
44
45