diff --git a/backend/routes/dashboard/devices.js b/backend/routes/dashboard/devices.js index 2bb7681..21bfa9f 100644 --- a/backend/routes/dashboard/devices.js +++ b/backend/routes/dashboard/devices.js @@ -64,11 +64,27 @@ router.get('/devices', async (req, res) => { if (skip > 0) pipeline.push({ $skip: skip }); if (limit > 0) pipeline.push({ $limit: limit }); - [data, customLabelsMap] = await Promise.all([ + const flowSeenQuery = query.site_uuid ? { site_uuid: query.site_uuid } : {}; + const [rawDevs, customLabelsMap, flowRecent] = await Promise.all([ DeviceStat.aggregate(pipeline), - getCustomLabelsMap() + getCustomLabelsMap(), + Flow.aggregate([ + { $match: flowSeenQuery }, + { $group: { _id: { agent: "$agent_uuid", ip: "$src_ip" }, maxTs: { $max: "$timestamp" } } } + ]) ]); - data = filterRowsByAgentSubnets(data, subnetsMap, 'ip_address', 'agent_uuid'); + const flowSeenMap = {}; + for (const fr of flowRecent) { + if (fr._id?.ip && fr.maxTs) { + flowSeenMap[`${fr._id.agent || ''}:${fr._id.ip}`] = fr.maxTs; + } + } + data = filterRowsByAgentSubnets(rawDevs, subnetsMap, 'ip_address', 'agent_uuid').map(d => { + const fTs = flowSeenMap[`${d.agent_uuid || ''}:${d.ip_address}`]; + const dTs = d.last_seen || d.timestamp; + const bestSeen = (fTs && (!dTs || new Date(fTs) > new Date(dTs))) ? fTs : dTs; + return { ...d, last_seen: bestSeen }; + }).sort((a, b) => new Date(b.last_seen || 0) - new Date(a.last_seen || 0) || (b.download || 0) - (a.download || 0)); } const mapped = data.map(obj => { diff --git a/backend/routes/deviceDetailsHandler.js b/backend/routes/deviceDetailsHandler.js index 48d27be..8361f70 100644 --- a/backend/routes/deviceDetailsHandler.js +++ b/backend/routes/deviceDetailsHandler.js @@ -173,7 +173,16 @@ module.exports = async function deviceDetailsHandler(req, res, helpers) { else if (appNames.includes('apple') || appNames.includes('icloud')) os = 'macOS / iOS'; else if (appNames.includes('android') || appNames.includes('google-play')) os = 'Android OS'; } - const lastSeen = device?.last_seen || device?.timestamp || new Date().toISOString(); + const rawCandidates = [ + flowsQuery[0]?.last_seen, + flowsQuery[0]?.timestamp, + deviceAppStats[0]?.timestamp, + device?.last_seen, + device?.timestamp + ].filter(Boolean).map(t => new Date(t).getTime()).filter(n => !isNaN(n)); + const lastSeen = rawCandidates.length > 0 ? new Date(Math.max(...rawCandidates)).toISOString() : new Date().toISOString(); + const oldestFlow = flowsQuery[flowsQuery.length - 1]; + const firstSeen = oldestFlow?.first_seen || oldestFlow?.timestamp || device?.created_at || lastSeen; const customLabelDoc = await CustomDeviceLabel.findOne({ mac_address: targetMac }).lean(); const baseLabel = customLabelDoc?.device_label; @@ -227,6 +236,7 @@ module.exports = async function deviceDetailsHandler(req, res, helpers) { device_type: type, os_label: os, manufacturer: man, + first_seen: firstSeen, last_seen: lastSeen, total_download: totalDownload, total_upload: totalUpload, diff --git a/proxy/collectorHelperDpi2.js b/proxy/collectorHelperDpi2.js index 451e726..fc7492d 100644 --- a/proxy/collectorHelperDpi2.js +++ b/proxy/collectorHelperDpi2.js @@ -95,7 +95,7 @@ async function collectDevicesAndApps(agentUuid, timestamp, SITE_UUID, backone, l device_label, device_type, os_label, manufacturer, download: d.download || 0, upload: d.upload || 0, flows: d.flows || 0, - last_seen: d.last_seen, + last_seen: d.last_seen || timestamp.toISOString(), }; }).filter(d => d.ip_address && ipMatchesSubnets(d.ip_address, allowedSubnets)); diff --git a/src/components/ui/ActiveDurationDisplay.tsx b/src/components/ui/ActiveDurationDisplay.tsx index e5d5d2a..7528532 100644 --- a/src/components/ui/ActiveDurationDisplay.tsx +++ b/src/components/ui/ActiveDurationDisplay.tsx @@ -33,7 +33,7 @@ export function formatActiveDuration(firstSeen?: string, lastSeen?: string) { else if (s > 0) durationText = `${s}s`; const timeSinceLastSeen = Math.floor((now - end) / 1000); - const isActive = timeSinceLastSeen <= 300; + const isActive = timeSinceLastSeen <= 1800; let lastSeenText = ""; if (!isActive) { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 9ea8760..2dc824d 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -88,7 +88,7 @@ export function calculateDuration(start: string | Date | null | undefined, end: } } -export function getActiveStatus(lastSeen: string | Date | null | undefined, thresholdMinutes: number = 5): boolean { +export function getActiveStatus(lastSeen: string | Date | null | undefined, thresholdMinutes: number = 30): boolean { if (!lastSeen) return false; try { const last = new Date(lastSeen).getTime();