581e1aaecfa80d4cb34ce1a19fd6fb0194984d96
[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 _0x0f49=['status','end','json','count','offset','limit','set','Content-Range','apply','save','update','then','destroy','get','CustomReports','UserProfileResource','sendStatus','error','stack','name','send','index','map','rawAttributes','fieldName','type','key','keys','query','intersection','model','attributes','fields','length','hasOwnProperty','nolimit','order','sort','filters','filter','where','merge','options','AnalyticCustomReport','includeAll','findAll','rows','catch','show','params','include','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','find','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','join','cross_join','COUNT(%s)','COUNT(DISTINCT\x20%s)','MAX(%s)','GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20DESC)','SEC_TO_TIME(%s)','DATE(%s)','ROUND(%s)','UNIX_TIMESTAMP(%s)','includes','IS\x20NULL','IS\x20NOT\x20NULL','IS\x20NOT\x20EMPTY','rules','group','field','condition','value','getFields','ValidationErrorItem','AnalyticMetric','keyBy','from','table','MetricId','metric','function','replace','format','groupBy','orderBy','DESC','alias','joins','parse','foreignTable','parentKey','foreignKey','conditions','clone','debug','Limited\x20Query:','toString','sequelize','QueryTypes','SELECT','left_join','right_join','SUM(%s)','AVG(%s)','GROUP_CONCAT(%s)','GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20ASC)','HOUR(%s)','operator','Sequelize','Please\x20insert\x20at\x20least\x20one\x20field','select','output','web','randomstring','csv','custom','.calldate\x20BETWEEN\x20?\x20AND\x20?','startDate','.createdAt\x20BETWEEN\x20?\x20AND\x20?','endDate','info','Run\x20Query:','child_process','fork','../../components/export/','../../files/reports','savename','countTable','COUNT(*)','run','runReport','MIN(%s)','IS\x20EMPTY','%s.%s\x20=\x20%s.%s','eml-format','rimraf','zip-dir','fast-json-patch','bluebird','mustache','util','path','sox','to-csv','ejs','lodash','squel','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb'];(function(_0x387583,_0x127b08){var _0x45d2f9=function(_0x1d82ac){while(--_0x1d82ac){_0x387583['push'](_0x387583['shift']());}};_0x45d2f9(++_0x127b08);}(_0x0f49,0x8e));var _0x90f4=function(_0x5f52a4,_0x5706d9){_0x5f52a4=_0x5f52a4-0x0;var _0xde1bce=_0x0f49[_0x5f52a4];return _0xde1bce;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0x90f4('0x0'));var rimraf=require(_0x90f4('0x1'));var zipdir=require(_0x90f4('0x2'));var jsonpatch=require(_0x90f4('0x3'));var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0x90f4('0x4'));var Mustache=require(_0x90f4('0x5'));var util=require(_0x90f4('0x6'));var path=require(_0x90f4('0x7'));var sox=require(_0x90f4('0x8'));var csv=require(_0x90f4('0x9'));var ejs=require(_0x90f4('0xa'));var fs=require('fs');var _=require(_0x90f4('0xb'));var squel=require(_0x90f4('0xc'));var crypto=require('crypto');var jsforce=require(_0x90f4('0xd'));var deskjs=require(_0x90f4('0xe'));var toCsv=require(_0x90f4('0x9'));var querystring=require(_0x90f4('0xf'));var Papa=require('papaparse');var Redis=require(_0x90f4('0x10'));var authService=require(_0x90f4('0x11'));var qs=require(_0x90f4('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x90f4('0x13'));var logger=require(_0x90f4('0x14'))(_0x90f4('0x15'));var utils=require(_0x90f4('0x16'));var config=require(_0x90f4('0x17'));var licenseUtil=require(_0x90f4('0x18'));var db=require('../../mysqldb')['db'];var dbH=require(_0x90f4('0x19'))['dbH'];function respondWithStatusCode(_0x4f9b64,_0xa947f9){_0xa947f9=_0xa947f9||0xcc;return function(_0xf6dd74){if(_0xf6dd74){return _0x4f9b64['sendStatus'](_0xa947f9);}return _0x4f9b64[_0x90f4('0x1a')](_0xa947f9)[_0x90f4('0x1b')]();};}function respondWithResult(_0x28ca17,_0x34759a){_0x34759a=_0x34759a||0xc8;return function(_0x28c6ad){if(_0x28c6ad){return _0x28ca17[_0x90f4('0x1a')](_0x34759a)[_0x90f4('0x1c')](_0x28c6ad);}};}function respondWithFilteredResult(_0x40dd0d,_0x5dc24e){return function(_0x5d0792){if(_0x5d0792){var _0x9b1067=_0x5d0792[_0x90f4('0x1d')],_0x5e5c6d=_0x5dc24e[_0x90f4('0x1e')],_0x187dd1=_0x5dc24e['offset']+_0x5dc24e[_0x90f4('0x1f')],_0x501b6c;if(_0x187dd1>=_0x9b1067){_0x187dd1=_0x9b1067;_0x501b6c=0xc8;}else{_0x501b6c=0xce;}_0x40dd0d[_0x90f4('0x1a')](_0x501b6c);return _0x40dd0d[_0x90f4('0x20')](_0x90f4('0x21'),_0x5e5c6d+'-'+_0x187dd1+'/'+_0x9b1067)[_0x90f4('0x1c')](_0x5d0792);}return null;};}function patchUpdates(_0x11b730){return function(_0x45ea28){try{jsonpatch[_0x90f4('0x22')](_0x45ea28,_0x11b730,!![]);}catch(_0x376822){return BPromise['reject'](_0x376822);}return _0x45ea28[_0x90f4('0x23')]();};}function saveUpdates(_0x2e16cb,_0x49d9bc){return function(_0x298d80){if(_0x298d80){return _0x298d80[_0x90f4('0x24')](_0x2e16cb)[_0x90f4('0x25')](function(_0x4ea43b){return _0x4ea43b;});}return null;};}function removeEntity(_0x5ae0b2,_0x52c3ad){return function(_0x3d2d4b){if(_0x3d2d4b){return _0x3d2d4b[_0x90f4('0x26')]()[_0x90f4('0x25')](function(){var _0x574935=_0x3d2d4b[_0x90f4('0x27')]({'plain':!![]});var _0x454e28=_0x90f4('0x28');return db[_0x90f4('0x29')]['destroy']({'where':{'type':_0x454e28,'resourceId':_0x574935['id']}})[_0x90f4('0x25')](function(){return _0x3d2d4b;});})[_0x90f4('0x25')](function(){_0x5ae0b2[_0x90f4('0x1a')](0xcc)[_0x90f4('0x1b')]();});}};}function handleEntityNotFound(_0x4a0630,_0x1fa720){return function(_0x175cd6){if(!_0x175cd6){_0x4a0630[_0x90f4('0x2a')](0x194);}return _0x175cd6;};}function handleError(_0x443419,_0xbd26f2){_0xbd26f2=_0xbd26f2||0x1f4;return function(_0x31655d){logger[_0x90f4('0x2b')](_0x31655d[_0x90f4('0x2c')]);if(_0x31655d[_0x90f4('0x2d')]){delete _0x31655d[_0x90f4('0x2d')];}_0x443419[_0x90f4('0x1a')](_0xbd26f2)[_0x90f4('0x2e')](_0x31655d);};}exports[_0x90f4('0x2f')]=function(_0x3aa915,_0xac8cd7){var _0x1809d1={},_0x332ee2={},_0x1009ca={'count':0x0,'rows':[]};var _0x4d8b30=_[_0x90f4('0x30')](db['AnalyticCustomReport'][_0x90f4('0x31')],function(_0x2ebdfe){return{'name':_0x2ebdfe[_0x90f4('0x32')],'type':_0x2ebdfe[_0x90f4('0x33')][_0x90f4('0x34')]};});_0x332ee2['model']=_[_0x90f4('0x30')](_0x4d8b30,'name');_0x332ee2['query']=_[_0x90f4('0x35')](_0x3aa915[_0x90f4('0x36')]);_0x332ee2['filters']=_[_0x90f4('0x37')](_0x332ee2[_0x90f4('0x38')],_0x332ee2[_0x90f4('0x36')]);_0x1809d1[_0x90f4('0x39')]=_['intersection'](_0x332ee2[_0x90f4('0x38')],qs[_0x90f4('0x3a')](_0x3aa915[_0x90f4('0x36')][_0x90f4('0x3a')]));_0x1809d1['attributes']=_0x1809d1[_0x90f4('0x39')][_0x90f4('0x3b')]?_0x1809d1['attributes']:_0x332ee2['model'];if(!_0x3aa915[_0x90f4('0x36')][_0x90f4('0x3c')](_0x90f4('0x3d'))){_0x1809d1[_0x90f4('0x1f')]=qs[_0x90f4('0x1f')](_0x3aa915[_0x90f4('0x36')][_0x90f4('0x1f')]);_0x1809d1[_0x90f4('0x1e')]=qs[_0x90f4('0x1e')](_0x3aa915['query'][_0x90f4('0x1e')]);}_0x1809d1[_0x90f4('0x3e')]=qs[_0x90f4('0x3f')](_0x3aa915[_0x90f4('0x36')][_0x90f4('0x3f')]);_0x1809d1['where']=qs[_0x90f4('0x40')](_['pick'](_0x3aa915[_0x90f4('0x36')],_0x332ee2['filters']),_0x4d8b30);if(_0x3aa915[_0x90f4('0x36')][_0x90f4('0x41')]){_0x1809d1[_0x90f4('0x42')]=_[_0x90f4('0x43')](_0x1809d1['where'],{'$or':_[_0x90f4('0x30')](_0x4d8b30,function(_0x3011b6){if(_0x3011b6[_0x90f4('0x33')]!=='VIRTUAL'){var _0x244ba4={};_0x244ba4[_0x3011b6[_0x90f4('0x2d')]]={'$like':'%'+_0x3aa915[_0x90f4('0x36')]['filter']+'%'};return _0x244ba4;}})});}_0x1809d1=_[_0x90f4('0x43')]({},_0x1809d1,_0x3aa915[_0x90f4('0x44')]);var _0x3d0411={'where':_0x1809d1[_0x90f4('0x42')]};return db[_0x90f4('0x45')][_0x90f4('0x1d')](_0x3d0411)['then'](function(_0x430a30){_0x1009ca['count']=_0x430a30;if(_0x3aa915[_0x90f4('0x36')][_0x90f4('0x46')]){_0x1809d1['include']=[{'all':!![]}];}return db['AnalyticCustomReport'][_0x90f4('0x47')](_0x1809d1);})[_0x90f4('0x25')](function(_0xc847b){_0x1009ca[_0x90f4('0x48')]=_0xc847b;return _0x1009ca;})[_0x90f4('0x25')](respondWithFilteredResult(_0xac8cd7,_0x1809d1))[_0x90f4('0x49')](handleError(_0xac8cd7,null));};exports[_0x90f4('0x4a')]=function(_0x21f743,_0xcb26c6){var _0x19575f={'raw':![],'where':{'id':_0x21f743[_0x90f4('0x4b')]['id']}},_0x2eb786={};_0x2eb786[_0x90f4('0x38')]=_[_0x90f4('0x35')](db[_0x90f4('0x45')][_0x90f4('0x31')]);_0x2eb786[_0x90f4('0x36')]=_[_0x90f4('0x35')](_0x21f743['query']);_0x2eb786[_0x90f4('0x40')]=_[_0x90f4('0x37')](_0x2eb786[_0x90f4('0x38')],_0x2eb786[_0x90f4('0x36')]);_0x19575f[_0x90f4('0x39')]=_['intersection'](_0x2eb786[_0x90f4('0x38')],qs[_0x90f4('0x3a')](_0x21f743[_0x90f4('0x36')][_0x90f4('0x3a')]));_0x19575f[_0x90f4('0x39')]=_0x19575f[_0x90f4('0x39')][_0x90f4('0x3b')]?_0x19575f[_0x90f4('0x39')]:_0x2eb786[_0x90f4('0x38')];if(_0x21f743[_0x90f4('0x36')][_0x90f4('0x46')]){_0x19575f[_0x90f4('0x4c')]=[{'all':!![]}];}_0x19575f=_[_0x90f4('0x43')]({},_0x19575f,_0x21f743[_0x90f4('0x44')]);return db['AnalyticCustomReport']['find'](_0x19575f)[_0x90f4('0x25')](handleEntityNotFound(_0xcb26c6,null))[_0x90f4('0x25')](respondWithResult(_0xcb26c6,null))[_0x90f4('0x49')](handleError(_0xcb26c6,null));};exports[_0x90f4('0x4d')]=function(_0x1fae6d,_0x55e420){return db[_0x90f4('0x45')][_0x90f4('0x4d')](_0x1fae6d[_0x90f4('0x4e')],{})[_0x90f4('0x25')](function(_0x45a404){var _0x1ebdb9=_0x1fae6d[_0x90f4('0x4f')][_0x90f4('0x27')]({'plain':!![]});if(!_0x1ebdb9)throw new Error(_0x90f4('0x50'));if(_0x1ebdb9[_0x90f4('0x51')]===_0x90f4('0x4f')){var _0x50227d=_0x45a404[_0x90f4('0x27')]({'plain':!![]});var _0x287630='Reports';return db[_0x90f4('0x52')][_0x90f4('0x53')]({'where':{'name':_0x287630,'userProfileId':_0x1ebdb9[_0x90f4('0x54')]},'raw':!![]})[_0x90f4('0x25')](function(_0x152836){if(_0x152836&&_0x152836[_0x90f4('0x55')]===0x0){return db['UserProfileResource']['create']({'name':_0x50227d['name'],'resourceId':_0x50227d['id'],'type':_0x90f4('0x28'),'sectionId':_0x152836['id']},{})['then'](function(){return _0x45a404;});}else{return _0x45a404;}})[_0x90f4('0x49')](function(_0xaf0190){logger[_0x90f4('0x2b')](_0x90f4('0x56'),_0xaf0190);throw _0xaf0190;});}return _0x45a404;})[_0x90f4('0x25')](respondWithResult(_0x55e420,0xc9))[_0x90f4('0x49')](handleError(_0x55e420,null));};exports[_0x90f4('0x24')]=function(_0x4378a2,_0x4d0970){if(_0x4378a2[_0x90f4('0x4e')]['id']){delete _0x4378a2[_0x90f4('0x4e')]['id'];}return db['AnalyticCustomReport'][_0x90f4('0x53')]({'where':{'id':_0x4378a2[_0x90f4('0x4b')]['id']}})[_0x90f4('0x25')](handleEntityNotFound(_0x4d0970,null))['then'](saveUpdates(_0x4378a2['body'],null))[_0x90f4('0x25')](respondWithResult(_0x4d0970,null))[_0x90f4('0x49')](handleError(_0x4d0970,null));};exports[_0x90f4('0x26')]=function(_0x2cc16a,_0x230346){return db[_0x90f4('0x45')][_0x90f4('0x53')]({'where':{'id':_0x2cc16a[_0x90f4('0x4b')]['id']}})[_0x90f4('0x25')](handleEntityNotFound(_0x230346,null))[_0x90f4('0x25')](removeEntity(_0x230346,null))[_0x90f4('0x49')](handleError(_0x230346,null));};exports['preview']=function(_0x226573,_0x722433){var _0x552249=0xa;var _0x1a0ce6=[_0x90f4('0x57'),'left_join','right_join',_0x90f4('0x58')];var _0x179b0b={'SUM':'SUM(%s)','COUNT':_0x90f4('0x59'),'COUNT DISTINCT':_0x90f4('0x5a'),'MAX':_0x90f4('0x5b'),'MIN':'MIN(%s)','AVG':'AVG(%s)','GROUP_CONCAT':'GROUP_CONCAT(%s)','GROUP_CONCAT ASC':'GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20ASC)','GROUP_CONCAT DESC':_0x90f4('0x5c')};var _0x238b10={'SEC_TO_TIME':_0x90f4('0x5d'),'DATE':_0x90f4('0x5e'),'HOUR':'HOUR(%s)','ROUND':_0x90f4('0x5f'),'UNIX_TIMESTAMP':_0x90f4('0x60')};var _0x46a5bb=function(_0x431bc2){return!_[_0x90f4('0x61')]([_0x90f4('0x62'),_0x90f4('0x63'),'IS\x20EMPTY',_0x90f4('0x64')],_0x431bc2);};var _0x2106d7=function(_0x167949){if(!_0x167949||!_0x167949[_0x90f4('0x65')][_0x90f4('0x3b')]){return'';}for(var _0x3e4a92='(',_0x3a009b=0x0;_0x3a009b<_0x167949[_0x90f4('0x65')]['length'];_0x3a009b+=0x1){_0x3a009b>0x0&&(_0x3e4a92+='\x20'+_0x167949['operator']+'\x20');_0x3e4a92+=_0x167949[_0x90f4('0x65')][_0x3a009b][_0x90f4('0x66')]?_0x2106d7(_0x167949[_0x90f4('0x65')][_0x3a009b]['group']):_0x167949[_0x90f4('0x65')][_0x3a009b][_0x90f4('0x67')]+'\x20'+_0x167949[_0x90f4('0x65')][_0x3a009b][_0x90f4('0x68')]+(_0x46a5bb(_0x167949['rules'][_0x3a009b][_0x90f4('0x68')])?'\x20'+_0x167949[_0x90f4('0x65')][_0x3a009b][_0x90f4('0x69')]:'');}return _0x3e4a92+')';};var _0x564b1b={'where':{'id':_0x226573[_0x90f4('0x4b')]['id']}},_0x484d83,_0xe2c6ed,_0x3139b5,_0x4adf63,_0x1551b5;_0x564b1b=_['merge']({},_0x564b1b,_0x226573[_0x90f4('0x44')]);return db[_0x90f4('0x45')][_0x90f4('0x53')](_0x564b1b)[_0x90f4('0x25')](function(_0x852769){if(_0x852769){_0x484d83=_0x852769;return _0x852769[_0x90f4('0x6a')]();}return null;})[_0x90f4('0x25')](function(_0x406bff){if(_0x406bff){if(!_0x406bff[_0x90f4('0x3b')]){throw new db['Sequelize'][(_0x90f4('0x6b'))]('Please\x20insert\x20at\x20least\x20one\x20field',0x193);}_0xe2c6ed=_0x406bff;return db[_0x90f4('0x6c')]['findAll']({'raw':!![]});}return null;})[_0x90f4('0x25')](function(_0x590448){if(_0x590448){_0x3139b5=_[_0x90f4('0x6d')](_0x590448,'id');var _0x4da05c;var _0x553e05=squel['select']();_0x553e05[_0x90f4('0x6e')](_0x484d83[_0x90f4('0x6f')]);for(var _0x2ee9cc=0x0;_0x2ee9cc<_0xe2c6ed[_0x90f4('0x3b')];_0x2ee9cc+=0x1){_0x4da05c=_0xe2c6ed[_0x2ee9cc][_0x90f4('0x70')]?_0x3139b5[_0xe2c6ed[_0x2ee9cc][_0x90f4('0x70')]][_0x90f4('0x71')]:_0xe2c6ed[_0x2ee9cc]['field'];_0x4da05c=_0xe2c6ed[_0x2ee9cc][_0x90f4('0x72')]?_[_0x90f4('0x73')](_0x179b0b[_0xe2c6ed[_0x2ee9cc][_0x90f4('0x72')]],'%s',_0x4da05c):_0x4da05c;_0x4da05c=_0xe2c6ed[_0x2ee9cc][_0x90f4('0x74')]?_[_0x90f4('0x73')](_0x238b10[_0xe2c6ed[_0x2ee9cc][_0x90f4('0x74')]],'%s',_0x4da05c):_0x4da05c;if(_0xe2c6ed[_0x2ee9cc][_0x90f4('0x75')]){_0x553e05[_0x90f4('0x66')](_0x4da05c);}_0xe2c6ed[_0x2ee9cc][_0x90f4('0x76')]&&_0x553e05[_0x90f4('0x3e')](_0x4da05c,_0xe2c6ed[_0x2ee9cc][_0x90f4('0x76')]===_0x90f4('0x77')?![]:!![]);_0x553e05[_0x90f4('0x67')](_0x4da05c,_0xe2c6ed[_0x2ee9cc][_0x90f4('0x78')]||_0x4da05c);}if(_0x484d83[_0x90f4('0x79')]){_0x484d83[_0x90f4('0x79')]=JSON[_0x90f4('0x7a')](_0x484d83[_0x90f4('0x79')]);for(var _0xb02667=0x0;_0xb02667<_0x484d83[_0x90f4('0x79')][_0x90f4('0x3b')];_0xb02667+=0x1){if(_0x484d83[_0x90f4('0x79')][_0xb02667][_0x90f4('0x33')]&&_[_0x90f4('0x61')](_0x1a0ce6,_0x484d83['joins'][_0xb02667][_0x90f4('0x33')])&&_0x484d83[_0x90f4('0x79')][_0xb02667]['parentKey']&&_0x484d83[_0x90f4('0x79')][_0xb02667][_0x90f4('0x7b')]&&_0x484d83[_0x90f4('0x79')][_0xb02667]['foreignKey']){_0x553e05[_0x484d83[_0x90f4('0x79')][_0xb02667][_0x90f4('0x33')]](_0x484d83[_0x90f4('0x79')][_0xb02667][_0x90f4('0x7b')],null,util[_0x90f4('0x74')]('%s.%s\x20=\x20%s.%s',_0x484d83[_0x90f4('0x6f')],_0x484d83[_0x90f4('0x79')][_0xb02667][_0x90f4('0x7c')],_0x484d83[_0x90f4('0x79')][_0xb02667][_0x90f4('0x7b')],_0x484d83[_0x90f4('0x79')][_0xb02667][_0x90f4('0x7d')]));}}}if(_0x484d83[_0x90f4('0x7e')]){_0x484d83[_0x90f4('0x7e')]=JSON['parse'](_0x484d83[_0x90f4('0x7e')]);_0x553e05[_0x90f4('0x42')](_0x2106d7(_0x484d83[_0x90f4('0x7e')][_0x90f4('0x66')]));}_0x1551b5=_0x553e05[_0x90f4('0x7f')]();_0x553e05[_0x90f4('0x1f')](_0x552249);_0x553e05[_0x90f4('0x1e')](0x0);logger[_0x90f4('0x80')](_0x90f4('0x81'),_0x553e05[_0x90f4('0x82')]());return dbH[_0x90f4('0x83')][_0x90f4('0x36')](_0x553e05['toString'](),{'type':dbH[_0x90f4('0x83')][_0x90f4('0x84')][_0x90f4('0x85')]});}})[_0x90f4('0x25')](respondWithResult(_0x722433,null))[_0x90f4('0x49')](handleError(_0x722433,null));};function runReport(_0x352608,_0x332884,_0x497140){var _0x1ddd91=_0x332884[_0x90f4('0x2d')];var _0x887446=0xa;var _0x517bbe=[_0x90f4('0x57'),_0x90f4('0x86'),_0x90f4('0x87'),_0x90f4('0x58')];var _0x1ee737={'SUM':_0x90f4('0x88'),'COUNT':_0x90f4('0x59'),'COUNT DISTINCT':_0x90f4('0x5a'),'MAX':_0x90f4('0x5b'),'MIN':'MIN(%s)','AVG':_0x90f4('0x89'),'GROUP_CONCAT':_0x90f4('0x8a'),'GROUP_CONCAT ASC':_0x90f4('0x8b'),'GROUP_CONCAT DESC':_0x90f4('0x5c')};var _0x4fb487={'SEC_TO_TIME':_0x90f4('0x5d'),'DATE':_0x90f4('0x5e'),'HOUR':_0x90f4('0x8c'),'ROUND':'ROUND(%s)','UNIX_TIMESTAMP':'UNIX_TIMESTAMP(%s)'};var _0x55b8ad=function(_0x48a344){return!_[_0x90f4('0x61')]([_0x90f4('0x62'),_0x90f4('0x63'),'IS\x20EMPTY',_0x90f4('0x64')],_0x48a344);};var _0x5e0792=function(_0x4a73a8){if(!_0x4a73a8||!_0x4a73a8[_0x90f4('0x65')]['length']){return'';}for(var _0x198d97='(',_0x564435=0x0;_0x564435<_0x4a73a8['rules'][_0x90f4('0x3b')];_0x564435+=0x1){_0x564435>0x0&&(_0x198d97+='\x20'+_0x4a73a8[_0x90f4('0x8d')]+'\x20');_0x198d97+=_0x4a73a8[_0x90f4('0x65')][_0x564435][_0x90f4('0x66')]?_0x5e0792(_0x4a73a8[_0x90f4('0x65')][_0x564435][_0x90f4('0x66')]):_0x4a73a8[_0x90f4('0x65')][_0x564435]['field']+'\x20'+_0x4a73a8['rules'][_0x564435]['condition']+(_0x55b8ad(_0x4a73a8['rules'][_0x564435]['condition'])?'\x20'+_0x4a73a8[_0x90f4('0x65')][_0x564435][_0x90f4('0x69')]:'');}return _0x198d97+')';};var _0xa7017={'where':{'id':_0x352608['id']}},_0x383ecb,_0x73a3bc,_0x5b6b8c,_0x3ad40f,_0x40fc3b;_0xa7017=_[_0x90f4('0x43')]({},_0xa7017,_0x497140);return db[_0x90f4('0x45')][_0x90f4('0x53')](_0xa7017)[_0x90f4('0x25')](function(_0x366004){if(_0x366004){_0x383ecb=_0x366004;return _0x366004[_0x90f4('0x6a')]();}return null;})[_0x90f4('0x25')](function(_0x439674){if(_0x439674){if(!_0x439674[_0x90f4('0x3b')]){throw new db[(_0x90f4('0x8e'))][(_0x90f4('0x6b'))](_0x90f4('0x8f'),0x193);}_0x73a3bc=_0x439674;return db[_0x90f4('0x6c')][_0x90f4('0x47')]({'raw':!![]});}return null;})[_0x90f4('0x25')](function(_0x37c8e2){if(_0x37c8e2){_0x5b6b8c=_[_0x90f4('0x6d')](_0x37c8e2,'id');var _0x4c1fe9;var _0x54ea66=squel[_0x90f4('0x90')]();_0x54ea66[_0x90f4('0x6e')](_0x383ecb[_0x90f4('0x6f')]);for(var _0x2553b4=0x0;_0x2553b4<_0x73a3bc[_0x90f4('0x3b')];_0x2553b4+=0x1){_0x4c1fe9=_0x73a3bc[_0x2553b4][_0x90f4('0x70')]?_0x5b6b8c[_0x73a3bc[_0x2553b4]['MetricId']][_0x90f4('0x71')]:_0x73a3bc[_0x2553b4]['field'];_0x4c1fe9=_0x73a3bc[_0x2553b4][_0x90f4('0x72')]?_[_0x90f4('0x73')](_0x1ee737[_0x73a3bc[_0x2553b4][_0x90f4('0x72')]],'%s',_0x4c1fe9):_0x4c1fe9;_0x4c1fe9=_0x73a3bc[_0x2553b4][_0x90f4('0x74')]?_[_0x90f4('0x73')](_0x4fb487[_0x73a3bc[_0x2553b4]['format']],'%s',_0x4c1fe9):_0x4c1fe9;if(_0x73a3bc[_0x2553b4][_0x90f4('0x75')]){_0x54ea66[_0x90f4('0x66')](_0x4c1fe9);}_0x73a3bc[_0x2553b4]['orderBy']&&_0x54ea66[_0x90f4('0x3e')](_0x4c1fe9,_0x73a3bc[_0x2553b4]['orderBy']===_0x90f4('0x77')?![]:!![]);_0x54ea66[_0x90f4('0x67')](_0x4c1fe9,_0x73a3bc[_0x2553b4][_0x90f4('0x78')]||_0x4c1fe9);}if(_0x383ecb[_0x90f4('0x79')]){_0x383ecb[_0x90f4('0x79')]=JSON[_0x90f4('0x7a')](_0x383ecb[_0x90f4('0x79')]);for(var _0x57186d=0x0;_0x57186d<_0x383ecb[_0x90f4('0x79')][_0x90f4('0x3b')];_0x57186d+=0x1){if(_0x383ecb['joins'][_0x57186d][_0x90f4('0x33')]&&_[_0x90f4('0x61')](_0x517bbe,_0x383ecb[_0x90f4('0x79')][_0x57186d]['type'])&&_0x383ecb[_0x90f4('0x79')][_0x57186d]['parentKey']&&_0x383ecb[_0x90f4('0x79')][_0x57186d][_0x90f4('0x7b')]&&_0x383ecb[_0x90f4('0x79')][_0x57186d][_0x90f4('0x7d')]){_0x54ea66[_0x383ecb['joins'][_0x57186d][_0x90f4('0x33')]](_0x383ecb[_0x90f4('0x79')][_0x57186d]['foreignTable'],null,util[_0x90f4('0x74')]('%s.%s\x20=\x20%s.%s',_0x383ecb[_0x90f4('0x6f')],_0x383ecb[_0x90f4('0x79')][_0x57186d][_0x90f4('0x7c')],_0x383ecb[_0x90f4('0x79')][_0x57186d][_0x90f4('0x7b')],_0x383ecb['joins'][_0x57186d][_0x90f4('0x7d')]));}}}if(_0x383ecb[_0x90f4('0x7e')]){_0x383ecb[_0x90f4('0x7e')]=JSON[_0x90f4('0x7a')](_0x383ecb[_0x90f4('0x7e')]);_0x54ea66[_0x90f4('0x42')](_0x5e0792(_0x383ecb[_0x90f4('0x7e')]['group']));}_0x40fc3b=_0x54ea66[_0x90f4('0x7f')]();_0x54ea66['limit'](_0x887446);_0x54ea66['offset'](0x0);return;}})[_0x90f4('0x25')](function(){if(_0x332884[_0x90f4('0x91')]===_0x90f4('0x92')){return null;}var _0x19de8e=_[_0x90f4('0x43')](_0x332884,{'name':_0x332884['fullPath']||_0x332884[_0x90f4('0x2d')]||_0x383ecb[_0x90f4('0x2d')],'basename':_0x1ddd91,'savename':util[_0x90f4('0x74')]('%s-%s.%s',_0x332884[_0x90f4('0x2d')]||_0x383ecb[_0x90f4('0x2d')],require(_0x90f4('0x93'))['generate'](0x4),_0x332884['output']||_0x90f4('0x94')),'reportId':_0x383ecb['id'],'reportType':_0x90f4('0x95')});return db['AnalyticExtractedReport']['create'](_0x19de8e,{'raw':!![]});})['then'](function(_0x23e324){if(_0x383ecb[_0x90f4('0x6f')]==='cdr'){_0x40fc3b[_0x90f4('0x42')](_0x383ecb[_0x90f4('0x6f')]+_0x90f4('0x96'),_0x332884[_0x90f4('0x97')],_0x332884['endDate']);}else{_0x40fc3b[_0x90f4('0x42')](_0x383ecb[_0x90f4('0x6f')]+_0x90f4('0x98'),_0x332884[_0x90f4('0x97')],_0x332884[_0x90f4('0x99')]);}if(_0x23e324){logger[_0x90f4('0x9a')](_0x90f4('0x9b'),_0x40fc3b[_0x90f4('0x82')]());require(_0x90f4('0x9c'))[_0x90f4('0x9d')](path[_0x90f4('0x57')](__dirname,_0x90f4('0x9e'),_0x23e324['output']),[_0x23e324['id'],_0x40fc3b[_0x90f4('0x82')](),path['join'](__dirname,_0x90f4('0x9f'),_0x23e324[_0x90f4('0xa0')]),_0x1ddd91]);return _0x23e324;}else{var _0x55c0ed=squel[_0x90f4('0x90')]();_0x55c0ed[_0x90f4('0x6e')]('('+_0x40fc3b[_0x90f4('0x82')]()+')',_0x90f4('0xa1'));_0x55c0ed['field'](_0x90f4('0xa2'),'count');_0x40fc3b[_0x90f4('0x1f')](_0x332884[_0x90f4('0x1f')]||_0x887446);_0x40fc3b['offset'](_0x332884['offset']||0x0);return dbH['sequelize'][_0x90f4('0x36')](_0x55c0ed[_0x90f4('0x82')](),{'type':dbH[_0x90f4('0x83')]['QueryTypes'][_0x90f4('0x85')]})[_0x90f4('0x25')](function(_0x1544b8){if(!_0x1544b8[_0x90f4('0x3b')]){_0x3ad40f=0x0;}else{_0x3ad40f=_0x1544b8[0x0][_0x90f4('0x1d')]||0x0;}return dbH[_0x90f4('0x83')][_0x90f4('0x36')](_0x40fc3b[_0x90f4('0x82')](),{'type':dbH[_0x90f4('0x83')][_0x90f4('0x84')]['SELECT']});})[_0x90f4('0x25')](function(_0x11c77f){return{'rows':_0x11c77f||[],'count':_0x3ad40f};});}});}exports[_0x90f4('0xa3')]=function(_0x3a6f52,_0x4937c7){return runReport(_0x3a6f52[_0x90f4('0x4b')],_0x3a6f52[_0x90f4('0x36')],_0x3a6f52[_0x90f4('0x44')])[_0x90f4('0x25')](respondWithResult(_0x4937c7,null))[_0x90f4('0x49')](handleError(_0x4937c7,null));};exports[_0x90f4('0xa4')]=runReport;exports['query']=function(_0x274a06,_0x18f1ef){var _0x13f02e=0xa;var _0x4b2868=[_0x90f4('0x57'),_0x90f4('0x86'),'right_join',_0x90f4('0x58')];var _0x528fb9={'SUM':_0x90f4('0x88'),'COUNT':'COUNT(%s)','COUNT DISTINCT':'COUNT(DISTINCT\x20%s)','MAX':_0x90f4('0x5b'),'MIN':_0x90f4('0xa5'),'AVG':_0x90f4('0x89'),'GROUP_CONCAT':_0x90f4('0x8a'),'GROUP_CONCAT ASC':_0x90f4('0x8b'),'GROUP_CONCAT DESC':_0x90f4('0x5c')};var _0x27d653={'SEC_TO_TIME':_0x90f4('0x5d'),'DATE':_0x90f4('0x5e'),'HOUR':_0x90f4('0x8c'),'ROUND':_0x90f4('0x5f'),'UNIX_TIMESTAMP':_0x90f4('0x60')};var _0x311833=function(_0x471c98){return!_[_0x90f4('0x61')]([_0x90f4('0x62'),_0x90f4('0x63'),_0x90f4('0xa6'),_0x90f4('0x64')],_0x471c98);};var _0x4f10d9=function(_0x3abe0e){if(!_0x3abe0e||!_0x3abe0e[_0x90f4('0x65')]['length']){return'';}for(var _0x4e5617='(',_0x71f00c=0x0;_0x71f00c<_0x3abe0e[_0x90f4('0x65')][_0x90f4('0x3b')];_0x71f00c+=0x1){_0x71f00c>0x0&&(_0x4e5617+='\x20'+_0x3abe0e['operator']+'\x20');_0x4e5617+=_0x3abe0e[_0x90f4('0x65')][_0x71f00c][_0x90f4('0x66')]?_0x4f10d9(_0x3abe0e['rules'][_0x71f00c]['group']):_0x3abe0e['rules'][_0x71f00c][_0x90f4('0x67')]+'\x20'+_0x3abe0e['rules'][_0x71f00c][_0x90f4('0x68')]+(_0x311833(_0x3abe0e[_0x90f4('0x65')][_0x71f00c]['condition'])?'\x20'+_0x3abe0e[_0x90f4('0x65')][_0x71f00c][_0x90f4('0x69')]:'');}return _0x4e5617+')';};var _0x2f318b={'where':{'id':_0x274a06['params']['id']}},_0x4a2e6b,_0x5bfb8b,_0x2a3e14,_0x4f6bc3,_0x11bcb0;_0x2f318b=_[_0x90f4('0x43')]({},_0x2f318b,_0x274a06[_0x90f4('0x44')]);return db['AnalyticCustomReport'][_0x90f4('0x53')](_0x2f318b)[_0x90f4('0x25')](function(_0x52c878){if(_0x52c878){_0x4a2e6b=_0x52c878;return _0x52c878[_0x90f4('0x6a')]();}return null;})[_0x90f4('0x25')](function(_0x53effb){if(_0x53effb){if(!_0x53effb[_0x90f4('0x3b')]){throw new db[(_0x90f4('0x8e'))][(_0x90f4('0x6b'))]('Please\x20insert\x20at\x20least\x20one\x20field',0x193);}_0x5bfb8b=_0x53effb;return db[_0x90f4('0x6c')][_0x90f4('0x47')]({'raw':!![]});}return null;})[_0x90f4('0x25')](function(_0x458fce){if(_0x458fce){_0x2a3e14=_['keyBy'](_0x458fce,'id');var _0x3eba2c;var _0x3139fc=squel['select']();_0x3139fc['from'](_0x4a2e6b[_0x90f4('0x6f')]);for(var _0x17e3db=0x0;_0x17e3db<_0x5bfb8b['length'];_0x17e3db+=0x1){_0x3eba2c=_0x5bfb8b[_0x17e3db][_0x90f4('0x70')]?_0x2a3e14[_0x5bfb8b[_0x17e3db][_0x90f4('0x70')]]['metric']:_0x5bfb8b[_0x17e3db][_0x90f4('0x67')];_0x3eba2c=_0x5bfb8b[_0x17e3db][_0x90f4('0x72')]?_[_0x90f4('0x73')](_0x528fb9[_0x5bfb8b[_0x17e3db][_0x90f4('0x72')]],'%s',_0x3eba2c):_0x3eba2c;_0x3eba2c=_0x5bfb8b[_0x17e3db]['format']?_[_0x90f4('0x73')](_0x27d653[_0x5bfb8b[_0x17e3db][_0x90f4('0x74')]],'%s',_0x3eba2c):_0x3eba2c;if(_0x5bfb8b[_0x17e3db][_0x90f4('0x75')]){_0x3139fc[_0x90f4('0x66')](_0x3eba2c);}_0x5bfb8b[_0x17e3db]['orderBy']&&_0x3139fc[_0x90f4('0x3e')](_0x3eba2c,_0x5bfb8b[_0x17e3db][_0x90f4('0x76')]===_0x90f4('0x77')?![]:!![]);_0x3139fc[_0x90f4('0x67')](_0x3eba2c,_0x5bfb8b[_0x17e3db][_0x90f4('0x78')]||_0x3eba2c);}if(_0x4a2e6b[_0x90f4('0x79')]){_0x4a2e6b[_0x90f4('0x79')]=JSON[_0x90f4('0x7a')](_0x4a2e6b['joins']);for(var _0x50d38a=0x0;_0x50d38a<_0x4a2e6b[_0x90f4('0x79')]['length'];_0x50d38a+=0x1){if(_0x4a2e6b[_0x90f4('0x79')][_0x50d38a]['type']&&_['includes'](_0x4b2868,_0x4a2e6b[_0x90f4('0x79')][_0x50d38a]['type'])&&_0x4a2e6b['joins'][_0x50d38a][_0x90f4('0x7c')]&&_0x4a2e6b['joins'][_0x50d38a][_0x90f4('0x7b')]&&_0x4a2e6b[_0x90f4('0x79')][_0x50d38a][_0x90f4('0x7d')]){_0x3139fc[_0x4a2e6b[_0x90f4('0x79')][_0x50d38a]['type']](_0x4a2e6b[_0x90f4('0x79')][_0x50d38a]['foreignTable'],null,util[_0x90f4('0x74')](_0x90f4('0xa7'),_0x4a2e6b[_0x90f4('0x6f')],_0x4a2e6b[_0x90f4('0x79')][_0x50d38a][_0x90f4('0x7c')],_0x4a2e6b['joins'][_0x50d38a][_0x90f4('0x7b')],_0x4a2e6b[_0x90f4('0x79')][_0x50d38a][_0x90f4('0x7d')]));}}}if(_0x4a2e6b[_0x90f4('0x7e')]){_0x4a2e6b[_0x90f4('0x7e')]=JSON[_0x90f4('0x7a')](_0x4a2e6b[_0x90f4('0x7e')]);_0x3139fc[_0x90f4('0x42')](_0x4f10d9(_0x4a2e6b[_0x90f4('0x7e')]['group']));}_0x11bcb0=_0x3139fc[_0x90f4('0x7f')]();_0x3139fc['limit'](_0x13f02e);_0x3139fc[_0x90f4('0x1e')](0x0);logger[_0x90f4('0x80')](_0x90f4('0x81'),_0x3139fc[_0x90f4('0x82')]());return dbH[_0x90f4('0x83')][_0x90f4('0x36')](_0x3139fc[_0x90f4('0x82')](),{'type':dbH['sequelize'][_0x90f4('0x84')][_0x90f4('0x85')]});}})[_0x90f4('0x25')](function(){return{'sql':_0x11bcb0[_0x90f4('0x82')]()};})['then'](respondWithResult(_0x18f1ef,null))[_0x90f4('0x49')](handleError(_0x18f1ef,null));};