From 5857e716b022d6a1f96e72a183b1c3f44b917e1c Mon Sep 17 00:00:00 2001 From: Erik Taubeneck Date: Tue, 9 Jul 2024 12:50:12 -0700 Subject: [PATCH] fix some bugs --- server/app/query/view/[id]/components.tsx | 15 ++++++---- sidecar/app/query/base.py | 35 +++++++++++++---------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/server/app/query/view/[id]/components.tsx b/server/app/query/view/[id]/components.tsx index 7e3e1b8..2cacdc8 100644 --- a/server/app/query/view/[id]/components.tsx +++ b/server/app/query/view/[id]/components.tsx @@ -73,19 +73,22 @@ export function RunTimePill({ startTime: number | null; endTime: number | null; }) { - const [runTime, setRunTime] = useState(0); + const [runTime, setRunTime] = useState(null); const runTimeStr = runTime ? secondsToTime(runTime) : "N/A"; const intervalId = useRef | null>(null); useEffect(() => { - if (startTime !== null) { + if (intervalId.current !== null) { + // any time startTime or endTime change, we remove the old setInterval + // which runs the timer. if a new one is needed, it's created. + clearTimeout(intervalId.current); + } + if (startTime === null) { + setRunTime(null); + } else { if (endTime !== null && startTime !== null) { setRunTime(endTime - startTime); } else { - if (intervalId.current !== null) { - clearTimeout(intervalId.current); - } - let newIntervalId = setInterval(() => { setRunTime(Date.now() / 1000 - startTime); }, 1000); diff --git a/sidecar/app/query/base.py b/sidecar/app/query/base.py index ff416d3..cf63149 100644 --- a/sidecar/app/query/base.py +++ b/sidecar/app/query/base.py @@ -83,15 +83,25 @@ def get_from_query_id(cls, query_id) -> Optional["Query"]: if query: return query raise e - if query.status_file_path.exists(): - with query.status_file_path.open("r") as f: + query.load_history_from_file() + if query.status == Status.UNKNOWN: + return None + return query + + def load_history_from_file(self): + if self.status_file_path.exists(): + self.logger.debug( + f"Loading query {self.query_id} status history " + f"from file {self.status_file_path}" + ) + with self.status_file_path.open("r") as f: for line in f: status_str, timestamp = line.split(",") - query.add_status_event( - status=Status[status_str], timestamp=float(timestamp) + self._status_history.append( + StatusChangeEvent( + status=Status[status_str], timestamp=float(timestamp) + ) ) - return query - return None @property def _last_status_event(self): @@ -118,15 +128,10 @@ def status(self) -> Status: def status(self, status: Status): if self.status <= Status.COMPLETE: now = time.time() - self.add_status_event(status, now) - - def add_status_event(self, status: Status, timestamp: float): - self._status_history.append( - StatusChangeEvent(status=status, timestamp=timestamp) - ) - with self.status_file_path.open("a") as f: - self.logger.debug(f"updating status: {status=}") - f.write(f"{status.name},{timestamp}\n") + self._status_history.append(StatusChangeEvent(status=status, timestamp=now)) + with self.status_file_path.open("a") as f: + self.logger.debug(f"updating status: {status=}") + f.write(f"{status.name},{now}\n") @property def running(self):