projeto-hit/backend/src/helpers/WhoIsOnlineMonitor.ts

225 lines
5.2 KiB
TypeScript
Raw Normal View History

2022-05-03 21:20:58 +00:00
import e from "express";
import { bool, number } from "yup";
import { getIO } from "../libs/socket";
import Queue from "../models/Queue";
import ListUserParamiterService from "../services/UserServices/ListUserParamiterService";
import createOrUpdateOnlineUserService from "../services/UserServices/CreateOrUpdateOnlineUserService";
import { splitDateTime } from "../helpers/SplitDateTime";
import format from 'date-fns/format';
import ptBR from 'date-fns/locale/pt-BR';
import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService";
import { check } from "prettier";
import { addHours, addMinutes, addSeconds, intervalToDuration, add } from "date-fns";
import { date } from "faker";
import sumOnlineTimeNow from '../helpers/SumOlineTimeNow'
import UserOnlineTime from "../models/UserOnlineTime";
let _fifo: any
let whoIsOnline_monitor: any;
// const listUserId:any[] = [{'id':8, status: 'offline'},{'id':3, status: 'offline'},{'id':5, status: 'offline'}]
let listUserId: any[] = []
let count = 0
// let countTest = 0
let uuid: any = 0
let dateTime = splitDateTime(new Date(format(new Date(), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR })))
let timeInterval = 5
let lstOnline: any = []
let deleted: boolean = false;
const emitterOnline = (user: any, status: string, showOnlineTime: boolean = true) => {
if (!user.id || !user.updatedAt || !user.onlineTime) return
let newOnlinetime = sumOnlineTimeNow(user)
let onlineTime: any = {
userId: user.id,
status: status,
onlineTime: newOnlinetime,
}
if (!showOnlineTime) {
onlineTime = {
userId: user.id,
status: status
}
}
2022-05-03 21:20:58 +00:00
// console.log('-------------> onlineTime: ', onlineTime)
const io = getIO();
io.emit("onlineStatus", {
action: "update",
userOnlineTime: onlineTime
});
}
const monitor = async () => {
2022-05-16 04:03:16 +00:00
try {
2022-05-16 04:03:16 +00:00
const usersSocket = require('./../libs/socket');
if (usersSocket?.ob) {
2022-05-16 04:03:16 +00:00
if (count > 1) {
count = 0
}
2022-05-16 04:03:16 +00:00
if (count == 0) {
uuid = usersSocket.ob.uuid
}
2022-05-16 04:03:16 +00:00
if (count == 1) {
if (uuid) {
if (uuid == usersSocket.ob.uuid) {
console.log('ALL USERS CLIENTS OFFLINE 1...........')
2022-05-16 04:03:16 +00:00
usersSocket.ob.listOnline = []
usersSocket.ob.listOnlineAux = []
usersSocket.ob.uuid = undefined
}
}
} else {
usersSocket.ob.listOnline.forEach(async (el: any) => {
2022-05-16 04:03:16 +00:00
const indexAux = lstOnline.findIndex((e: any) => e.id == el.id)
if (indexAux == -1) {
2022-05-16 04:03:16 +00:00
const userOnline = await createOrUpdateOnlineUserService({ userId: el.id, status: 'online' })
if (userOnline) {
2022-05-16 04:03:16 +00:00
el.onlineTime = userOnline.onlineTime
el.updatedAt = userOnline.updatedAt,
2022-05-16 04:03:16 +00:00
console.log(' * CREATED OR UPDATED USER TO ONLINE: ', userOnline.userId)
2022-05-16 04:03:16 +00:00
lstOnline.push({ 'id': el.id, 'status': 'online' })
}
}
// console.log('el: ', el)
2022-05-16 04:03:16 +00:00
if (el.status == 'waiting...') {
emitterOnline(el, el.status, false)
}
// else {
// emitterOnline(el, el.status)
// }
2022-05-16 04:03:16 +00:00
});
2022-05-16 04:03:16 +00:00
let difference = lstOnline.filter((x: any) => !usersSocket.ob.listOnline.map((e: any) => e.id).includes(x.id));
2022-05-16 04:03:16 +00:00
difference.forEach(async (e: any) => {
2022-05-16 04:03:16 +00:00
const index = lstOnline.findIndex((x: any) => x.id == e.id)
2022-05-16 04:03:16 +00:00
if (index != -1) {
lstOnline.splice(index, 1)
2022-05-16 04:03:16 +00:00
const userOnline = await createOrUpdateOnlineUserService({ userId: e.id, status: 'offline' })
2022-05-16 04:03:16 +00:00
}
2022-05-16 04:03:16 +00:00
})
}
count++
}
2022-05-16 04:03:16 +00:00
} catch (error) {
console.log('>>> There was an error no WhoIsOnlineMonitor.ts: ', error)
listUserId = []
count = 0
// countTest = 0
uuid = 0
}
2022-05-03 21:20:58 +00:00
};
const WhoIsOnlineMonitor = async () => {
try {
clearInterval(_fifo);
await monitor()
} catch (error) {
console.log('error on WhoIsOnlineMonitor: ', error)
}
finally {
_fifo = setInterval(WhoIsOnlineMonitor, 3000);
}
}
_fifo = setInterval(WhoIsOnlineMonitor, 3000);
const startWhoIsOnlineMonitor = async (mileseconds?: number) => {
listUserId = []
count = 0
// countTest = 0
uuid = 0
clearInterval(_fifo);
_fifo = setInterval(WhoIsOnlineMonitor, 3000);
// if (mileseconds) {
// timeInterval = mileseconds
// }
}
2022-05-03 21:20:58 +00:00
const stopWhoIsOnlineMonitor = async () => {
clearInterval(_fifo);
// _fifo = setInterval(WhoIsOnlineMonitor, 3000);
2022-05-03 21:20:58 +00:00
}
2022-05-03 21:20:58 +00:00
export { WhoIsOnlineMonitor, startWhoIsOnlineMonitor, stopWhoIsOnlineMonitor }
2022-05-03 21:20:58 +00:00