Skip to content

Commit 40ec420

Browse files
committed
WIP, this is a broken axios
1 parent fb86ac2 commit 40ec420

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

site/src/contexts/ProxyContext.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import { useQuery } from "@tanstack/react-query"
22
import { getWorkspaceProxies } from "api/api"
33
import { Region } from "api/typesGenerated"
4+
import axios from "axios"
45
import { useDashboard } from "components/Dashboard/DashboardProvider"
56
import {
67
createContext,
78
FC,
89
PropsWithChildren,
910
useContext,
11+
useEffect,
1012
useState,
1113
} from "react"
1214

1315
interface ProxyContextValue {
1416
proxy: PreferredProxy
1517
proxies?: Region[]
18+
// proxyLatenciesMS are recorded in milliseconds.
19+
proxyLatenciesMS?: Record<string, number>
1620
// isfetched is true when the proxy api call is complete.
1721
isFetched: boolean
1822
// isLoading is true if the proxy is in the process of being fetched.
@@ -52,6 +56,9 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
5256
}
5357

5458
const [proxy, setProxy] = useState<PreferredProxy>(savedProxy)
59+
const [proxyLatenciesMS, setProxyLatenciesMS] = useState<
60+
Record<string, number>
61+
>({})
5562

5663
const dashboard = useDashboard()
5764
const experimentEnabled = dashboard?.experiments.includes("moons")
@@ -72,6 +79,58 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
7279
},
7380
})
7481

82+
// Everytime we get a new proxiesResponse, update the latency check
83+
// to each workspace proxy.
84+
useEffect(() => {
85+
const latencyAxios = axios.create()
86+
latencyAxios.interceptors.request.use((config) => {
87+
config.data = config.data || {}
88+
config.data.startTime = new Date()
89+
console.log("Hey kira", config, config.data)
90+
return config
91+
})
92+
93+
latencyAxios.interceptors.response.use(
94+
// Success 200
95+
(x) => {
96+
// Get elapsed time (in milliseconds)
97+
const end = new Date()
98+
x.config.data = {
99+
...x.config.data,
100+
...{
101+
endTime: end,
102+
responseTime: end.getTime() - x.config.data.requestStartedAt,
103+
},
104+
}
105+
return x
106+
},
107+
// Handle 4xx & 5xx responses
108+
(x) => {
109+
// Get elapsed time (in milliseconds)
110+
const end = new Date()
111+
x.config.data = x.config.data || {
112+
...x.config.data,
113+
...{
114+
endTime: end,
115+
responseTime: end.getTime() - x.config.data.requestStartedAt,
116+
},
117+
}
118+
return x
119+
},
120+
)
121+
122+
// AgentLatency.tsx for colors
123+
console.log("update workspace proxies", proxiesResp)
124+
latencyAxios
125+
.get<any>("/api/v2/users/authmethods")
126+
.then((resp) => {
127+
console.log("latency", resp)
128+
})
129+
.catch((err) => {
130+
console.log("latency error", err)
131+
})
132+
}, [proxiesResp])
133+
75134
const setAndSaveProxy = (
76135
selectedProxy?: Region,
77136
// By default the proxies come from the api call above.
@@ -95,6 +154,7 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
95154
return (
96155
<ProxyContext.Provider
97156
value={{
157+
proxyLatenciesMS: proxyLatenciesMS,
98158
proxy: experimentEnabled
99159
? proxy
100160
: {

0 commit comments

Comments
 (0)