fix(dns,tables): aggregate real DNS connections from Flow and enforce max 20 rows per page
This commit is contained in:
1 parent
1a2b58c707
commit
7abb56e328
8 files changed
+141
-23
No files matched your search
+132
-17
@@ -212,36 +212,151 @@ router.get('/dns', async (req, res) => {
|
||||
const limit = req.query.limit !== undefined ? parseInt(req.query.limit, 10) : 0;
|
||||
const timeFilter = getTimeFilter(req);
|
||||
const matchBase = getBaseFilter(req, timeFilter);
|
||||
const { SniHostnameStat } = require('../../models/SchemasTelemetry');
|
||||
await applySubnetFilterToQuery(matchBase, 'src_ip');
|
||||
const { Flow } = require('../../models/Schemas');
|
||||
|
||||
// Aggregate real domain connections directly from Flow
|
||||
const pipeline = [
|
||||
{ $match: { ...matchBase, sni_hostname: { $ne: null } } },
|
||||
{ $group: {
|
||||
_id: '$sni_hostname',
|
||||
{
|
||||
$match: {
|
||||
...matchBase,
|
||||
$or: [
|
||||
{ domain: { $ne: null, $ne: '', $nin: ['Unknown', '-'] } },
|
||||
{ sni_hostname: { $ne: null, $ne: '', $nin: ['Unknown', '-'] } }
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
resolved_domain: { $ifNull: ['$domain', '$sni_hostname'] },
|
||||
download: { $ifNull: ['$download', 0] },
|
||||
upload: { $ifNull: ['$upload', 0] },
|
||||
last_seen: 1,
|
||||
app_label: 1
|
||||
}
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
resolved_domain: { $ne: null, $ne: '', $nin: ['Unknown', '-'] }
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: '$resolved_domain',
|
||||
download: { $sum: '$download' },
|
||||
upload: { $sum: '$upload' },
|
||||
count: { $sum: '$flows' }
|
||||
}},
|
||||
{ $project: {
|
||||
domain: '$_id',
|
||||
query_count: '$count',
|
||||
download: 1,
|
||||
upload: 1,
|
||||
app_label: { $literal: null },
|
||||
category: { $literal: null },
|
||||
_id: 0
|
||||
}},
|
||||
{ $sort: { query_count: -1 } },
|
||||
query_count: { $sum: 1 },
|
||||
last_seen: { $max: '$last_seen' },
|
||||
app_label: { $first: '$app_label' }
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
domain: '$_id',
|
||||
query_count: 1,
|
||||
download: 1,
|
||||
upload: 1,
|
||||
last_seen: 1,
|
||||
app_label: 1,
|
||||
_id: 0
|
||||
}
|
||||
},
|
||||
{ $sort: { query_count: -1, download: -1 } }
|
||||
];
|
||||
|
||||
if (limit > 0) {
|
||||
pipeline.push({ $limit: limit });
|
||||
}
|
||||
|
||||
const data = await SniHostnameStat.aggregate(pipeline);
|
||||
let data = await Flow.aggregate(pipeline);
|
||||
|
||||
// Fallback to SniHostnameStat if Flow has 0 matching records
|
||||
if (data.length === 0) {
|
||||
const { SniHostnameStat } = require('../../models/SchemasTelemetry');
|
||||
const sniPipeline = [
|
||||
{ $match: { ...matchBase, sni_hostname: { $ne: null, $ne: '', $nin: ['Unknown', '-'] } } },
|
||||
{ $group: {
|
||||
_id: '$sni_hostname',
|
||||
download: { $sum: '$download' },
|
||||
upload: { $sum: '$upload' },
|
||||
query_count: { $sum: { $cond: [{ $gt: ['$flows', 0] }, '$flows', 1] } },
|
||||
last_seen: { $max: '$timestamp' }
|
||||
}},
|
||||
{ $project: {
|
||||
domain: '$_id',
|
||||
query_count: 1,
|
||||
download: 1,
|
||||
upload: 1,
|
||||
last_seen: 1,
|
||||
app_label: { $literal: null },
|
||||
_id: 0
|
||||
}},
|
||||
{ $sort: { query_count: -1, download: -1 } }
|
||||
];
|
||||
if (limit > 0) sniPipeline.push({ $limit: limit });
|
||||
data = await SniHostnameStat.aggregate(sniPipeline);
|
||||
}
|
||||
|
||||
res.json({ ok: true, data });
|
||||
} catch (err) {
|
||||
res.status(500).json({ ok: false, error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/dashboard/domain-details?domain=...
|
||||
router.get('/domain-details', async (req, res) => {
|
||||
const { domain, agentUuid } = req.query;
|
||||
if (!domain) return res.status(400).json({ ok: false, error: 'domain is required' });
|
||||
|
||||
try {
|
||||
const { Flow } = require('../../models/Schemas');
|
||||
const { generateMacFromIp, resolveVendorFromIp, resolveDeviceTypeFromIp, generateAutoLabel } = require('../../deviceResolver');
|
||||
|
||||
const filter = {
|
||||
$or: [
|
||||
{ domain: domain },
|
||||
{ sni_hostname: domain }
|
||||
]
|
||||
};
|
||||
if (agentUuid) {
|
||||
filter.agent_uuid = agentUuid;
|
||||
}
|
||||
|
||||
const raw = await Flow.find(filter).sort({ timestamp: -1, last_seen: -1 }).limit(100).lean();
|
||||
if (!raw || !Array.isArray(raw)) {
|
||||
return res.json({ ok: true, data: [] });
|
||||
}
|
||||
|
||||
const results = [];
|
||||
const seen = new Set();
|
||||
|
||||
for (const r of raw) {
|
||||
const ip = r.src_ip;
|
||||
const mac = r.src_mac || generateMacFromIp(ip);
|
||||
if (!ip) continue;
|
||||
|
||||
const key = `${ip}-${mac}`;
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
|
||||
const vendor = resolveVendorFromIp(ip, mac);
|
||||
const type = resolveDeviceTypeFromIp(ip, mac);
|
||||
const autoLabel = generateAutoLabel(ip, mac, vendor, type);
|
||||
|
||||
results.push({
|
||||
ip,
|
||||
mac,
|
||||
device_name: autoLabel,
|
||||
download: r.download || 0,
|
||||
upload: r.upload || 0,
|
||||
last_seen: r.last_seen || r.timestamp
|
||||
});
|
||||
}
|
||||
|
||||
res.json({ ok: true, data: results });
|
||||
} catch (err) {
|
||||
res.status(500).json({ ok: false, error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -229,6 +229,7 @@ export default function AppsPage() {
|
||||
<DataTable
|
||||
data={processedData}
|
||||
columns={columns}
|
||||
pageSize={20}
|
||||
searchPlaceholder="Search by application or category..."
|
||||
searchFilter={(row, query) => {
|
||||
const q = query.toLowerCase();
|
||||
|
||||
@@ -31,7 +31,7 @@ export default function DNSPage() {
|
||||
useEffect(() => {
|
||||
if (selectedDomain) {
|
||||
setIsDetailLoading(true);
|
||||
fetch(`http://localhost:4000/domain-details?domain=${encodeURIComponent(selectedDomain)}`)
|
||||
fetch(`/api/dashboard/domain-details?domain=${encodeURIComponent(selectedDomain)}`)
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
if (data.ok) {
|
||||
@@ -133,6 +133,7 @@ export default function DNSPage() {
|
||||
<DataTable
|
||||
data={filteredData}
|
||||
columns={columns}
|
||||
pageSize={20}
|
||||
searchPlaceholder="Search by domain, application, or category..."
|
||||
onRowClick={(row) => setSelectedDomain(row.domain)}
|
||||
searchFilter={(row, query) => {
|
||||
|
||||
@@ -214,7 +214,7 @@ function EventsContent() {
|
||||
? "bg-yellow-500/10 border-l-2 border-yellow-500 animate-pulse font-semibold"
|
||||
: "";
|
||||
}}
|
||||
pageSize={50}
|
||||
pageSize={20}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
@@ -39,7 +39,7 @@ export default function FlowsPage() {
|
||||
}, [filters, dateFrom, dateTo, debouncedSearch]);
|
||||
|
||||
const { data: optionsData } = useFlowsOptions();
|
||||
const { data: flows = [], total = 0, isLoading } = useFlows(currentPage, 50, {
|
||||
const { data: flows = [], total = 0, isLoading } = useFlows(currentPage, 20, {
|
||||
...filters,
|
||||
dateFrom,
|
||||
dateTo,
|
||||
@@ -162,6 +162,7 @@ export default function FlowsPage() {
|
||||
<DataTable
|
||||
data={flows}
|
||||
columns={columns}
|
||||
pageSize={20}
|
||||
serverSide={true}
|
||||
totalCount={total}
|
||||
currentPage={currentPage}
|
||||
|
||||
@@ -133,7 +133,7 @@ export default function NetworkIntelligencePage() {
|
||||
<DataTable
|
||||
data={processedData}
|
||||
columns={appCols}
|
||||
pageSize={50}
|
||||
pageSize={20}
|
||||
onRowClick={(row) => setSelectedCategory(row.category_label)}
|
||||
/>
|
||||
</CardContent>
|
||||
|
||||
@@ -40,7 +40,7 @@ export function CategoryDetailPanel({
|
||||
const [activeTab, setActiveTab] = useState<"apps" | "devices">("apps");
|
||||
const [page, setPage] = useState(1);
|
||||
const [isMounted, setIsMounted] = useState(false);
|
||||
const PAGE_SIZE = 25;
|
||||
const PAGE_SIZE = 20;
|
||||
|
||||
const handleClose = useCallback(() => {
|
||||
setIsClosing(true);
|
||||
|
||||
@@ -41,7 +41,7 @@ export function DataTable<T>({
|
||||
searchFilter,
|
||||
isLoading,
|
||||
getRowClassName,
|
||||
pageSize = 50,
|
||||
pageSize = 20,
|
||||
csvExport,
|
||||
pdfExport,
|
||||
onRowClick,
|
||||
|
||||
Reference in new issue
Block a user