From b61a2727c3182e83b9baa7f643bf78ddb92a62eb Mon Sep 17 00:00:00 2001 From: Douglas Barone Date: Mon, 22 May 2023 14:13:49 -0400 Subject: [PATCH] Consider RECENT as ONLINE. Don't fail if as failed page request. --- server/src/lib/accessPointStats.js | 4 ++- server/src/lib/ciscoController.js | 42 ++++++++++++++-------- server/src/lib/networkStats.js | 4 ++- server/src/lib/paloalto.js | 14 ++++---- server/src/lib/wifiDevices.js | 4 ++- server/src/resolvers/AccessPoint.js | 4 ++- server/src/resolvers/Query/accessPoint.js | 4 ++- server/src/resolvers/Query/accessPoints.js | 4 ++- server/src/resolvers/Stats.js | 8 ++++- server/src/resolvers/User.js | 4 ++- 10 files changed, 64 insertions(+), 28 deletions(-) diff --git a/server/src/lib/accessPointStats.js b/server/src/lib/accessPointStats.js index 121e024..2a619f8 100644 --- a/server/src/lib/accessPointStats.js +++ b/server/src/lib/accessPointStats.js @@ -12,7 +12,9 @@ async function generateStatsForAccessPoint(accessPoint) { accessPoint: { mac: accessPoint.mac }, - status: 'ONLINE' + status: { + in: ['ONLINE', 'RECENT'] + } }, _count: { _all: true diff --git a/server/src/lib/ciscoController.js b/server/src/lib/ciscoController.js index e2bfdde..6786e00 100644 --- a/server/src/lib/ciscoController.js +++ b/server/src/lib/ciscoController.js @@ -7,7 +7,7 @@ const TIMEOUT_IN_MS = 120000 const REQUEST_TIMEOUT_IN_MS = 20000 const getDevicesUri = (skip = 0, page = 1, take = 50) => - `data/client-table.html?columns=524287&take=${take}&skip=${skip}&page=${page}&pageSize=50&sort[0][field]=ST&sort[0][dir]=desc` + `https://${process.env.CISCO_HOST}/data/client-table.html?columns=524287&take=${take}&skip=${skip}&page=${page}&pageSize=50&sort[0][field]=ST&sort[0][dir]=desc` const httpsAgent = new https.Agent({ rejectUnauthorized: false @@ -16,7 +16,6 @@ const httpsAgent = new https.Agent({ const ciscoAxios = create({ withCredentials: true, httpsAgent: httpsAgent, - baseURL: `https://${process.env.CISCO_HOST}/`, timeout: REQUEST_TIMEOUT_IN_MS, auth: { username: process.env.CISCO_USER, @@ -24,6 +23,20 @@ const ciscoAxios = create({ } }) +async function renewSessionId() { + try { + await ciscoAxios.get(getDevicesUri()) + } catch (e) { + if (e.response?.status === 401) { + const newSessionId = e.response.headers['set-cookie'][0] + .split(';')[0] + .split('=')[1] + + ciscoAxios.defaults.headers.Cookie = `sessionId=${newSessionId}` + } + } +} + function getDevices() { return new Promise(async (resolve, reject) => { const source = CancelToken.source() @@ -43,17 +56,7 @@ function getDevices() { const responsesPromises = [] - try { - await ciscoAxios.get(getDevicesUri()) - } catch (e) { - if (e.response?.status === 401) { - const newSessionId = e.response.headers['set-cookie'][0] - .split(';')[0] - .split('=')[1] - - ciscoAxios.defaults.headers.Cookie = `sessionId=${newSessionId}` - } - } + await renewSessionId() try { const firstResponse = await ciscoAxios.get(getDevicesUri(), { @@ -76,7 +79,16 @@ function getDevices() { page++ } - const responses = await Promise.all(responsesPromises) + const fulfilledResponses = await Promise.allSettled(responsesPromises) + + const responses = fulfilledResponses + .filter(resp => resp.status === 'fulfilled') + .map(resp => resp.value) + + console.log( + responses.length, + fulfilledResponses.filter(resp => resp.status === 'rejected').length + ) clearTimeout(timeout) @@ -145,6 +157,8 @@ export function getAccessPoints() { ) }, TIMEOUT_IN_MS) + await renewSessionId() + try { const { data: { Data: accessPoints } diff --git a/server/src/lib/networkStats.js b/server/src/lib/networkStats.js index d13a0d7..eed43f8 100644 --- a/server/src/lib/networkStats.js +++ b/server/src/lib/networkStats.js @@ -10,7 +10,9 @@ async function generateStatsForNetwork(shortName) { const dbStats = await prisma.wifiDevice.aggregate({ where: { - status: 'ONLINE', + status: { + in: ['ONLINE', 'RECENT'] + }, network: { shortName } diff --git a/server/src/lib/paloalto.js b/server/src/lib/paloalto.js index f3d99dc..b1c5c84 100644 --- a/server/src/lib/paloalto.js +++ b/server/src/lib/paloalto.js @@ -27,7 +27,9 @@ async function getDevicesWithUser() { const wifiDevices = await prisma.wifiDevice.findMany({ where: { userId: { not: null }, - status: 'ONLINE', + status: { + in: ['ONLINE', 'RECENT'] + }, lastSeen: { gt: timeoutThreshold } }, select: { @@ -47,13 +49,13 @@ function createCommand(devices) { ) return ` - - 1.0 - update - + + 1.0 + update + ${entries} - + ` } diff --git a/server/src/lib/wifiDevices.js b/server/src/lib/wifiDevices.js index 207fc93..201c47f 100644 --- a/server/src/lib/wifiDevices.js +++ b/server/src/lib/wifiDevices.js @@ -232,7 +232,9 @@ function updateDevicesInfo() { include: { wifiDevices: { where: { - status: 'ONLINE' + status: { + in: ['ONLINE', 'RECENT'] + } } } } diff --git a/server/src/resolvers/AccessPoint.js b/server/src/resolvers/AccessPoint.js index 403c0ad..d8cfdda 100644 --- a/server/src/resolvers/AccessPoint.js +++ b/server/src/resolvers/AccessPoint.js @@ -12,7 +12,9 @@ export const AccessPoint = { async clients(parent, data, context, info) { const clientsCount = await prisma.wifiDevice.count({ where: { - status: 'ONLINE', + status: { + in: ['ONLINE', 'RECENT'] + }, accessPoint: { id: parent.id } diff --git a/server/src/resolvers/Query/accessPoint.js b/server/src/resolvers/Query/accessPoint.js index e2bb9fb..9426c5b 100644 --- a/server/src/resolvers/Query/accessPoint.js +++ b/server/src/resolvers/Query/accessPoint.js @@ -9,7 +9,9 @@ export async function accessPoint(parent, { id }, context, info) { ip: 'asc' }, where: { - status: 'ONLINE' + status: { + in: ['ONLINE', 'RECENT'] + } } } } diff --git a/server/src/resolvers/Query/accessPoints.js b/server/src/resolvers/Query/accessPoints.js index 76d7a7f..16c4758 100644 --- a/server/src/resolvers/Query/accessPoints.js +++ b/server/src/resolvers/Query/accessPoints.js @@ -17,7 +17,9 @@ export async function accessPoints(_, { networkShortName }) { network: true, wifiDevices: { where: { - status: 'ONLINE' + status: { + in: ['ONLINE', 'RECENT'] + } } } } diff --git a/server/src/resolvers/Stats.js b/server/src/resolvers/Stats.js index 4b501c8..bfc1425 100644 --- a/server/src/resolvers/Stats.js +++ b/server/src/resolvers/Stats.js @@ -40,7 +40,13 @@ const Stats = { totalWifiDevices: async () => prisma.wifiDevice.count(), onlineWifiDevices: async () => - prisma.wifiDevice.count({ where: { status: 'ONLINE' } }) + prisma.wifiDevice.count({ + where: { + status: { + in: ['ONLINE', 'RECENT'] + } + } + }) } export { Stats } diff --git a/server/src/resolvers/User.js b/server/src/resolvers/User.js index fd86ab8..2b05720 100755 --- a/server/src/resolvers/User.js +++ b/server/src/resolvers/User.js @@ -79,7 +79,9 @@ const User = { onlineWifiDevicesCount: (parent, data, { auth }) => prisma.wifiDevice.count({ where: { - status: 'ONLINE', + status: { + in: ['ONLINE', 'RECENT'] + }, user: { id: parent.id } } }),