Skip to content

Commit

Permalink
Fetch on an interval
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffxy committed Mar 30, 2024
1 parent 7f5c246 commit ea43c05
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
11 changes: 5 additions & 6 deletions ui/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ function App() {
};

// Fetch updated system state periodically.
useEffect(() => {
let timeoutId = null;
useEffect(async () => {
const refreshData = async () => {
const newSystemState = await fetchSystemState(
/*filterTablesForDemo=*/ false,
Expand All @@ -76,16 +75,16 @@ function App() {
if (JSON.stringify(systemState) !== JSON.stringify(newSystemState)) {
setSystemState(newSystemState);
}
timeoutId = setTimeout(refreshData, REFRESH_INTERVAL_MS);
};

// Run first fetch immediately.
timeoutId = setTimeout(refreshData, 0);
await refreshData();
const intervalId = setInterval(refreshData, REFRESH_INTERVAL_MS);
return () => {
if (timeoutId === null) {
if (intervalId === null) {
return;
}
clearTimeout(timeoutId);
clearInterval(intervalId);
};
}, [systemState]);

Expand Down
11 changes: 5 additions & 6 deletions ui/src/components/PerfView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ function PerfView({ virtualInfra }) {
return metricsManagerRef.current;
}

useEffect(() => {
let timeoutId = null;
useEffect(async () => {
const refreshData = async () => {
const rawMetrics = await fetchMetrics(60, /*useGenerated=*/ false);
const fetchedMetrics = parseMetrics(rawMetrics);
Expand All @@ -94,16 +93,16 @@ function PerfView({ virtualInfra }) {
),
});
}
timeoutId = setTimeout(refreshData, REFRESH_INTERVAL_MS);
};

// Run first fetch immediately.
timeoutId = setTimeout(refreshData, 0);
await refreshData();
const intervalId = setInterval(refreshData, REFRESH_INTERVAL_MS);
return () => {
if (timeoutId === null) {
if (intervalId === null) {
return;
}
clearTimeout(timeoutId);
clearInterval(intervalId);
};
}, [metricsData, windowSizeMinutes]);

Expand Down

0 comments on commit ea43c05

Please sign in to comment.