Criação do broker para otimização do csv export all finalizada!

pull/20/head
adriano 2022-08-28 22:09:00 -03:00
parent d4e6fc9a24
commit 46c9119195
7 changed files with 220 additions and 122 deletions

View File

@ -22,9 +22,11 @@ import ShowUserServiceReport from "../services/UserServices/ShowUserServiceRepor
import CountTicketsByUserQueue from "../services/UserServices/CountTicketsByUserQueue";
import ShowQueuesByUser from "../services/UserServices/ShowQueuesByUser";
import { filter } from "bluebird";
// import { filter } from "bluebird";
import { getIO } from "../libs/socket";
type IndexQuery = {
userId: string;
@ -33,6 +35,11 @@ type IndexQuery = {
pageNumber: string;
};
type ReportOnQueue = {
userId: string;
identifier: string;
}
export const reportUserByDateStartDateEnd = async (req: Request, res: Response): Promise<Response> => {
@ -42,14 +49,8 @@ export const reportUserByDateStartDateEnd = async (req: Request, res: Response):
const { userId, startDate, endDate, pageNumber } = req.query as IndexQuery
console.log('PAGE NUMBER: ', pageNumber)
const { tickets, count, hasMore } = await ShowTicketReport({ userId, startDate, endDate, pageNumber });
// return res.status(200).json(data_query);
return res.status(200).json({ tickets, count, hasMore });
};
@ -87,7 +88,7 @@ export const reportUserService = async (req: Request, res: Response): Promise<Re
let openCloseOutQueue = openByUserOutQueue.concat(closedUserOutQueue)
for (let i = 0; i < queuesByUser.length; i++) {
queuesByUser[i].countOpen = 0
queuesByUser[i].countClosed = 0
@ -100,25 +101,11 @@ export const reportUserService = async (req: Request, res: Response): Promise<Re
(queuesByUser[i].queueId == openCloseOnQueue[x].queueId && openCloseOnQueue[x].status == 'closed')) {
queuesByUser[i].countClosed = openCloseOnQueue[x].totAttendance
}
}
}
// console.log('queuesByUser: ', queuesByUser)
// console.log('queuesByUser: ',queuesByUser)
// console.log()
// console.log('CLIENT START TRUE openByUserOnQueue: ', openByUserOnQueue)
// console.log()
// console.log('CLIENT START FALSE openByUserOutQueue: ', openByUserOutQueue)
// console.log()
// console.log('CLIENT START TRUE closedByUserOnQueue: ', closedByUserOnQueue)
// console.log()
// console.log('CLIENT START FALSE closedUserOutQueue: ', closedUserOutQueue)
usersProfile.map((user: any) => {
@ -141,33 +128,6 @@ export const reportUserService = async (req: Request, res: Response): Promise<Re
}
// let openByUserOut = openQueueInOut.filter((e: any) => e.userId == user.id && !e.queueName)
// let openByUserIn = openQueueInOut.filter((e: any) => e.userId == user.id && e.queueName)
// if (openByUserOut && openByUserOut.length > 0) {
// user.openTicketByUserOut = openByUserOut
// }
// if (openByUserIn && openByUserIn.length > 0) {
// user.openTicketByUserIn = openByUserIn
// }
// let closedByUserOut = closedQueueInOut.filter((e: any) => e.userId == user.id && !e.queueName)
// let closedByUserIn = closedQueueInOut.filter((e: any) => e.userId == user.id && e.queueName)
// if (closedByUserOut && closedByUserOut.length > 0) {
// user.closedTicketByUserOut = closedByUserOut
// }
// if (closedByUserIn && closedByUserIn.length > 0) {
// user.closedTicketByUserIn = closedByUserIn
// }
// OPEN, CLOSED TICKETS STARTED BY USERS
let openClosedOutQueue = {}
let open = openCloseOutQueue.filter((e) => e.userId == user.id && e.status == 'open')
@ -187,16 +147,10 @@ export const reportUserService = async (req: Request, res: Response): Promise<Re
let openClosedInQueue = queuesByUser.filter((e) => e.userId == user.id)
if (openClosedInQueue && openClosedInQueue.length > 0) {
user.openClosedInQueue = openClosedInQueue
user.openClosedInQueue = openClosedInQueue
}
index = onlineUsers.findIndex((e: any) => e.userId == user.id)
if (index != -1) {
@ -229,9 +183,9 @@ export const reportMessagesUserByDateStartDateEnd = async (req: Request, res: Re
const { userId, startDate, endDate } = req.query as IndexQuery
let data_query_messages = await ShowMessageReport(userId, startDate, endDate);
let data_query_messages = await ShowMessageReport(userId, startDate, endDate);
for (var i = 0; i < data_query_messages.length; i++) {
for (var i = 0; i < data_query_messages.length; i++) {
if (data_query_messages[i].fromMe) {
data_query_messages[i].fromMe = 'Atendente'
@ -240,7 +194,7 @@ export const reportMessagesUserByDateStartDateEnd = async (req: Request, res: Re
data_query_messages[i].fromMe = 'Cliente'
}
data_query_messages[i].id = (i+1)
data_query_messages[i].id = (i + 1)
console.log('data_query_messages: ', data_query_messages[i])
}
@ -251,5 +205,27 @@ export const reportMessagesUserByDateStartDateEnd = async (req: Request, res: Re
export const reportOnQueue = async (req: Request, res: Response): Promise<Response> => {
// console.log(req.body)
const { adminId, identifier, queueStatus, file } = req.body
const io = getIO();
io.emit("queryOnQueueStatus", {
action: "update",
queryOnQueue: {
adminId: adminId,
identifier: identifier,
queueStatus: queueStatus,
file: file
}
});
return res.status(200).json({ message: 'ok' })
};

View File

@ -13,9 +13,7 @@ import QuickAnswer from "../models/QuickAnswer";
import SchedulingNotify from "../models/SchedulingNotify";
import StatusChatEnd from "../models/StatusChatEnd";
import UserOnlineTime from "../models/UserOnlineTime";
import UserOnlineTime from "../models/UserOnlineTime";
// eslint-disable-next-line
const dbConfig = require("../config/database");
// import dbConfig from "../config/database";
@ -37,7 +35,7 @@ const models = [
SchedulingNotify,
StatusChatEnd,
UserOnlineTime,
UserOnlineTime,
];
sequelize.addModels(models);

View File

@ -17,7 +17,7 @@ import { hash, compare } from "bcryptjs";
import Ticket from "./Ticket";
import Queue from "./Queue";
import UserQueue from "./UserQueue";
import UserOnlineTime from "./UserOnlineTime";
import UserOnlineTime from "./UserOnlineTime";
@Table
class User extends Model<User> {
@ -54,11 +54,9 @@ class User extends Model<User> {
@HasMany(() => Ticket)
tickets: Ticket[];
//test del
@HasMany(() => UserOnlineTime)
UserOnlineTime: UserOnlineTime[];
//
UserOnlineTime: UserOnlineTime[];
@BelongsToMany(() => Queue, () => UserQueue)
queues: Queue[];

View File

@ -9,6 +9,8 @@ const reportRoutes = express.Router();
reportRoutes.get("/reports", isAuth, ReportController.reportUserByDateStartDateEnd);
reportRoutes.post("/reports/onqueue", ReportController.reportOnQueue);
reportRoutes.get("/reports/user/services", isAuth, ReportController.reportUserService);
reportRoutes.get("/reports/messages", isAuth, ReportController.reportMessagesUserByDateStartDateEnd);

View File

@ -5,7 +5,7 @@
"dependencies": {
"@date-io/date-fns": "^1.3.13",
"@emotion/react": "^11.7.1",
"@emotion/styled": "^11.6.0",
"@emotion/styled": "^11.6.0",
"@material-ui/core": "^4.12.1",
"@material-ui/icons": "^4.9.1",
"@material-ui/lab": "^4.0.0-alpha.56",
@ -17,10 +17,12 @@
"@testing-library/user-event": "^12.1.7",
"axios": "^0.21.1",
"date-fns": "^2.28.0",
"dotenv": "^16.0.1",
"emoji-mart": "^3.0.1",
"formik": "^2.2.0",
"i18next": "^19.8.2",
"i18next-browser-languagedetector": "^6.0.1",
"js-file-download": "^0.4.12",
"markdown-to-jsx": "^7.1.0",
"material-table": "^1.69.3",
"mic-recorder-to-mp3": "^2.2.2",

View File

@ -18,13 +18,18 @@ import MaterialTable from 'material-table';
import LogoutIcon from '@material-ui/icons/CancelOutlined';
import apiBroker from "../../services/apiBroker";
import fileDownload from 'js-file-download'
import fs from 'fs'
import { CSVLink } from "react-csv";
// import CircularProgress from '@mui/material/CircularProgress';
import openSocket from "socket.io-client";
import { set } from "date-fns";
const report = [{ 'value': '1', 'label': 'Atendimento por atendentes' }, { 'value': '2', 'label': 'Usuários online/offline' }]
@ -96,9 +101,6 @@ let columns = [
const reducerQ = (state, action) => {
if (action.type === "DELETE_USER_STATUS") {
const userId = action.payload;
@ -339,6 +341,9 @@ const Report = () => {
const [profile, setProfile] = useState('')
const [dataRows, setData] = useState([]);
const [onQueueStatus, setOnQueueProcessStatus] = useState(undefined)
const [csvFile, setCsvFile] = useState()
@ -477,32 +482,103 @@ const Report = () => {
}, [reportOption])
useEffect(() => {
const delayDebounceFn = setTimeout(() => {
const fetchReportOnQueue = async () => {
try {
const queryOnQueue = await apiBroker.get("/reports/status/query/onqueue", {
params: {
adminId: userA.id,
baseURL: process.env.REACT_APP_BACKEND_URL,
identifier: 'csv'
}
});
if (queryOnQueue.data) {
setCsvFile(queryOnQueue.data.app.file)
setOnQueueProcessStatus(queryOnQueue.data.app.status)
}
else {
setOnQueueProcessStatus('empty')
}
console.log('>>>>> queryOnqueue: ', queryOnQueue.data)
} catch (err) {
console.log(err);
}
};
fetchReportOnQueue();
}, 500);
return () => clearTimeout(delayDebounceFn);
}, [])
const handleCSVDownload = async () => {
try {
let res = await apiBroker.get(`/reports/download/${csvFile}`, { responseType: 'blob' });
if (res) {
fileDownload(res.data, `${csvFile}`);
setOnQueueProcessStatus('empty')
}
} catch (err) {
console.log(err);
}
}
const handleCSVMessages = () => {
const fetchQueries = async () => {
try {
const dataQuery = await api.get("/reports/messages", { params: { userId, startDate, endDate }, });
// const dataQuery = await api.get("/reports/messages", { params: { userId, startDate, endDate }, });
console.log('dataQuery messages: ', dataQuery.data)
if (dataQuery.data.length > 0) {
let dataCSVFormat = dataQuery.data;
for (var i = 0; i < dataCSVFormat.length; i++) {
if (dataCSVFormat[i].fromMe) {
dataCSVFormat[i].fromMe = 'Atendente'
const querySavedOnQueue = await apiBroker.post("/reports/messages",
{
app: {
adminId: userA.id,
baseURL: process.env.REACT_APP_BACKEND_URL,
frontURL: process.env.REACT_APP_FRONTEND_URL,
identifier: 'csv'
},
query_params: {
userId: userId,
startDate: startDate,
endDate: endDate
}
else {
dataCSVFormat[i].fromMe = 'Cliente'
}
}
});
setDataCSV(dataCSVFormat)
setIsMount(false);
}
console.log('dataQuery messages: ', querySavedOnQueue.data.queueStatus)
const onQueueStatus = querySavedOnQueue.data.queueStatus
setOnQueueProcessStatus(onQueueStatus)
// if (onQueueStatus.data.length > 0) {
// let dataCSVFormat = queueStatus.data;
// setIsMount(false);
// }
} catch (err) {
console.log(err);
@ -532,9 +608,24 @@ const Report = () => {
useEffect(() => {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
socket.on("queryOnQueueStatus", (data) => {
if (data.action === 'update') {
if (String(data.queryOnQueue.adminId) === String(userA.id)) {
setCsvFile(data.queryOnQueue.file)
setOnQueueProcessStatus(data.queryOnQueue.queueStatus)
}
}
})
if (reportOption === '2') {
const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
// const socket = openSocket(process.env.REACT_APP_BACKEND_URL);
socket.on("onlineStatus", (data) => {
@ -563,9 +654,10 @@ const Report = () => {
}
});
return () => {
socket.disconnect();
};
// return () => {
// socket.disconnect();
// };
}
else if (reportOption === "1") {
@ -573,6 +665,10 @@ const Report = () => {
setTicketsPageNumber(1)
}
return () => {
socket.disconnect();
};
}, [reportOption, startDate, endDate, userId]);
@ -641,6 +737,48 @@ const Report = () => {
};
const renderSwitch = (param) => {
switch (param) {
case 'empty':
return (
<>
<Button
variant="contained"
color="primary"
onClick={(e) => {
handleCSVMessages()
}}
>
{"CSV ALL"}
</Button>
</>);
case 'pending' || 'processing':
return (
<>
<span>PROCESSING...</span>
</>);
case 'success':
return (
<>
<Button
variant="contained"
color="primary"
onClick={(e) => {
handleCSVDownload()
}}
>
{"DOWNLOAD"}
</Button>
</>);
default:
return (<><span>WAITING...</span></>);
}
}
return (
<Can
@ -664,35 +802,9 @@ const Report = () => {
<div style={{ margin: '2px' }}></div>
{reportOption === '1' &&
{reportOption === '1' &&
<div>
{/* <Button style={{ display: "none" }}
variant="contained"
color="primary"
onClick={(e) => {
handleCSVMessages()
}}
> */}
{/* <Button
variant="contained"
color="primary"
onClick={(e) => {
handleCSVMessages()
}}
>
{"CSV ALL"}
</Button> */}
<div>
<CSVLink
data={dataCSV}
headers={columns}
filename={'Relatorio_detalhado_atendimento_atendentes.csv'}
target={'_blank'}
ref={csvLink} />
</div>
{renderSwitch(onQueueStatus)}
</div>
}

View File

@ -0,0 +1,10 @@
import axios from "axios";
const apiBroker = axios.create({
baseURL: process.env.REACT_APP_URL_API_BROKER,
headers: {
Authorization: `Bearer ${process.env.REACT_APP_API_BROKER_TOKEN}`
}
});
export default apiBroker;