This commit is contained in:
Douglas Barone 2022-04-20 07:40:21 -04:00
parent 8f2ab2ba45
commit 33233f5947
5 changed files with 40 additions and 23 deletions

View File

@ -1,7 +1,7 @@
import prisma from '../prisma'
import { ACCESS_POINTS_UPDATED, pubsub } from '../pubsub'
import { getAccessPoints as getCiscoAccessPoints } from './ciscoController'
import { logInfo } from './logger'
import { logInfo, logSuccess } from './logger'
import { getAccessPoints as getUnifiAccessPoints } from './unifiController'
async function getAccessPoints() {
@ -37,18 +37,16 @@ export async function updateAccessPoints() {
message: 'Atualizando informações dos APs'
})
const accessPoints = await getAccessPoints()
const onlineAccessPoints = await getAccessPoints()
const updatedAccessPoints = await updateDB(accessPoints)
await updateDB(onlineAccessPoints)
const dbAccessPoints = await prisma.accessPoint.findMany()
const offLineAps = dbAccessPoints.filter(dbAccessPoint => !updatedAccessPoints.find(updatedAccessPoint => updatedAccessPoint.mac === dbAccessPoint.mac))
const onlineAccessPointsMACs = onlineAccessPoints.map(ap => ap.mac)
await prisma.accessPoint.updateMany({
where: {
mac: {
in: offLineAps.map(offLineAccessPoint => offLineAccessPoint.mac)
notIn: onlineAccessPointsMACs
}
},
data: {
@ -56,6 +54,20 @@ export async function updateAccessPoints() {
}
})
const updatedDbAccessPoints = await prisma.accessPoint.findMany({
include: {
wifiDevices: {
where: {
status: 'ONLINE'
}
}
}
})
pubsub.publish(ACCESS_POINTS_UPDATED, { accessPointsUpdated: dbAccessPoints })
pubsub.publish(ACCESS_POINTS_UPDATED, { accessPointsUpdated: updatedDbAccessPoints })
logSuccess({
tags: ['accessPoints', 'updateAccessPoints'],
message: `${updatedDbAccessPoints.length} APs atualizados`
})
}

View File

@ -20,8 +20,7 @@ async function log(
const entryTags = tags.length ? ` [${tags}] ` : ''
console.log(
`${color}[${format(now, 'HH:mm:ss')}]${
entryTags ? entryTags : ''
`${color}[${format(now, 'HH:mm:ss')}]${entryTags ? entryTags : ''
}\x1b[0m${message}`
)

View File

@ -2,15 +2,21 @@ import prisma from "../prisma";
import { getSubnetInfo } from "../utils/subnetInfo";
export const AccessPoint = {
updatedAt: parent => parent.updatedAt?.toISOString(),
clients: parent => prisma.wifiDevice.count({
updatedAt: (parent, data, context, info) => parent.updatedAt?.toISOString(),
async clients(parent, data, context, info) {
const clientsCount = await prisma.wifiDevice.count({
where: {
status: 'ONLINE',
accessPoint: {
id: parent.id
}
}
}),
})
return clientsCount;
},
subnetInfo: parent => getSubnetInfo(parent.ip)
}

View File

@ -17,7 +17,7 @@ export async function updateAccessPoint(
})
logInfo({
tags: ['accessPointUpdated', 'accessPoints'],
tags: ['accessPointEdited', 'accessPoints'],
message: `O usuário ${auth.displayName} (${auth.sAMAccountName}) atualizou as informações do AP ${accessPoint.name || accessPoint.hostname}`,
data: accessPoint
})

View File

@ -1,6 +1,6 @@
import prisma from '../../prisma'
const MAX_RESULT = 5000
const MAX_RESULT = 1000
export async function logs(parent, { search, dateIn, dateOut, limit = 100 }) {
try {