fix: adding more time log for the run status
This commit is contained in:
		
							parent
							
								
									c7727fc1be
								
							
						
					
					
						commit
						0ce07c88f8
					
				@ -16,96 +16,114 @@ import { z } from "zod";
 | 
				
			|||||||
const Request = z.object({
 | 
					const Request = z.object({
 | 
				
			||||||
  run_id: z.string(),
 | 
					  run_id: z.string(),
 | 
				
			||||||
  status: WorkflowRunStatusSchema.optional(),
 | 
					  status: WorkflowRunStatusSchema.optional(),
 | 
				
			||||||
  time: z.date().optional(),
 | 
					  time: z.coerce.date().optional(),
 | 
				
			||||||
  output_data: z.any().optional(),
 | 
					  output_data: z.any().optional(),
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function POST(request: Request) {
 | 
					export async function POST(request: Request) {
 | 
				
			||||||
  const [data, error] = await parseDataSafe(Request, request);
 | 
					  try {
 | 
				
			||||||
  if (!data || error) return error;
 | 
					    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) {
 | 
					    const { run_id, status, time, output_data } = data;
 | 
				
			||||||
    // It successfully started, update the started_at time
 | 
					 | 
				
			||||||
    await db
 | 
					 | 
				
			||||||
      .update(workflowRunsTable)
 | 
					 | 
				
			||||||
      .set({
 | 
					 | 
				
			||||||
        started_at: time,
 | 
					 | 
				
			||||||
      })
 | 
					 | 
				
			||||||
      .where(eq(workflowRunsTable.id, run_id));
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (status == "queued" && time != undefined) {
 | 
					    if (status == "started" && time != undefined) {
 | 
				
			||||||
    // It successfully started, update the started_at time
 | 
					      // It successfully started, update the started_at time
 | 
				
			||||||
    await db
 | 
					      await db
 | 
				
			||||||
      .update(workflowRunsTable)
 | 
					        .update(workflowRunsTable)
 | 
				
			||||||
      .set({
 | 
					        .set({
 | 
				
			||||||
        queued_at: time,
 | 
					          started_at: time,
 | 
				
			||||||
      })
 | 
					        })
 | 
				
			||||||
      .where(eq(workflowRunsTable.id, run_id));
 | 
					        .where(eq(workflowRunsTable.id, run_id));
 | 
				
			||||||
  }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (output_data) {
 | 
					    if (status == "queued" && time != undefined) {
 | 
				
			||||||
    const workflow_run_output = await db.insert(workflowRunOutputs).values({
 | 
					      // It successfully started, update the started_at time
 | 
				
			||||||
      run_id: run_id,
 | 
					      await db
 | 
				
			||||||
      data: output_data,
 | 
					        .update(workflowRunsTable)
 | 
				
			||||||
    });
 | 
					        .set({
 | 
				
			||||||
  } else if (status) {
 | 
					          queued_at: time,
 | 
				
			||||||
    const [workflow_run] = await db
 | 
					        })
 | 
				
			||||||
      .update(workflowRunsTable)
 | 
					        .where(eq(workflowRunsTable.id, run_id));
 | 
				
			||||||
      .set({
 | 
					    }
 | 
				
			||||||
        status: status,
 | 
					 | 
				
			||||||
        ended_at:
 | 
					 | 
				
			||||||
          status === "success" || status === "failed" ? new Date() : null,
 | 
					 | 
				
			||||||
      })
 | 
					 | 
				
			||||||
      .where(eq(workflowRunsTable.id, run_id))
 | 
					 | 
				
			||||||
      .returning();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Need to filter out only comfy deploy serverless
 | 
					    if (output_data) {
 | 
				
			||||||
    // Also multiply with the gpu selection
 | 
					      const workflow_run_output = await db.insert(workflowRunOutputs).values({
 | 
				
			||||||
    if (workflow_run.machine_type == "comfy-deploy-serverless") {
 | 
					        run_id: run_id,
 | 
				
			||||||
      if (
 | 
					        data: output_data,
 | 
				
			||||||
        (status === "success" || status === "failed") &&
 | 
					      });
 | 
				
			||||||
        workflow_run.user_id
 | 
					    } else if (status) {
 | 
				
			||||||
      ) {
 | 
					      const [workflow_run] = await db
 | 
				
			||||||
        const sub = await getCurrentPlan({
 | 
					        .update(workflowRunsTable)
 | 
				
			||||||
          user_id: workflow_run.user_id,
 | 
					        .set({
 | 
				
			||||||
          org_id: workflow_run.org_id,
 | 
					          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) {
 | 
					      // Need to filter out only comfy deploy serverless
 | 
				
			||||||
          let durationInSec = Math.abs(
 | 
					      // Also multiply with the gpu selection
 | 
				
			||||||
            (workflow_run.ended_at.getTime() -
 | 
					      if (workflow_run.machine_type == "comfy-deploy-serverless") {
 | 
				
			||||||
              workflow_run.created_at.getTime()) /
 | 
					        if (
 | 
				
			||||||
              1000,
 | 
					          (status === "success" || status === "failed") &&
 | 
				
			||||||
          );
 | 
					          workflow_run.user_id
 | 
				
			||||||
          durationInSec = Math.ceil(durationInSec);
 | 
					        ) {
 | 
				
			||||||
          switch (workflow_run.gpu) {
 | 
					          const sub = await getCurrentPlan({
 | 
				
			||||||
            case "A100":
 | 
					            user_id: workflow_run.user_id,
 | 
				
			||||||
              durationInSec *= 7;
 | 
					            org_id: workflow_run.org_id,
 | 
				
			||||||
              break;
 | 
					          });
 | 
				
			||||||
            case "A10G":
 | 
					
 | 
				
			||||||
              durationInSec *= 4;
 | 
					          if (sub && sub.subscription_item_api_id && workflow_run.ended_at) {
 | 
				
			||||||
              break;
 | 
					            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(
 | 
					    return NextResponse.json(
 | 
				
			||||||
    {
 | 
					      {
 | 
				
			||||||
      message: "success",
 | 
					        message: "success",
 | 
				
			||||||
    },
 | 
					      },
 | 
				
			||||||
    {
 | 
					      {
 | 
				
			||||||
      status: 200,
 | 
					        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,
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -29,7 +29,12 @@ export async function RunDisplay({
 | 
				
			|||||||
    <Dialog>
 | 
					    <Dialog>
 | 
				
			||||||
      <DialogTrigger asChild className="appearance-none hover:cursor-pointer">
 | 
					      <DialogTrigger asChild className="appearance-none hover:cursor-pointer">
 | 
				
			||||||
        <TableRow>
 | 
					        <TableRow>
 | 
				
			||||||
          <TableCell>{run.number}</TableCell>
 | 
					          <TableCell>
 | 
				
			||||||
 | 
					            <Tooltip>
 | 
				
			||||||
 | 
					              <TooltipTrigger>{run.number}</TooltipTrigger>
 | 
				
			||||||
 | 
					              <TooltipContent>{run.id}</TooltipContent>
 | 
				
			||||||
 | 
					            </Tooltip>
 | 
				
			||||||
 | 
					          </TableCell>
 | 
				
			||||||
          <TableCell className="font-medium truncate">
 | 
					          <TableCell className="font-medium truncate">
 | 
				
			||||||
            {run.machine?.name}
 | 
					            {run.machine?.name}
 | 
				
			||||||
          </TableCell>
 | 
					          </TableCell>
 | 
				
			||||||
@ -46,7 +51,12 @@ export async function RunDisplay({
 | 
				
			|||||||
            <Tooltip>
 | 
					            <Tooltip>
 | 
				
			||||||
              <TooltipTrigger>{getDuration(run.duration)}</TooltipTrigger>
 | 
					              <TooltipTrigger>{getDuration(run.duration)}</TooltipTrigger>
 | 
				
			||||||
              <TooltipContent>
 | 
					              <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>
 | 
					                <div>Run duration: {getDuration(run.run_duration)}</div>
 | 
				
			||||||
              </TooltipContent>
 | 
					              </TooltipContent>
 | 
				
			||||||
            </Tooltip>
 | 
					            </Tooltip>
 | 
				
			||||||
 | 
				
			|||||||
@ -12,13 +12,21 @@ export function getRelativeTime(time: string | Date | null | undefined) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function formatDuration(seconds: number) {
 | 
					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;
 | 
					  const remainingSeconds = seconds % 60;
 | 
				
			||||||
  if (minutes > 0) {
 | 
					
 | 
				
			||||||
    return `${minutes}.${remainingSeconds} mins`;
 | 
					  let result = "";
 | 
				
			||||||
  } else {
 | 
					  if (hours > 0) {
 | 
				
			||||||
    return `${remainingSeconds.toFixed(1)} secs`;
 | 
					    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) {
 | 
					export function getDuration(durationInSecs: number) {
 | 
				
			||||||
 | 
				
			|||||||
@ -148,14 +148,14 @@ export const createRun = withServerPromise(
 | 
				
			|||||||
            body: JSON.stringify(_data),
 | 
					            body: JSON.stringify(_data),
 | 
				
			||||||
            cache: "no-store",
 | 
					            cache: "no-store",
 | 
				
			||||||
          });
 | 
					          });
 | 
				
			||||||
          console.log(___result);
 | 
					          // console.log(___result);
 | 
				
			||||||
          if (!___result.ok)
 | 
					          if (!___result.ok)
 | 
				
			||||||
            throw new Error(
 | 
					            throw new Error(
 | 
				
			||||||
              `Error creating run, ${
 | 
					              `Error creating run, ${
 | 
				
			||||||
                ___result.statusText
 | 
					                ___result.statusText
 | 
				
			||||||
              } ${await ___result.text()}`,
 | 
					              } ${await ___result.text()}`,
 | 
				
			||||||
            );
 | 
					            );
 | 
				
			||||||
          console.log(_data, ___result);
 | 
					          // console.log(_data, ___result);
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "runpod-serverless":
 | 
					        case "runpod-serverless":
 | 
				
			||||||
          const data = {
 | 
					          const data = {
 | 
				
			||||||
@ -182,14 +182,14 @@ export const createRun = withServerPromise(
 | 
				
			|||||||
            body: JSON.stringify(data),
 | 
					            body: JSON.stringify(data),
 | 
				
			||||||
            cache: "no-store",
 | 
					            cache: "no-store",
 | 
				
			||||||
          });
 | 
					          });
 | 
				
			||||||
          console.log(__result);
 | 
					          // console.log(__result);
 | 
				
			||||||
          if (!__result.ok)
 | 
					          if (!__result.ok)
 | 
				
			||||||
            throw new Error(
 | 
					            throw new Error(
 | 
				
			||||||
              `Error creating run, ${
 | 
					              `Error creating run, ${
 | 
				
			||||||
                __result.statusText
 | 
					                __result.statusText
 | 
				
			||||||
              } ${await __result.text()}`,
 | 
					              } ${await __result.text()}`,
 | 
				
			||||||
            );
 | 
					            );
 | 
				
			||||||
          console.log(data, __result);
 | 
					          // console.log(data, __result);
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "classic":
 | 
					        case "classic":
 | 
				
			||||||
          const body = {
 | 
					          const body = {
 | 
				
			||||||
 | 
				
			|||||||
@ -27,6 +27,10 @@ export async function findAllRuns({
 | 
				
			|||||||
        sql<number>`(extract(epoch from ended_at) - extract(epoch from created_at))`.as(
 | 
					        sql<number>`(extract(epoch from ended_at) - extract(epoch from created_at))`.as(
 | 
				
			||||||
          "duration",
 | 
					          "duration",
 | 
				
			||||||
        ),
 | 
					        ),
 | 
				
			||||||
 | 
					      comfy_deploy_cold_start:
 | 
				
			||||||
 | 
					        sql<number>`(extract(epoch from queued_at) - extract(epoch from created_at))`.as(
 | 
				
			||||||
 | 
					          "cold_start_duration",
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
      cold_start_duration:
 | 
					      cold_start_duration:
 | 
				
			||||||
        sql<number>`(extract(epoch from started_at) - extract(epoch from queued_at))`.as(
 | 
					        sql<number>`(extract(epoch from started_at) - extract(epoch from queued_at))`.as(
 | 
				
			||||||
          "cold_start_duration",
 | 
					          "cold_start_duration",
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user