Skip to content

Commit a5585ca

Browse files
committed
perf: remove sf extend, code reformatting
1 parent c4ccb43 commit a5585ca

15 files changed

Lines changed: 43 additions & 149 deletions

File tree

src/helpers/format-duration.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import {
77
} from "date-fns";
88
import dayjs, { type Dayjs } from "dayjs";
99
import utc from "dayjs/plugin/utc";
10+
import duration from "dayjs/plugin/duration";
1011
import { formatDateRange } from "little-date";
1112

1213
dayjs.extend(utc);
14+
dayjs.extend(duration);
1315

1416
const shortenAmPm = (text: string): string => {
1517
const shortened = (text || "").replace(/ AM/g, "am").replace(/ PM/g, "pm");
@@ -133,3 +135,30 @@ export const formatDateAsUTC = (date: Dayjs): string => {
133135

134136
return `${utcDate.format("MMM D")}, ${timeStr} UTC`;
135137
};
138+
139+
/**
140+
* Formats a duration in milliseconds to a compact string like "1d2h30m".
141+
*/
142+
export function formatDuration(ms: number) {
143+
const d = dayjs.duration(ms);
144+
145+
const years = Math.floor(d.asYears());
146+
const weeks = Math.floor(d.asWeeks()) % 52;
147+
const days = d.days();
148+
const hours = d.hours();
149+
const minutes = d.minutes();
150+
const seconds = d.seconds();
151+
const milliseconds = d.milliseconds();
152+
153+
let result = "";
154+
155+
if (years > 0) result += `${years}y`;
156+
if (weeks > 0) result += `${weeks}w`;
157+
if (days > 0) result += `${days}d`;
158+
if (hours > 0) result += `${hours}h`;
159+
if (minutes > 0) result += `${minutes}m`;
160+
if (seconds > 0) result += `${seconds}s`;
161+
if (milliseconds > 0) result += `${milliseconds}ms`;
162+
163+
return result || "0ms";
164+
}

src/helpers/quote.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import dayjs from "dayjs";
22
import { apiClient } from "../apiClient.ts";
33
import { GPUS_PER_NODE } from "../lib/constants.ts";
4+
import type { components } from "../schema.ts";
45
import { logAndQuit, logSessionTokenExpiredAndQuit } from "./errors.ts";
56
import { parseStartDateOrNow, roundDateUpToNextMinute } from "./units.ts";
67

@@ -22,7 +23,7 @@ export function getPricePerGpuHourFromQuote(
2223
return quote.price / GPUS_PER_NODE / quote.quantity / durationHours;
2324
}
2425

25-
type QuoteOptions = {
26+
export async function getQuote(options: {
2627
instanceType?: string;
2728
quantity: number;
2829
minStartTime: Date | "NOW";
@@ -31,9 +32,7 @@ type QuoteOptions = {
3132
maxDurationSeconds: number;
3233
cluster?: string;
3334
colocateWith?: string;
34-
};
35-
36-
export async function getQuote(options: QuoteOptions) {
35+
}) {
3736
const api = await apiClient();
3837

3938
const params = {
@@ -96,21 +95,4 @@ export async function getQuote(options: QuoteOptions) {
9695
};
9796
}
9897

99-
export type Quote =
100-
| {
101-
price: number;
102-
quantity: number;
103-
start_at: string;
104-
end_at: string;
105-
instance_type: string;
106-
zone?: string;
107-
}
108-
| {
109-
price: number;
110-
quantity: number;
111-
start_at: string;
112-
end_at: string;
113-
contract_id: string;
114-
zone?: string;
115-
}
116-
| null;
98+
export type Quote = components["schemas"]["quoter_ApiQuoteDetails"] | null;

src/helpers/units.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import dayjs from "dayjs";
55
import timezone from "dayjs/plugin/timezone";
66
import utc from "dayjs/plugin/utc";
77
import type { Nullable } from "../types/empty.ts";
8-
import { formatDate, formatDateAsUTC } from "./format-date.ts";
8+
import { formatDate, formatDateAsUTC } from "./format-time.ts";
99

1010
dayjs.extend(utc);
1111
dayjs.extend(timezone);

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { getAppBanner } from "./lib/app-banner.ts";
1919
import { registerBalance } from "./lib/balance.ts";
2020
import { registerContracts } from "./lib/contracts/index.tsx";
2121
import { registerDev } from "./lib/dev.ts";
22-
import { registerExtend } from "./lib/extend/index.tsx";
2322
import { registerLogin } from "./lib/login.ts";
2423
import { registerMe } from "./lib/me.ts";
2524
import { registerNodes } from "./lib/nodes/index.ts";
@@ -44,7 +43,6 @@ async function main() {
4443

4544
// commands
4645
registerLogin(program);
47-
registerExtend(program);
4846
registerContracts(program);
4947
registerBalance(program);
5048
registerTokens(program);

src/lib/extend/index.tsx

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/lib/nodes/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import utc from "dayjs/plugin/utc";
1212
import ora from "ora";
1313

1414
import { logAndQuit } from "../../helpers/errors.ts";
15-
import { formatDate } from "../../helpers/format-date.ts";
15+
import { formatDate } from "../../helpers/format-time.ts";
1616
import { getPricePerGpuHourFromQuote, getQuote } from "../../helpers/quote.ts";
1717
import {
1818
parseStartDate,

src/lib/nodes/image/list.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ora from "ora";
66

77
import { getAuthToken } from "../../../helpers/config.ts";
88
import { logAndQuit } from "../../../helpers/errors.ts";
9-
import { formatDate } from "../../../helpers/format-date.ts";
9+
import { formatDate } from "../../../helpers/format-time.ts";
1010
import { handleNodesError, nodesClient } from "../../../nodesClient.ts";
1111

1212
const list = new Command("list")

src/lib/nodes/image/show.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import timezone from "dayjs/plugin/timezone";
88
import utc from "dayjs/plugin/utc";
99
import { Box, render, Text } from "ink";
1010
import Link from "ink-link";
11-
import { formatDate } from "../../../helpers/format-date.ts";
11+
import { formatDate } from "../../../helpers/format-time.ts";
1212
import { handleNodesError, nodesClient } from "../../../nodesClient.ts";
1313
import { Row } from "../../Row.tsx";
1414

src/lib/nodes/list.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { logAndQuit } from "../../helpers/errors.ts";
1515
import {
1616
formatDate,
1717
formatNullableDateRange,
18-
} from "../../helpers/format-date.ts";
18+
} from "../../helpers/format-time.ts";
1919
import { handleNodesError, nodesClient } from "../../nodesClient.ts";
2020
import { Row } from "../Row.tsx";
2121
import {

0 commit comments

Comments
 (0)