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 prisma from '../prisma'
import { ACCESS_POINTS_UPDATED, pubsub } from '../pubsub' import { ACCESS_POINTS_UPDATED, pubsub } from '../pubsub'
import { getAccessPoints as getCiscoAccessPoints } from './ciscoController' import { getAccessPoints as getCiscoAccessPoints } from './ciscoController'
import { logInfo } from './logger' import { logInfo, logSuccess } from './logger'
import { getAccessPoints as getUnifiAccessPoints } from './unifiController' import { getAccessPoints as getUnifiAccessPoints } from './unifiController'
async function getAccessPoints() { async function getAccessPoints() {
@ -37,18 +37,16 @@ export async function updateAccessPoints() {
message: 'Atualizando informações dos APs' 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 onlineAccessPointsMACs = onlineAccessPoints.map(ap => ap.mac)
const offLineAps = dbAccessPoints.filter(dbAccessPoint => !updatedAccessPoints.find(updatedAccessPoint => updatedAccessPoint.mac === dbAccessPoint.mac))
await prisma.accessPoint.updateMany({ await prisma.accessPoint.updateMany({
where: { where: {
mac: { mac: {
in: offLineAps.map(offLineAccessPoint => offLineAccessPoint.mac) notIn: onlineAccessPointsMACs
} }
}, },
data: { 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}] ` : '' const entryTags = tags.length ? ` [${tags}] ` : ''
console.log( console.log(
`${color}[${format(now, 'HH:mm:ss')}]${ `${color}[${format(now, 'HH:mm:ss')}]${entryTags ? entryTags : ''
entryTags ? entryTags : ''
}\x1b[0m${message}` }\x1b[0m${message}`
) )

View File

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

View File

@ -17,7 +17,7 @@ export async function updateAccessPoint(
}) })
logInfo({ 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}`, message: `O usuário ${auth.displayName} (${auth.sAMAccountName}) atualizou as informações do AP ${accessPoint.name || accessPoint.hostname}`,
data: accessPoint data: accessPoint
}) })

View File

@ -1,6 +1,6 @@
import prisma from '../../prisma' import prisma from '../../prisma'
const MAX_RESULT = 5000 const MAX_RESULT = 1000
export async function logs(parent, { search, dateIn, dateOut, limit = 100 }) { export async function logs(parent, { search, dateIn, dateOut, limit = 100 }) {
try { try {
@ -16,9 +16,9 @@ export async function logs(parent, { search, dateIn, dateOut, limit = 100 }) {
}, },
OR: search OR: search
? [ ? [
{ message: { contains: search, mode: 'insensitive' } }, { message: { contains: search, mode: 'insensitive' } },
{ tags: { contains: search, mode: 'insensitive' } } { tags: { contains: search, mode: 'insensitive' } }
] ]
: undefined : undefined
}, },
orderBy: { timestamp: 'desc' }, orderBy: { timestamp: 'desc' },