From c6d6ec2103b39caab5f263d9386756dc39e8c1e9 Mon Sep 17 00:00:00 2001 From: adriano Date: Mon, 16 May 2022 01:03:16 -0300 Subject: [PATCH] =?UTF-8?q?Limpeza=20de=20coment=C3=A1rios=20na=20master?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/controllers/ReportController.ts | 23 +- .../helpers/SchedulingNotifySendMessage.ts | 5 + backend/src/helpers/WhoIsOnlineMonitor.ts | 249 ++++++++---------- backend/src/libs/socket.ts | 10 - 4 files changed, 121 insertions(+), 166 deletions(-) diff --git a/backend/src/controllers/ReportController.ts b/backend/src/controllers/ReportController.ts index f0adbe4..bf59bbb 100644 --- a/backend/src/controllers/ReportController.ts +++ b/backend/src/controllers/ReportController.ts @@ -49,12 +49,7 @@ export const reportUserService= async (req: Request, res: Response): Promise { @@ -109,13 +98,7 @@ export const reportUserService= async (req: Request, res: Response): Promise { @@ -45,12 +46,16 @@ const monitor = async () => { } catch (error) { console.log('>>> SchedulingNotifiySendMessage.ts error: ', error) + stopSchedulingMonitor() + startSchedulingMonitor(timeInterval) } }; export const startSchedulingMonitor =async (mileseconds: number) => { + + timeInterval = mileseconds scheduler_monitor = setInterval(monitor, mileseconds) diff --git a/backend/src/helpers/WhoIsOnlineMonitor.ts b/backend/src/helpers/WhoIsOnlineMonitor.ts index 411a5d8..dd1947d 100644 --- a/backend/src/helpers/WhoIsOnlineMonitor.ts +++ b/backend/src/helpers/WhoIsOnlineMonitor.ts @@ -30,19 +30,7 @@ let timeInterval = 5 let lstOnline: any = [] let deleted: boolean = false; - -const setOfflineAllUsers = async () => { - - // const onlineUsers = await ListUserOnlineOffline({ date: dateTime.fullDate, status: 'online' }) - - // console.log('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX onlineUsers: ', onlineUsers) - - // if (onlineUsers.length > 0) { - // onlineUsers.forEach(async user => { - // await createOrUpdateOnlineUserService({ userId: user.userId, status: 'offline' }) - // }); - // } -} + const emitterOnline = (user: any, status: string, showOnlineTime: boolean = true) => { @@ -73,137 +61,126 @@ const emitterOnline = (user: any, status: string, showOnlineTime: boolean = true const monitor = async () => { - const test = require('./../libs/socket'); + try { - if (test.ob) { - - if (count > 1) { - count = 0 - } - - if (count == 0) { - uuid = test.ob.uuid - } - - if (count == 1) { - if (uuid) { - if (uuid == test.ob.uuid) { - - console.log('ALL USERS CLIENTS OFFLINE 1...........') - - //await setOfflineAllUsers() - test.ob.listOnline = [] - test.ob.listOnlineAux = [] - test.ob.uuid = undefined - - } + const usersSocket = require('./../libs/socket'); + if (usersSocket.ob) { + + if (count > 1) { + count = 0 } - - } else { - // console.log(' * TEST OBJ', test.ob) - - - test.ob.listOnline.forEach(async (el: any) => { - - - const indexAux = lstOnline.findIndex((e: any) => e.id == el.id) - - - if (indexAux == -1) { - console.log(' entrou indexAux: ', indexAux) - - const userOnline = await createOrUpdateOnlineUserService({ userId: el.id, status: 'online' }) - - if (userOnline) { - - el.onlineTime = userOnline.onlineTime - el.updatedAt = userOnline.updatedAt, - - console.log(' * CREATED OR UPDATED USER TO ONLINE: ', userOnline.userId) - - lstOnline.push({ 'id': el.id, 'status': 'online' }) + + if (count == 0) { + uuid = usersSocket.ob.uuid + } + + if (count == 1) { + if (uuid) { + if (uuid == usersSocket.ob.uuid) { + + console.log('ALL USERS CLIENTS OFFLINE 1...........') + + usersSocket.ob.listOnline = [] + usersSocket.ob.listOnlineAux = [] + usersSocket.ob.uuid = undefined + } - + } - - if (el.status == 'waiting...') { - emitterOnline(el, el.status, false) - } - else { - emitterOnline(el, el.status) - } - - - - }); - - - // console.log('----------- 1 lstOnline: ', lstOnline) - - - let difference = lstOnline.filter((x: any) => !test.ob.listOnline.map((e: any) => e.id).includes(x.id)); - - // console.log(' * diference: ', difference) - - difference.forEach(async (e: any) => { - - const index = lstOnline.findIndex((x: any) => x.id == e.id) - - if (index != -1) { - - lstOnline.splice(index, 1) - - // console.log('----------- 2 lstOnline: ', lstOnline) - - const userOnline = await createOrUpdateOnlineUserService({ userId: e.id, status: 'offline' }) - - if (userOnline) { - // console.log(' * UPDATED USER TO OFFLINE: ', userOnline.userId) + + } else { + + usersSocket.ob.listOnline.forEach(async (el: any) => { + + + const indexAux = lstOnline.findIndex((e: any) => e.id == el.id) + + + if (indexAux == -1) { + console.log(' entrou indexAux: ', indexAux) + + const userOnline = await createOrUpdateOnlineUserService({ userId: el.id, status: 'online' }) + + if (userOnline) { + + el.onlineTime = userOnline.onlineTime + el.updatedAt = userOnline.updatedAt, + + console.log(' * CREATED OR UPDATED USER TO ONLINE: ', userOnline.userId) + + lstOnline.push({ 'id': el.id, 'status': 'online' }) + } + } - - - } - - }) - - + + if (el.status == 'waiting...') { + emitterOnline(el, el.status, false) + } + else { + emitterOnline(el, el.status) + } + + + + }); + + + + let difference = lstOnline.filter((x: any) => !usersSocket.ob.listOnline.map((e: any) => e.id).includes(x.id)); + + difference.forEach(async (e: any) => { + + const index = lstOnline.findIndex((x: any) => x.id == e.id) + + if (index != -1) { + + lstOnline.splice(index, 1) + + const userOnline = await createOrUpdateOnlineUserService({ userId: e.id, status: 'offline' }) + + + } + + }) + + + } + + count++ } - - - count++ - - } - - - if (countTest > 5) { - countTest = 0 - } - - if (countTest == 0) { - try { - console.log(' Carregando usuárion no listUserId...') - listUserId = await ListUserParamiterService({ profile: 'user' }) - - } catch (error) { - console.log('There was an erro on ListUserParamiterService: ', error) - return + + + if (countTest > 5) { + countTest = 0 } - - } - - countTest = 1 - - listUserId.forEach((u) => { - - // console.log(' *** listUserId: ', listUserId) - const io = getIO(); - io.emit("isOnline", { action: "online", userId: u.id }); - }); - - - // console.log('***************** TIME INTERVAL: ', timeInterval) - + + if (countTest == 0) { + try { + // console.log(' Carregando usuárion no listUserId...') + listUserId = await ListUserParamiterService({ profile: 'user' }) + + } catch (error) { + console.log('There was an erro on ListUserParamiterService: ', error) + return + } + + } + + countTest = 1 + + listUserId.forEach((u) => { + + const io = getIO(); + io.emit("isOnline", { action: "online", userId: u.id }); + }); + + } catch (error) { + console.log('>>> There was an error no WhoIsOnlineMonitor.ts: ',error) + stopWhoIsOnlineMonitor() + startWhoIsOnlineMonitor() + } + }; diff --git a/backend/src/libs/socket.ts b/backend/src/libs/socket.ts index 0de47f5..9e8f48f 100644 --- a/backend/src/libs/socket.ts +++ b/backend/src/libs/socket.ts @@ -33,11 +33,6 @@ let lstTry: any[] = [] let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }))) -// - - - - export const initIO = (httpServer: Server): SocketIO => { io = new SocketIO(httpServer, { @@ -131,7 +126,6 @@ export const initIO = (httpServer: Server): SocketIO => { var difference = lstOnline.filter((x: any) => !lstOnlineAux.map((e: any) => e.id).includes(x.id)); - //console.log('>>>>>>>>>>> difference: ', difference) if (difference.length > 0) { @@ -156,15 +150,11 @@ export const initIO = (httpServer: Server): SocketIO => { }) - // console.log(' difference2: ', difference) } obj.listOnline = lstOnline obj.listOnlineAux = lstOnlineAux - // console.log(' >>>>>> lstOnline: ', lstOnline) - // console.log('') - // console.log(' >>>>>> lstOnlineAux: ', lstOnlineAux) lstOnlineAux = [] listOnlineAux = []