Skip to content

feat: Auto select workspace proxy based on lowest latency #7515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 22, 2023
Merged
32 changes: 19 additions & 13 deletions site/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Blob } from "buffer"
import jestFetchMock from "jest-fetch-mock"
import { ProxyLatencyReport } from "contexts/useProxyLatency"
import { RegionsResponse } from "api/typesGenerated"
import { useMemo } from "react"

jestFetchMock.enableMocks()

Expand All @@ -16,20 +17,25 @@ jestFetchMock.enableMocks()
// actual network requests. So just globally mock this hook.
jest.mock("contexts/useProxyLatency", () => ({
useProxyLatency: (proxies?: RegionsResponse) => {
if (!proxies) {
return {} as Record<string, ProxyLatencyReport>
}

return proxies.regions.reduce((acc, proxy) => {
acc[proxy.id] = {
accurate: true,
// Return a constant latency of 8ms.
// If you make this random it could break stories.
latencyMS: 8,
at: new Date(),
// Must use `useMemo` here to avoid infinite loop.
// Mocking the hook with a hook.
const latencies = useMemo(() => {
if (!proxies) {
return {} as Record<string, ProxyLatencyReport>
}
return acc
}, {} as Record<string, ProxyLatencyReport>)
return proxies.regions.reduce((acc, proxy) => {
acc[proxy.id] = {
accurate: true,
// Return a constant latency of 8ms.
// If you make this random it could break stories.
latencyMS: 8,
at: new Date(),
}
return acc
}, {} as Record<string, ProxyLatencyReport>)
}, [proxies])

return latencies
},
}))

Expand Down
3 changes: 3 additions & 0 deletions site/src/components/AppLink/AppLink.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const Template: Story<AppLinkProps> = (args) => (
setProxy: () => {
return
},
clearProxy: () => {
return
},
}}
>
<AppLink {...args} />
Expand Down
3 changes: 3 additions & 0 deletions site/src/components/Resources/AgentRow.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ const TemplateFC = (
setProxy: () => {
return
},
clearProxy: () => {
return
},
}}
>
<AgentRow {...args} />
Expand Down
6 changes: 6 additions & 0 deletions site/src/components/Resources/ResourceCard.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Example.args = {
setProxy: () => {
return
},
clearProxy: () => {
return
},
}}
>
<AgentRow
Expand Down Expand Up @@ -97,6 +100,9 @@ BunchOfMetadata.args = {
setProxy: () => {
return
},
clearProxy: () => {
return
},
}}
>
<AgentRow
Expand Down
3 changes: 3 additions & 0 deletions site/src/components/Workspace/Workspace.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ const Template: Story<WorkspaceProps> = (args) => (
proxies: [],
isLoading: false,
isFetched: true,
clearProxy: () => {
return
},
setProxy: () => {
return
},
Expand Down
62 changes: 0 additions & 62 deletions site/src/contexts/ProxyContext.test.ts

This file was deleted.

Loading