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