Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/compute/cloud/hyperstack/compute-cost.ts
Views: 687
import type { HyperstackConfiguration } from "@cocalc/util/db-schema/compute-servers";1import type { HyperstackPriceData } from "./pricing";2import { optionKey, markup } from "./pricing";34// This is what we intend to use and charge for the boot disk.5export const BOOT_DISK_SIZE_GB = 50;67interface Options {8configuration: HyperstackConfiguration;9// output of getData from this package -- https://www.npmjs.com/package/@cocalc/gcloud-pricing-calculator10// except that package is backend only (it caches to disk), so data is obtained via an api, then used here.11priceData: HyperstackPriceData;12state?: "running" | "off" | "suspended";13}1415export default function computeCost({16configuration,17priceData,18state = "running",19}: Options): number {20if (priceData == null) {21throw Error("priceData must not be null");22}23if (state == "off") {24return computeOffCost({ configuration, priceData });25} else if (state == "suspended") {26throw Error("Hyperstack does not support suspended");27} else if (state == "running") {28return computeRunningCost({ configuration, priceData });29} else {30throw Error(`computing cost for state "${state}" not implemented`);31}32}3334function throwCostNotKnownError(configuration) {35const { flavor_name, region_name } = configuration ?? {};36throw Error(37`no price known for flavor_name=${flavor_name}, region_name=${region_name}`,38);39}4041export function computeDiskCost({ configuration, priceData }): number {42if (priceData == null) {43throwCostNotKnownError(configuration);44}45return (configuration?.diskSizeGb ?? 10) * priceData.ssd_cost_per_hour;46}4748// export function computeBootVolumeCost({ configuration, priceData }): number {49// if (priceData == null) {50// throwCostNotKnownError(configuration);51// }52// return BOOT_DISK_SIZE_GB * priceData.ssd_cost_per_hour;53// }5455export function computeVolumeCost(opts) {56return computeDiskCost(opts);57}5859// For the cocalc integration "off" means that we 100% delete the VM60// and *ONLY* keep the associated storage volumes.61function computeOffCost({ configuration, priceData }) {62return markup({63cost: computeVolumeCost({ configuration, priceData }),64priceData,65});66}6768function computeRunningCost({ configuration, priceData }) {69const data = priceData?.options[optionKey(configuration)];70if (data == null) {71throwCostNotKnownError(configuration);72}73// data.cost_per_hour *includes* GPUs, any ephemeral storage and external ip (assume: not cpu only!)74const cost =75data.cost_per_hour + computeVolumeCost({ configuration, priceData });76return markup({ cost, priceData });77}787980