Skip to content

Commit

Permalink
Progress page working
Browse files Browse the repository at this point in the history
  • Loading branch information
namesty committed Jan 25, 2024
1 parent 08839b1 commit 43fe899
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default async function StrategyPage({
}: {
params: { id: string };
}) {
const workerId = params.id;
const supabase = createSupabaseServerClient();

// // Fetch the runs for this worker
Expand All @@ -25,13 +24,13 @@ export default async function StrategyPage({
)
`
)
.eq("worker_id", workerId)
.eq("id", params.id)
.order("created_at", { ascending: false })
.single();

if (runs.error || !runs.data) {
console.error(runs.error);
throw Error(`Runs with worker_id ${workerId} not found.`);
throw Error(`Runs with id ${params.id} not found.`);
}

const data = runs.data.strategy_entries as unknown as StrategyWithProjects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import Logs from "@/components/Logs";

export default function ProgressPage(props: {
params: {
runId: string
id: string
}
}) {
return (
<div className="w-full flex justify-center h-full p-16">
<Logs runId={props.params.runId} />
<Logs runId={props.params.id} />
</div>
)
}
1 change: 0 additions & 1 deletion web/components/Logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default async function Logs(props: { runId: string }) {
step_name
)
`).eq("id", props.runId).single()


if (!run) {
throw new Error(`Run with ID '${props.runId}' not found`)
Expand Down
60 changes: 45 additions & 15 deletions web/components/RealtimeLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import clsx from "clsx";
import { useRouter } from "next/navigation";
import { useState, useEffect } from "react";
import LoadingCircle from "./LoadingCircle";
import TextField from "./TextField";
import Button from "./Button";

const UNSTARTED_TEXTS: Record<Tables<"logs">["step_name"], string> = {
FETCH_PROJECTS: "Search for relevant projects",
Expand Down Expand Up @@ -37,6 +39,16 @@ const getLogMessage = (log: Tables<"logs">) => {
}
}

const checkIfFinished = (logs: Tables<"logs">[]) => {
const sortedLogs = logs.sort((a, b) => {
return STEPS_ORDER[a.step_name] - STEPS_ORDER[b.step_name]
})
const lastStep = sortedLogs.slice(-1)[0];
const isFinished = lastStep.status === "COMPLETED" && lastStep.step_name === "SYNTHESIZE_RESULTS"

return isFinished
}

export default function RealtimeLogs(props: {
logs: Tables<"logs">[]
run: {
Expand All @@ -52,6 +64,12 @@ export default function RealtimeLogs(props: {
return STEPS_ORDER[a.step_name] - STEPS_ORDER[b.step_name]
})

const isFinished = checkIfFinished(sortedLogsWithSteps)

const navigateToStrategy = () => {
router.push(`../${props.run.id}`)
}

useEffect(() => {
const channel = supabase
.channel("logs-added")
Expand All @@ -63,18 +81,26 @@ export default function RealtimeLogs(props: {
schema: "public",
filter: `run_id=eq.${props.run.id}`,
},
(payload: { new: Tables<"logs"> }) => {
const updatedLogs = logs.map(log => {
if (log.id === payload.new.id) {
log = payload.new
}

return log
})
async () => {
const response = await supabase.from("logs").select(`
id,
run_id,
created_at,
value,
ended_at,
status,
step_name
`).eq("run_id", props.run.id)
const updatedLogs = response.data

if (!updatedLogs) {
throw new Error(`Logs for Run with ID '${props.run.id}' not found`)
}

setLogs([...updatedLogs])

if (payload.new.step_name === "SYNTHESIZE_RESULTS" && payload.new.status === "COMPLETED") {
router.push(`/${props.run.id}/strategy`)
if (checkIfFinished(updatedLogs)) {
navigateToStrategy()
return;
}
}
Expand All @@ -89,18 +115,19 @@ export default function RealtimeLogs(props: {
return (
<div className="w-full max-w-3xl flex flex-col gap-8">
<div className="flex flex-col gap-2">
<p>Results for:</p>
<div className="p-4 flex flex-nowrap items-center gap-1 border border-indigo-500 rounded-lg bg-indigo-500/50">
<span className="flex-1">{props.run.prompt}</span>
</div>
<TextField
label='Results for'
value={props.run.prompt}
readOnly={true}
/>
</div>
<div className="w-full h-[1px] bg-indigo-500" />
<div className="flex flex-col gap-4">
<p>Results:</p>
<div className="flex flex-col gap-2">
{ sortedLogsWithSteps.map(log => (
<div className={clsx(
"p-4 flex flex-nowrap items-center gap-2 border border-indigo-500 rounded-lg bg-indigo-500/50",
"p-4 flex flex-nowrap items-center gap-2 border border-indigo-500 rounded-lg bg-indigo-500/50 cursor-pointer",
log.status === "IN_PROGRESS" ? "text-indigo-50" : ""
)}>
{ log.status === "IN_PROGRESS" ? <LoadingCircle hideText={true} className="!stroke-indigo-500 text-indigo-200" /> : <></>}
Expand All @@ -116,6 +143,9 @@ export default function RealtimeLogs(props: {
)) }
</div>
</div>
<Button disabled={!isFinished} onClick={() => navigateToStrategy()}>
View Results
</Button>
</div>
)
}

0 comments on commit 43fe899

Please sign in to comment.