Skip to content

Commit

Permalink
Simplified DB schema for step names
Browse files Browse the repository at this point in the history
  • Loading branch information
namesty committed Jan 25, 2024
1 parent bc86e2b commit 66fee47
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 68 deletions.
19 changes: 2 additions & 17 deletions web/components/Logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ import RealtimeLogs from "./RealtimeLogs";
export default async function Logs(props: { runId: string }) {
const supabase = createSupabaseServerClient()

const { data: steps } = await supabase.from('steps').select(`
id,
name,
order
`)

if (!steps) {
throw new Error(`Error fetching steps`)
}

const { data: run } = await supabase.from('runs').select(`
id,
prompt,
Expand All @@ -24,14 +14,9 @@ export default async function Logs(props: { runId: string }) {
run_id,
created_at,
value,
step_id,
ended_at,
status,
steps(
id,
name,
order
)
step_name
)
`).eq("id", props.runId).single()

Expand All @@ -41,6 +26,6 @@ export default async function Logs(props: { runId: string }) {
}

return (
<RealtimeLogs logs={run.logs} run={run} steps={steps} />
<RealtimeLogs logs={run.logs} run={run} />
)
}
42 changes: 19 additions & 23 deletions web/components/RealtimeLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,49 @@ import { createSupabaseBrowserClient } from "@/utils/supabase-browser";
import router from "next/router";
import { useState, useEffect } from "react";

type LogWithStep = (Tables<"logs"> & { steps: Tables<"steps"> | null })

const UNSTARTED_TEXTS: Record<Tables<"steps">["name"], string> = {
const UNSTARTED_TEXTS: Record<Tables<"logs">["step_name"], string> = {
FETCH_PROJECTS: "Search for relevant projects",
EVALUATE_PROJECTS: "Evaluate proof of impact",
ANALYZE_FUNDING: "Analyze funding needs",
SYNTHESIZE_RESULTS: "Synthesize results",
}

const LOADING_TEXTS: Record<Tables<"steps">["name"], string> = {
const LOADING_TEXTS: Record<Tables<"logs">["step_name"], string> = {
FETCH_PROJECTS: "Searching for relevant projects",
EVALUATE_PROJECTS: "Evaluating proof of impact",
ANALYZE_FUNDING: "Analyzing funding needs",
SYNTHESIZE_RESULTS: "Synthesizing results",
}

const getLogMessage = (log: LogWithStep) => {
const STEPS_ORDER: Record<Tables<"logs">["step_name"], number> = {
FETCH_PROJECTS: 1,
EVALUATE_PROJECTS: 2,
ANALYZE_FUNDING: 3,
SYNTHESIZE_RESULTS: 4,
}

const getLogMessage = (log: Tables<"logs">) => {
switch (log.status) {
case "NOT_STARTED": return UNSTARTED_TEXTS[log.steps!.name]
case "IN_PROGRESS": return LOADING_TEXTS[log.steps!.name]
case "COMPLETED": return log.value ?? `Completed: ${UNSTARTED_TEXTS[log.steps!.name]}`
case "ERRORED": return `Error while ${LOADING_TEXTS[log.steps!.name].toLowerCase()}`
case "NOT_STARTED": return UNSTARTED_TEXTS[log.step_name]
case "IN_PROGRESS": return LOADING_TEXTS[log.step_name]
case "COMPLETED": return log.value ?? `Completed: ${UNSTARTED_TEXTS[log.step_name]}`
case "ERRORED": return `Error while ${LOADING_TEXTS[log.step_name].toLowerCase()}`
}
}

export default function RealtimeLogs(props: {
logs: LogWithStep[]
steps: Tables<"steps">[]
logs: Tables<"logs">[]
run: {
id: string;
prompt: string;
}
}) {
const [logs, setLogs] = useState<LogWithStep[]>(props.logs)
const [logs, setLogs] = useState<Tables<"logs">[]>(props.logs)
const supabase = createSupabaseBrowserClient();
const runId = props.run

const orderedSteps = props.steps.sort((a, b) => a.order - b.order)
const lastStep = orderedSteps.slice(-1)[0]

const sortedLogsWithSteps = logs.sort((a, b) => {
return a.steps!.order - b.steps!.order
return STEPS_ORDER[a.step_name] - STEPS_ORDER[b.step_name]
})

useEffect(() => {
Expand All @@ -62,14 +63,9 @@ export default function RealtimeLogs(props: {
},
(payload: { new: Tables<"logs"> }) => {
const logsMinusTheUpdatedOne = logs.filter(log => log.id !== payload.new.id)
const newWithStep = {
...payload.new,
steps: props.steps.find(step => step.id === payload.new.step_id) ?? null
}

setLogs([...logsMinusTheUpdatedOne, newWithStep])
setLogs([...logsMinusTheUpdatedOne, payload.new])

if (payload.new.step_id === lastStep.id && payload.new.status === "COMPLETED") {
if (payload.new.step_name === "SYNTHESIZE_RESULTS" && payload.new.status === "COMPLETED") {
router.push(`/${runId}/strategy`)
return;
}
Expand Down
36 changes: 8 additions & 28 deletions web/supabase/dbTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export interface Database {
id: string
run_id: string
status: Database["public"]["Enums"]["step_status"]
step_id: string
step_name: Database["public"]["Enums"]["step_name"]
value: string | null
}
Insert: {
Expand All @@ -155,7 +155,7 @@ export interface Database {
id?: string
run_id: string
status: Database["public"]["Enums"]["step_status"]
step_id: string
step_name: Database["public"]["Enums"]["step_name"]
value?: string | null
}
Update: {
Expand All @@ -164,7 +164,7 @@ export interface Database {
id?: string
run_id?: string
status?: Database["public"]["Enums"]["step_status"]
step_id?: string
step_name?: Database["public"]["Enums"]["step_name"]
value?: string | null
}
Relationships: [
Expand All @@ -174,13 +174,6 @@ export interface Database {
isOneToOne: false
referencedRelation: "runs"
referencedColumns: ["id"]
},
{
foreignKeyName: "logs_step_id_fkey"
columns: ["step_id"]
isOneToOne: false
referencedRelation: "steps"
referencedColumns: ["id"]
}
]
}
Expand Down Expand Up @@ -234,24 +227,6 @@ export interface Database {
}
]
}
steps: {
Row: {
id: string
name: string
order: number
}
Insert: {
id?: string
name: string
order: number
}
Update: {
id?: string
name?: string
order?: number
}
Relationships: []
}
strategy_entries: {
Row: {
created_at: string
Expand Down Expand Up @@ -323,6 +298,11 @@ export interface Database {
[_ in never]: never
}
Enums: {
step_name:
| "FETCH_PROJECTS"
| "EVALUATE_PROJECTS"
| "ANALYZE_FUNDING"
| "SYNTHESIZE_RESULTS"
step_status: "NOT_STARTED" | "IN_PROGRESS" | "COMPLETED" | "ERRORED"
}
CompositeTypes: {
Expand Down

0 comments on commit 66fee47

Please sign in to comment.