fix(devices): enrich last_seen from Flow and align Active status threshold with collector interval
This commit is contained in:
1 parent
4421b15393
commit
fc441e4f1f
5 files changed
+33
-7
No files matched your search
@@ -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 => {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user