Skip to content

Commit

Permalink
Merge pull request #199 from polywrap/rollback/progress-bar
Browse files Browse the repository at this point in the history
rollback: progress bar init
  • Loading branch information
dOrgJelli committed Feb 20, 2024
2 parents 0c98b0b + e69f4f3 commit 984fb8b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 57 deletions.
12 changes: 2 additions & 10 deletions web/components/RealtimeLogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ export default function RealtimeLogs(props: {
});
const progressInformation = useProgressTime(
Object.values(STEP_TIME_ESTS),
sortedLogsWithSteps,
props.run.prompt
sortedLogsWithSteps
);

const supabase = createSupabaseBrowserClient(
Expand All @@ -73,17 +72,10 @@ export default function RealtimeLogs(props: {
},
(payload) => {
if (payload.new.status === "ERRORED") {
router.refresh();
setHasErrored(true);
return;
}

if (
payload.new.step_name === "SYNTHESIZE_RESULTS" &&
payload.new.status === "COMPLETED"
) {
router.refresh();
}
router.refresh();
}
)
.subscribe();
Expand Down
2 changes: 1 addition & 1 deletion web/components/TimeRemaining.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function formatTime(seconds: number) {
const roundedSeconds = Math.round(seconds);
const minutes = Math.floor(roundedSeconds / 60);
const secs = roundedSeconds % 60;
const timeRemaining = seconds ? `${minutes}:${secs.toString().padStart(2, "0")}` : "1:40"
const timeRemaining = seconds ? `${minutes}:${secs.toString().padStart(2, "0")}` : "1:10"
return `Estimated time: ~${timeRemaining}`
}

Expand Down
66 changes: 24 additions & 42 deletions web/hooks/useProgressTime.ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,56 @@
import { Tables } from "@/supabase/dbTypes";
import { COMPLETED_TEXTS } from "@/utils/logs";
import { useState, useEffect } from "react";

export function useProgressTime(
stepTimes: number[],
logs: Array<Tables<"logs">>,
prompt: string,
logs: Array<Tables<"logs">>
) {
const [startTime] = useState(Date.now());
const [startTime, setStartTime] = useState(Date.now());
const [progressInformation, setProgressInformation] = useState({
logs,
time: 0,
progress: 0,
});

// Capture the start time when the hook is first called or when stepTimes or currentStep changes
const totalTime = stepTimes.reduce((a, b) => a + b, 0);
let currentStep = logs.findIndex(
(x) => x.status === "IN_PROGRESS"
);

useEffect(() => {
const totalTime = stepTimes.reduce((a, b) => a + b, 0);
setStartTime(Date.now())
}, [currentStep])

useEffect(() => {
const intervalId = setInterval(function () {
const now = Date.now();
const secondsFromStart = (now - startTime) / 1000; // Convert ms to seconds
let currentStep = progressInformation.logs.findIndex(
(x) => x.status === "IN_PROGRESS"
);
const elapsedTimeSinceStart = (now - startTime) / 1000; // Convert ms to seconds

if (elapsedTimeSinceStart > stepTimes[currentStep]) {
return
}

const elapsedTimeInSteps = stepTimes
.slice(0, currentStep + 1)
.slice(0, currentStep)
.reduce((a, b) => a + b, 0);

const timeRemaining = Math.max(totalTime - secondsFromStart, 0);

const progress = (secondsFromStart / totalTime) * 100;
if (timeRemaining <= 1) {
clearInterval(intervalId);
return;
}
const totalElapsedTime = elapsedTimeSinceStart + elapsedTimeInSteps;

if (
secondsFromStart > elapsedTimeInSteps &&
stepTimes.length > currentStep &&
currentStep !== -1
) {
setProgressInformation(({ logs }) => {
const newLogs = [...logs];
newLogs[currentStep].status = "COMPLETED";
newLogs[currentStep].value = COMPLETED_TEXTS[newLogs[currentStep].step_name]
if (currentStep === 0) {
newLogs[currentStep].value += " to " + prompt
}
const timeRemaining = Math.max(totalTime - totalElapsedTime, 0); // Prevent negative time
const progress = (totalElapsedTime / totalTime) * 100;

if (stepTimes.length > currentStep + 1) {
newLogs[currentStep + 1].status = "IN_PROGRESS";
}
return {
time: timeRemaining,
progress: progress,
logs: newLogs,
};
});
return;
if (timeRemaining < 1) {
return
}

setProgressInformation((i) => ({
...i,
time: timeRemaining,
progress: progress,
}));

}, 1000);
return () => clearInterval(intervalId);
}, [stepTimes]);
}, [stepTimes, currentStep]);

return progressInformation;
}
8 changes: 4 additions & 4 deletions web/utils/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export const STEPS_ORDER: Record<StepName, number> = {
}

export const STEP_TIME_ESTS: Record<StepName, number> = {
FETCH_PROJECTS: 30,
EVALUATE_PROJECTS: 30,
ANALYZE_FUNDING: 30,
SYNTHESIZE_RESULTS: 10
FETCH_PROJECTS: 35,
EVALUATE_PROJECTS: 15,
ANALYZE_FUNDING: 5,
SYNTHESIZE_RESULTS: 15
}

0 comments on commit 984fb8b

Please sign in to comment.