feat(dashboard): show top IPs and domains/apps in GlobeMap point popup

This commit is contained in:
ypratama committed 2026-11-10 13:22:17 +07:00
1 parent 87dc19f63f
commit 7c70d7c001
6 files changed
+270 -60

No files matched your search

+45 -4
View File
@@ -13,14 +13,24 @@ router.get('/countries', async (req, res) => {
const matchBase = getBaseFilter(req, timeFilter);
await applySubnetFilterToQuery(matchBase, 'src_ip');
// Aggregate directly from per-agent Flow collection to avoid upstream CountryStat global pollution
// Aggregate directly from per-agent Flow collection including domain & app_label per dst_ip
const raw = await Flow.aggregate([
{ $match: { ...matchBase, dst_ip: { $ne: null, $ne: '' } } },
{ $group: {
_id: { dst_ip: '$dst_ip', agent_uuid: '$agent_uuid' },
download: { $sum: '$download' },
upload: { $sum: '$upload' },
flow_count: { $sum: 1 }
flow_count: { $sum: 1 },
domain: { $max: { $ifNull: ['$domain', '$sni_hostname'] } },
app_label: {
$max: {
$cond: [
{ $and: [{ $ne: ['$app_label', null] }, { $ne: ['$app_label', ''] }, { $ne: ['$app_label', 'Unknown'] }] },
'$app_label',
null
]
}
}
}}
]);
@@ -44,7 +54,9 @@ router.get('/countries', async (req, res) => {
download: 0,
upload: 0,
flow_count: 0,
byAgent: {}
byAgent: {},
byIp: {},
byDomain: {}
};
}
countryMap[cc].download += dl;
@@ -57,6 +69,33 @@ router.get('/countries', async (req, res) => {
countryMap[cc].byAgent[agentUuid].download += dl;
countryMap[cc].byAgent[agentUuid].upload += ul;
countryMap[cc].byAgent[agentUuid].flow_count += fc;
if (!countryMap[cc].byIp[dstIp]) {
countryMap[cc].byIp[dstIp] = {
ip: dstIp,
domain: r.domain || null,
app_label: r.app_label || null,
download: 0,
upload: 0,
total: 0
};
}
const ipEntry = countryMap[cc].byIp[dstIp];
ipEntry.download += dl;
ipEntry.upload += ul;
ipEntry.total += (dl + ul);
if (!ipEntry.domain && r.domain) ipEntry.domain = r.domain;
if (!ipEntry.app_label && r.app_label) ipEntry.app_label = r.app_label;
const domKey = (r.domain && String(r.domain).trim()) || (r.app_label && String(r.app_label).trim()) || null;
if (domKey && domKey !== 'Unknown') {
if (!countryMap[cc].byDomain[domKey]) {
countryMap[cc].byDomain[domKey] = { domain: domKey, download: 0, upload: 0, total: 0 };
}
countryMap[cc].byDomain[domKey].download += dl;
countryMap[cc].byDomain[domKey].upload += ul;
countryMap[cc].byDomain[domKey].total += (dl + ul);
}
}
let data = Object.values(countryMap)
@@ -66,7 +105,9 @@ router.get('/countries', async (req, res) => {
download: c.download,
upload: c.upload,
flow_count: c.flow_count,
agent_breakdown: Object.values(c.byAgent).sort((a, b) => (b.download + b.upload) - (a.download + a.upload))
agent_breakdown: Object.values(c.byAgent).sort((a, b) => (b.download + b.upload) - (a.download + a.upload)),
top_ips: Object.values(c.byIp).sort((a, b) => b.total - a.total).slice(0, 8),
top_domains: Object.values(c.byDomain).sort((a, b) => b.total - a.total).slice(0, 8),
}))
.sort((a, b) => (b.download + b.upload) - (a.download + a.upload));
+10 -4
View File
@@ -130,7 +130,9 @@ export default function SummaryPage() {
dest: GeoLocation,
downloadBytes: number,
uploadBytes: number,
countryCode: string
countryCode: string,
topIps?: any[],
topDomains?: any[]
) => {
const dl = downloadBytes || 0;
const ul = uploadBytes || 0;
@@ -148,6 +150,8 @@ export default function SummaryPage() {
countryCode,
direction: 'outbound',
curveSign: 1,
topIps: topIps || [],
topDomains: topDomains || [],
});
}
if (dl > 0) {
@@ -164,6 +168,8 @@ export default function SummaryPage() {
countryCode,
direction: 'inbound',
curveSign: -0.65,
topIps: topIps || [],
topDomains: topDomains || [],
});
}
};
@@ -177,17 +183,17 @@ export default function SummaryPage() {
if (activeAgentUuid) {
const origin = resolveOriginForAgent(activeAgentUuid, activeAgentLabel);
pushBidirectionalArcs(origin, dest, row.download, row.upload, row.country_code);
pushBidirectionalArcs(origin, dest, row.download, row.upload, row.country_code, row.top_ips, row.top_domains);
} 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);
pushBidirectionalArcs(origin, dest, ag.download, ag.upload, row.country_code);
pushBidirectionalArcs(origin, dest, ag.download, ag.upload, row.country_code, row.top_ips, row.top_domains);
}
} else {
const origin = resolveOriginForAgent(null);
pushBidirectionalArcs(origin, dest, row.download, row.upload, row.country_code);
pushBidirectionalArcs(origin, dest, row.download, row.upload, row.country_code, row.top_ips, row.top_domains);
}
}
+2 -52
View File
@@ -3,9 +3,8 @@
import React, { useState } from 'react';
import { ComposableMap, Geographies, Geography, Line, Marker, ZoomableGroup } from "react-simple-maps";
import { useTheme } from "next-themes";
import { X, MapPin, ArrowDownLeft, ArrowUpRight, Activity } from "lucide-react";
import { fmtBytes } from "@/lib/utils";
import { useGlobeData, GlobeDataInput, GlobePointData } from './useGlobeData';
import { GlobePointPopup } from './GlobePointPopup';
const geoUrl = "/ne_110m_admin_0_countries.geojson";
@@ -172,56 +171,7 @@ export const GlobeMap = ({ data }: GlobeMapProps) => {
</ComposableMap>
{selectedPoint && (
<div className="absolute top-3 right-3 z-30 w-60 rounded-xl border border-white/15 bg-slate-950/95 p-3 text-slate-200 shadow-2xl backdrop-blur-md animate-fade-in">
<div className="flex items-start justify-between gap-2 border-b border-white/10 pb-2">
<div className="min-w-0">
<span
className={`inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-[9px] font-bold uppercase tracking-wider ${
selectedPoint.isOrigin
? "bg-rose-500/20 text-rose-400 border border-rose-500/30"
: "bg-sky-500/20 text-sky-400 border border-sky-500/30"
}`}
>
<MapPin className="h-2.5 w-2.5" />
{selectedPoint.isOrigin ? "Lokasi Server Agent" : "Negara Tujuan / Luar"}
</span>
<h4 className="mt-1 text-xs font-bold text-white break-words">{selectedPoint.name}</h4>
<p className="text-[9px] font-mono text-slate-400">
Lat: {selectedPoint.lat.toFixed(6)}, Lng: {selectedPoint.lng.toFixed(6)}
</p>
</div>
<button
type="button"
onClick={() => setSelectedPoint(null)}
className="rounded-lg p-1 text-slate-400 hover:bg-white/10 hover:text-white transition-colors"
aria-label="Close detail"
>
<X className="h-3.5 w-3.5" />
</button>
</div>
<div className="mt-2 space-y-1 text-[10px]">
<div className="flex items-center justify-between">
<span className="flex items-center gap-1 text-emerald-400">
<ArrowDownLeft className="h-3 w-3" /> Download (Luar → Agent)
</span>
<span className="font-mono font-semibold text-emerald-400">{fmtBytes(selectedPoint.download || 0)}</span>
</div>
<div className="flex items-center justify-between">
<span className="flex items-center gap-1 text-sky-400">
<ArrowUpRight className="h-3 w-3" /> Upload (Agent → Luar)
</span>
<span className="font-mono font-semibold text-sky-400">{fmtBytes(selectedPoint.upload || 0)}</span>
</div>
<div className="flex items-center justify-between border-t border-white/10 pt-1">
<span className="flex items-center gap-1 text-slate-300 font-medium">
<Activity className="h-3 w-3 text-purple-400" /> Total Trafik
</span>
<span className="font-mono font-bold text-white">
{fmtBytes((selectedPoint.download || 0) + (selectedPoint.upload || 0))}
</span>
</div>
</div>
</div>
<GlobePointPopup point={selectedPoint} onClose={() => setSelectedPoint(null)} />
)}
{/* Legenda Berderet ke Bawah */}
+137
View File
@@ -0,0 +1,137 @@
"use client";
import React from "react";
import { X, MapPin, ArrowDownLeft, ArrowUpRight, Activity, Globe, Server } from "lucide-react";
import { fmtBytes } from "@/lib/utils";
import { formatAppLabel } from "@/lib/domainUtils";
import { GlobePointData } from "./useGlobeData";
interface GlobePointPopupProps {
point: GlobePointData;
onClose: () => void;
}
export const GlobePointPopup = ({ point, onClose }: GlobePointPopupProps) => {
const topIps = point.topIps || [];
const topDomains = point.topDomains || [];
return (
<div
className="absolute top-3 right-3 z-30 w-72 max-h-[460px] flex flex-col rounded-xl border border-white/15 bg-slate-950/95 p-3 text-slate-200 shadow-2xl backdrop-blur-md animate-fade-in cursor-default"
onClick={(e) => e.stopPropagation()}
>
<div className="flex items-start justify-between gap-2 border-b border-white/10 pb-2 shrink-0">
<div className="min-w-0">
<span
className={`inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-[9px] font-bold uppercase tracking-wider ${
point.isOrigin
? "bg-rose-500/20 text-rose-400 border border-rose-500/30"
: "bg-sky-500/20 text-sky-400 border border-sky-500/30"
}`}
>
<MapPin className="h-2.5 w-2.5" />
{point.isOrigin ? "Lokasi Server Agent" : "Negara Tujuan / Luar"}
</span>
<h4 className="mt-1 text-xs font-bold text-white break-words">{point.name}</h4>
<p className="text-[9px] font-mono text-slate-400">
Lat: {point.lat.toFixed(6)}, Lng: {point.lng.toFixed(6)}
</p>
</div>
<button
type="button"
onClick={onClose}
className="rounded-lg p-1 text-slate-400 hover:bg-white/10 hover:text-white transition-colors"
aria-label="Close detail"
>
<X className="h-3.5 w-3.5" />
</button>
</div>
<div className="mt-2 space-y-1 text-[10px] shrink-0">
<div className="flex items-center justify-between">
<span className="flex items-center gap-1 text-emerald-400">
<ArrowDownLeft className="h-3 w-3" /> Download (Luar → Agent)
</span>
<span className="font-mono font-semibold text-emerald-400">{fmtBytes(point.download || 0)}</span>
</div>
<div className="flex items-center justify-between">
<span className="flex items-center gap-1 text-sky-400">
<ArrowUpRight className="h-3 w-3" /> Upload (Agent → Luar)
</span>
<span className="font-mono font-semibold text-sky-400">{fmtBytes(point.upload || 0)}</span>
</div>
<div className="flex items-center justify-between border-t border-white/10 pt-1">
<span className="flex items-center gap-1 text-slate-300 font-medium">
<Activity className="h-3 w-3 text-purple-400" /> Total Trafik
</span>
<span className="font-mono font-bold text-white">
{fmtBytes((point.download || 0) + (point.upload || 0))}
</span>
</div>
</div>
{(topIps.length > 0 || topDomains.length > 0) && (
<div className="mt-2.5 pt-2 border-t border-white/10 overflow-y-auto space-y-2.5 pr-1 custom-scrollbar">
{topIps.length > 0 && (
<div>
<div className="flex items-center gap-1 text-[9px] font-bold uppercase tracking-wider text-sky-400 mb-1">
<Server className="h-2.5 w-2.5" />
<span>Daftar IP ({topIps.length})</span>
</div>
<div className="space-y-1">
{topIps.map((item, idx) => {
const hostLabel = item.domain
? formatAppLabel(item.domain)
: item.app_label
? formatAppLabel(item.app_label)
: null;
return (
<div
key={`${item.ip}-${idx}`}
className="rounded-md bg-white/[0.04] border border-white/5 px-2 py-1 text-[9px]"
>
<div className="flex items-center justify-between gap-2">
<span className="font-mono font-semibold text-slate-100 truncate">{item.ip}</span>
<span className="font-mono text-sky-300 shrink-0">{fmtBytes(item.total || 0)}</span>
</div>
{hostLabel && (
<div className="text-[8.5px] text-slate-400 truncate mt-0.5" title={hostLabel}>
{hostLabel}
</div>
)}
</div>
);
})}
</div>
</div>
)}
{topDomains.length > 0 && (
<div>
<div className="flex items-center gap-1 text-[9px] font-bold uppercase tracking-wider text-emerald-400 mb-1">
<Globe className="h-2.5 w-2.5" />
<span>Domain / Aplikasi ({topDomains.length})</span>
</div>
<div className="space-y-1">
{topDomains.map((item, idx) => {
const label = formatAppLabel(item.domain);
return (
<div
key={`${item.domain}-${idx}`}
className="flex items-center justify-between gap-2 rounded-md bg-white/[0.04] border border-white/5 px-2 py-1 text-[9px]"
>
<span className="text-slate-200 truncate" title={label}>
{label}
</span>
<span className="font-mono text-emerald-300 shrink-0">{fmtBytes(item.total || 0)}</span>
</div>
);
})}
</div>
</div>
)}
</div>
)}
</div>
);
};
+62
View File
@@ -2,6 +2,22 @@
import { useMemo } from 'react';
import { SMALL_LABEL_COUNTRIES, getStaggeredCoordinates } from './globeOffsets';
export interface GlobeTopIpItem {
ip: string;
domain?: string | null;
app_label?: string | null;
download: number;
upload: number;
total: number;
}
export interface GlobeTopDomainItem {
domain: string;
download: number;
upload: number;
total: number;
}
export interface GlobeDataInput {
startLat: number;
startLng: number;
@@ -15,6 +31,8 @@ export interface GlobeDataInput {
countryCode?: string;
direction?: 'outbound' | 'inbound';
curveSign?: number;
topIps?: GlobeTopIpItem[];
topDomains?: GlobeTopDomainItem[];
}
export interface GlobePointData {
@@ -28,6 +46,42 @@ export interface GlobePointData {
download: number;
isOrigin: boolean;
countryCode?: string;
topIps?: GlobeTopIpItem[];
topDomains?: GlobeTopDomainItem[];
}
function mergeTopIps(existing: GlobeTopIpItem[] = [], incoming: GlobeTopIpItem[] = []): GlobeTopIpItem[] {
const map = new Map<string, GlobeTopIpItem>();
for (const item of [...existing, ...incoming]) {
if (!item?.ip) continue;
const prev = map.get(item.ip);
if (prev) {
prev.download += item.download || 0;
prev.upload += item.upload || 0;
prev.total += item.total || 0;
if (!prev.domain && item.domain) prev.domain = item.domain;
if (!prev.app_label && item.app_label) prev.app_label = item.app_label;
} else {
map.set(item.ip, { ...item });
}
}
return Array.from(map.values()).sort((a, b) => b.total - a.total).slice(0, 8);
}
function mergeTopDomains(existing: GlobeTopDomainItem[] = [], incoming: GlobeTopDomainItem[] = []): GlobeTopDomainItem[] {
const map = new Map<string, GlobeTopDomainItem>();
for (const item of [...existing, ...incoming]) {
if (!item?.domain) continue;
const prev = map.get(item.domain);
if (prev) {
prev.download += item.download || 0;
prev.upload += item.upload || 0;
prev.total += item.total || 0;
} else {
map.set(item.domain, { ...item });
}
}
return Array.from(map.values()).sort((a, b) => b.total - a.total).slice(0, 8);
}
export function useGlobeData(data: GlobeDataInput[], isLight: boolean) {
@@ -77,6 +131,8 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) {
download: 0,
isOrigin: false,
countryCode: arc.countryCode,
topIps: [],
topDomains: [],
});
}
if (isFirstForPair) {
@@ -84,6 +140,8 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) {
dp.download += (arc.download || 0);
dp.upload += (arc.upload || 0);
dp.value = dp.download + dp.upload;
dp.topIps = mergeTopIps(dp.topIps, arc.topIps);
dp.topDomains = mergeTopDomains(dp.topDomains, arc.topDomains);
}
if (!originMap.has(originKey)) {
@@ -98,6 +156,8 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) {
download: 0,
isOrigin: true,
countryCode: 'ID',
topIps: [],
topDomains: [],
});
}
if (isFirstForPair) {
@@ -105,6 +165,8 @@ export function useGlobeData(data: GlobeDataInput[], isLight: boolean) {
op.download += (arc.download || 0);
op.upload += (arc.upload || 0);
op.value = op.download + op.upload;
op.topIps = mergeTopIps(op.topIps, arc.topIps);
op.topDomains = mergeTopDomains(op.topDomains, arc.topDomains);
}
});
+14
View File
@@ -42,6 +42,20 @@ export interface CountryStat {
upload: number;
flow_count: number;
}>;
top_ips?: Array<{
ip: string;
domain?: string | null;
app_label?: string | null;
download: number;
upload: number;
total: number;
}>;
top_domains?: Array<{
domain: string;
download: number;
upload: number;
total: number;
}>;
}
export interface DnsStat {