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/licenses/store/compute-cost.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { AVG_MONTH_DAYS } from "@cocalc/util/consts/billing";6import {7compute_cost,8compute_cost_dedicated,9} from "@cocalc/util/licenses/purchase/compute-cost";10import type {11CostInputPeriod,12PurchaseInfo,13} from "@cocalc/util/licenses/purchase/types";14import { fixRange } from "@cocalc/util/licenses/purchase/purchase-info";15import { getDays } from "@cocalc/util/stripe/timecalcs";16import { PRICES } from "@cocalc/util/upgrades/dedicated";17import type { ComputeCostProps } from "@cocalc/util/upgrades/shopping";18import { CURRENT_VERSION } from "@cocalc/util/licenses/purchase/consts";1920function computeDedicatedDiskCost(21props: ComputeCostProps,22): CostInputPeriod | undefined {23if (props.type !== "disk") {24throw new Error("compute cost for disk only");25}26if (props.dedicated_disk == null)27throw new Error("missing props.dedicated_disk");28const { dedicated_disk } = props;29if (props.period != "monthly") throw new Error("period must be monthly");30if (dedicated_disk === false) throw new Error(`should not happen`);3132try {33return {34input: { ...props, subscription: props.period },35...compute_cost_dedicated({36dedicated_disk,37subscription: props.period,38}),39};40} catch (err) {41console.log(`problem calculating dedicated price: ${err}`);42}43}4445function computeDedicatedVMCost(46props: ComputeCostProps,47): CostInputPeriod | undefined {48if (props.type !== "vm") {49throw new Error("compute cost for VM only");50}51if (props.dedicated_vm == null) {52throw new Error("missing props.dedicated_vm");53}54const { range, dedicated_vm } = props;55const machine = dedicated_vm.machine;56if (range == null || range[0] == null || range[1] == null) return;57const price_day = PRICES.vms[machine]?.price_day;58if (price_day == null) return;59const days = getDays({ start: range[0], end: range[1] });60const price = days * price_day;61return {62cost: price,63cost_per_unit: price,64cost_per_project_per_month: AVG_MONTH_DAYS * price_day,65cost_sub_month: AVG_MONTH_DAYS * price_day,66cost_sub_year: 12 * AVG_MONTH_DAYS * price_day,67input: {68...props,69subscription: "no",70start: range[0] ?? new Date(),71end: range?.[1],72},73period: "range",74quantity: 1,75};76}7778function computeCashVoucherPrice(props: ComputeCostProps) {79if (props.type != "cash-voucher") {80throw Error("BUG");81}82const cost = props.amount;83return {84// a lot of this is mainly for typescript.85cost,86cost_per_unit: cost,87input: {88...props,89subscription: "no",90},91period: "range",92cost_per_project_per_month: 0,93cost_sub_month: 0,94cost_sub_year: 0,95quantity: 1,96} as const;97}9899export function computeCost(100props: ComputeCostProps,101): CostInputPeriod | undefined {102const type = props.type ?? "quota";103switch (type) {104case "cash-voucher":105return computeCashVoucherPrice(props);106107case "disk":108return computeDedicatedDiskCost(props);109110case "vm":111return computeDedicatedVMCost(props);112113case "quota":114default:115if (116props.type == "disk" ||117props.type == "vm" ||118props.type == "cash-voucher"119) {120throw Error("must be a quota upgrade license");121}122const {123user,124run_limit,125period,126range,127ram,128cpu,129disk,130always_running,131member,132uptime,133boost = false, // if true, allow "all zero" values and start at 0 USD134} = props;135136if (period == "range" && range?.[1] == null) {137return undefined;138}139140const input: PurchaseInfo = {141version: CURRENT_VERSION,142type: "quota",143user,144upgrade: "custom" as "custom",145quantity: run_limit,146subscription: (period == "range" ? "no" : period) as147| "no"148| "monthly"149| "yearly",150custom_ram: ram,151custom_dedicated_ram: 0,152custom_cpu: cpu,153custom_dedicated_cpu: 0,154custom_disk: disk,155custom_always_running: always_running,156custom_member: member,157custom_uptime: uptime,158boost,159...fixRange(range, period),160};161return {162...compute_cost(input),163input,164period,165};166}167}168169170