Built motion from commit eddce310.|2.6.12
[motion2.git] / server / api / analyticCustomReport / analyticCustomReport.controller.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x5fcc=['cdr','startDate','endDate','.createdAt\x20BETWEEN\x20?\x20AND\x20?','info','Run\x20Query:','child_process','fork','../../components/export/','../../files/reports','savename','countTable','COUNT(*)','SELECT','run','runReport','Please\x20insert\x20at\x20least\x20one\x20field','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','bluebird','mustache','path','sox','fs-extra','lodash','squel','jsforce','desk.js','to-csv','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','dbH','status','end','json','offset','undefined','limit','count','Content-Range','save','then','destroy','get','CustomReports','sendStatus','stack','name','send','index','rawAttributes','fieldName','type','model','map','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','where','pick','filter','merge','VIRTUAL','options','AnalyticCustomReport','includeAll','findAll','rows','catch','params','include','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','Reports','UserProfileSection','userProfileId','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','preview','join','left_join','right_join','cross_join','SUM(%s)','COUNT(%s)','COUNT(DISTINCT\x20%s)','MAX(%s)','MIN(%s)','AVG(%s)','GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20DESC)','SEC_TO_TIME(%s)','DATE(%s)','ROUND(%s)','UNIX_TIMESTAMP(%s)','IS\x20NULL','IS\x20NOT\x20NULL','IS\x20EMPTY','IS\x20NOT\x20EMPTY','rules','operator','group','field','condition','value','Sequelize','ValidationErrorItem','AnalyticMetric','keyBy','select','from','table','MetricId','metric','function','format','replace','groupBy','orderBy','DESC','alias','joins','parentKey','foreignTable','foreignKey','%s.%s\x20=\x20%s.%s','conditions','clone','debug','Limited\x20Query:','toString','sequelize','QueryTypes','GROUP_CONCAT(%s)','GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20ASC)','HOUR(%s)','includes','getFields','parse','%s-%s.%s','randomstring','output','csv','custom','AnalyticExtractedReport'];(function(_0x96a2cb,_0x51f751){var _0x4bccfa=function(_0x175691){while(--_0x175691){_0x96a2cb['push'](_0x96a2cb['shift']());}};_0x4bccfa(++_0x51f751);}(_0x5fcc,0x161));var _0xc5fc=function(_0x243394,_0x1437c1){_0x243394=_0x243394-0x0;var _0x3c2306=_0x5fcc[_0x243394];return _0x3c2306;};'use strict';var emlformat=require(_0xc5fc('0x0'));var rimraf=require(_0xc5fc('0x1'));var zipdir=require(_0xc5fc('0x2'));var jsonpatch=require(_0xc5fc('0x3'));var rp=require(_0xc5fc('0x4'));var moment=require('moment');var BPromise=require(_0xc5fc('0x5'));var Mustache=require(_0xc5fc('0x6'));var util=require('util');var path=require(_0xc5fc('0x7'));var sox=require(_0xc5fc('0x8'));var csv=require('to-csv');var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0xc5fc('0x9'));var _=require(_0xc5fc('0xa'));var squel=require(_0xc5fc('0xb'));var crypto=require('crypto');var jsforce=require(_0xc5fc('0xc'));var deskjs=require(_0xc5fc('0xd'));var toCsv=require(_0xc5fc('0xe'));var querystring=require('querystring');var Papa=require(_0xc5fc('0xf'));var Redis=require(_0xc5fc('0x10'));var authService=require(_0xc5fc('0x11'));var qs=require(_0xc5fc('0x12'));var as=require(_0xc5fc('0x13'));var hardwareService=require(_0xc5fc('0x14'));var logger=require(_0xc5fc('0x15'))(_0xc5fc('0x16'));var utils=require(_0xc5fc('0x17'));var config=require(_0xc5fc('0x18'));var licenseUtil=require(_0xc5fc('0x19'));var db=require('../../mysqldb')['db'];var dbH=require(_0xc5fc('0x1a'))[_0xc5fc('0x1b')];function respondWithStatusCode(_0x38236d,_0x13824c){_0x13824c=_0x13824c||0xcc;return function(_0x20e196){if(_0x20e196){return _0x38236d['sendStatus'](_0x13824c);}return _0x38236d[_0xc5fc('0x1c')](_0x13824c)[_0xc5fc('0x1d')]();};}function respondWithResult(_0x5a3cdd,_0x33eb26){_0x33eb26=_0x33eb26||0xc8;return function(_0x41a630){if(_0x41a630){return _0x5a3cdd['status'](_0x33eb26)[_0xc5fc('0x1e')](_0x41a630);}};}function respondWithFilteredResult(_0xef1f2e,_0x28d5f7){return function(_0xc996b8){if(_0xc996b8){var _0x5a2299=typeof _0x28d5f7[_0xc5fc('0x1f')]===_0xc5fc('0x20')&&typeof _0x28d5f7[_0xc5fc('0x21')]===_0xc5fc('0x20');var _0xe7b3f3=_0xc996b8[_0xc5fc('0x22')];var _0x1c58b3=_0x5a2299?0x0:_0x28d5f7[_0xc5fc('0x1f')];var _0x501b95=_0x5a2299?_0xc996b8[_0xc5fc('0x22')]:_0x28d5f7[_0xc5fc('0x1f')]+_0x28d5f7[_0xc5fc('0x21')];var _0x2db80d;if(_0x501b95>=_0xe7b3f3){_0x501b95=_0xe7b3f3;_0x2db80d=0xc8;}else{_0x2db80d=0xce;}_0xef1f2e[_0xc5fc('0x1c')](_0x2db80d);return _0xef1f2e['set'](_0xc5fc('0x23'),_0x1c58b3+'-'+_0x501b95+'/'+_0xe7b3f3)[_0xc5fc('0x1e')](_0xc996b8);}return null;};}function patchUpdates(_0x2d88ac){return function(_0x53007b){try{jsonpatch['apply'](_0x53007b,_0x2d88ac,!![]);}catch(_0x2bdbbc){return BPromise['reject'](_0x2bdbbc);}return _0x53007b[_0xc5fc('0x24')]();};}function saveUpdates(_0xd32eb1,_0x3d0147){return function(_0x4a26d1){if(_0x4a26d1){return _0x4a26d1['update'](_0xd32eb1)[_0xc5fc('0x25')](function(_0x20322e){return _0x20322e;});}return null;};}function removeEntity(_0x2b0e4b,_0x54bed1){return function(_0xb290ea){if(_0xb290ea){return _0xb290ea[_0xc5fc('0x26')]()[_0xc5fc('0x25')](function(){var _0x532b2d=_0xb290ea[_0xc5fc('0x27')]({'plain':!![]});var _0x11760e=_0xc5fc('0x28');return db['UserProfileResource'][_0xc5fc('0x26')]({'where':{'type':_0x11760e,'resourceId':_0x532b2d['id']}})['then'](function(){return _0xb290ea;});})[_0xc5fc('0x25')](function(){_0x2b0e4b[_0xc5fc('0x1c')](0xcc)[_0xc5fc('0x1d')]();});}};}function handleEntityNotFound(_0x254d4f,_0x1b7f9e){return function(_0x4bcd0c){if(!_0x4bcd0c){_0x254d4f[_0xc5fc('0x29')](0x194);}return _0x4bcd0c;};}function handleError(_0x31696b,_0x139284){_0x139284=_0x139284||0x1f4;return function(_0x2d373f){logger['error'](_0x2d373f[_0xc5fc('0x2a')]);if(_0x2d373f[_0xc5fc('0x2b')]){delete _0x2d373f[_0xc5fc('0x2b')];}_0x31696b[_0xc5fc('0x1c')](_0x139284)[_0xc5fc('0x2c')](_0x2d373f);};}exports[_0xc5fc('0x2d')]=function(_0x3e4f00,_0x3b4307){var _0x1770c1={},_0x41decb={},_0x48724f={'count':0x0,'rows':[]};var _0x43d6e3=_['map'](db['AnalyticCustomReport'][_0xc5fc('0x2e')],function(_0x22caa4){return{'name':_0x22caa4[_0xc5fc('0x2f')],'type':_0x22caa4[_0xc5fc('0x30')]['key']};});_0x41decb[_0xc5fc('0x31')]=_[_0xc5fc('0x32')](_0x43d6e3,'name');_0x41decb[_0xc5fc('0x33')]=_[_0xc5fc('0x34')](_0x3e4f00[_0xc5fc('0x33')]);_0x41decb[_0xc5fc('0x35')]=_[_0xc5fc('0x36')](_0x41decb[_0xc5fc('0x31')],_0x41decb['query']);_0x1770c1[_0xc5fc('0x37')]=_[_0xc5fc('0x36')](_0x41decb['model'],qs[_0xc5fc('0x38')](_0x3e4f00[_0xc5fc('0x33')][_0xc5fc('0x38')]));_0x1770c1[_0xc5fc('0x37')]=_0x1770c1[_0xc5fc('0x37')][_0xc5fc('0x39')]?_0x1770c1[_0xc5fc('0x37')]:_0x41decb[_0xc5fc('0x31')];if(!_0x3e4f00[_0xc5fc('0x33')][_0xc5fc('0x3a')](_0xc5fc('0x3b'))){_0x1770c1['limit']=qs['limit'](_0x3e4f00[_0xc5fc('0x33')][_0xc5fc('0x21')]);_0x1770c1['offset']=qs['offset'](_0x3e4f00[_0xc5fc('0x33')][_0xc5fc('0x1f')]);}_0x1770c1[_0xc5fc('0x3c')]=qs['sort'](_0x3e4f00['query']['sort']);_0x1770c1[_0xc5fc('0x3d')]=qs[_0xc5fc('0x35')](_[_0xc5fc('0x3e')](_0x3e4f00[_0xc5fc('0x33')],_0x41decb[_0xc5fc('0x35')]),_0x43d6e3);if(_0x3e4f00[_0xc5fc('0x33')][_0xc5fc('0x3f')]){_0x1770c1[_0xc5fc('0x3d')]=_[_0xc5fc('0x40')](_0x1770c1[_0xc5fc('0x3d')],{'$or':_[_0xc5fc('0x32')](_0x43d6e3,function(_0xaedd30){if(_0xaedd30['type']!==_0xc5fc('0x41')){var _0x52bf37={};_0x52bf37[_0xaedd30[_0xc5fc('0x2b')]]={'$like':'%'+_0x3e4f00[_0xc5fc('0x33')][_0xc5fc('0x3f')]+'%'};return _0x52bf37;}})});}_0x1770c1=_[_0xc5fc('0x40')]({},_0x1770c1,_0x3e4f00[_0xc5fc('0x42')]);var _0x5235fd={'where':_0x1770c1[_0xc5fc('0x3d')]};return db[_0xc5fc('0x43')][_0xc5fc('0x22')](_0x5235fd)['then'](function(_0x2a2b08){_0x48724f[_0xc5fc('0x22')]=_0x2a2b08;if(_0x3e4f00[_0xc5fc('0x33')][_0xc5fc('0x44')]){_0x1770c1['include']=[{'all':!![]}];}return db['AnalyticCustomReport'][_0xc5fc('0x45')](_0x1770c1);})[_0xc5fc('0x25')](function(_0x387fd7){_0x48724f[_0xc5fc('0x46')]=_0x387fd7;return _0x48724f;})[_0xc5fc('0x25')](respondWithFilteredResult(_0x3b4307,_0x1770c1))[_0xc5fc('0x47')](handleError(_0x3b4307,null));};exports['show']=function(_0x22771d,_0x3b757b){var _0x1d3230={'raw':![],'where':{'id':_0x22771d[_0xc5fc('0x48')]['id']}},_0x2fd2bc={};_0x2fd2bc['model']=_[_0xc5fc('0x34')](db[_0xc5fc('0x43')][_0xc5fc('0x2e')]);_0x2fd2bc[_0xc5fc('0x33')]=_[_0xc5fc('0x34')](_0x22771d[_0xc5fc('0x33')]);_0x2fd2bc[_0xc5fc('0x35')]=_[_0xc5fc('0x36')](_0x2fd2bc[_0xc5fc('0x31')],_0x2fd2bc[_0xc5fc('0x33')]);_0x1d3230[_0xc5fc('0x37')]=_['intersection'](_0x2fd2bc[_0xc5fc('0x31')],qs[_0xc5fc('0x38')](_0x22771d[_0xc5fc('0x33')]['fields']));_0x1d3230[_0xc5fc('0x37')]=_0x1d3230[_0xc5fc('0x37')][_0xc5fc('0x39')]?_0x1d3230[_0xc5fc('0x37')]:_0x2fd2bc[_0xc5fc('0x31')];if(_0x22771d[_0xc5fc('0x33')]['includeAll']){_0x1d3230[_0xc5fc('0x49')]=[{'all':!![]}];}_0x1d3230=_[_0xc5fc('0x40')]({},_0x1d3230,_0x22771d['options']);return db['AnalyticCustomReport'][_0xc5fc('0x4a')](_0x1d3230)[_0xc5fc('0x25')](handleEntityNotFound(_0x3b757b,null))[_0xc5fc('0x25')](respondWithResult(_0x3b757b,null))[_0xc5fc('0x47')](handleError(_0x3b757b,null));};exports[_0xc5fc('0x4b')]=function(_0x2134dd,_0x116dd5){return db[_0xc5fc('0x43')][_0xc5fc('0x4b')](_0x2134dd[_0xc5fc('0x4c')],{})[_0xc5fc('0x25')](function(_0x5a30d0){var _0x5a6b62=_0x2134dd[_0xc5fc('0x4d')][_0xc5fc('0x27')]({'plain':!![]});if(!_0x5a6b62)throw new Error(_0xc5fc('0x4e'));if(_0x5a6b62[_0xc5fc('0x4f')]===_0xc5fc('0x4d')){var _0x5ef055=_0x5a30d0[_0xc5fc('0x27')]({'plain':!![]});var _0x5e2613=_0xc5fc('0x50');return db[_0xc5fc('0x51')][_0xc5fc('0x4a')]({'where':{'name':_0x5e2613,'userProfileId':_0x5a6b62[_0xc5fc('0x52')]},'raw':!![]})['then'](function(_0x2f87ba){if(_0x2f87ba&&_0x2f87ba['autoAssociation']===0x0){return db['UserProfileResource'][_0xc5fc('0x4b')]({'name':_0x5ef055[_0xc5fc('0x2b')],'resourceId':_0x5ef055['id'],'type':_0xc5fc('0x28'),'sectionId':_0x2f87ba['id']},{})[_0xc5fc('0x25')](function(){return _0x5a30d0;});}else{return _0x5a30d0;}})['catch'](function(_0x46b533){logger['error'](_0xc5fc('0x53'),_0x46b533);throw _0x46b533;});}return _0x5a30d0;})['then'](respondWithResult(_0x116dd5,0xc9))[_0xc5fc('0x47')](handleError(_0x116dd5,null));};exports[_0xc5fc('0x54')]=function(_0x3834a0,_0x6d0793){if(_0x3834a0['body']['id']){delete _0x3834a0[_0xc5fc('0x4c')]['id'];}return db['AnalyticCustomReport']['find']({'where':{'id':_0x3834a0[_0xc5fc('0x48')]['id']}})[_0xc5fc('0x25')](handleEntityNotFound(_0x6d0793,null))[_0xc5fc('0x25')](saveUpdates(_0x3834a0[_0xc5fc('0x4c')],null))['then'](respondWithResult(_0x6d0793,null))['catch'](handleError(_0x6d0793,null));};exports[_0xc5fc('0x26')]=function(_0x3fa26c,_0x51df8d){return db[_0xc5fc('0x43')][_0xc5fc('0x4a')]({'where':{'id':_0x3fa26c['params']['id']}})[_0xc5fc('0x25')](handleEntityNotFound(_0x51df8d,null))[_0xc5fc('0x25')](removeEntity(_0x51df8d,null))['catch'](handleError(_0x51df8d,null));};exports[_0xc5fc('0x55')]=function(_0x4ba291,_0x567f0a){var _0x4a7891=0xa;var _0x3b3896=[_0xc5fc('0x56'),_0xc5fc('0x57'),_0xc5fc('0x58'),_0xc5fc('0x59')];var _0x11a127={'SUM':_0xc5fc('0x5a'),'COUNT':_0xc5fc('0x5b'),'COUNT DISTINCT':_0xc5fc('0x5c'),'MAX':_0xc5fc('0x5d'),'MIN':_0xc5fc('0x5e'),'AVG':_0xc5fc('0x5f'),'GROUP_CONCAT':'GROUP_CONCAT(%s)','GROUP_CONCAT ASC':'GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20ASC)','GROUP_CONCAT DESC':_0xc5fc('0x60')};var _0x5a8bb8={'SEC_TO_TIME':_0xc5fc('0x61'),'DATE':_0xc5fc('0x62'),'HOUR':'HOUR(%s)','ROUND':_0xc5fc('0x63'),'UNIX_TIMESTAMP':_0xc5fc('0x64')};var _0x53f654=function(_0x1500c5){return!_['includes']([_0xc5fc('0x65'),_0xc5fc('0x66'),_0xc5fc('0x67'),_0xc5fc('0x68')],_0x1500c5);};var _0x2f5fe7=function(_0x300ee6){if(!_0x300ee6||!_0x300ee6[_0xc5fc('0x69')][_0xc5fc('0x39')]){return'';}for(var _0x5e9903='(',_0x135e08=0x0;_0x135e08<_0x300ee6[_0xc5fc('0x69')]['length'];_0x135e08+=0x1){_0x135e08>0x0&&(_0x5e9903+='\x20'+_0x300ee6[_0xc5fc('0x6a')]+'\x20');_0x5e9903+=_0x300ee6[_0xc5fc('0x69')][_0x135e08][_0xc5fc('0x6b')]?_0x2f5fe7(_0x300ee6[_0xc5fc('0x69')][_0x135e08][_0xc5fc('0x6b')]):_0x300ee6['rules'][_0x135e08][_0xc5fc('0x6c')]+'\x20'+_0x300ee6[_0xc5fc('0x69')][_0x135e08]['condition']+(_0x53f654(_0x300ee6[_0xc5fc('0x69')][_0x135e08][_0xc5fc('0x6d')])?'\x20'+_0x300ee6['rules'][_0x135e08][_0xc5fc('0x6e')]:'');}return _0x5e9903+')';};var _0x1884c6={'where':{'id':_0x4ba291[_0xc5fc('0x48')]['id']}},_0x460d85,_0x2aeaed,_0x21404e,_0x38fa5c,_0x30a81f;_0x1884c6=_[_0xc5fc('0x40')]({},_0x1884c6,_0x4ba291[_0xc5fc('0x42')]);return db['AnalyticCustomReport'][_0xc5fc('0x4a')](_0x1884c6)[_0xc5fc('0x25')](function(_0x2544fa){if(_0x2544fa){_0x460d85=_0x2544fa;return _0x2544fa['getFields']();}return null;})[_0xc5fc('0x25')](function(_0x2c396b){if(_0x2c396b){if(!_0x2c396b[_0xc5fc('0x39')]){throw new db[(_0xc5fc('0x6f'))][(_0xc5fc('0x70'))]('Please\x20insert\x20at\x20least\x20one\x20field',0x193);}_0x2aeaed=_0x2c396b;return db[_0xc5fc('0x71')][_0xc5fc('0x45')]({'raw':!![]});}return null;})['then'](function(_0x498a1e){if(_0x498a1e){_0x21404e=_[_0xc5fc('0x72')](_0x498a1e,'id');var _0x2461ff;var _0x128afe=squel[_0xc5fc('0x73')]();_0x128afe[_0xc5fc('0x74')](_0x460d85[_0xc5fc('0x75')]);for(var _0x28dd79=0x0;_0x28dd79<_0x2aeaed[_0xc5fc('0x39')];_0x28dd79+=0x1){_0x2461ff=_0x2aeaed[_0x28dd79][_0xc5fc('0x76')]?_0x21404e[_0x2aeaed[_0x28dd79]['MetricId']][_0xc5fc('0x77')]:_0x2aeaed[_0x28dd79]['field'];_0x2461ff=_0x2aeaed[_0x28dd79][_0xc5fc('0x78')]?_['replace'](_0x11a127[_0x2aeaed[_0x28dd79][_0xc5fc('0x78')]],'%s',_0x2461ff):_0x2461ff;_0x2461ff=_0x2aeaed[_0x28dd79][_0xc5fc('0x79')]?_[_0xc5fc('0x7a')](_0x5a8bb8[_0x2aeaed[_0x28dd79]['format']],'%s',_0x2461ff):_0x2461ff;if(_0x2aeaed[_0x28dd79][_0xc5fc('0x7b')]){_0x128afe[_0xc5fc('0x6b')](_0x2461ff);}_0x2aeaed[_0x28dd79]['orderBy']&&_0x128afe[_0xc5fc('0x3c')](_0x2461ff,_0x2aeaed[_0x28dd79][_0xc5fc('0x7c')]===_0xc5fc('0x7d')?![]:!![]);_0x128afe[_0xc5fc('0x6c')](_0x2461ff,_0x2aeaed[_0x28dd79][_0xc5fc('0x7e')]||_0x2461ff);}if(_0x460d85[_0xc5fc('0x7f')]){_0x460d85['joins']=JSON['parse'](_0x460d85[_0xc5fc('0x7f')]);for(var _0x2bdc48=0x0;_0x2bdc48<_0x460d85[_0xc5fc('0x7f')]['length'];_0x2bdc48+=0x1){if(_0x460d85[_0xc5fc('0x7f')][_0x2bdc48]['type']&&_['includes'](_0x3b3896,_0x460d85[_0xc5fc('0x7f')][_0x2bdc48][_0xc5fc('0x30')])&&_0x460d85[_0xc5fc('0x7f')][_0x2bdc48][_0xc5fc('0x80')]&&_0x460d85[_0xc5fc('0x7f')][_0x2bdc48][_0xc5fc('0x81')]&&_0x460d85['joins'][_0x2bdc48][_0xc5fc('0x82')]){_0x128afe[_0x460d85['joins'][_0x2bdc48][_0xc5fc('0x30')]](_0x460d85[_0xc5fc('0x7f')][_0x2bdc48][_0xc5fc('0x81')],null,util[_0xc5fc('0x79')](_0xc5fc('0x83'),_0x460d85[_0xc5fc('0x75')],_0x460d85[_0xc5fc('0x7f')][_0x2bdc48][_0xc5fc('0x80')],_0x460d85['joins'][_0x2bdc48][_0xc5fc('0x81')],_0x460d85['joins'][_0x2bdc48][_0xc5fc('0x82')]));}}}if(_0x460d85[_0xc5fc('0x84')]){_0x460d85[_0xc5fc('0x84')]=JSON['parse'](_0x460d85[_0xc5fc('0x84')]);_0x128afe[_0xc5fc('0x3d')](_0x2f5fe7(_0x460d85[_0xc5fc('0x84')][_0xc5fc('0x6b')]));}_0x30a81f=_0x128afe[_0xc5fc('0x85')]();_0x128afe[_0xc5fc('0x21')](_0x4a7891);_0x128afe['offset'](0x0);logger[_0xc5fc('0x86')](_0xc5fc('0x87'),_0x128afe[_0xc5fc('0x88')]());return dbH[_0xc5fc('0x89')][_0xc5fc('0x33')](_0x128afe[_0xc5fc('0x88')](),{'type':dbH[_0xc5fc('0x89')][_0xc5fc('0x8a')]['SELECT']});}})[_0xc5fc('0x25')](respondWithResult(_0x567f0a,null))[_0xc5fc('0x47')](handleError(_0x567f0a,null));};function runReport(_0x1105b8,_0x4458a7,_0x3e8f0c){var _0x13976f=_0x4458a7[_0xc5fc('0x2b')];var _0x10b251=0xa;var _0x14d244=[_0xc5fc('0x56'),_0xc5fc('0x57'),'right_join',_0xc5fc('0x59')];var _0x24312f={'SUM':_0xc5fc('0x5a'),'COUNT':_0xc5fc('0x5b'),'COUNT DISTINCT':_0xc5fc('0x5c'),'MAX':_0xc5fc('0x5d'),'MIN':_0xc5fc('0x5e'),'AVG':_0xc5fc('0x5f'),'GROUP_CONCAT':_0xc5fc('0x8b'),'GROUP_CONCAT ASC':_0xc5fc('0x8c'),'GROUP_CONCAT DESC':_0xc5fc('0x60')};var _0x2311a8={'SEC_TO_TIME':_0xc5fc('0x61'),'DATE':_0xc5fc('0x62'),'HOUR':_0xc5fc('0x8d'),'ROUND':_0xc5fc('0x63'),'UNIX_TIMESTAMP':'UNIX_TIMESTAMP(%s)'};var _0x3a46df=function(_0x445fee){return!_[_0xc5fc('0x8e')]([_0xc5fc('0x65'),'IS\x20NOT\x20NULL','IS\x20EMPTY',_0xc5fc('0x68')],_0x445fee);};var _0x4c9f7d=function(_0x2f5e5c){if(!_0x2f5e5c||!_0x2f5e5c[_0xc5fc('0x69')][_0xc5fc('0x39')]){return'';}for(var _0x56fa49='(',_0x1d0a6b=0x0;_0x1d0a6b<_0x2f5e5c[_0xc5fc('0x69')][_0xc5fc('0x39')];_0x1d0a6b+=0x1){_0x1d0a6b>0x0&&(_0x56fa49+='\x20'+_0x2f5e5c['operator']+'\x20');_0x56fa49+=_0x2f5e5c[_0xc5fc('0x69')][_0x1d0a6b]['group']?_0x4c9f7d(_0x2f5e5c['rules'][_0x1d0a6b][_0xc5fc('0x6b')]):_0x2f5e5c[_0xc5fc('0x69')][_0x1d0a6b][_0xc5fc('0x6c')]+'\x20'+_0x2f5e5c[_0xc5fc('0x69')][_0x1d0a6b][_0xc5fc('0x6d')]+(_0x3a46df(_0x2f5e5c[_0xc5fc('0x69')][_0x1d0a6b][_0xc5fc('0x6d')])?'\x20'+_0x2f5e5c[_0xc5fc('0x69')][_0x1d0a6b][_0xc5fc('0x6e')]:'');}return _0x56fa49+')';};var _0x450b91={'where':{'id':_0x1105b8['id']}},_0x2204e5,_0xda0547,_0x279de0,_0x165808,_0x5ed3ef;_0x450b91=_['merge']({},_0x450b91,_0x3e8f0c);return db['AnalyticCustomReport'][_0xc5fc('0x4a')](_0x450b91)[_0xc5fc('0x25')](function(_0x2189ff){if(_0x2189ff){_0x2204e5=_0x2189ff;return _0x2189ff[_0xc5fc('0x8f')]();}return null;})[_0xc5fc('0x25')](function(_0xa74c4f){if(_0xa74c4f){if(!_0xa74c4f[_0xc5fc('0x39')]){throw new db['Sequelize']['ValidationErrorItem']('Please\x20insert\x20at\x20least\x20one\x20field',0x193);}_0xda0547=_0xa74c4f;return db[_0xc5fc('0x71')][_0xc5fc('0x45')]({'raw':!![]});}return null;})[_0xc5fc('0x25')](function(_0x1cca90){if(_0x1cca90){_0x279de0=_[_0xc5fc('0x72')](_0x1cca90,'id');var _0x48b7cd;var _0x497ef1=squel[_0xc5fc('0x73')]();_0x497ef1[_0xc5fc('0x74')](_0x2204e5[_0xc5fc('0x75')]);for(var _0x4aa7de=0x0;_0x4aa7de<_0xda0547['length'];_0x4aa7de+=0x1){_0x48b7cd=_0xda0547[_0x4aa7de]['MetricId']?_0x279de0[_0xda0547[_0x4aa7de]['MetricId']][_0xc5fc('0x77')]:_0xda0547[_0x4aa7de][_0xc5fc('0x6c')];_0x48b7cd=_0xda0547[_0x4aa7de][_0xc5fc('0x78')]?_['replace'](_0x24312f[_0xda0547[_0x4aa7de][_0xc5fc('0x78')]],'%s',_0x48b7cd):_0x48b7cd;_0x48b7cd=_0xda0547[_0x4aa7de]['format']?_[_0xc5fc('0x7a')](_0x2311a8[_0xda0547[_0x4aa7de][_0xc5fc('0x79')]],'%s',_0x48b7cd):_0x48b7cd;if(_0xda0547[_0x4aa7de]['groupBy']){_0x497ef1[_0xc5fc('0x6b')](_0x48b7cd);}_0xda0547[_0x4aa7de]['orderBy']&&_0x497ef1['order'](_0x48b7cd,_0xda0547[_0x4aa7de][_0xc5fc('0x7c')]===_0xc5fc('0x7d')?![]:!![]);_0x497ef1[_0xc5fc('0x6c')](_0x48b7cd,_0xda0547[_0x4aa7de][_0xc5fc('0x7e')]||_0x48b7cd);}if(_0x2204e5[_0xc5fc('0x7f')]){_0x2204e5[_0xc5fc('0x7f')]=JSON[_0xc5fc('0x90')](_0x2204e5[_0xc5fc('0x7f')]);for(var _0x40ccbf=0x0;_0x40ccbf<_0x2204e5[_0xc5fc('0x7f')][_0xc5fc('0x39')];_0x40ccbf+=0x1){if(_0x2204e5[_0xc5fc('0x7f')][_0x40ccbf]['type']&&_[_0xc5fc('0x8e')](_0x14d244,_0x2204e5[_0xc5fc('0x7f')][_0x40ccbf]['type'])&&_0x2204e5[_0xc5fc('0x7f')][_0x40ccbf]['parentKey']&&_0x2204e5[_0xc5fc('0x7f')][_0x40ccbf][_0xc5fc('0x81')]&&_0x2204e5['joins'][_0x40ccbf]['foreignKey']){_0x497ef1[_0x2204e5[_0xc5fc('0x7f')][_0x40ccbf][_0xc5fc('0x30')]](_0x2204e5[_0xc5fc('0x7f')][_0x40ccbf]['foreignTable'],null,util[_0xc5fc('0x79')](_0xc5fc('0x83'),_0x2204e5[_0xc5fc('0x75')],_0x2204e5['joins'][_0x40ccbf][_0xc5fc('0x80')],_0x2204e5[_0xc5fc('0x7f')][_0x40ccbf][_0xc5fc('0x81')],_0x2204e5['joins'][_0x40ccbf]['foreignKey']));}}}if(_0x2204e5[_0xc5fc('0x84')]){_0x2204e5[_0xc5fc('0x84')]=JSON['parse'](_0x2204e5[_0xc5fc('0x84')]);_0x497ef1['where'](_0x4c9f7d(_0x2204e5[_0xc5fc('0x84')]['group']));}_0x5ed3ef=_0x497ef1['clone']();_0x497ef1[_0xc5fc('0x21')](_0x10b251);_0x497ef1[_0xc5fc('0x1f')](0x0);return;}})[_0xc5fc('0x25')](function(){if(_0x4458a7['output']==='web'){return null;}var _0x52fa6b=_[_0xc5fc('0x40')](_0x4458a7,{'name':_0x4458a7['fullPath']||_0x4458a7['name']||_0x2204e5[_0xc5fc('0x2b')],'basename':_0x13976f,'savename':util['format'](_0xc5fc('0x91'),_0x4458a7['name']||_0x2204e5[_0xc5fc('0x2b')],require(_0xc5fc('0x92'))['generate'](0x4),_0x4458a7[_0xc5fc('0x93')]||_0xc5fc('0x94')),'reportId':_0x2204e5['id'],'reportType':_0xc5fc('0x95')});return db[_0xc5fc('0x96')][_0xc5fc('0x4b')](_0x52fa6b,{'raw':!![]});})[_0xc5fc('0x25')](function(_0x1975b7){if(_0x2204e5[_0xc5fc('0x75')]===_0xc5fc('0x97')){_0x5ed3ef[_0xc5fc('0x3d')](_0x2204e5[_0xc5fc('0x75')]+'.calldate\x20BETWEEN\x20?\x20AND\x20?',_0x4458a7[_0xc5fc('0x98')],_0x4458a7[_0xc5fc('0x99')]);}else{_0x5ed3ef[_0xc5fc('0x3d')](_0x2204e5['table']+_0xc5fc('0x9a'),_0x4458a7[_0xc5fc('0x98')],_0x4458a7[_0xc5fc('0x99')]);}if(_0x1975b7){logger[_0xc5fc('0x9b')](_0xc5fc('0x9c'),_0x5ed3ef[_0xc5fc('0x88')]());require(_0xc5fc('0x9d'))[_0xc5fc('0x9e')](path[_0xc5fc('0x56')](__dirname,_0xc5fc('0x9f'),_0x1975b7['output']),[_0x1975b7['id'],_0x5ed3ef[_0xc5fc('0x88')](),path[_0xc5fc('0x56')](__dirname,_0xc5fc('0xa0'),_0x1975b7[_0xc5fc('0xa1')]),_0x13976f]);return _0x1975b7;}else{var _0x1971b5=squel[_0xc5fc('0x73')]();_0x1971b5[_0xc5fc('0x74')]('('+_0x5ed3ef[_0xc5fc('0x88')]()+')',_0xc5fc('0xa2'));_0x1971b5['field'](_0xc5fc('0xa3'),_0xc5fc('0x22'));_0x5ed3ef['limit'](_0x4458a7['limit']||_0x10b251);_0x5ed3ef['offset'](_0x4458a7['offset']||0x0);return dbH[_0xc5fc('0x89')][_0xc5fc('0x33')](_0x1971b5[_0xc5fc('0x88')](),{'type':dbH[_0xc5fc('0x89')][_0xc5fc('0x8a')][_0xc5fc('0xa4')]})[_0xc5fc('0x25')](function(_0x1094ab){if(!_0x1094ab['length']){_0x165808=0x0;}else{_0x165808=_0x1094ab[0x0][_0xc5fc('0x22')]||0x0;}return dbH['sequelize'][_0xc5fc('0x33')](_0x5ed3ef[_0xc5fc('0x88')](),{'type':dbH[_0xc5fc('0x89')][_0xc5fc('0x8a')][_0xc5fc('0xa4')]});})[_0xc5fc('0x25')](function(_0x4add6b){return{'rows':_0x4add6b||[],'count':_0x165808};});}});}exports[_0xc5fc('0xa5')]=function(_0x3e350b,_0x38f4f0){return runReport(_0x3e350b[_0xc5fc('0x48')],_0x3e350b[_0xc5fc('0x33')],_0x3e350b[_0xc5fc('0x42')])[_0xc5fc('0x25')](respondWithResult(_0x38f4f0,null))['catch'](handleError(_0x38f4f0,null));};exports[_0xc5fc('0xa6')]=runReport;exports['query']=function(_0x309bfc,_0x5b05b4){var _0x32475d=0xa;var _0x26a667=[_0xc5fc('0x56'),_0xc5fc('0x57'),'right_join',_0xc5fc('0x59')];var _0x22e4d2={'SUM':_0xc5fc('0x5a'),'COUNT':_0xc5fc('0x5b'),'COUNT DISTINCT':_0xc5fc('0x5c'),'MAX':'MAX(%s)','MIN':_0xc5fc('0x5e'),'AVG':_0xc5fc('0x5f'),'GROUP_CONCAT':_0xc5fc('0x8b'),'GROUP_CONCAT ASC':_0xc5fc('0x8c'),'GROUP_CONCAT DESC':_0xc5fc('0x60')};var _0x581567={'SEC_TO_TIME':_0xc5fc('0x61'),'DATE':_0xc5fc('0x62'),'HOUR':_0xc5fc('0x8d'),'ROUND':_0xc5fc('0x63'),'UNIX_TIMESTAMP':_0xc5fc('0x64')};var _0x5cae48=function(_0x2796b2){return!_[_0xc5fc('0x8e')]([_0xc5fc('0x65'),'IS\x20NOT\x20NULL',_0xc5fc('0x67'),_0xc5fc('0x68')],_0x2796b2);};var _0x5f3781=function(_0x57cd02){if(!_0x57cd02||!_0x57cd02[_0xc5fc('0x69')]['length']){return'';}for(var _0x1682e0='(',_0x10045c=0x0;_0x10045c<_0x57cd02['rules']['length'];_0x10045c+=0x1){_0x10045c>0x0&&(_0x1682e0+='\x20'+_0x57cd02['operator']+'\x20');_0x1682e0+=_0x57cd02[_0xc5fc('0x69')][_0x10045c][_0xc5fc('0x6b')]?_0x5f3781(_0x57cd02[_0xc5fc('0x69')][_0x10045c][_0xc5fc('0x6b')]):_0x57cd02['rules'][_0x10045c][_0xc5fc('0x6c')]+'\x20'+_0x57cd02[_0xc5fc('0x69')][_0x10045c][_0xc5fc('0x6d')]+(_0x5cae48(_0x57cd02[_0xc5fc('0x69')][_0x10045c][_0xc5fc('0x6d')])?'\x20'+_0x57cd02['rules'][_0x10045c][_0xc5fc('0x6e')]:'');}return _0x1682e0+')';};var _0x1a88b8={'where':{'id':_0x309bfc[_0xc5fc('0x48')]['id']}},_0x26996d,_0x1a1f81,_0x2f094d,_0x267c1b,_0x43dc1e;_0x1a88b8=_['merge']({},_0x1a88b8,_0x309bfc['options']);return db['AnalyticCustomReport']['find'](_0x1a88b8)[_0xc5fc('0x25')](function(_0x402dbd){if(_0x402dbd){_0x26996d=_0x402dbd;return _0x402dbd['getFields']();}return null;})['then'](function(_0x2f3942){if(_0x2f3942){if(!_0x2f3942[_0xc5fc('0x39')]){throw new db[(_0xc5fc('0x6f'))][(_0xc5fc('0x70'))](_0xc5fc('0xa7'),0x193);}_0x1a1f81=_0x2f3942;return db[_0xc5fc('0x71')][_0xc5fc('0x45')]({'raw':!![]});}return null;})[_0xc5fc('0x25')](function(_0x39580c){if(_0x39580c){_0x2f094d=_['keyBy'](_0x39580c,'id');var _0x40230e;var _0x805882=squel[_0xc5fc('0x73')]();_0x805882[_0xc5fc('0x74')](_0x26996d['table']);for(var _0x4f87f5=0x0;_0x4f87f5<_0x1a1f81['length'];_0x4f87f5+=0x1){_0x40230e=_0x1a1f81[_0x4f87f5][_0xc5fc('0x76')]?_0x2f094d[_0x1a1f81[_0x4f87f5][_0xc5fc('0x76')]][_0xc5fc('0x77')]:_0x1a1f81[_0x4f87f5][_0xc5fc('0x6c')];_0x40230e=_0x1a1f81[_0x4f87f5][_0xc5fc('0x78')]?_[_0xc5fc('0x7a')](_0x22e4d2[_0x1a1f81[_0x4f87f5][_0xc5fc('0x78')]],'%s',_0x40230e):_0x40230e;_0x40230e=_0x1a1f81[_0x4f87f5]['format']?_[_0xc5fc('0x7a')](_0x581567[_0x1a1f81[_0x4f87f5]['format']],'%s',_0x40230e):_0x40230e;if(_0x1a1f81[_0x4f87f5][_0xc5fc('0x7b')]){_0x805882[_0xc5fc('0x6b')](_0x40230e);}_0x1a1f81[_0x4f87f5][_0xc5fc('0x7c')]&&_0x805882[_0xc5fc('0x3c')](_0x40230e,_0x1a1f81[_0x4f87f5][_0xc5fc('0x7c')]==='DESC'?![]:!![]);_0x805882['field'](_0x40230e,_0x1a1f81[_0x4f87f5][_0xc5fc('0x7e')]||_0x40230e);}if(_0x26996d[_0xc5fc('0x7f')]){_0x26996d['joins']=JSON[_0xc5fc('0x90')](_0x26996d[_0xc5fc('0x7f')]);for(var _0x407708=0x0;_0x407708<_0x26996d['joins'][_0xc5fc('0x39')];_0x407708+=0x1){if(_0x26996d[_0xc5fc('0x7f')][_0x407708][_0xc5fc('0x30')]&&_['includes'](_0x26a667,_0x26996d[_0xc5fc('0x7f')][_0x407708][_0xc5fc('0x30')])&&_0x26996d[_0xc5fc('0x7f')][_0x407708]['parentKey']&&_0x26996d[_0xc5fc('0x7f')][_0x407708][_0xc5fc('0x81')]&&_0x26996d['joins'][_0x407708][_0xc5fc('0x82')]){_0x805882[_0x26996d[_0xc5fc('0x7f')][_0x407708]['type']](_0x26996d['joins'][_0x407708][_0xc5fc('0x81')],null,util[_0xc5fc('0x79')](_0xc5fc('0x83'),_0x26996d['table'],_0x26996d[_0xc5fc('0x7f')][_0x407708][_0xc5fc('0x80')],_0x26996d['joins'][_0x407708]['foreignTable'],_0x26996d[_0xc5fc('0x7f')][_0x407708][_0xc5fc('0x82')]));}}}if(_0x26996d[_0xc5fc('0x84')]){_0x26996d['conditions']=JSON[_0xc5fc('0x90')](_0x26996d[_0xc5fc('0x84')]);_0x805882[_0xc5fc('0x3d')](_0x5f3781(_0x26996d['conditions'][_0xc5fc('0x6b')]));}_0x43dc1e=_0x805882[_0xc5fc('0x85')]();_0x805882[_0xc5fc('0x21')](_0x32475d);_0x805882['offset'](0x0);logger[_0xc5fc('0x86')]('Limited\x20Query:',_0x805882[_0xc5fc('0x88')]());return dbH[_0xc5fc('0x89')][_0xc5fc('0x33')](_0x805882[_0xc5fc('0x88')](),{'type':dbH[_0xc5fc('0x89')]['QueryTypes'][_0xc5fc('0xa4')]});}})[_0xc5fc('0x25')](function(){return{'sql':_0x43dc1e[_0xc5fc('0x88')]()};})[_0xc5fc('0x25')](respondWithResult(_0x5b05b4,null))[_0xc5fc('0x47')](handleError(_0x5b05b4,null));};