fix: adding more time log for the run status

This commit is contained in:
bennykok 2024-01-27 18:06:48 +08:00
parent c7727fc1be
commit 0ce07c88f8
5 changed files with 128 additions and 88 deletions

View File

@ -16,96 +16,114 @@ import { z } from "zod";
const Request = z.object({
run_id: z.string(),
status: WorkflowRunStatusSchema.optional(),
time: z.date().optional(),
time: z.coerce.date().optional(),
output_data: z.any().optional(),
});
export async function POST(request: Request) {
const [data, error] = await parseDataSafe(Request, request);
if (!data || error) return error;
try {
const [data, error] = await parseDataSafe(Request, request);
const { run_id, status, time, output_data } = data;
if (!data || error) return error;
if (status == "started" && time != undefined) {
// It successfully started, update the started_at time
await db
.update(workflowRunsTable)
.set({
started_at: time,
})
.where(eq(workflowRunsTable.id, run_id));
}
const { run_id, status, time, output_data } = data;
if (status == "queued" && time != undefined) {
// It successfully started, update the started_at time
await db
.update(workflowRunsTable)
.set({
queued_at: time,
})
.where(eq(workflowRunsTable.id, run_id));
}
if (status == "started" && time != undefined) {
// It successfully started, update the started_at time
await db
.update(workflowRunsTable)
.set({
started_at: time,
})
.where(eq(workflowRunsTable.id, run_id));
}
if (output_data) {
const workflow_run_output = await db.insert(workflowRunOutputs).values({
run_id: run_id,
data: output_data,
});
} else if (status) {
const [workflow_run] = await db
.update(workflowRunsTable)
.set({
status: status,
ended_at:
status === "success" || status === "failed" ? new Date() : null,
})
.where(eq(workflowRunsTable.id, run_id))
.returning();
if (status == "queued" && time != undefined) {
// It successfully started, update the started_at time
await db
.update(workflowRunsTable)
.set({
queued_at: time,
})
.where(eq(workflowRunsTable.id, run_id));
}
// Need to filter out only comfy deploy serverless
// Also multiply with the gpu selection
if (workflow_run.machine_type == "comfy-deploy-serverless") {
if (
(status === "success" || status === "failed") &&
workflow_run.user_id
) {
const sub = await getCurrentPlan({
user_id: workflow_run.user_id,
org_id: workflow_run.org_id,
});
if (output_data) {
const workflow_run_output = await db.insert(workflowRunOutputs).values({
run_id: run_id,
data: output_data,
});
} else if (status) {
const [workflow_run] = await db
.update(workflowRunsTable)
.set({
status: status,
ended_at:
status === "success" || status === "failed" ? new Date() : null,
})
.where(eq(workflowRunsTable.id, run_id))
.returning();
if (sub && sub.subscription_item_api_id && workflow_run.ended_at) {
let durationInSec = Math.abs(
(workflow_run.ended_at.getTime() -
workflow_run.created_at.getTime()) /
1000,
);
durationInSec = Math.ceil(durationInSec);
switch (workflow_run.gpu) {
case "A100":
durationInSec *= 7;
break;
case "A10G":
durationInSec *= 4;
break;
// Need to filter out only comfy deploy serverless
// Also multiply with the gpu selection
if (workflow_run.machine_type == "comfy-deploy-serverless") {
if (
(status === "success" || status === "failed") &&
workflow_run.user_id
) {
const sub = await getCurrentPlan({
user_id: workflow_run.user_id,
org_id: workflow_run.org_id,
});
if (sub && sub.subscription_item_api_id && workflow_run.ended_at) {
let durationInSec = Math.abs(
(workflow_run.ended_at.getTime() -
workflow_run.created_at.getTime()) /
1000,
);
durationInSec = Math.ceil(durationInSec);
switch (workflow_run.gpu) {
case "A100":
durationInSec *= 7;
break;
case "A10G":
durationInSec *= 4;
break;
}
await stripe.subscriptionItems.createUsageRecord(
sub.subscription_item_api_id,
{
quantity: durationInSec,
},
);
}
await stripe.subscriptionItems.createUsageRecord(
sub.subscription_item_api_id,
{
quantity: durationInSec,
},
);
}
}
}
}
return NextResponse.json(
{
message: "success",
},
{
status: 200,
},
);
return NextResponse.json(
{
message: "success",
},
{
status: 200,
},
);
} catch (error: unknown) {
console.log("An error here");
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
console.log(errorMessage);
return NextResponse.json(
{
error: errorMessage,
},
{
status: 500,
},
);
}
}

View File

@ -29,7 +29,12 @@ export async function RunDisplay({
<Dialog>
<DialogTrigger asChild className="appearance-none hover:cursor-pointer">
<TableRow>
<TableCell>{run.number}</TableCell>
<TableCell>
<Tooltip>
<TooltipTrigger>{run.number}</TooltipTrigger>
<TooltipContent>{run.id}</TooltipContent>
</Tooltip>
</TableCell>
<TableCell className="font-medium truncate">
{run.machine?.name}
</TableCell>
@ -46,7 +51,12 @@ export async function RunDisplay({
<Tooltip>
<TooltipTrigger>{getDuration(run.duration)}</TooltipTrigger>
<TooltipContent>
<div>Cold start: {getDuration(run.cold_start_duration)}</div>
<div>
Serverless latency: {getDuration(run.comfy_deploy_cold_start)}
</div>
<div>
GPU Cold start: {getDuration(run.cold_start_duration)}
</div>
<div>Run duration: {getDuration(run.run_duration)}</div>
</TooltipContent>
</Tooltip>

View File

@ -12,13 +12,21 @@ export function getRelativeTime(time: string | Date | null | undefined) {
}
function formatDuration(seconds: number) {
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const remainingSeconds = seconds % 60;
if (minutes > 0) {
return `${minutes}.${remainingSeconds} mins`;
} else {
return `${remainingSeconds.toFixed(1)} secs`;
let result = "";
if (hours > 0) {
result += `${hours} hrs `;
}
if (minutes > 0) {
result += `${minutes} mins `;
}
if (remainingSeconds > 0) {
result += `${remainingSeconds.toFixed(1)} secs`;
}
return result.trim();
}
export function getDuration(durationInSecs: number) {

View File

@ -148,14 +148,14 @@ export const createRun = withServerPromise(
body: JSON.stringify(_data),
cache: "no-store",
});
console.log(___result);
// console.log(___result);
if (!___result.ok)
throw new Error(
`Error creating run, ${
___result.statusText
} ${await ___result.text()}`,
);
console.log(_data, ___result);
// console.log(_data, ___result);
break;
case "runpod-serverless":
const data = {
@ -182,14 +182,14 @@ export const createRun = withServerPromise(
body: JSON.stringify(data),
cache: "no-store",
});
console.log(__result);
// console.log(__result);
if (!__result.ok)
throw new Error(
`Error creating run, ${
__result.statusText
} ${await __result.text()}`,
);
console.log(data, __result);
// console.log(data, __result);
break;
case "classic":
const body = {

View File

@ -27,6 +27,10 @@ export async function findAllRuns({
sql<number>`(extract(epoch from ended_at) - extract(epoch from created_at))`.as(
"duration",
),
comfy_deploy_cold_start:
sql<number>`(extract(epoch from queued_at) - extract(epoch from created_at))`.as(
"cold_start_duration",
),
cold_start_duration:
sql<number>`(extract(epoch from started_at) - extract(epoch from queued_at))`.as(
"cold_start_duration",