Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion api/handlers/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type Device struct {
Status string `json:"status"`
DeviceType string `json:"device_type"`
MetroPK string `json:"metro_pk"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
ContributorPK string `json:"contributor_pk"`
ContributorCode string `json:"contributor_code"`
UserCount uint64 `json:"user_count"`
Expand Down Expand Up @@ -238,11 +240,14 @@ func (a *API) FetchTopologyData(ctx context.Context) (TopologyResponse, error) {
WHEN ts.total_lamports > 0 THEN COALESCE(ds.stake_sol, 0) * 1e9 / ts.total_lamports * 100
ELSE 0
END as stake_share,
COALESCE(f.lat, 0) as latitude,
COALESCE(f.lng, 0) as longitude,
COALESCE(d.interfaces, '[]') as interfaces
FROM dz_devices_current d
CROSS JOIN total_stake ts
LEFT JOIN device_stats ds ON d.pk = ds.device_pk
LEFT JOIN dz_contributors_current c ON d.contributor_pk = c.pk
LEFT JOIN dz_facilities_current f ON d.location_pk = f.pk

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new LEFT JOIN dz_facilities_current f ON d.location_pk = f.pk references d.location_pk, which is not guaranteed present in every environment the API serves (e.g. remote proxy tables created before the location_pk migration). Every sibling handler that touches this column guards against its absence: devices.go:204-217 builds a fallback query stripping the identical join and retries on isUnknownIdentifierError (ClickHouse code 47); facilities.go:202/:290 and metros.go:246 do the same.

This query runs unguarded inside an errgroup goroutine, so on an environment lacking location_pk the error fails FetchTopologyData wholesale and /api/topology returns 500 — taking down map, globe, and the non-geo graph — while the devices/facilities pages on the same deployment keep working via their fallback.

Fix: mirror the sibling pattern — on isUnknownIdentifierError, retry with the join removed and 0 as latitude, 0 as longitude (the frontend already treats 0/0 as "no facility coords" and falls back to the metro fanout, so it degrades cleanly).

WHERE d.status = 'activated'
`
rows, err := a.envDB(ctx).Query(ctx, query)
Expand All @@ -254,7 +259,7 @@ func (a *API) FetchTopologyData(ctx context.Context) (TopologyResponse, error) {
for rows.Next() {
var d Device
var interfacesJSON string
if err := rows.Scan(&d.PK, &d.Code, &d.Status, &d.DeviceType, &d.MetroPK, &d.ContributorPK, &d.ContributorCode, &d.UserCount, &d.UnicastUsersCount, &d.MulticastSubscribersCount, &d.MulticastPublishersCount, &d.MaxUnicastUsers, &d.MaxMulticastSubscribers, &d.MaxMulticastPublishers, &d.ValidatorCount, &d.StakeSol, &d.StakeShare, &interfacesJSON); err != nil {
if err := rows.Scan(&d.PK, &d.Code, &d.Status, &d.DeviceType, &d.MetroPK, &d.ContributorPK, &d.ContributorCode, &d.UserCount, &d.UnicastUsersCount, &d.MulticastSubscribersCount, &d.MulticastPublishersCount, &d.MaxUnicastUsers, &d.MaxMulticastSubscribers, &d.MaxMulticastPublishers, &d.ValidatorCount, &d.StakeSol, &d.StakeShare, &d.Latitude, &d.Longitude, &interfacesJSON); err != nil {
return err
}
if err := json.Unmarshal([]byte(interfacesJSON), &d.Interfaces); err != nil {
Expand Down
30 changes: 7 additions & 23 deletions web/src/components/topology-globe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Globe from 'react-globe.gl'
import type { GlobeInstance } from 'react-globe.gl'
import { useQuery } from '@tanstack/react-query'
import type { TopologyMetro, TopologyDevice, TopologyLink, TopologyValidator, MultiPathResponse, SimulateLinkRemovalResponse, SimulateLinkAdditionResponse, WhatIfRemovalResponse, MetroDevicePathsResponse } from '@/lib/api'
import { computeDevicePositions } from './topology/devicePositions'
import { fetchISISPaths, fetchISISTopology, fetchCriticalLinks, fetchSimulateLinkRemoval, fetchSimulateLinkAddition, fetchWhatIfRemoval, fetchLinkHealth, fetchTopologyCompare, fetchMetroDevicePaths } from '@/lib/api'
import { useTopology, useMulticastState, TopologyControlBar, TopologyPanel, DeviceDetails, LinkDetails, MetroDetails, ValidatorDetails, EntityLink as TopologyEntityLink, PathModePanel, MetroPathModePanel, CriticalityPanel, WhatIfRemovalPanel, WhatIfAdditionPanel, ImpactPanel, ComparePanel, StakeOverlayPanel, LinkHealthOverlayPanel, TrafficFlowOverlayPanel, MetroClusteringOverlayPanel, ContributorsOverlayPanel, ValidatorsOverlayPanel, DeviceTypeOverlayPanel, LinkTypeOverlayPanel, MulticastTreesOverlayPanel, LINK_TYPE_COLORS, MULTICAST_PUBLISHER_COLORS, type DeviceOption, type MetroOption } from '@/components/topology'
import type { LinkInfo, SelectedItemData } from '@/components/topology'
Expand Down Expand Up @@ -281,22 +282,6 @@ function arcAnimateTime(avgLatencyUs: number): number {
}

// Calculate device position with radial offset around metro center
function calculateDevicePosition(
metroLat: number,
metroLng: number,
deviceIndex: number,
totalDevices: number
): { lat: number; lng: number } {
if (totalDevices === 1) {
return { lat: metroLat, lng: metroLng }
}
const radius = 0.3
const angle = (2 * Math.PI * deviceIndex) / totalDevices
const latOffset = radius * Math.cos(angle)
const lngOffset = radius * Math.sin(angle) / Math.cos(metroLat * Math.PI / 180)
return { lat: metroLat + latOffset, lng: metroLng + lngOffset }
}

export function TopologyGlobe({ metros, devices, links, validators }: TopologyGlobeProps) {
const { resolvedTheme } = useTheme()
const isDark = resolvedTheme === 'dark'
Expand Down Expand Up @@ -600,17 +585,16 @@ export function TopologyGlobe({ metros, devices, links, validators }: TopologyGl
}, [links])

// Device positions
// Facility-anchored when the precise-locations toggle is on, otherwise a tight
// metro-centroid fanout (#652). Shared helper returns [lng, lat]; globe uses {lat, lng}.
const devicePositions = useMemo(() => {
const positions = new Map<string, { lat: number; lng: number }>()
for (const [metroPk, metroDevices] of devicesByMetro) {
const metro = metroMap.get(metroPk)
if (!metro) continue
metroDevices.forEach((device, index) => {
positions.set(device.pk, calculateDevicePosition(metro.latitude, metro.longitude, index, metroDevices.length))
})
const tuples = computeDevicePositions(devices, metroMap, overlays.preciseLocations ? 'facility' : 'fanout')
for (const [pk, [lng, lat]] of tuples) {
positions.set(pk, { lat, lng })
}
return positions
}, [devicesByMetro, metroMap])
}, [devices, metroMap, overlays.preciseLocations])

// ─── Derived overlay maps ────────────────────────────────────────────

Expand Down
49 changes: 7 additions & 42 deletions web/src/components/topology-map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useQuery } from '@tanstack/react-query'
import { useTheme } from '@/hooks/use-theme'
import type { TopologyMetro, TopologyDevice, TopologyLink, TopologyValidator, MultiPathResponse, SimulateLinkRemovalResponse, SimulateLinkAdditionResponse, WhatIfRemovalResponse, MetroDevicePathsResponse } from '@/lib/api'
import { fetchISISPaths, fetchISISTopology, fetchCriticalLinks, fetchSimulateLinkRemoval, fetchSimulateLinkAddition, fetchWhatIfRemoval, fetchLinkHealth, fetchTopologyCompare, fetchMetroDevicePaths } from '@/lib/api'
import { computeDevicePositions } from './topology/devicePositions'
import { useTopology, useMulticastState, TopologyControlBar, TopologyPanel, DeviceDetails, LinkDetails, MetroDetails, ValidatorDetails, EntityLink as TopologyEntityLink, PathModePanel, MetroPathModePanel, CriticalityPanel, WhatIfRemovalPanel, WhatIfAdditionPanel, ImpactPanel, ComparePanel, StakeOverlayPanel, LinkHealthOverlayPanel, TrafficFlowOverlayPanel, MetroClusteringOverlayPanel, ContributorsOverlayPanel, ValidatorsOverlayPanel, DeviceTypeOverlayPanel, LinkTypeOverlayPanel, MulticastTreesOverlayPanel, FlexAlgoOverlayPanel, LINK_TYPE_COLORS, MULTICAST_PUBLISHER_COLORS, type DeviceOption, type MetroOption } from '@/components/topology'
import { useActiveOpsTickets } from '@/hooks/use-ops-tickets'
import { opsTicketUrl } from '@/lib/ops-api'
Expand Down Expand Up @@ -252,27 +253,6 @@ type SelectedItem =
| { type: 'metro'; data: HoveredMetroInfo }
| { type: 'validator'; data: HoveredValidatorInfo }

// Calculate device position with radial offset for multiple devices at same metro
function calculateDevicePosition(
metroLat: number,
metroLng: number,
deviceIndex: number,
totalDevices: number
): [number, number] {
if (totalDevices === 1) {
return [metroLng, metroLat]
}

// Distribute devices in a circle around metro center
const radius = 0.3 // degrees offset
const angle = (2 * Math.PI * deviceIndex) / totalDevices
const latOffset = radius * Math.cos(angle)
// Adjust for latitude distortion
const lngOffset = radius * Math.sin(angle) / Math.cos(metroLat * Math.PI / 180)

return [metroLng + lngOffset, metroLat + latOffset]
}

// Calculate curved path between two points (returns GeoJSON coordinates [lng, lat])
function calculateCurvedPath(
start: [number, number],
Expand Down Expand Up @@ -924,27 +904,12 @@ export function TopologyMap({ metros, devices, links, validators }: TopologyMapP
return map
}, [links])

// Calculate device positions
const devicePositions = useMemo(() => {
const positions = new Map<string, [number, number]>()

for (const [metroPk, metroDevices] of devicesByMetro) {
const metro = metroMap.get(metroPk)
if (!metro) continue

metroDevices.forEach((device, index) => {
const pos = calculateDevicePosition(
metro.latitude,
metro.longitude,
index,
metroDevices.length
)
positions.set(device.pk, pos)
})
}

return positions
}, [devicesByMetro, metroMap])
// Calculate device positions ([lng, lat]). Facility-anchored when the
// precise-locations toggle is on, otherwise a tight metro-centroid fanout (#652).
const devicePositions = useMemo(
() => computeDevicePositions(devices, metroMap, overlays.preciseLocations ? 'facility' : 'fanout'),
[devices, metroMap, overlays.preciseLocations],
)

// Map style based on theme
const mapStyle = useMemo(() => createMapStyle(isDark), [isDark])
Expand Down
3 changes: 3 additions & 0 deletions web/src/components/topology/TopologyContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface OverlayState {
// Independent overlays
multicastTrees: boolean // Multicast tree visualization
userCounts: boolean // Show U/S/P user count badges on devices
preciseLocations: boolean // Anchor device markers at real facility coordinates (vs metro-centroid fanout)
}

// Context value type
Expand Down Expand Up @@ -125,6 +126,7 @@ function parseOverlaysFromUrl(param: string | null, view: 'map' | 'graph' | 'glo
flexAlgo: false,
multicastTrees: false,
userCounts: false,
preciseLocations: false,
}
if (!param) return defaultState

Expand All @@ -145,6 +147,7 @@ function parseOverlaysFromUrl(param: string | null, view: 'map' | 'graph' | 'glo
flexAlgo: false,
multicastTrees: false,
userCounts: false,
preciseLocations: false,
}
const activeOverlays = param.split(',').filter(Boolean)
for (const overlay of activeOverlays) {
Expand Down
13 changes: 13 additions & 0 deletions web/src/components/topology/TopologyControlBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Activity,
BarChart3,
MapPin,
LocateFixed,
GitCompare,
ChevronLeft,
ChevronRight,
Expand Down Expand Up @@ -491,6 +492,18 @@ export function TopologyControlBar({
/>
)}

{(view === 'map' || view === 'globe') && (
<NavItem
icon={<LocateFixed className="h-3.5 w-3.5" />}
label="Precise locations"
tooltip="Place devices at their real facility coordinates instead of spreading them around the metro center"
onClick={() => handleToggleOverlay('preciseLocations')}
active={overlays.preciseLocations}
activeColor="blue"
collapsed={collapsed}
/>
)}

{/* Link Overlays */}
<SectionHeader title="Link Overlays" collapsed={collapsed} />

Expand Down
77 changes: 77 additions & 0 deletions web/src/components/topology/devicePositions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, it, expect } from 'vitest'
import {
computeDevicePositions,
FANOUT_RADIUS_DEG,
FACILITY_JITTER_DEG,
type PositionableDevice,
type PositionableMetro,
} from './devicePositions'

const metroMap = new Map<string, PositionableMetro>([
['metro-nyc', { latitude: 40.779, longitude: -74.072 }],
])

function dev(pk: string, over: Partial<PositionableDevice> = {}): PositionableDevice {
return { pk, metro_pk: 'metro-nyc', latitude: 0, longitude: 0, ...over }
}

function dist(a: [number, number], b: [number, number]): number {
return Math.hypot(a[0] - b[0], a[1] - b[1])
}

describe('computeDevicePositions — fanout mode', () => {
it('places a lone device at the metro center', () => {
const pos = computeDevicePositions([dev('d1')], metroMap, 'fanout')
expect(pos.get('d1')).toEqual([-74.072, 40.779])
})

it('spreads co-metro devices within the (small) fanout radius and keeps them distinct', () => {
const devices = [dev('d1'), dev('d2'), dev('d3'), dev('d4')]
const pos = computeDevicePositions(devices, metroMap, 'fanout')
const center: [number, number] = [-74.072, 40.779]
const coords = devices.map(d => pos.get(d.pk)!)
// every marker stays close to the metro center (latitude offset bounded by the radius)
for (const c of coords) {
expect(Math.abs(c[1] - center[1])).toBeLessThanOrEqual(FANOUT_RADIUS_DEG + 1e-9)
expect(c).not.toEqual(center)
}
// markers are distinct from one another
const keys = new Set(coords.map(c => `${c[0]},${c[1]}`))
expect(keys.size).toBe(4)
})
})

describe('computeDevicePositions — facility mode', () => {
it('anchors a lone device exactly at its facility coordinates', () => {
const pos = computeDevicePositions(
[dev('d1', { latitude: 40.7968, longitude: -74.03088 })],
metroMap,
'facility',
)
expect(pos.get('d1')).toEqual([-74.03088, 40.7968])
})

it('jitters co-located devices around the shared facility, within the jitter radius', () => {
const fac = { latitude: 40.7968, longitude: -74.03088 }
const devices = [
dev('d1', fac),
dev('d2', fac),
dev('d3', fac),
]
const pos = computeDevicePositions(devices, metroMap, 'facility')
const anchor: [number, number] = [fac.longitude, fac.latitude]
const coords = devices.map(d => pos.get(d.pk)!)
for (const c of coords) {
// stays within the small jitter radius of the real facility (not flung away)
expect(dist(c, anchor)).toBeLessThanOrEqual(FACILITY_JITTER_DEG / Math.cos(fac.latitude * Math.PI / 180) + 1e-6)
}
const keys = new Set(coords.map(c => `${c[0]},${c[1]}`))
expect(keys.size).toBe(3)
})

it('falls back to the metro fanout when a device has no facility coordinates', () => {
// facility coords 0/0 => missing; should NOT end up at [0,0]
const pos = computeDevicePositions([dev('d1')], metroMap, 'facility')
expect(pos.get('d1')).toEqual([-74.072, 40.779])
})
})
Loading
Loading