|
| 1 | +import dayjs from "dayjs"; |
| 2 | +import { apiClient } from "../apiClient.ts"; |
| 3 | +import { GPUS_PER_NODE } from "../lib/constants.ts"; |
| 4 | +import type { components } from "../schema.ts"; |
| 5 | +import { logAndQuit, logSessionTokenExpiredAndQuit } from "./errors.ts"; |
| 6 | +import { parseStartDateOrNow, roundDateUpToNextMinute } from "./units.ts"; |
| 7 | + |
| 8 | +export function getPricePerGpuHourFromQuote( |
| 9 | + quote: Pick<NonNullable<Quote>, "start_at" | "end_at" | "price" | "quantity">, |
| 10 | +) { |
| 11 | + const startTimeOrNow = parseStartDateOrNow(quote.start_at); |
| 12 | + |
| 13 | + // from the market's perspective, "NOW" means at the beginning of the next minute. |
| 14 | + // when the order duration is very short, this can cause the rate to be computed incorrectly |
| 15 | + // if we implicitly assume it to mean `new Date()`. |
| 16 | + const coercedStartTime = |
| 17 | + startTimeOrNow === "NOW" |
| 18 | + ? roundDateUpToNextMinute(new Date()) |
| 19 | + : startTimeOrNow; |
| 20 | + const durationSeconds = dayjs(quote.end_at).diff(dayjs(coercedStartTime)); |
| 21 | + const durationHours = durationSeconds / 3600 / 1000; |
| 22 | + |
| 23 | + return quote.price / GPUS_PER_NODE / quote.quantity / durationHours; |
| 24 | +} |
| 25 | + |
| 26 | +export async function getQuote(options: { |
| 27 | + instanceType?: string; |
| 28 | + quantity: number; |
| 29 | + minStartTime: Date | "NOW"; |
| 30 | + maxStartTime: Date | "NOW"; |
| 31 | + minDurationSeconds: number; |
| 32 | + maxDurationSeconds: number; |
| 33 | + cluster?: string; |
| 34 | + colocateWith?: string; |
| 35 | +}) { |
| 36 | + const api = await apiClient(); |
| 37 | + |
| 38 | + const params = { |
| 39 | + query: { |
| 40 | + side: "buy", |
| 41 | + instance_type: options.instanceType, |
| 42 | + quantity: options.quantity, |
| 43 | + min_start_date: |
| 44 | + options.minStartTime === "NOW" |
| 45 | + ? ("NOW" as const) |
| 46 | + : options.minStartTime.toISOString(), |
| 47 | + max_start_date: |
| 48 | + options.maxStartTime === "NOW" |
| 49 | + ? ("NOW" as const) |
| 50 | + : options.maxStartTime.toISOString(), |
| 51 | + min_duration: options.minDurationSeconds, |
| 52 | + max_duration: options.maxDurationSeconds, |
| 53 | + cluster: options.cluster, |
| 54 | + colocate_with: options.colocateWith, |
| 55 | + }, |
| 56 | + } as const; |
| 57 | + |
| 58 | + const { data, error, response } = await api.GET("/v0/quote", { |
| 59 | + params, |
| 60 | + // timeout after 600 seconds |
| 61 | + signal: AbortSignal.timeout(600 * 1000), |
| 62 | + }); |
| 63 | + |
| 64 | + if (!response.ok) { |
| 65 | + switch (response.status) { |
| 66 | + case 400: |
| 67 | + return logAndQuit(`Bad Request: ${JSON.stringify(error, null, 2)}`); |
| 68 | + case 401: |
| 69 | + return await logSessionTokenExpiredAndQuit(); |
| 70 | + case 500: |
| 71 | + return logAndQuit( |
| 72 | + `Failed to get quote: ${JSON.stringify(error, null, 2)}`, |
| 73 | + ); |
| 74 | + default: |
| 75 | + return logAndQuit(`Failed to get quote: ${response.statusText}`); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (!data) { |
| 80 | + return logAndQuit( |
| 81 | + `Failed to get quote: Unexpected response from server: ${response}`, |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + if (!data.quote) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + return { |
| 90 | + ...data.quote, |
| 91 | + price: Number(data.quote.price), |
| 92 | + quantity: Number(data.quote.quantity), |
| 93 | + start_at: data.quote.start_at, |
| 94 | + end_at: data.quote.end_at, |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +export type Quote = components["schemas"]["quoter_ApiQuoteDetails"] | null; |
0 commit comments