diff --git a/server/src/cronTasks.js b/server/src/cronTasks.js index 2bb6f0b..5db0916 100644 --- a/server/src/cronTasks.js +++ b/server/src/cronTasks.js @@ -1,20 +1,25 @@ import cron from 'node-cron' -import { updateDBWithOnlineDevices } from './utils/wifiUtils' +import { updateDevicesInfo } from './utils/wifiUtils' import { User } from './classes/User' import { format } from 'date-fns' import oui from 'oui' +import { updateUserIdMappings } from './utils/paloalto' + function logMsg(msg) { console.log(`[${format(new Date(), 'HH:mm:ss')}] cron: ${msg}`) } -logMsg('Scheduling tasks') +logMsg('Scheduling tasks...') cron.schedule('*/1 * * * *', async () => { - logMsg('updateDBWithOnlineDevices started.') + logMsg('updateDevicesInfo started.') + + const devices = await updateDevicesInfo() + const mappings = await updateUserIdMappings() logMsg( - `updateDBWithOnlineDevices updated ${await updateDBWithOnlineDevices()} devices.` + `updateDevicesInfo updated ${devices} devices and ${mappings} user-id mappings.` ) }) diff --git a/server/src/resolvers/Query.js b/server/src/resolvers/Query.js index c545ea2..5ff4b1a 100755 --- a/server/src/resolvers/Query.js +++ b/server/src/resolvers/Query.js @@ -1,4 +1,3 @@ -import { updateDBWithOnlineDevices } from '../utils/wifiUtils' import { User } from '../classes/User' import prisma from '../prisma' @@ -94,8 +93,6 @@ const Query = { if (identifiedOnly && nonIdentifiedOnly) throw new Error('Invalid combination of filters') - updateDBWithOnlineDevices() - return prisma.wifiDevice.findMany({ orderBy: [{ lastSeen: 'desc' }], where: identifiedOnly @@ -117,8 +114,6 @@ const Query = { async userPresence(_, { search }) { if (!search) search = '' - updateDBWithOnlineDevices() - const usersWithWifiDevices = await prisma.user.findMany({ where: { wifiDevices: { some: { lastSeen: { not: null } } } diff --git a/server/src/utils/paloalto.js b/server/src/utils/paloalto.js index a96e3b7..972bd3d 100644 --- a/server/src/utils/paloalto.js +++ b/server/src/utils/paloalto.js @@ -62,7 +62,7 @@ async function updateUserIdMappings() { return wifiDevices.length } catch (e) { console.log('Error updating user-id mappings:', e.message) - return "Não foi possível atualizar. Veja o log do servidor" + return 'Não foi possível atualizar. Veja o log do servidor' } finally { setTimeout(() => { working = false diff --git a/server/src/utils/wifiUtils.js b/server/src/utils/wifiUtils.js index a60a4b5..6192686 100644 --- a/server/src/utils/wifiUtils.js +++ b/server/src/utils/wifiUtils.js @@ -1,15 +1,13 @@ import { getOnlineWifiDevices as getOnlineUnifiDevices } from './unifiController' import { getOnlineWifiDevices as getOnlineCiscoDevices } from './ciscoController' -import { updateUserIdMappings } from './paloalto' - import prisma from '../prisma' const DEBOUNCE_TIME_MS = 10000 let working = false -async function updateDBWithOnlineDevices() { +async function updateDevicesInfo() { if (working) return -1 // Debounce updates working = true @@ -63,8 +61,6 @@ async function updateDBWithOnlineDevices() { } } - updateUserIdMappings() - setTimeout(() => { working = false }, DEBOUNCE_TIME_MS) @@ -77,4 +73,4 @@ async function updateDBWithOnlineDevices() { } } -export { updateDBWithOnlineDevices } +export { updateDevicesInfo }