From 35a064e42fbb92a611afcd638f6c1b8dc246026c Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Wed, 20 Apr 2022 14:31:38 -0400 Subject: [PATCH] Improve performance --- server/src/lib/wifiDevices.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/server/src/lib/wifiDevices.js b/server/src/lib/wifiDevices.js index 8580634..28cfbdd 100644 --- a/server/src/lib/wifiDevices.js +++ b/server/src/lib/wifiDevices.js @@ -26,29 +26,27 @@ async function getOnlineDevices() { return onlineDevices } -async function updateDevicesStatus() { +async function updateDevicesStatus(onlineDevices) { const lastSeenThreshold = subMinutes(new Date(), RECENT_THRESHOLD_IN_MINUTES) + const onlineDevicesMacs = onlineDevices.map(device => device.mac) + const recent = prisma.wifiDevice.updateMany({ where: { - lastSeen: { - gt: lastSeenThreshold - } + lastSeen: { gt: lastSeenThreshold }, + status: 'ONLINE', + mac: { notIn: onlineDevicesMacs } }, - data: { - status: 'RECENT' - } + data: { status: 'RECENT' } }) const offline = prisma.wifiDevice.updateMany({ where: { - lastSeen: { - lte: lastSeenThreshold - } + lastSeen: { lte: lastSeenThreshold }, + status: { not: 'OFFLINE' }, + mac: { notIn: onlineDevicesMacs } }, - data: { - status: 'OFFLINE' - } + data: { status: 'OFFLINE' } }) return prisma.$transaction([recent, offline]) @@ -172,7 +170,8 @@ function updateDevicesInfo() { const startTime = performance.now() const onlineDevices = await getOnlineDevices() - await updateDevicesStatus() + + await updateDevicesStatus(onlineDevices) const endTime = performance.now()