Skip to content

Commit

Permalink
use new api correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaque committed Sep 12, 2024
1 parent 3c4a656 commit c307993
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { registerOrders } from "./lib/orders";
import { registerSell } from "./lib/sell";
import { registerSSH } from "./lib/ssh";
import { registerTokens } from "./lib/tokens";
import { registerUp } from "./lib/updown";
import { registerDown, registerUp } from "./lib/updown";
import { registerUpgrade } from "./lib/upgrade";

const program = new Command();
Expand All @@ -34,6 +34,7 @@ registerBalance(program);
registerTokens(program);
registerUpgrade(program);
registerUp(program);
registerDown(program);

// (development commands)
registerDev(program);
Expand Down
1 change: 1 addition & 0 deletions src/lib/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function printAsTable(orders: Array<HydratedOrder>) {
const duration = formatDuration(
dayjs(order.end_at).diff(dayjs(startDate), "ms"),
);
console.log(order.price);
table.push([
order.id,
order.side,
Expand Down
59 changes: 54 additions & 5 deletions src/lib/updown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ export function registerUp(program: Command) {
});
}

export function registerDown(program: Command) {
const cmd = program
.command("down")
.description("Turn off nodes")
.option("-t, --type <type>", "Specify the type of node", "h100i");

cmd.action(async (options) => {
down(options);
});
}

const DEFAULT_PRICE_PER_NODE_HOUR_IN_CENTICENTS = 2.65 * 8 * 10_000;

async function getDefaultProcurementOptions(props: {
Expand Down Expand Up @@ -106,13 +117,13 @@ function confirmPlaceOrderMessage(options: {

const timeDescription = `starting ${c.green("ASAP")} until you turn it off`;

const topLine = `Turn on ${totalNodesLabel} ${instanceTypeLabel} ${nodesLabel} continuously for ${c.green(formatDuration(durationInMilliseconds))} ${timeDescription}`;
const topLine = `Turning on ${totalNodesLabel} ${instanceTypeLabel} ${nodesLabel} continuously for ${c.green(formatDuration(durationInMilliseconds))} ${timeDescription}`;

const dollarsLabel = c.green(
centicentsToDollarsFormatted(options.pricePerNodeHourInCenticents * options.durationHours * options.n),
centicentsToDollarsFormatted(options.pricePerNodeHourInCenticents),
);

const priceLine = `\n Pay a minimum of ${dollarsLabel}?`;
const priceLine = `\n Pay ${dollarsLabel} per node hour?`;

return `${topLine}\n${priceLine} `;
}
Expand Down Expand Up @@ -187,7 +198,7 @@ async function up(props: {
quantity: n,

// we only update the duration & price if it's set
block_duration_in_hours: props.duration ? durationHours : undefined,
min_duration_in_hours: props.duration ? durationHours : undefined,
max_price_per_node_hour: props.price ? pricePerNodeHourInCenticents : undefined,
},
});
Expand All @@ -200,7 +211,7 @@ async function up(props: {
instance_type: type,
quantity: n,
max_price_per_node_hour: pricePerNodeHourInCenticents,
block_duration_in_hours: Math.max(durationHours, 1),
min_duration_in_hours: Math.max(durationHours, 1),
},
});

Expand All @@ -212,3 +223,41 @@ async function up(props: {
return res.data;
}

async function down(props: {
type: string;
}) {
const client = await apiClient();

// check if there's already a procurement like this
const procurements = await client.GET("/v0/procurements");
if (!procurements.response.ok) {
console.error(procurements.error?.message, procurements.error?.details);
throw new Error("Failed to list procurements");
}

const procurement = procurements.data?.data.find((p: any) => p.instance_group === props.type);

if (!procurement) {
console.error(`No procurement found for ${props.type}`);
return;
}

const res = await client.PUT("/v0/procurements/{id}", {
params: {
path: {
id: procurement.id,
},
},
body: {
quantity: 0,
block_duration_in_hours: 0,
},
});

if (!res.response.ok) {
console.error(res.error?.message, res.error?.details);
throw new Error("Failed to turn off nodes");
}

return res.data;
}

0 comments on commit c307993

Please sign in to comment.