From 0ce07c88f8b182e6b55115edd0abf67db8301ed3 Mon Sep 17 00:00:00 2001 From: bennykok Date: Sat, 27 Jan 2024 18:06:48 +0800 Subject: [PATCH] fix: adding more time log for the run status --- web/src/app/(app)/api/update-run/route.ts | 172 ++++++++++++---------- web/src/components/RunDisplay.tsx | 14 +- web/src/lib/getRelativeTime.tsx | 18 ++- web/src/server/createRun.ts | 8 +- web/src/server/findAllRuns.tsx | 4 + 5 files changed, 128 insertions(+), 88 deletions(-) diff --git a/web/src/app/(app)/api/update-run/route.ts b/web/src/app/(app)/api/update-run/route.ts index 1dddf8c..c80f0ae 100644 --- a/web/src/app/(app)/api/update-run/route.ts +++ b/web/src/app/(app)/api/update-run/route.ts @@ -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, + }, + ); + } } diff --git a/web/src/components/RunDisplay.tsx b/web/src/components/RunDisplay.tsx index 6446f9b..3d980ca 100644 --- a/web/src/components/RunDisplay.tsx +++ b/web/src/components/RunDisplay.tsx @@ -29,7 +29,12 @@ export async function RunDisplay({ - {run.number} + + + {run.number} + {run.id} + + {run.machine?.name} @@ -46,7 +51,12 @@ export async function RunDisplay({ {getDuration(run.duration)} -
Cold start: {getDuration(run.cold_start_duration)}
+
+ Serverless latency: {getDuration(run.comfy_deploy_cold_start)} +
+
+ GPU Cold start: {getDuration(run.cold_start_duration)} +
Run duration: {getDuration(run.run_duration)}
diff --git a/web/src/lib/getRelativeTime.tsx b/web/src/lib/getRelativeTime.tsx index 3e7c31d..8a8fc97 100644 --- a/web/src/lib/getRelativeTime.tsx +++ b/web/src/lib/getRelativeTime.tsx @@ -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) { diff --git a/web/src/server/createRun.ts b/web/src/server/createRun.ts index 02bbf6e..c7ac72c 100644 --- a/web/src/server/createRun.ts +++ b/web/src/server/createRun.ts @@ -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 = { diff --git a/web/src/server/findAllRuns.tsx b/web/src/server/findAllRuns.tsx index 3375847..77be9d1 100644 --- a/web/src/server/findAllRuns.tsx +++ b/web/src/server/findAllRuns.tsx @@ -27,6 +27,10 @@ export async function findAllRuns({ sql`(extract(epoch from ended_at) - extract(epoch from created_at))`.as( "duration", ), + comfy_deploy_cold_start: + sql`(extract(epoch from queued_at) - extract(epoch from created_at))`.as( + "cold_start_duration", + ), cold_start_duration: sql`(extract(epoch from started_at) - extract(epoch from queued_at))`.as( "cold_start_duration",