Skip to content

Commit

Permalink
Merge branch 'main' into bug-fixes-2
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnPhamous authored Feb 7, 2025
2 parents 57e3ff2 + 4ba6dd1 commit e668595
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"release": "deno run --allow-all src/scripts/release.ts",
"prod": "deno run --allow-all src/index.ts",
"schema": "npx openapi-typescript https://api.sfcompute.com/docs/json -o src/schema.ts",
"lint": "prettier --write ."
"lint": "prettier --write .",
"test": "deno test"
},
"dependencies": {
"@inquirer/prompts": "^5.1.2",
Expand Down Expand Up @@ -43,5 +44,5 @@
"peerDependencies": {
"typescript": "^5.6.2"
},
"version": "0.1.33"
"version": "0.1.32"
}
41 changes: 41 additions & 0 deletions src/helpers/duration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import parseDurationLibrary from "parse-duration";

export const parseDurationArgument = (rawDuration: string | undefined) => {
if (rawDuration === undefined) {
return undefined;
}
const duration = rawDuration?.replaceAll("_", "").replace(/\s+/g, "");
if (
duration.length === 0 ||
duration.startsWith("-") ||
duration.includes(".")
) {
return undefined;
}

// For backwards compatibility, we want to support users passing in seconds directly.
// Some of the CLI use to support seconds directly, now we support durations strings.
const attemptedParseAsNumber = Number.parseInt(duration);

if (
!Number.isNaN(attemptedParseAsNumber) &&
duration === attemptedParseAsNumber.toString()
) {
return attemptedParseAsNumber;
}

if (Number.isNaN(attemptedParseAsNumber)) {
// Ensure it's a positive integer and matches exactly
if (duration !== attemptedParseAsNumber.toString()) {
return undefined;
}
}

const parsed = parseDurationLibrary(duration.toLowerCase());
if (parsed == null || parsed === undefined) {
return undefined;
}

// Convert from milliseconds to seconds
return parsed / 1000;
};
41 changes: 41 additions & 0 deletions src/helpers/test/duration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { parseDurationArgument } from "../duration.ts";

Deno.test("parseDurationArgument", () => {
const testCases: [string, number | undefined][] = [
["", undefined],
["0", 0],
["100", 100],
["3600", 3600],
["1w", 604800],
["1d", 86400],
["1hr", 3600],
["30s", 30],
["4s", 4],
["0s", 0],
["invalid", undefined],
["123abc", undefined],
["s30", undefined],
["1H", 3600],
["1HR", 3600],
["1Hour", 3600],
["1W", 604800],
["1D", 86400],
["1h30m", 5400],
["1d 12h", 129600],
["2w3d", 1468800],
["1.5h", undefined],
["2.5d", undefined],
["-1h", undefined],
["-30s", undefined],
["-100", undefined],
["1 h", 3600],
["2 d", 172800],
["1_d", 86400],
];

for (const [input, expected] of testCases) {
const result = parseDurationArgument(input);
assertEquals(result, expected);
}
});
8 changes: 7 additions & 1 deletion src/lib/orders/OrderDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function orderDetails(order: HydratedOrder) {
const duration = dayjs(order.end_at).diff(order.start_at);
const durationInHours = duration === 0 ? 1 : duration / 1000 / 60 / 60;
const pricePerGPUHour =
(order.price * order.quantity) / GPUS_PER_NODE / durationInHours / 100;
order.price / (order.quantity * durationInHours * GPUS_PER_NODE) / 100;
const durationFormatted = formatDuration(duration);

let executedPricePerGPUHour;
Expand Down Expand Up @@ -159,6 +159,12 @@ export function OrderDisplay(props: {
}
});

useEffect(() => {
if (props.orders.length === 0) {
process.exit(0);
}
}, [props.orders]);

if (props.orders.length === 0) {
return (
<Box flexDirection="column" gap={1} paddingBottom={1}>
Expand Down
8 changes: 6 additions & 2 deletions src/lib/orders/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import duration from "npm:[email protected]/plugin/duration.js";
import relativeTime from "npm:[email protected]/plugin/relativeTime.js";
import React from "react";
import { getAuthToken, isLoggedIn } from "../../helpers/config.ts";
import { parseDurationArgument } from "../../helpers/duration.ts";
import {
logAndQuit,
logLoginMessageAndQuit,
Expand Down Expand Up @@ -122,6 +123,9 @@ export function registerOrders(program: Command) {
.option("--offset <number>", "Offset the results (for pagination)")
.option("--json", "Output in JSON format")
.action(async options => {
const minDuration = parseDurationArgument(options.minDuration);
const maxDuration = parseDurationArgument(options.maxDuration);

const orders = await getOrders({
side: options.side,
instance_type: options.type,
Expand All @@ -132,8 +136,8 @@ export function registerOrders(program: Command) {
max_price: options.maxPrice,
min_start_date: options.minStart,
max_start_date: options.maxStart,
min_duration: options.minDuration,
max_duration: options.maxDuration,
min_duration: minDuration,
max_duration: maxDuration,
min_quantity: options.minQuantity,
max_quantity: options.maxQuantity,

Expand Down

0 comments on commit e668595

Please sign in to comment.