Criação parcial de cache para consultar lembretes para envio

pull/21/head
adriano 2022-12-18 11:45:41 -03:00
parent 3980814c5e
commit bc4712362d
10 changed files with 404 additions and 121 deletions

View File

@ -4,15 +4,8 @@ import { getIO } from "../libs/socket";
import DeleteSchedulingNotifyService from "../services/SchedulingNotifyServices/DeleteSchedulingNotifyService"; import DeleteSchedulingNotifyService from "../services/SchedulingNotifyServices/DeleteSchedulingNotifyService";
import ListSchedulingNotifyContactService from "../services/SchedulingNotifyServices/ListSchedulingNotifyContactService"; import ListSchedulingNotifyContactService from "../services/SchedulingNotifyServices/ListSchedulingNotifyContactService";
import CreateSchedulingNotifyService from "../services/SchedulingNotifyServices/CreateSchedulingNotifyService"; import CreateSchedulingNotifyService from "../services/SchedulingNotifyServices/CreateSchedulingNotifyService";
import ShowSchedulingNotifyService from "../services/SchedulingNotifyServices/ShowSchedulingNotifyService";
// const test = await ListSchedulingNotifyContactService('5517988310949','2022-03-18','2022-03-19'); import { deleteScheduleByTicketIdCache } from "../helpers/SchedulingNotifyCache";
// const test = await ListSchedulingNotifyContactService('','2022-03-18','2022-03-19');
// const test = await ListSchedulingNotifyContactService('5517988310949');
//
type IndexQuery = { type IndexQuery = {
@ -67,6 +60,10 @@ export const remove = async ( req: Request, res: Response ): Promise<Response> =
const { scheduleId } = req.params; const { scheduleId } = req.params;
let schedule: any = await ShowSchedulingNotifyService(scheduleId)
await deleteScheduleByTicketIdCache(schedule.ticketId)
await DeleteSchedulingNotifyService(scheduleId); await DeleteSchedulingNotifyService(scheduleId);
return res.status(200).send(); return res.status(200).send();

View File

@ -0,0 +1,221 @@
import Redis from 'ioredis'
import { type } from 'os'
const unflatten = require('flat').unflatten
var flatten = require('flat')
import ListContactsServiceCache from "../services/ContactServices/ListContactsServiceCache"
import { redisConn } from './TicketCache'
import SchedulingNotify from '../models/SchedulingNotify'
import { format } from "date-fns";
import ptBR from 'date-fns/locale/pt-BR';
import { escapeCharCache } from './ContactsCache'
const deleteScheduleByTicketIdCache = async (ticketId: string | number) => {
const redis: any = await redisConn();
if (!redis) return
if (redis.status !== 'connect') return
const schedule_cache: any = await redis.hgetall(`schedule:${ticketId}`)
try {
if (schedule_cache && Object.keys(schedule_cache).length > 0) {
await redis.del(`schedule:${ticketId}`)
console.log(`Schedule cache ticketId ${schedule_cache.ticketId} deleted!`)
}
else {
console.log('SCHEDULE CACHE NOT FOUND!')
}
} catch (error) {
console.log(`There was an error on deleteScheduleByTicketIdCache: ${error}`)
}
redis.quit()
}
const updateScheduleCacheByTicketId = async (scheduleNotify: any) => {
const redis: any = await redisConn();
if (!redis) return
if (redis.status !== 'connect') return
const pipeline = redis.pipeline()
let entries = Object.entries(scheduleNotify)
entries.forEach((e: any) => {
pipeline.hset(`schedule:${scheduleNotify.ticketId}`, e[0], e[1])
})
await pipeline.exec(() => { console.log("schedule Key/value inserted/updated") });
redis.quit()
}
const createSchedulingNotifyCache = async (scheduleNotify: any) => {
const redis: any = await redisConn();
if (!redis) return
if (redis.status !== 'connect') return
let date_time: any = format(new Date(scheduleNotify.schedulingTime), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }).split(' ')
delete scheduleNotify.schedulingTime
delete scheduleNotify.updatedAt
delete scheduleNotify.createdAt
delete scheduleNotify.schedulingDate
console.log('created date_time: ', date_time)
scheduleNotify.date = date_time[0]
scheduleNotify.date_escaped = escapeCharCache(date_time[0])
scheduleNotify.hour = date_time[1].split(':')[0]
scheduleNotify.minute = date_time[1].split(':')[1]
await redis.hmset(`schedule:${scheduleNotify.ticketId}`, scheduleNotify);
console.log(`${scheduleNotify.length} SCHEDULE NOTIFY INSERTED IN CACHE!`)
redis.quit()
}
async function searchScheduleCache(date: string, hour: number | string, minute: number | string) {
const redis: any = await redisConn();
if (!redis) return
if (redis.status !== 'connect') return null
date = escapeCharCache(date).trim()
console.log('kkkkkkkkkkk date: ', date)
const response: any = await redis.call('FT.SEARCH', 'idx_schedule', `(@date_escaped:${date}) (@hour:${hour}) (@minute:${minute})`)
redis.quit()
if (response.length === 1) {
return []
}
const results: any = []
for (let n = 2; n < response.length; n += 2) {
const result: any = {}
const fieldNamesAndValues = response[n]
for (let m = 0; m < fieldNamesAndValues.length; m += 2) {
const k = fieldNamesAndValues[m]
const v = fieldNamesAndValues[m + 1]
result[k] = v
}
results.push(result)
}
return results
}
const loadSchedulesCache = async () => {
await createScheduleIndexCache('idx_schedule')
const redis: any = await redisConn();
if (!redis) return
if (redis.status !== 'connect') return
let schedules: any = await SchedulingNotify.findAll({ raw: true, attributes: ["id", "statusChatEndId", "ticketId", "schedulingTime", "message"] });
// console.log('SCHEDULE NOTIFY CACHE2: ', schedules)
const pipeline = redis.pipeline()
for (let i = 0; i < schedules.length; i++) {
let date_time: any = format(new Date(schedules[i].schedulingTime), 'yyyy-MM-dd HH:mm:ss', { locale: ptBR }).split(' ')
delete schedules[i].schedulingTime
console.log('date_time: ', date_time)
schedules[i].date = date_time[0]
schedules[i].date_escaped = escapeCharCache(date_time[0])
schedules[i].hour = date_time[1].split(':')[0]
schedules[i].minute = date_time[1].split(':')[1]
pipeline.hmset(`schedule:${schedules[i].ticketId}`, schedules[i]);
}
await pipeline.exec(() => { console.log(`${schedules.length} SCHEDULES NOTIFY INSERTED IN CACHE!`) });
redis.quit()
// let test = await searchScheduleCache('2022-12-16', '18', '30')
// console.log('--------------> TEST: ', test)
}
const createScheduleIndexCache = async (hashIndex: string) => {
const redis: any = await redisConn();
if (!redis) return
if (redis.status !== 'connect') return
try {
const lst_index_redis: any = await redis.call('FT._LIST')
if (lst_index_redis.includes(hashIndex)) {
console.log('entrou...')
await redis.call('FT.DROPINDEX', hashIndex)
}
const response = await redis.call('FT.CREATE', hashIndex, 'ON', 'HASH', 'PREFIX', '1', 'schedule:', 'SCHEMA', 'id', 'TEXT', 'SORTABLE', 'date_escaped', 'TEXT', 'SORTABLE', 'hour', 'TEXT', 'SORTABLE', 'minute', 'TEXT', 'SORTABLE')
console.log('Schedule index created: ', response)
} catch (error) {
console.log('There was an error on createScheduleIndexCache: ', error)
}
redis.quit()
}
export {
loadSchedulesCache,
searchScheduleCache,
updateScheduleCacheByTicketId,
createSchedulingNotifyCache,
deleteScheduleByTicketIdCache
}

View File

@ -8,6 +8,8 @@ import { getIO } from "../libs/socket";
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService"; import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
import path from "path"; import path from "path";
import { convertBytes } from "./ConvertBytes"; import { convertBytes } from "./ConvertBytes";
import { deleteScheduleByTicketIdCache } from "./SchedulingNotifyCache";
import SchedulingNotify from "../models/SchedulingNotify";
const fastFolderSize = require('fast-folder-size') const fastFolderSize = require('fast-folder-size')
const { promisify } = require('util') const { promisify } = require('util')
@ -15,6 +17,8 @@ const fs = require('fs')
const { exec } = require("child_process"); const { exec } = require("child_process");
let _fifo: any
let scheduler_monitor: any; let scheduler_monitor: any;
let timeInterval = 5 let timeInterval = 5
@ -36,11 +40,32 @@ const monitor = async () => {
try { try {
// const { schedulingNotifies, count, hasMore } = await ListSchedulingNotifyService({ searchParam: dateParm, pageNumber: "1" }); const { schedulingNotifies, count, hasMore } = await ListSchedulingNotifyService({ searchParam: dateParm, pageNumber: "1" });
// // console.log('schedulingNotifies: ',schedulingNotifies) // console.log('schedulingNotifies: ',schedulingNotifies)
// if (schedulingNotifies && schedulingNotifies.length > 0) { if (schedulingNotifies && schedulingNotifies.length > 0) {
console.log('ENTROU NA DATA schedulingNotifies: ', schedulingNotifies)
for (let i = 0; i < schedulingNotifies.length; i++) {
const ticket = await ShowTicketService(schedulingNotifies[i].ticketId);
SetTicketMessagesAsRead(ticket);
await SendWhatsAppMessage({
body: schedulingNotifies[i].message, ticket
});
await deleteScheduleByTicketIdCache(schedulingNotifies[i].ticketId)
await DeleteSchedulingNotifyService(schedulingNotifies[i].id)
}
// const ticket = await ShowTicketService(schedulingNotifies[0].ticketId); // const ticket = await ShowTicketService(schedulingNotifies[0].ticketId);
@ -50,9 +75,11 @@ const monitor = async () => {
// body: schedulingNotifies[0].message, ticket // body: schedulingNotifies[0].message, ticket
// }); // });
// DeleteSchedulingNotifyService(schedulingNotifies[0].id) // await deleteScheduleByTicketIdCache(schedulingNotifies[0].ticketId)
// } // await DeleteSchedulingNotifyService(schedulingNotifies[0].id)
}
exec("df -h /", (error: any, stdout: any, stderr: any) => { exec("df -h /", (error: any, stdout: any, stderr: any) => {
@ -132,27 +159,46 @@ const monitor = async () => {
} catch (error) { } catch (error) {
console.log('>>> SchedulingNotifiySendMessage.ts error: ', error) console.log('>>> SchedulingNotifiySendMessage.ts error: ', error)
stopSchedulingMonitor()
startSchedulingMonitor(timeInterval)
} }
}; };
export const startSchedulingMonitor = async (mileseconds: number) => { const SchedulingNotifySendMessage = async () => {
timeInterval = mileseconds try {
clearInterval(_fifo);
scheduler_monitor = setInterval(monitor, mileseconds) await monitor()
} catch (error) {
console.log('error on SchedulingNotifySendMessage: ', error)
}
finally {
_fifo = setInterval(SchedulingNotifySendMessage, 5000);
}
} }
_fifo = setInterval(SchedulingNotifySendMessage, 5000);
export const stopSchedulingMonitor = async () => {
clearInterval(scheduler_monitor) module.exports = SchedulingNotifySendMessage
}
// export const startSchedulingMonitor = async (mileseconds: number) => {
// timeInterval = mileseconds
// scheduler_monitor = setInterval(monitor, mileseconds)
// }
// export const stopSchedulingMonitor = async () => {
// clearInterval(scheduler_monitor)
// }

View File

@ -4,14 +4,15 @@ import { initIO } from "./libs/socket";
import { logger } from "./utils/logger"; import { logger } from "./utils/logger";
import { StartAllWhatsAppsSessions } from "./services/WbotServices/StartAllWhatsAppsSessions"; import { StartAllWhatsAppsSessions } from "./services/WbotServices/StartAllWhatsAppsSessions";
import { startSchedulingMonitor } from "./helpers/SchedulingNotifySendMessage" import "./helpers/SchedulingNotifySendMessage"
import { startWhoIsOnlineMonitor } from "./helpers/WhoIsOnlineMonitor"
import { startWhoIsOnlineMonitor } from "./helpers/WhoIsOnlineMonitor"
import { loadTicketsCache, flushCache, cacheSize } from './helpers/TicketCache' import { loadTicketsCache, flushCache, cacheSize } from './helpers/TicketCache'
import { loadContactsCache } from './helpers/ContactsCache' import { loadContactsCache } from './helpers/ContactsCache'
import { loadWhatsappCache } from './helpers/WhatsCache' import { loadWhatsappCache } from './helpers/WhatsCache'
import { delRestoreControllFile } from "./helpers/RestoreControll"; import { delRestoreControllFile } from "./helpers/RestoreControll";
import { createSessionDir } from "./helpers/CreateSessionDir"; import { createSessionDir } from "./helpers/CreateSessionDir";
import { loadSchedulesCache, } from "./helpers/SchedulingNotifyCache";
const server = app.listen(process.env.PORT, () => { const server = app.listen(process.env.PORT, () => {
logger.info(`Server started on port: ${process.env.PORT}`); logger.info(`Server started on port: ${process.env.PORT}`);
@ -33,6 +34,7 @@ gracefulShutdown(server);
await loadContactsCache() await loadContactsCache()
await loadTicketsCache() await loadTicketsCache()
await loadWhatsappCache() await loadWhatsappCache()
await loadSchedulesCache()
} }
@ -40,6 +42,6 @@ gracefulShutdown(server);
createSessionDir() createSessionDir()
delRestoreControllFile() delRestoreControllFile()
startSchedulingMonitor(5000)
startWhoIsOnlineMonitor(3000) startWhoIsOnlineMonitor(3000)

View File

@ -1,4 +1,5 @@
import AppError from "../../errors/AppError"; import AppError from "../../errors/AppError";
import { createSchedulingNotifyCache } from "../../helpers/SchedulingNotifyCache";
import SchedulingNotify from "../../models/SchedulingNotify"; import SchedulingNotify from "../../models/SchedulingNotify";
@ -56,9 +57,10 @@ const CreateSchedulingNotifyService = async ({
message message
}) })
} }
await createSchedulingNotifyCache(JSON.parse(JSON.stringify(schedulingNotify)))
return schedulingNotify return schedulingNotify
} }

View File

@ -1,4 +1,5 @@
import { Op, Sequelize } from "sequelize"; import { Op, Sequelize } from "sequelize";
import { searchScheduleCache } from "../../helpers/SchedulingNotifyCache";
import SchedulingNotify from "../../models/SchedulingNotify"; import SchedulingNotify from "../../models/SchedulingNotify";
interface Request { interface Request {
@ -29,6 +30,21 @@ interface Request {
let hour = searchParam.split(' ')[1].split(':')[0] let hour = searchParam.split(' ')[1].split(':')[0]
let minute = searchParam.split(' ')[1].split(':')[1] let minute = searchParam.split(' ')[1].split(':')[1]
let fromCache = null
fromCache = await searchScheduleCache(date, hour, minute)
if (fromCache) {
return {
schedulingNotifies: fromCache,
count: 0,
hasMore: false,
};
}
const whereCondition = { const whereCondition = {
[Op.and]: [ [Op.and]: [
{ {
@ -65,4 +81,3 @@ interface Request {
}; };
export default ListSchedulingNotifyService; export default ListSchedulingNotifyService;

View File

@ -92,7 +92,7 @@ const TableUser = ({ classes, usersOnlineInfo, logout }) => {
<TableCell>{i18n.t("dashboard.table_users.column2")}</TableCell> <TableCell>{i18n.t("dashboard.table_users.column2")}</TableCell>
<TableCell>{i18n.t("dashboard.table_users.column3")}</TableCell> <TableCell>{i18n.t("dashboard.table_users.column3")}</TableCell>
<TableCell>{i18n.t("dashboard.table_users.column4")}</TableCell> <TableCell>{i18n.t("dashboard.table_users.column4")}</TableCell>
<TableCell>Ações</TableCell> <TableCell>{i18n.t("dashboard.table_users.column5")}</TableCell>
</TableRow> </TableRow>
</TableHead> </TableHead>

View File

@ -58,7 +58,7 @@ const messages = {
column2: 'Open by Queue', column2: 'Open by Queue',
column3: 'Closed by Queue', column3: 'Closed by Queue',
column4: 'Online time', column4: 'Online time',
column4: 'Actions', column5: 'Actions',
} }
}, },
connections: { connections: {

View File

@ -60,7 +60,7 @@ const messages = {
column2: 'Abrir por cola', column2: 'Abrir por cola',
column3: 'Cerrado por cola', column3: 'Cerrado por cola',
column4: 'Tiempo Online', column4: 'Tiempo Online',
column4: 'Actions', column5: 'Actions',
} }
}, },
connections: { connections: {

View File

@ -58,7 +58,7 @@ const messages = {
column2: 'Abertos Por Fila', column2: 'Abertos Por Fila',
column3: 'Fechados Por Fila', column3: 'Fechados Por Fila',
column4: 'Tempo Online', column4: 'Tempo Online',
column4: 'Ações', column5: 'Ações',
} }
}, },
connections: { connections: {