From 2482e0c670e895e28d46ed6903d8034957409e47 Mon Sep 17 00:00:00 2001 From: Yoga Pratama Date: Mon, 9 Nov 2026 16:34:06 +0700 Subject: [PATCH] feat(dashboard): compact vertical map legend, clickable node detail popup, and full 6-bar labels in Top Apps/Protocols --- src/components/dashboard/TopWidgets.tsx | 242 +++++++++++++--------- src/components/ui/GlobeMap.tsx | 265 ++++++++++++++---------- src/components/ui/useGlobeData.ts | 111 +++++----- 3 files changed, 356 insertions(+), 262 deletions(-) diff --git a/src/components/dashboard/TopWidgets.tsx b/src/components/dashboard/TopWidgets.tsx index b2693ce..653afd6 100644 --- a/src/components/dashboard/TopWidgets.tsx +++ b/src/components/dashboard/TopWidgets.tsx @@ -3,10 +3,9 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/Card"; import { ResponsiveContainer, BarChart, Bar, CartesianGrid, XAxis, YAxis, - Tooltip as RechartsTooltip, PieChart, Pie, Cell + Tooltip as RechartsTooltip, Cell } from "recharts"; import { fmtBytes, formatAppLabel } from "@/lib/utils"; - import { AppStat, ProtocolStat } from "@/lib/api-types"; interface TopWidgetsProps { @@ -18,11 +17,13 @@ interface TopWidgetsProps { }; } -const COLORS = ['#3b82f6', '#10b981', '#f59e0b', '#8b5cf6', '#ec4899', '#3b82f6']; +const APP_COLORS = ['#8b5cf6', '#3b82f6', '#06b6d4', '#10b981', '#f59e0b', '#ec4899', '#6366f1']; +const PROTO_COLORS = ['#10b981', '#14b8a6', '#0ea5e9', '#8b5cf6', '#f59e0b', '#f43f5e', '#6366f1']; export function TopWidgets({ topApps, topProtocols }: TopWidgetsProps) { const appsData = (topApps.data || []).map(app => ({ ...app, + display_label: formatAppLabel(app.app_label), total_bytes: (app.download || 0) + (app.upload || 0) })); @@ -30,124 +31,167 @@ export function TopWidgets({ topApps, topProtocols }: TopWidgetsProps) { ...p, total: (p.download || 0) + (p.upload || 0) })); + return (
- + Top Apps - + {(!appsData || appsData.length === 0) ? (
No Data
) : ( -
- - - - { - const formatted = formatAppLabel(val); - return formatted.length > 12 ? `${formatted.substring(0, 10)}...` : formatted; - }} - /> - { - if (value === 0) return '0'; - const k = 1024; - const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; - const i = Math.floor(Math.log(value) / Math.log(k)); - const scaled = parseFloat((value / Math.pow(k, i)).toFixed(1)); - return `${scaled}${sizes[i].charAt(0)}`; - }} - /> - [fmtBytes(value as number), 'Total Bandwidth']} - /> - - - -
+ <> +
+ + + + { + const str = String(val || ''); + return str.length > 9 ? `${str.substring(0, 8)}..` : str; + }} + /> + { + if (value === 0) return '0'; + const k = 1024; + const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; + const i = Math.floor(Math.log(value) / Math.log(k)); + const scaled = parseFloat((value / Math.pow(k, i)).toFixed(1)); + return `${scaled}${sizes[i].charAt(0)}`; + }} + /> + [fmtBytes(value as number), 'Total Bandwidth']} + /> + + {appsData.map((_, idx) => ( + + ))} + + + +
+
+ {appsData.map((app, idx) => ( +
+ + + {app.display_label} + + {fmtBytes(app.total_bytes)} +
+ ))} +
+ )}
- + Top Protocols - + {(!protocolsData || protocolsData.length === 0) ? (
No Data
) : ( -
- - - - { - const formatted = val || ''; - return formatted.length > 12 ? `${formatted.substring(0, 10)}...` : formatted; - }} - /> - { - if (value === 0) return '0'; - const k = 1024; - const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; - const i = Math.floor(Math.log(value) / Math.log(k)); - const scaled = parseFloat((value / Math.pow(k, i)).toFixed(1)); - return `${scaled}${sizes[i].charAt(0)}`; - }} - /> - [fmtBytes(value as number), 'Total Traffic']} - /> - - - -
+ <> +
+ + + + { + const formatted = String(val || ''); + return formatted.length > 9 ? `${formatted.substring(0, 8)}..` : formatted; + }} + /> + { + if (value === 0) return '0'; + const k = 1024; + const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; + const i = Math.floor(Math.log(value) / Math.log(k)); + const scaled = parseFloat((value / Math.pow(k, i)).toFixed(1)); + return `${scaled}${sizes[i].charAt(0)}`; + }} + /> + [fmtBytes(value as number), 'Total Traffic']} + /> + + {protocolsData.map((_, idx) => ( + + ))} + + + +
+
+ {protocolsData.map((proto, idx) => ( +
+ + + {proto.protocol_label} + + {fmtBytes(proto.total)} +
+ ))} +
+ )}
diff --git a/src/components/ui/GlobeMap.tsx b/src/components/ui/GlobeMap.tsx index f484a57..b2920ce 100644 --- a/src/components/ui/GlobeMap.tsx +++ b/src/components/ui/GlobeMap.tsx @@ -3,7 +3,9 @@ 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'; +import { X, MapPin, ArrowDownLeft, ArrowUpRight, Activity } from "lucide-react"; +import { fmtBytes } from "@/lib/utils"; +import { useGlobeData, GlobeDataInput, GlobePointData } from './useGlobeData'; const geoUrl = "/ne_110m_admin_0_countries.geojson"; @@ -14,15 +16,12 @@ interface GlobeMapProps { export const GlobeMap = ({ data }: GlobeMapProps) => { const { theme } = useTheme(); const isLight = theme === 'light'; - const [position, setPosition] = useState({ coordinates: [0, 20], zoom: 1 }); - function handleMoveEnd(newPosition: any) { - setPosition(newPosition); - } + const [selectedPoint, setSelectedPoint] = useState(null); const { arcsData, pointsData, labelsData } = useGlobeData(data, isLight); - const destPoints = pointsData.filter((pt: any) => !pt.isOrigin); - const originPoints = pointsData.filter((pt: any) => pt.isOrigin); + const destPoints = pointsData.filter((pt) => !pt.isOrigin); + const originPoints = pointsData.filter((pt) => pt.isOrigin); const mapBorderColor = isLight ? "#ffffff" : "#0ea5e9"; const mapFillColor = isLight ? "#4ade80" : "#0f172a"; @@ -31,28 +30,12 @@ export const GlobeMap = ({ data }: GlobeMapProps) => { return (
- - {({ geographies }) => - geographies.map((geo) => ( - - )) - } - + {({ geographies }) => + geographies.map((geo) => ( + setSelectedPoint(null)} + style={{ + default: { outline: "none" }, + hover: { fill: mapHoverFill, outline: "none" }, + pressed: { outline: "none" }, + }} + /> + )) + } + {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 ( @@ -110,10 +91,7 @@ export const GlobeMap = ({ data }: GlobeMapProps) => { 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 dist = Math.hypot(arc.endLng - arc.startLng, arc.endLat - arc.startLat); const height = Math.min(dist * 0.15, 25); for (let j = 0; j <= steps; j++) { @@ -138,68 +116,125 @@ export const GlobeMap = ({ data }: GlobeMapProps) => { ); })} - {/* External Country Destination Markers */} - {destPoints.map((pt: any, i: number) => ( - - - - - ))} - - {/* Agent Server Origin Markers (Rendered on top with large pulsing red beacon) */} - {originPoints.map((pt: any, i: number) => ( - - - - - - ))} - - {labelsData.map((lbl: any, i: number) => ( - - ( + { e.stopPropagation(); setSelectedPoint(pt); }} + style={{ default: { cursor: "pointer" }, hover: { cursor: "pointer" }, pressed: { cursor: "pointer" } }} > - {lbl.name} - - - ))} + + + + + ))} + + {originPoints.map((pt, i) => ( + { e.stopPropagation(); setSelectedPoint(pt); }} + style={{ default: { cursor: "pointer" }, hover: { cursor: "pointer" }, pressed: { cursor: "pointer" } }} + > + + + + + + ))} + + {labelsData.map((lbl: any, i: number) => ( + { e.stopPropagation(); if (lbl.point) setSelectedPoint(lbl.point); }} + style={{ default: { cursor: "pointer" }, hover: { cursor: "pointer" }, pressed: { cursor: "pointer" } }} + > + + {lbl.name} + + + ))} - {/* Keterangan / Noted Warna & Cara Baca Peta */} -
-
- Keterangan Peta & Warna (Cara Baca): + {selectedPoint && ( +
+
+
+ + + {selectedPoint.isOrigin ? "Lokasi Server Agent" : "Negara Tujuan / Luar"} + +

{selectedPoint.name}

+

+ Lat: {selectedPoint.lat.toFixed(4)}, Lng: {selectedPoint.lng.toFixed(4)} +

+
+ +
+
+
+ + Download (Luar → Agent) + + {fmtBytes(selectedPoint.download || 0)} +
+
+ + Upload (Agent → Luar) + + {fmtBytes(selectedPoint.upload || 0)} +
+
+ + Total Trafik + + + {fmtBytes((selectedPoint.download || 0) + (selectedPoint.upload || 0))} + +
+
-
-
- - - - - Titik Merah: Lokasi Server Agent -
-
- - Titik Biru: Negara Tujuan / Luar -
-
- - Garis Biru (→): Upload (Agent ke Luar) -
-
- - Garis Hijau (←): Download (Luar ke Agent) -
+ )} + + {/* Legenda Berderet ke Bawah */} +
+
+ + + + + Titik Merah: Lokasi Server Agent +
+
+ + Titik Biru: Negara Tujuan / Luar +
+
+ + Garis Biru (→): Upload (Agent ke Luar) +
+
+ + Garis Hijau (←): Download (Luar ke Agent)
diff --git a/src/components/ui/useGlobeData.ts b/src/components/ui/useGlobeData.ts index 96e2907..b2ddcf4 100644 --- a/src/components/ui/useGlobeData.ts +++ b/src/components/ui/useGlobeData.ts @@ -17,6 +17,19 @@ export interface GlobeDataInput { curveSign?: number; } +export interface GlobePointData { + lat: number; + lng: number; + size: number; + color: string; + name: string; + value: number; + upload: number; + download: number; + isOrigin: boolean; + countryCode?: string; +} + export function useGlobeData(data: GlobeDataInput[], isLight: boolean) { const arcsData = useMemo(() => { return data.map(d => { @@ -32,9 +45,9 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) { }, [data, isLight]); const pointsData = useMemo(() => { - const points: any[] = []; - const originSet = new Set(); - const destSet = new Set(); + const originMap = new Map(); + const destMap = new Map(); + const pairSeen = new Set(); arcsData.forEach(arc => { const isInbound = arc.direction === 'inbound'; @@ -48,24 +61,33 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) { const originKey = `${agentLat},${agentLng}`; const destKey = `${extLat},${extLng}`; - - if (!destSet.has(destKey)) { - points.push({ + const pairKey = `${originKey}->${destKey}`; + const isFirstForPair = !pairSeen.has(pairKey); + if (isFirstForPair) pairSeen.add(pairKey); + + if (!destMap.has(destKey)) { + destMap.set(destKey, { lat: extLat, lng: extLng, size: 0.1, color: isLight ? '#0284c7' : '#38bdf8', name: extLabel, - value: arc.value, - upload: arc.upload, - download: arc.download, - isOrigin: false + value: 0, + upload: 0, + download: 0, + isOrigin: false, + countryCode: arc.countryCode, }); - destSet.add(destKey); } - - if (!originSet.has(originKey)) { - points.push({ + if (isFirstForPair) { + const dp = destMap.get(destKey)!; + dp.download += (arc.download || 0); + dp.upload += (arc.upload || 0); + dp.value = dp.download + dp.upload; + } + + if (!originMap.has(originKey)) { + originMap.set(originKey, { lat: agentLat, lng: agentLng, size: 0.2, @@ -74,62 +96,55 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) { value: 0, upload: 0, download: 0, - isOrigin: true + isOrigin: true, + countryCode: 'ID', }); - originSet.add(originKey); + } + if (isFirstForPair) { + const op = originMap.get(originKey)!; + op.download += (arc.download || 0); + op.upload += (arc.upload || 0); + op.value = op.download + op.upload; } }); - return points; + return [...Array.from(destMap.values()), ...Array.from(originMap.values())]; }, [arcsData, isLight]); const labelsData = useMemo(() => { const labels: any[] = []; - const originSet = new Set(); - const destSet = new Set(); + const seen = new Set(); - arcsData.forEach(arc => { - 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; + pointsData.forEach(pt => { + const key = `${pt.isOrigin ? 'orig' : 'dest'}:${pt.lat},${pt.lng}`; + if (seen.has(key)) return; + seen.add(key); - 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 || '', extLat, extLng); - const size = SMALL_LABEL_COUNTRIES.has(arc.countryCode || '') ? 0.9 : 1.4; - + if (!pt.isOrigin) { + const { lat, lng } = getStaggeredCoordinates(pt.countryCode || '', pt.lat, pt.lng); + const size = SMALL_LABEL_COUNTRIES.has(pt.countryCode || '') ? 0.9 : 1.4; labels.push({ lat, lng, - name: extLabel, + name: pt.name, color: isLight ? '#0f172a' : '#f8fafc', - size + size, + point: pt, }); - destSet.add(destKey); - } - - if (!originSet.has(originKey)) { + } else { labels.push({ - lat: agentLat, - lng: agentLng, - name: agentLabel, + lat: pt.lat, + lng: pt.lng, + name: pt.name, color: isLight ? '#0f172a' : '#f8fafc', - size: 1.4 + size: 1.4, + point: pt, }); - originSet.add(originKey); } }); return labels; - }, [arcsData, isLight]); + }, [pointsData, isLight]); return { arcsData, pointsData, labelsData }; }