1
1
import { useQuery } from "@tanstack/react-query"
2
2
import { getWorkspaceProxies } from "api/api"
3
3
import { Region } from "api/typesGenerated"
4
+ import axios from "axios"
4
5
import { useDashboard } from "components/Dashboard/DashboardProvider"
5
6
import {
6
7
createContext ,
7
8
FC ,
8
9
PropsWithChildren ,
9
10
useContext ,
11
+ useEffect ,
10
12
useState ,
11
13
} from "react"
12
14
13
15
interface ProxyContextValue {
14
16
proxy : PreferredProxy
15
17
proxies ?: Region [ ]
18
+ // proxyLatenciesMS are recorded in milliseconds.
19
+ proxyLatenciesMS ?: Record < string , number >
16
20
// isfetched is true when the proxy api call is complete.
17
21
isFetched : boolean
18
22
// isLoading is true if the proxy is in the process of being fetched.
@@ -52,6 +56,9 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
52
56
}
53
57
54
58
const [ proxy , setProxy ] = useState < PreferredProxy > ( savedProxy )
59
+ const [ proxyLatenciesMS , setProxyLatenciesMS ] = useState <
60
+ Record < string , number >
61
+ > ( { } )
55
62
56
63
const dashboard = useDashboard ( )
57
64
const experimentEnabled = dashboard ?. experiments . includes ( "moons" )
@@ -72,6 +79,58 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
72
79
} ,
73
80
} )
74
81
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
+
75
134
const setAndSaveProxy = (
76
135
selectedProxy ?: Region ,
77
136
// By default the proxies come from the api call above.
@@ -95,6 +154,7 @@ export const ProxyProvider: FC<PropsWithChildren> = ({ children }) => {
95
154
return (
96
155
< ProxyContext . Provider
97
156
value = { {
157
+ proxyLatenciesMS : proxyLatenciesMS ,
98
158
proxy : experimentEnabled
99
159
? proxy
100
160
: {
0 commit comments