feat(dashboard): add bidirectional Inbound (Luar -> Agent) and Outbound (Agent -> Luar) animated lines on GlobeMap
This commit is contained in:
1 parent
4f7fd999a4
commit
7e65eba01f
3 files changed
+117
-70
No files matched your search
@@ -125,6 +125,49 @@ export default function SummaryPage() {
|
||||
const arcs: any[] = [];
|
||||
const minShareThreshold = countries.data.length > 3 ? totalTraffic * 0.0025 : 0;
|
||||
|
||||
const pushBidirectionalArcs = (
|
||||
origin: GeoLocation,
|
||||
dest: GeoLocation,
|
||||
downloadBytes: number,
|
||||
uploadBytes: number,
|
||||
countryCode: string
|
||||
) => {
|
||||
const dl = downloadBytes || 0;
|
||||
const ul = uploadBytes || 0;
|
||||
if (ul > 0) {
|
||||
arcs.push({
|
||||
startLat: origin.lat,
|
||||
startLng: origin.lng,
|
||||
endLat: dest.lat,
|
||||
endLng: dest.lng,
|
||||
fromLabel: origin.label,
|
||||
toLabel: dest.label,
|
||||
value: ul,
|
||||
download: dl,
|
||||
upload: ul,
|
||||
countryCode,
|
||||
direction: 'outbound',
|
||||
curveSign: 1,
|
||||
});
|
||||
}
|
||||
if (dl > 0) {
|
||||
arcs.push({
|
||||
startLat: dest.lat,
|
||||
startLng: dest.lng,
|
||||
endLat: origin.lat,
|
||||
endLng: origin.lng,
|
||||
fromLabel: dest.label,
|
||||
toLabel: origin.label,
|
||||
value: dl,
|
||||
download: dl,
|
||||
upload: ul,
|
||||
countryCode,
|
||||
direction: 'inbound',
|
||||
curveSign: -0.65,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
for (const row of countries.data) {
|
||||
const traffic = (row.download || 0) + (row.upload || 0);
|
||||
if (traffic === 0 || traffic < minShareThreshold) continue;
|
||||
@@ -134,50 +177,17 @@ export default function SummaryPage() {
|
||||
|
||||
if (activeAgentUuid) {
|
||||
const origin = resolveOriginForAgent(activeAgentUuid, activeAgentLabel);
|
||||
arcs.push({
|
||||
startLat: origin.lat,
|
||||
startLng: origin.lng,
|
||||
endLat: dest.lat,
|
||||
endLng: dest.lng,
|
||||
fromLabel: origin.label,
|
||||
toLabel: dest.label,
|
||||
value: traffic,
|
||||
download: row.download,
|
||||
upload: row.upload,
|
||||
countryCode: row.country_code,
|
||||
});
|
||||
pushBidirectionalArcs(origin, dest, row.download, row.upload, row.country_code);
|
||||
} else if (Array.isArray(row.agent_breakdown) && row.agent_breakdown.length > 0) {
|
||||
for (const ag of row.agent_breakdown) {
|
||||
const agTraffic = (ag.download || 0) + (ag.upload || 0);
|
||||
if (agTraffic === 0) continue;
|
||||
const origin = resolveOriginForAgent(ag.agent_uuid);
|
||||
arcs.push({
|
||||
startLat: origin.lat,
|
||||
startLng: origin.lng,
|
||||
endLat: dest.lat,
|
||||
endLng: dest.lng,
|
||||
fromLabel: origin.label,
|
||||
toLabel: dest.label,
|
||||
value: agTraffic,
|
||||
download: ag.download,
|
||||
upload: ag.upload,
|
||||
countryCode: row.country_code,
|
||||
});
|
||||
pushBidirectionalArcs(origin, dest, ag.download, ag.upload, row.country_code);
|
||||
}
|
||||
} else {
|
||||
const origin = resolveOriginForAgent(null);
|
||||
arcs.push({
|
||||
startLat: origin.lat,
|
||||
startLng: origin.lng,
|
||||
endLat: dest.lat,
|
||||
endLng: dest.lng,
|
||||
fromLabel: origin.label,
|
||||
toLabel: dest.label,
|
||||
value: traffic,
|
||||
download: row.download,
|
||||
upload: row.upload,
|
||||
countryCode: row.country_code,
|
||||
});
|
||||
pushBidirectionalArcs(origin, dest, row.download, row.upload, row.country_code);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import React, { useMemo, useState } from 'react';
|
||||
import React, { useState } from 'react';
|
||||
import { ComposableMap, Geographies, Geography, Line, Marker, ZoomableGroup } from "react-simple-maps";
|
||||
import { useTheme } from "next-themes";
|
||||
import { useGlobeData, GlobeDataInput } from './useGlobeData';
|
||||
@@ -22,7 +22,6 @@ export const GlobeMap = ({ data }: GlobeMapProps) => {
|
||||
|
||||
const { arcsData, pointsData, labelsData } = useGlobeData(data, isLight);
|
||||
|
||||
const mapBgColor = "transparent";
|
||||
const mapBorderColor = isLight ? "#ffffff" : "#0ea5e9";
|
||||
const mapFillColor = isLight ? "#4ade80" : "#0f172a";
|
||||
const mapHoverFill = isLight ? "#22c55e" : "#1e293b";
|
||||
@@ -78,6 +77,7 @@ export const GlobeMap = ({ data }: GlobeMapProps) => {
|
||||
{arcsData.map((arc: any, i: number) => {
|
||||
const isLocal = arc.startLng === arc.endLng && arc.startLat === arc.endLat;
|
||||
const color = Array.isArray(arc.color) ? arc.color[0] : arc.color || '#0ea5e9';
|
||||
const curveSign = typeof arc.curveSign === 'number' ? arc.curveSign : 1;
|
||||
|
||||
if (isLocal) {
|
||||
return (
|
||||
@@ -87,23 +87,18 @@ export const GlobeMap = ({ data }: GlobeMapProps) => {
|
||||
);
|
||||
}
|
||||
|
||||
// Force the line to go straight across the map (the "front")
|
||||
// by interpolating multiple points. This prevents d3-geo from
|
||||
// wrapping around the dateline for long distances.
|
||||
const coordinates: [number, number][] = [];
|
||||
const steps = 30;
|
||||
const dist = Math.sqrt(
|
||||
Math.pow(arc.endLng - arc.startLng, 2) +
|
||||
Math.pow(arc.endLat - arc.startLat, 2)
|
||||
);
|
||||
const height = Math.min(dist * 0.15, 25);
|
||||
|
||||
for (let j = 0; j <= steps; j++) {
|
||||
const t = j / steps;
|
||||
let lng = arc.startLng + (arc.endLng - arc.startLng) * t;
|
||||
|
||||
// Dynamic arc height based on distance
|
||||
const dist = Math.sqrt(
|
||||
Math.pow(arc.endLng - arc.startLng, 2) +
|
||||
Math.pow(arc.endLat - arc.startLat, 2)
|
||||
);
|
||||
const height = Math.min(dist * 0.15, 25);
|
||||
let lat = arc.startLat + (arc.endLat - arc.startLat) * t + Math.sin(t * Math.PI) * height;
|
||||
|
||||
const lng = arc.startLng + (arc.endLng - arc.startLng) * t;
|
||||
const lat = arc.startLat + (arc.endLat - arc.startLat) * t + Math.sin(t * Math.PI) * height * curveSign;
|
||||
coordinates.push([lng, lat]);
|
||||
}
|
||||
|
||||
@@ -147,6 +142,22 @@ export const GlobeMap = ({ data }: GlobeMapProps) => {
|
||||
))}
|
||||
</ZoomableGroup>
|
||||
</ComposableMap>
|
||||
|
||||
{/* Direction & Node Legend Overlay */}
|
||||
<div className="absolute bottom-3 left-3 z-10 flex flex-wrap items-center gap-3 rounded-lg border border-white/10 bg-slate-950/80 px-3 py-1.5 text-[10px] text-slate-300 backdrop-blur-md">
|
||||
<span className="flex items-center gap-1.5">
|
||||
<span className="inline-block h-2 w-2 rounded-full bg-rose-500" />
|
||||
<span>Agent Server</span>
|
||||
</span>
|
||||
<span className="flex items-center gap-1.5">
|
||||
<span className="inline-block h-0.5 w-4 rounded bg-sky-400" />
|
||||
<span>Outbound (Agent → Luar)</span>
|
||||
</span>
|
||||
<span className="flex items-center gap-1.5">
|
||||
<span className="inline-block h-0.5 w-4 rounded bg-emerald-400" />
|
||||
<span>Inbound (Luar → Agent)</span>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -13,14 +13,22 @@ export interface GlobeDataInput {
|
||||
download: number;
|
||||
upload: number;
|
||||
countryCode?: string;
|
||||
direction?: 'outbound' | 'inbound';
|
||||
curveSign?: number;
|
||||
}
|
||||
|
||||
export function useGlobeData(data: GlobeDataInput[], isLight: boolean) {
|
||||
const arcsData = useMemo(() => {
|
||||
return data.map(d => ({
|
||||
...d,
|
||||
color: isLight ? ['#38bdf8', '#0284c7'] : ['#0ea5e9', '#38bdf8'],
|
||||
}));
|
||||
return data.map(d => {
|
||||
const isInbound = d.direction === 'inbound';
|
||||
const color = isInbound
|
||||
? (isLight ? ['#10b981', '#059669'] : ['#34d399', '#10b981'])
|
||||
: (isLight ? ['#38bdf8', '#0284c7'] : ['#0ea5e9', '#38bdf8']);
|
||||
return {
|
||||
...d,
|
||||
color,
|
||||
};
|
||||
});
|
||||
}, [data, isLight]);
|
||||
|
||||
const pointsData = useMemo(() => {
|
||||
@@ -29,16 +37,25 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) {
|
||||
const destSet = new Set<string>();
|
||||
|
||||
arcsData.forEach(arc => {
|
||||
const originKey = `${arc.startLat},${arc.startLng}`;
|
||||
const destKey = `${arc.endLat},${arc.endLng}`;
|
||||
const isInbound = arc.direction === 'inbound';
|
||||
const agentLat = isInbound ? arc.endLat : arc.startLat;
|
||||
const agentLng = isInbound ? arc.endLng : arc.startLng;
|
||||
const agentLabel = isInbound ? arc.toLabel : arc.fromLabel;
|
||||
|
||||
const extLat = isInbound ? arc.startLat : arc.endLat;
|
||||
const extLng = isInbound ? arc.startLng : arc.endLng;
|
||||
const extLabel = isInbound ? arc.fromLabel : arc.toLabel;
|
||||
|
||||
const originKey = `${agentLat},${agentLng}`;
|
||||
const destKey = `${extLat},${extLng}`;
|
||||
|
||||
if (!destSet.has(destKey)) {
|
||||
points.push({
|
||||
lat: arc.endLat,
|
||||
lng: arc.endLng,
|
||||
lat: extLat,
|
||||
lng: extLng,
|
||||
size: 0.1,
|
||||
color: isLight ? '#0284c7' : '#38bdf8',
|
||||
name: arc.toLabel,
|
||||
name: extLabel,
|
||||
value: arc.value,
|
||||
upload: arc.upload,
|
||||
download: arc.download,
|
||||
@@ -49,11 +66,11 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) {
|
||||
|
||||
if (!originSet.has(originKey)) {
|
||||
points.push({
|
||||
lat: arc.startLat,
|
||||
lng: arc.startLng,
|
||||
lat: agentLat,
|
||||
lng: agentLng,
|
||||
size: 0.2,
|
||||
color: '#f43f5e',
|
||||
name: arc.fromLabel,
|
||||
name: agentLabel,
|
||||
value: 0,
|
||||
upload: 0,
|
||||
download: 0,
|
||||
@@ -72,18 +89,27 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) {
|
||||
const destSet = new Set();
|
||||
|
||||
arcsData.forEach(arc => {
|
||||
const originKey = `${arc.startLat},${arc.startLng}`;
|
||||
const destKey = `${arc.endLat},${arc.endLng}`;
|
||||
const isInbound = arc.direction === 'inbound';
|
||||
const agentLat = isInbound ? arc.endLat : arc.startLat;
|
||||
const agentLng = isInbound ? arc.endLng : arc.startLng;
|
||||
const agentLabel = isInbound ? arc.toLabel : arc.fromLabel;
|
||||
|
||||
const extLat = isInbound ? arc.startLat : arc.endLat;
|
||||
const extLng = isInbound ? arc.startLng : arc.endLng;
|
||||
const extLabel = isInbound ? arc.fromLabel : arc.toLabel;
|
||||
|
||||
const originKey = `${agentLat},${agentLng}`;
|
||||
const destKey = `${extLat},${extLng}`;
|
||||
|
||||
if (!destSet.has(destKey)) {
|
||||
// Shift and stagger coordinates (Southeast Asia, Europe, Caribbean, Indian Ocean, Latin America)
|
||||
const { lat, lng } = getStaggeredCoordinates(arc.countryCode || '', arc.endLat, arc.endLng);
|
||||
const { lat, lng } = getStaggeredCoordinates(arc.countryCode || '', extLat, extLng);
|
||||
const size = SMALL_LABEL_COUNTRIES.has(arc.countryCode || '') ? 0.9 : 1.4;
|
||||
|
||||
labels.push({
|
||||
lat,
|
||||
lng,
|
||||
name: arc.toLabel,
|
||||
name: extLabel,
|
||||
color: isLight ? '#0f172a' : '#f8fafc',
|
||||
size
|
||||
});
|
||||
@@ -92,9 +118,9 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) {
|
||||
|
||||
if (!originSet.has(originKey)) {
|
||||
labels.push({
|
||||
lat: arc.startLat,
|
||||
lng: arc.startLng,
|
||||
name: arc.fromLabel,
|
||||
lat: agentLat,
|
||||
lng: agentLng,
|
||||
name: agentLabel,
|
||||
color: isLight ? '#0f172a' : '#f8fafc',
|
||||
size: 1.4
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user