-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathOptimizations.jsx
More file actions
155 lines (135 loc) · 5.22 KB
/
Optimizations.jsx
File metadata and controls
155 lines (135 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React, { memo, useState, useEffect, useCallback, useRef } from 'react';
import PropTypes from 'prop-types';
import { axiosInstance as axios } from '../../../common/axios';
import { Box } from '@mui/material';
import ProgressGrid from './ProgressGrid/ProgressGrid';
import Timeline from './Timeline/Timeline';
import OptimizationsTree from './Tree/OptimizationsTree';
/** Poll interval while at least one optimization is running and the tab is visible (2–5s range). */
const POLL_ACTIVE_MS = 4000;
/** Max delay between retries after a failed poll (exponential backoff cap). */
const POLL_ERROR_RETRY_MAX_MS = 32000;
function isRequestCanceled(error) {
return error?.code === 'ERR_CANCELED' || error?.name === 'CanceledError';
}
const Optimizations = ({ collectionName }) => {
const [data, setData] = useState(null);
const [selectedOptimization, setSelectedOptimization] = useState(null);
const [requestTime, setRequestTime] = useState(null);
const [isRefreshing, setIsRefreshing] = useState(false);
const abortRef = useRef(null);
const pollTimeoutRef = useRef(null);
const lastRunningRef = useRef(false);
const mountedRef = useRef(true);
const pollErrorBackoffMsRef = useRef(POLL_ACTIVE_MS);
const runFetch = useCallback(
async ({ preserveSelection = false } = {}) => {
clearTimeout(pollTimeoutRef.current);
pollTimeoutRef.current = null;
abortRef.current?.abort();
const ac = new AbortController();
abortRef.current = ac;
if (!preserveSelection) {
pollErrorBackoffMsRef.current = POLL_ACTIVE_MS;
setIsRefreshing(true);
setSelectedOptimization(null);
}
const url = `/collections/${encodeURIComponent(
collectionName
)}/optimizations?with=queued,completed,idle_segments`;
try {
const { data: next } = await axios.get(url, { signal: ac.signal });
if (!mountedRef.current) return;
setData(next);
setRequestTime(Date.now());
const result = next?.result;
const hasRunning = Array.isArray(result?.running) && result.running.length > 0;
lastRunningRef.current = hasRunning;
pollErrorBackoffMsRef.current = POLL_ACTIVE_MS;
clearTimeout(pollTimeoutRef.current);
if (hasRunning && !document.hidden) {
pollTimeoutRef.current = window.setTimeout(() => {
pollTimeoutRef.current = null;
void runFetch({ preserveSelection: true });
}, POLL_ACTIVE_MS);
}
} catch (error) {
if (isRequestCanceled(error)) return;
console.error('Error fetching optimizations:', error);
if (mountedRef.current && lastRunningRef.current && !document.hidden) {
const delay = pollErrorBackoffMsRef.current;
pollErrorBackoffMsRef.current = Math.min(pollErrorBackoffMsRef.current * 2, POLL_ERROR_RETRY_MAX_MS);
pollTimeoutRef.current = window.setTimeout(() => {
pollTimeoutRef.current = null;
void runFetch({ preserveSelection: true });
}, delay);
}
} finally {
if (!preserveSelection && mountedRef.current) {
setIsRefreshing(false);
}
}
},
[collectionName]
);
const fetchData = useCallback(() => {
void runFetch({ preserveSelection: false });
}, [runFetch]);
useEffect(() => {
mountedRef.current = true;
return () => {
mountedRef.current = false;
clearTimeout(pollTimeoutRef.current);
pollTimeoutRef.current = null;
abortRef.current?.abort();
};
}, []);
useEffect(() => {
// this is used to stop polling when the user switches to another tab
const onVisibilityChange = () => {
if (document.hidden) {
clearTimeout(pollTimeoutRef.current);
pollTimeoutRef.current = null;
} else if (lastRunningRef.current) {
void runFetch({ preserveSelection: true });
}
};
document.addEventListener('visibilitychange', onVisibilityChange);
return () => document.removeEventListener('visibilitychange', onVisibilityChange);
}, [runFetch]);
useEffect(() => {
void runFetch({ preserveSelection: false });
return () => {
clearTimeout(pollTimeoutRef.current);
pollTimeoutRef.current = null;
abortRef.current?.abort();
};
}, [collectionName, runFetch]);
const handleOptimizationSelect = (optimization) => {
if (optimization) {
// Wrap the selected optimization in the expected format for OptimizationsTree
// The optimization already has the progress tree spread into it from preprocess
setSelectedOptimization({ result: { running: [optimization] } });
} else {
setSelectedOptimization(null);
}
};
return (
<Box display="flex" flexDirection="column" gap={5}>
<ProgressGrid data={data?.result} />
<Timeline
data={data?.result}
requestTime={requestTime}
onSelect={handleOptimizationSelect}
selectedItem={selectedOptimization?.result?.running?.[0]}
onRefresh={fetchData}
isRefreshing={isRefreshing}
/>
<OptimizationsTree data={selectedOptimization || data} requestTime={requestTime} />
</Box>
);
};
Optimizations.propTypes = {
collectionName: PropTypes.string.isRequired,
};
export default memo(Optimizations);