Built motion from commit (unavailable).|2.3.2
[motion2.git] / server / api / analyticDefaultReport / analyticDefaultReport.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 _0xf811=['fullPath','%s-%s.%s','randomstring','generate','csv','default','AnalyticExtractedReport','cdr','.calldate\x20BETWEEN\x20?\x20AND\x20?','startDate','endDate','.createdAt\x20BETWEEN\x20?\x20AND\x20?','info','Run\x20Query:','child_process','fork','../../files/reports','savename','COUNT(*)','run','runReport','getFields','alias','html-pdf','rimraf','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','squel','crypto','jsforce','desk.js','../../components/auth/service','../../config/license/hardware','../../components/parsers/qs','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','status','end','json','count','offset','limit','Content-Range','update','then','destroy','get','UserProfileResource','error','stack','name','send','index','AnalyticDefaultReport','rawAttributes','model','keys','query','intersection','attributes','fields','nolimit','order','sort','filters','pick','type','field','filter','merge','options','where','includeAll','include','findAll','rows','catch','show','params','length','find','create','body','user','Unable\x20to\x20retrieve\x20the\x20current\x20user','Reports','userProfileId','autoAssociation','DefaultReports','preview','join','left_join','right_join','cross_join','COUNT(%s)','COUNT(DISTINCT\x20%s)','MIN(%s)','AVG(%s)','GROUP_CONCAT(%s)','GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20DESC)','SEC_TO_TIME(%s)','DATE(%s)','HOUR(%s)','ROUND(%s)','includes','IS\x20NULL','IS\x20NOT\x20NULL','IS\x20EMPTY','IS\x20NOT\x20EMPTY','rules','operator','group','condition','Sequelize','ValidationErrorItem','keyBy','select','from','MetricId','metric','function','replace','format','orderBy','DESC','joins','parse','parentKey','foreignTable','foreignKey','%s.%s\x20=\x20%s.%s','table','conditions','clone','debug','Limited\x20Query:','toString','sequelize','QueryTypes','SUM(%s)','MAX(%s)','GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20ASC)','UNIX_TIMESTAMP(%s)','value','Please\x20insert\x20at\x20least\x20one\x20field','AnalyticMetric','groupBy','SELECT','No\x20available\x20data','output','web'];(function(_0x4a9948,_0x3ef774){var _0x11235c=function(_0x8c8dcb){while(--_0x8c8dcb){_0x4a9948['push'](_0x4a9948['shift']());}};_0x11235c(++_0x3ef774);}(_0xf811,0x159));var _0x1f81=function(_0x3826ad,_0x49a30d){_0x3826ad=_0x3826ad-0x0;var _0x17b900=_0xf811[_0x3826ad];return _0x17b900;};'use strict';var pdf=require(_0x1f81('0x0'));var emlformat=require('eml-format');var rimraf=require(_0x1f81('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0x1f81('0x2'));var rp=require(_0x1f81('0x3'));var moment=require(_0x1f81('0x4'));var BPromise=require(_0x1f81('0x5'));var Mustache=require(_0x1f81('0x6'));var util=require(_0x1f81('0x7'));var path=require(_0x1f81('0x8'));var sox=require(_0x1f81('0x9'));var csv=require(_0x1f81('0xa'));var ejs=require('ejs');var fs=require('fs');var _=require('lodash');var squel=require(_0x1f81('0xb'));var crypto=require(_0x1f81('0xc'));var jsforce=require(_0x1f81('0xd'));var deskjs=require(_0x1f81('0xe'));var toCsv=require(_0x1f81('0xa'));var querystring=require('querystring');var Papa=require('papaparse');var authService=require(_0x1f81('0xf'));var hardwareService=require(_0x1f81('0x10'));var Redis=require('ioredis');var qs=require(_0x1f81('0x11'));var logger=require('../../config/logger')(_0x1f81('0x12'));var utils=require(_0x1f81('0x13'));var config=require(_0x1f81('0x14'));var db=require(_0x1f81('0x15'))['db'];function respondWithStatusCode(_0x5dcdf9,_0x3d44f7){_0x3d44f7=_0x3d44f7||0xcc;return function(_0x649161){if(_0x649161){return _0x5dcdf9[_0x1f81('0x16')](_0x3d44f7);}return _0x5dcdf9[_0x1f81('0x17')](_0x3d44f7)[_0x1f81('0x18')]();};}function respondWithResult(_0x493f7a,_0x5bef11){_0x5bef11=_0x5bef11||0xc8;return function(_0x195f71){if(_0x195f71){return _0x493f7a['status'](_0x5bef11)[_0x1f81('0x19')](_0x195f71);}};}function respondWithFilteredResult(_0x64c764,_0x44cdd8){return function(_0xebc17b){if(_0xebc17b){var _0x46a22e=_0xebc17b[_0x1f81('0x1a')],_0x1c45fb=_0x44cdd8[_0x1f81('0x1b')],_0x39984a=_0x44cdd8[_0x1f81('0x1b')]+_0x44cdd8[_0x1f81('0x1c')],_0x3784cc;if(_0x39984a>=_0x46a22e){_0x39984a=_0x46a22e;_0x3784cc=0xc8;}else{_0x3784cc=0xce;}_0x64c764[_0x1f81('0x17')](_0x3784cc);return _0x64c764['set'](_0x1f81('0x1d'),_0x1c45fb+'-'+_0x39984a+'/'+_0x46a22e)[_0x1f81('0x19')](_0xebc17b);}return null;};}function patchUpdates(_0x2352a6){return function(_0x3d069c){try{jsonpatch['apply'](_0x3d069c,_0x2352a6,!![]);}catch(_0x5146a5){return BPromise['reject'](_0x5146a5);}return _0x3d069c['save']();};}function saveUpdates(_0x5d2422,_0x527aa9){return function(_0x328a07){if(_0x328a07){return _0x328a07[_0x1f81('0x1e')](_0x5d2422)[_0x1f81('0x1f')](function(_0x4d5a69){return _0x4d5a69;});}return null;};}function removeEntity(_0x134109,_0x5d277b){return function(_0x2ecbab){if(_0x2ecbab){return _0x2ecbab[_0x1f81('0x20')]()['then'](function(){var _0xc0880=_0x2ecbab[_0x1f81('0x21')]({'plain':!![]});var _0x177d3d='DefaultReports';return db[_0x1f81('0x22')]['destroy']({'where':{'type':_0x177d3d,'resourceId':_0xc0880['id']}})[_0x1f81('0x1f')](function(){return _0x2ecbab;});})[_0x1f81('0x1f')](function(){_0x134109[_0x1f81('0x17')](0xcc)['end']();});}};}function handleEntityNotFound(_0x4aa5ce,_0x4dd164){return function(_0x19247f){if(!_0x19247f){_0x4aa5ce[_0x1f81('0x16')](0x194);}return _0x19247f;};}function handleError(_0x5a5b8a,_0x5a2321){_0x5a2321=_0x5a2321||0x1f4;return function(_0x43154d){logger[_0x1f81('0x23')](_0x43154d[_0x1f81('0x24')]);if(_0x43154d[_0x1f81('0x25')]){delete _0x43154d[_0x1f81('0x25')];}_0x5a5b8a[_0x1f81('0x17')](_0x5a2321)[_0x1f81('0x26')](_0x43154d);};}exports[_0x1f81('0x27')]=function(_0x27731e,_0x5d1076){var _0x3857f6={},_0x436e89={},_0x1236aa={'count':0x0,'rows':[]};var _0x2da697=db[_0x1f81('0x28')][_0x1f81('0x29')];_0x436e89[_0x1f81('0x2a')]=_[_0x1f81('0x2b')](_0x2da697);_0x436e89[_0x1f81('0x2c')]=_[_0x1f81('0x2b')](_0x27731e[_0x1f81('0x2c')]);_0x436e89['filters']=_[_0x1f81('0x2d')](_0x436e89['model'],_0x436e89[_0x1f81('0x2c')]);_0x3857f6[_0x1f81('0x2e')]=_[_0x1f81('0x2d')](_0x436e89[_0x1f81('0x2a')],qs['fields'](_0x27731e[_0x1f81('0x2c')][_0x1f81('0x2f')]));_0x3857f6['attributes']=_0x3857f6[_0x1f81('0x2e')]['length']?_0x3857f6['attributes']:_0x436e89[_0x1f81('0x2a')];if(!_0x27731e[_0x1f81('0x2c')]['hasOwnProperty'](_0x1f81('0x30'))){_0x3857f6[_0x1f81('0x1c')]=qs[_0x1f81('0x1c')](_0x27731e[_0x1f81('0x2c')]['limit']);_0x3857f6[_0x1f81('0x1b')]=qs[_0x1f81('0x1b')](_0x27731e[_0x1f81('0x2c')][_0x1f81('0x1b')]);}_0x3857f6[_0x1f81('0x31')]=qs['sort'](_0x27731e[_0x1f81('0x2c')][_0x1f81('0x32')]);_0x3857f6['where']=qs[_0x1f81('0x33')](_[_0x1f81('0x34')](_0x27731e[_0x1f81('0x2c')],_0x436e89[_0x1f81('0x33')]));if(_0x27731e['query']['filter']){_0x3857f6['where']=_['merge'](_0x3857f6['where'],{'$or':_['map'](_0x2da697,function(_0x333129){if(_0x333129[_0x1f81('0x35')]['key']!=='VIRTUAL'){var _0x5daec3={};_0x5daec3[_0x333129[_0x1f81('0x36')]]={'$like':'%'+_0x27731e[_0x1f81('0x2c')][_0x1f81('0x37')]+'%'};return _0x5daec3;}})});}_0x3857f6=_[_0x1f81('0x38')]({},_0x3857f6,_0x27731e[_0x1f81('0x39')]);var _0x5c9e64={'where':_0x3857f6[_0x1f81('0x3a')]};return db['AnalyticDefaultReport'][_0x1f81('0x1a')](_0x5c9e64)['then'](function(_0x46d457){_0x1236aa[_0x1f81('0x1a')]=_0x46d457;if(_0x27731e[_0x1f81('0x2c')][_0x1f81('0x3b')]){_0x3857f6[_0x1f81('0x3c')]=[{'all':!![]}];}return db[_0x1f81('0x28')][_0x1f81('0x3d')](_0x3857f6);})[_0x1f81('0x1f')](function(_0x141d16){_0x1236aa[_0x1f81('0x3e')]=_0x141d16;return _0x1236aa;})[_0x1f81('0x1f')](respondWithFilteredResult(_0x5d1076,_0x3857f6))[_0x1f81('0x3f')](handleError(_0x5d1076,null));};exports[_0x1f81('0x40')]=function(_0x58c488,_0x3b4217){var _0x58402f={'raw':!![],'where':{'id':_0x58c488[_0x1f81('0x41')]['id']}},_0x1f4a74={};_0x1f4a74[_0x1f81('0x2a')]=_[_0x1f81('0x2b')](db['AnalyticDefaultReport']['rawAttributes']);_0x1f4a74[_0x1f81('0x2c')]=_[_0x1f81('0x2b')](_0x58c488['query']);_0x1f4a74[_0x1f81('0x33')]=_[_0x1f81('0x2d')](_0x1f4a74['model'],_0x1f4a74['query']);_0x58402f[_0x1f81('0x2e')]=_[_0x1f81('0x2d')](_0x1f4a74[_0x1f81('0x2a')],qs[_0x1f81('0x2f')](_0x58c488['query'][_0x1f81('0x2f')]));_0x58402f[_0x1f81('0x2e')]=_0x58402f[_0x1f81('0x2e')][_0x1f81('0x42')]?_0x58402f[_0x1f81('0x2e')]:_0x1f4a74[_0x1f81('0x2a')];if(_0x58c488[_0x1f81('0x2c')][_0x1f81('0x3b')]){_0x58402f[_0x1f81('0x3c')]=[{'all':!![]}];}_0x58402f=_[_0x1f81('0x38')]({},_0x58402f,_0x58c488[_0x1f81('0x39')]);return db[_0x1f81('0x28')][_0x1f81('0x43')](_0x58402f)[_0x1f81('0x1f')](handleEntityNotFound(_0x3b4217,null))[_0x1f81('0x1f')](respondWithResult(_0x3b4217,null))[_0x1f81('0x3f')](handleError(_0x3b4217,null));};exports['create']=function(_0x3b69a8,_0x12f4fb){return db['AnalyticDefaultReport'][_0x1f81('0x44')](_0x3b69a8[_0x1f81('0x45')],{})[_0x1f81('0x1f')](function(_0x45c98f){var _0x562457=_0x3b69a8[_0x1f81('0x46')]['get']({'plain':!![]});if(!_0x562457)throw new Error(_0x1f81('0x47'));if(_0x562457['role']===_0x1f81('0x46')){var _0xfd77f3=_0x45c98f[_0x1f81('0x21')]({'plain':!![]});var _0x8a8a87=_0x1f81('0x48');return db['UserProfileSection']['find']({'where':{'name':_0x8a8a87,'userProfileId':_0x562457[_0x1f81('0x49')]},'raw':!![]})['then'](function(_0x3d98c0){if(_0x3d98c0&&_0x3d98c0[_0x1f81('0x4a')]===0x0){return db['UserProfileResource']['create']({'name':_0xfd77f3[_0x1f81('0x25')],'resourceId':_0xfd77f3['id'],'type':_0x1f81('0x4b'),'sectionId':_0x3d98c0['id']},{})[_0x1f81('0x1f')](function(){return _0x45c98f;});}else{return _0x45c98f;}})[_0x1f81('0x3f')](function(_0x3407b4){logger['error']('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x3407b4);throw _0x3407b4;});}return _0x45c98f;})[_0x1f81('0x1f')](respondWithResult(_0x12f4fb,0xc9))[_0x1f81('0x3f')](handleError(_0x12f4fb,null));};exports[_0x1f81('0x1e')]=function(_0x249322,_0x9689b7){if(_0x249322['body']['id']){delete _0x249322['body']['id'];}return db[_0x1f81('0x28')][_0x1f81('0x43')]({'where':{'id':_0x249322[_0x1f81('0x41')]['id']}})['then'](handleEntityNotFound(_0x9689b7,null))[_0x1f81('0x1f')](saveUpdates(_0x249322['body'],null))[_0x1f81('0x1f')](respondWithResult(_0x9689b7,null))['catch'](handleError(_0x9689b7,null));};exports[_0x1f81('0x20')]=function(_0x14a051,_0x1e7617){return db[_0x1f81('0x28')][_0x1f81('0x43')]({'where':{'id':_0x14a051['params']['id']}})[_0x1f81('0x1f')](handleEntityNotFound(_0x1e7617,null))[_0x1f81('0x1f')](removeEntity(_0x1e7617,null))[_0x1f81('0x3f')](handleError(_0x1e7617,null));};exports[_0x1f81('0x4c')]=function(_0x264692,_0x5e434d){var _0xf901de=0xa;var _0xf26f37=[_0x1f81('0x4d'),_0x1f81('0x4e'),_0x1f81('0x4f'),_0x1f81('0x50')];var _0x167943={'SUM':'SUM(%s)','COUNT':_0x1f81('0x51'),'COUNT DISTINCT':_0x1f81('0x52'),'MAX':'MAX(%s)','MIN':_0x1f81('0x53'),'AVG':_0x1f81('0x54'),'GROUP_CONCAT':_0x1f81('0x55'),'GROUP_CONCAT ASC':'GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20ASC)','GROUP_CONCAT DESC':_0x1f81('0x56')};var _0x563367={'SEC_TO_TIME':_0x1f81('0x57'),'DATE':_0x1f81('0x58'),'HOUR':_0x1f81('0x59'),'ROUND':_0x1f81('0x5a'),'UNIX_TIMESTAMP':'UNIX_TIMESTAMP(%s)'};var _0x292f8c=function(_0x4ef5c0){return!_[_0x1f81('0x5b')]([_0x1f81('0x5c'),_0x1f81('0x5d'),_0x1f81('0x5e'),_0x1f81('0x5f')],_0x4ef5c0);};var _0x2851a4=function(_0x13822d){if(!_0x13822d||!_0x13822d[_0x1f81('0x60')][_0x1f81('0x42')]){return'';}for(var _0x508a85='(',_0x19c256=0x0;_0x19c256<_0x13822d[_0x1f81('0x60')][_0x1f81('0x42')];_0x19c256+=0x1){_0x19c256>0x0&&(_0x508a85+='\x20'+_0x13822d[_0x1f81('0x61')]+'\x20');_0x508a85+=_0x13822d[_0x1f81('0x60')][_0x19c256]['group']?_0x2851a4(_0x13822d[_0x1f81('0x60')][_0x19c256][_0x1f81('0x62')]):_0x13822d['rules'][_0x19c256][_0x1f81('0x36')]+'\x20'+_0x13822d[_0x1f81('0x60')][_0x19c256][_0x1f81('0x63')]+(_0x292f8c(_0x13822d[_0x1f81('0x60')][_0x19c256][_0x1f81('0x63')])?'\x20'+_0x13822d[_0x1f81('0x60')][_0x19c256]['value']:'');}return _0x508a85+')';};var _0x68e5bf={'where':{'id':_0x264692['params']['id']}},_0x190a2a,_0x2d793e,_0x575157,_0x6db9e4,_0x246112;_0x68e5bf=_['merge']({},_0x68e5bf,_0x264692[_0x1f81('0x39')]);return db[_0x1f81('0x28')][_0x1f81('0x43')](_0x68e5bf)['then'](function(_0x2d051c){if(_0x2d051c){_0x190a2a=_0x2d051c;return _0x2d051c['getFields']();}return null;})[_0x1f81('0x1f')](function(_0xbbc037){if(_0xbbc037){if(!_0xbbc037[_0x1f81('0x42')]){throw new db[(_0x1f81('0x64'))][(_0x1f81('0x65'))]('Please\x20insert\x20at\x20least\x20one\x20field',0x193);}_0x2d793e=_0xbbc037;return db['AnalyticMetric'][_0x1f81('0x3d')]({'raw':!![]});}return null;})[_0x1f81('0x1f')](function(_0x4d2075){if(_0x4d2075){_0x575157=_[_0x1f81('0x66')](_0x4d2075,'id');var _0x8c663b;var _0x1b3138=squel[_0x1f81('0x67')]();_0x1b3138[_0x1f81('0x68')](_0x190a2a['table']);for(var _0x1f378a=0x0;_0x1f378a<_0x2d793e[_0x1f81('0x42')];_0x1f378a+=0x1){_0x8c663b=_0x2d793e[_0x1f378a][_0x1f81('0x69')]?_0x575157[_0x2d793e[_0x1f378a][_0x1f81('0x69')]][_0x1f81('0x6a')]:_0x2d793e[_0x1f378a][_0x1f81('0x36')];_0x8c663b=_0x2d793e[_0x1f378a][_0x1f81('0x6b')]?_[_0x1f81('0x6c')](_0x167943[_0x2d793e[_0x1f378a]['function']],'%s',_0x8c663b):_0x8c663b;_0x8c663b=_0x2d793e[_0x1f378a][_0x1f81('0x6d')]?_[_0x1f81('0x6c')](_0x563367[_0x2d793e[_0x1f378a]['format']],'%s',_0x8c663b):_0x8c663b;if(_0x2d793e[_0x1f378a]['groupBy']){_0x1b3138['group'](_0x8c663b);}_0x2d793e[_0x1f378a][_0x1f81('0x6e')]&&_0x1b3138[_0x1f81('0x31')](_0x8c663b,_0x2d793e[_0x1f378a][_0x1f81('0x6e')]===_0x1f81('0x6f')?![]:!![]);_0x1b3138[_0x1f81('0x36')](_0x8c663b,_0x2d793e[_0x1f378a]['alias']||_0x8c663b);}if(_0x190a2a[_0x1f81('0x70')]){_0x190a2a['joins']=JSON[_0x1f81('0x71')](_0x190a2a[_0x1f81('0x70')]);for(var _0x102ad9=0x0;_0x102ad9<_0x190a2a[_0x1f81('0x70')][_0x1f81('0x42')];_0x102ad9+=0x1){if(_0x190a2a[_0x1f81('0x70')][_0x102ad9][_0x1f81('0x35')]&&_[_0x1f81('0x5b')](_0xf26f37,_0x190a2a[_0x1f81('0x70')][_0x102ad9][_0x1f81('0x35')])&&_0x190a2a['joins'][_0x102ad9][_0x1f81('0x72')]&&_0x190a2a['joins'][_0x102ad9][_0x1f81('0x73')]&&_0x190a2a[_0x1f81('0x70')][_0x102ad9][_0x1f81('0x74')]){_0x1b3138[_0x190a2a[_0x1f81('0x70')][_0x102ad9][_0x1f81('0x35')]](_0x190a2a[_0x1f81('0x70')][_0x102ad9][_0x1f81('0x73')],null,util[_0x1f81('0x6d')](_0x1f81('0x75'),_0x190a2a[_0x1f81('0x76')],_0x190a2a[_0x1f81('0x70')][_0x102ad9][_0x1f81('0x72')],_0x190a2a['joins'][_0x102ad9][_0x1f81('0x73')],_0x190a2a['joins'][_0x102ad9][_0x1f81('0x74')]));}}}if(_0x190a2a[_0x1f81('0x77')]){_0x190a2a[_0x1f81('0x77')]=JSON[_0x1f81('0x71')](_0x190a2a[_0x1f81('0x77')]);_0x1b3138[_0x1f81('0x3a')](_0x2851a4(_0x190a2a['conditions'][_0x1f81('0x62')]));}_0x246112=_0x1b3138[_0x1f81('0x78')]();_0x1b3138['limit'](_0xf901de);_0x1b3138[_0x1f81('0x1b')](0x0);logger[_0x1f81('0x79')](_0x1f81('0x7a'),_0x1b3138[_0x1f81('0x7b')]());return db[_0x1f81('0x7c')][_0x1f81('0x2c')](_0x1b3138['toString'](),{'type':db[_0x1f81('0x7c')][_0x1f81('0x7d')]['SELECT']});}})[_0x1f81('0x1f')](respondWithResult(_0x5e434d,null))[_0x1f81('0x3f')](handleError(_0x5e434d,null));};function runReport(_0x2c699b,_0x44c592,_0x1a9925){var _0x15c58b=_0x44c592[_0x1f81('0x25')];var _0x1c599d=0xa;var _0x205d02=['join',_0x1f81('0x4e'),_0x1f81('0x4f'),_0x1f81('0x50')];var _0x1c3719={'SUM':_0x1f81('0x7e'),'COUNT':_0x1f81('0x51'),'COUNT DISTINCT':_0x1f81('0x52'),'MAX':_0x1f81('0x7f'),'MIN':_0x1f81('0x53'),'AVG':_0x1f81('0x54'),'GROUP_CONCAT':_0x1f81('0x55'),'GROUP_CONCAT ASC':_0x1f81('0x80'),'GROUP_CONCAT DESC':_0x1f81('0x56')};var _0x4f5855={'SEC_TO_TIME':_0x1f81('0x57'),'DATE':'DATE(%s)','HOUR':'HOUR(%s)','ROUND':'ROUND(%s)','UNIX_TIMESTAMP':_0x1f81('0x81')};var _0x59be15=function(_0x1cd935){return!_[_0x1f81('0x5b')](['IS\x20NULL',_0x1f81('0x5d'),_0x1f81('0x5e'),_0x1f81('0x5f')],_0x1cd935);};var _0x4f7c54=function(_0x1263d3){if(!_0x1263d3||!_0x1263d3[_0x1f81('0x60')][_0x1f81('0x42')]){return'';}for(var _0x13f2f7='(',_0x1a1f12=0x0;_0x1a1f12<_0x1263d3['rules']['length'];_0x1a1f12+=0x1){_0x1a1f12>0x0&&(_0x13f2f7+='\x20'+_0x1263d3[_0x1f81('0x61')]+'\x20');_0x13f2f7+=_0x1263d3[_0x1f81('0x60')][_0x1a1f12][_0x1f81('0x62')]?_0x4f7c54(_0x1263d3[_0x1f81('0x60')][_0x1a1f12][_0x1f81('0x62')]):_0x1263d3['rules'][_0x1a1f12][_0x1f81('0x36')]+'\x20'+_0x1263d3['rules'][_0x1a1f12][_0x1f81('0x63')]+(_0x59be15(_0x1263d3['rules'][_0x1a1f12][_0x1f81('0x63')])?'\x20'+_0x1263d3[_0x1f81('0x60')][_0x1a1f12][_0x1f81('0x82')]:'');}return _0x13f2f7+')';};var _0x5188d6={'where':{'id':_0x2c699b['id']}},_0x335c32,_0x58a61d,_0x314fcd,_0x5c08c3,_0x1ffd68;_0x5188d6=_[_0x1f81('0x38')]({},_0x5188d6,_0x1a9925);return db[_0x1f81('0x28')]['find'](_0x5188d6)[_0x1f81('0x1f')](function(_0x42c9aa){if(_0x42c9aa){_0x335c32=_0x42c9aa;return _0x42c9aa['getFields']();}return null;})[_0x1f81('0x1f')](function(_0x20df9d){if(_0x20df9d){if(!_0x20df9d[_0x1f81('0x42')]){throw new db[(_0x1f81('0x64'))][(_0x1f81('0x65'))](_0x1f81('0x83'),0x193);}_0x58a61d=_0x20df9d;return db[_0x1f81('0x84')]['findAll']({'raw':!![]});}return null;})[_0x1f81('0x1f')](function(_0x53eb46){if(_0x53eb46){_0x314fcd=_[_0x1f81('0x66')](_0x53eb46,'id');var _0x5eccf3;var _0x18dcea=squel[_0x1f81('0x67')]();_0x18dcea[_0x1f81('0x68')](_0x335c32[_0x1f81('0x76')]);for(var _0x12158b=0x0;_0x12158b<_0x58a61d[_0x1f81('0x42')];_0x12158b+=0x1){_0x5eccf3=_0x58a61d[_0x12158b]['MetricId']?_0x314fcd[_0x58a61d[_0x12158b][_0x1f81('0x69')]][_0x1f81('0x6a')]:_0x58a61d[_0x12158b][_0x1f81('0x36')];_0x5eccf3=_0x58a61d[_0x12158b][_0x1f81('0x6b')]?_[_0x1f81('0x6c')](_0x1c3719[_0x58a61d[_0x12158b][_0x1f81('0x6b')]],'%s',_0x5eccf3):_0x5eccf3;_0x5eccf3=_0x58a61d[_0x12158b][_0x1f81('0x6d')]?_[_0x1f81('0x6c')](_0x4f5855[_0x58a61d[_0x12158b]['format']],'%s',_0x5eccf3):_0x5eccf3;if(_0x58a61d[_0x12158b][_0x1f81('0x85')]){_0x18dcea[_0x1f81('0x62')](_0x5eccf3);}_0x58a61d[_0x12158b]['orderBy']&&_0x18dcea['order'](_0x5eccf3,_0x58a61d[_0x12158b][_0x1f81('0x6e')]===_0x1f81('0x6f')?![]:!![]);_0x18dcea['field'](_0x5eccf3,_0x58a61d[_0x12158b]['alias']||_0x5eccf3);}if(_0x335c32['joins']){_0x335c32['joins']=JSON[_0x1f81('0x71')](_0x335c32[_0x1f81('0x70')]);for(var _0x37e0ce=0x0;_0x37e0ce<_0x335c32[_0x1f81('0x70')]['length'];_0x37e0ce+=0x1){if(_0x335c32[_0x1f81('0x70')][_0x37e0ce][_0x1f81('0x35')]&&_[_0x1f81('0x5b')](_0x205d02,_0x335c32['joins'][_0x37e0ce][_0x1f81('0x35')])&&_0x335c32[_0x1f81('0x70')][_0x37e0ce]['parentKey']&&_0x335c32[_0x1f81('0x70')][_0x37e0ce][_0x1f81('0x73')]&&_0x335c32['joins'][_0x37e0ce][_0x1f81('0x74')]){_0x18dcea[_0x335c32[_0x1f81('0x70')][_0x37e0ce][_0x1f81('0x35')]](_0x335c32[_0x1f81('0x70')][_0x37e0ce][_0x1f81('0x73')],null,util[_0x1f81('0x6d')](_0x1f81('0x75'),_0x335c32[_0x1f81('0x76')],_0x335c32['joins'][_0x37e0ce][_0x1f81('0x72')],_0x335c32[_0x1f81('0x70')][_0x37e0ce]['foreignTable'],_0x335c32[_0x1f81('0x70')][_0x37e0ce][_0x1f81('0x74')]));}}}if(_0x335c32[_0x1f81('0x77')]){_0x335c32[_0x1f81('0x77')]=JSON['parse'](_0x335c32[_0x1f81('0x77')]);_0x18dcea['where'](_0x4f7c54(_0x335c32[_0x1f81('0x77')][_0x1f81('0x62')]));}_0x1ffd68=_0x18dcea[_0x1f81('0x78')]();_0x18dcea[_0x1f81('0x1c')](_0x1c599d);_0x18dcea['offset'](0x0);logger[_0x1f81('0x79')](_0x1f81('0x7a'),_0x18dcea[_0x1f81('0x7b')]());return db[_0x1f81('0x7c')]['query'](_0x18dcea[_0x1f81('0x7b')](),{'type':db['sequelize'][_0x1f81('0x7d')][_0x1f81('0x86')]});}})[_0x1f81('0x1f')](function(_0x54b79e){if(!_0x54b79e[_0x1f81('0x42')]){throw new db[(_0x1f81('0x64'))][(_0x1f81('0x65'))](_0x1f81('0x87'),0xcc);}if(_0x44c592[_0x1f81('0x88')]===_0x1f81('0x89')){return null;}var _0x416296=_['merge'](_0x44c592,{'name':_0x44c592[_0x1f81('0x8a')]||_0x44c592[_0x1f81('0x25')]||_0x335c32[_0x1f81('0x25')],'basename':_0x15c58b,'savename':util[_0x1f81('0x6d')](_0x1f81('0x8b'),_0x44c592['name']||_0x335c32[_0x1f81('0x25')],require(_0x1f81('0x8c'))[_0x1f81('0x8d')](0x4),_0x44c592[_0x1f81('0x88')]||_0x1f81('0x8e')),'reportId':_0x335c32['id'],'reportType':_0x1f81('0x8f')});return db[_0x1f81('0x90')][_0x1f81('0x44')](_0x416296,{'raw':!![]});})[_0x1f81('0x1f')](function(_0xf8a8c2){if(_0x335c32['table']===_0x1f81('0x91')){_0x1ffd68[_0x1f81('0x3a')](_0x335c32[_0x1f81('0x76')]+_0x1f81('0x92'),_0x44c592[_0x1f81('0x93')],_0x44c592[_0x1f81('0x94')]);}else{_0x1ffd68[_0x1f81('0x3a')](_0x335c32[_0x1f81('0x76')]+_0x1f81('0x95'),_0x44c592['startDate'],_0x44c592[_0x1f81('0x94')]);}if(_0xf8a8c2){logger[_0x1f81('0x96')](_0x1f81('0x97'),_0x1ffd68[_0x1f81('0x7b')]());require(_0x1f81('0x98'))[_0x1f81('0x99')](path[_0x1f81('0x4d')](__dirname,'../../components/export/',_0xf8a8c2[_0x1f81('0x88')]),[_0xf8a8c2['id'],_0x1ffd68['toString'](),path[_0x1f81('0x4d')](__dirname,_0x1f81('0x9a'),_0xf8a8c2[_0x1f81('0x9b')]),_0x15c58b]);return _0xf8a8c2;}else{var _0xaf03d1=squel[_0x1f81('0x67')]();_0xaf03d1['from']('('+_0x1ffd68[_0x1f81('0x7b')]()+')','countTable');_0xaf03d1[_0x1f81('0x36')](_0x1f81('0x9c'),_0x1f81('0x1a'));_0x1ffd68['limit'](_0x44c592[_0x1f81('0x1c')]||_0x1c599d);_0x1ffd68[_0x1f81('0x1b')](_0x44c592['offset']||0x0);return db[_0x1f81('0x7c')][_0x1f81('0x2c')](_0xaf03d1[_0x1f81('0x7b')](),{'type':db['sequelize'][_0x1f81('0x7d')][_0x1f81('0x86')]})['then'](function(_0x42532f){if(!_0x42532f[_0x1f81('0x42')]){_0x5c08c3=0x0;}else{_0x5c08c3=_0x42532f[0x0][_0x1f81('0x1a')]||0x0;}return db[_0x1f81('0x7c')]['query'](_0x1ffd68['toString'](),{'type':db[_0x1f81('0x7c')][_0x1f81('0x7d')][_0x1f81('0x86')]});})[_0x1f81('0x1f')](function(_0x593e8d){return{'rows':_0x593e8d||[],'count':_0x5c08c3};});}});}exports[_0x1f81('0x9d')]=function(_0xac9bbd,_0x44e6f5){return runReport(_0xac9bbd[_0x1f81('0x41')],_0xac9bbd[_0x1f81('0x2c')],_0xac9bbd[_0x1f81('0x39')])['then'](respondWithResult(_0x44e6f5,null))['catch'](handleError(_0x44e6f5,null));};exports[_0x1f81('0x9e')]=runReport;exports[_0x1f81('0x2c')]=function(_0xa7d00a,_0x3b5a88){var _0x50710e=0xa;var _0x1fef19=[_0x1f81('0x4d'),_0x1f81('0x4e'),'right_join',_0x1f81('0x50')];var _0x38907f={'SUM':_0x1f81('0x7e'),'COUNT':_0x1f81('0x51'),'COUNT DISTINCT':_0x1f81('0x52'),'MAX':_0x1f81('0x7f'),'MIN':_0x1f81('0x53'),'AVG':'AVG(%s)','GROUP_CONCAT':_0x1f81('0x55'),'GROUP_CONCAT ASC':_0x1f81('0x80'),'GROUP_CONCAT DESC':'GROUP_CONCAT(%s\x20ORDER\x20BY\x20%s\x20DESC)'};var _0x46c6a2={'SEC_TO_TIME':_0x1f81('0x57'),'DATE':_0x1f81('0x58'),'HOUR':_0x1f81('0x59'),'ROUND':'ROUND(%s)','UNIX_TIMESTAMP':_0x1f81('0x81')};var _0x125dd2=function(_0x47e7a2){return!_['includes']([_0x1f81('0x5c'),_0x1f81('0x5d'),_0x1f81('0x5e'),_0x1f81('0x5f')],_0x47e7a2);};var _0x2bfea9=function(_0xfdf7a6){if(!_0xfdf7a6||!_0xfdf7a6[_0x1f81('0x60')][_0x1f81('0x42')]){return'';}for(var _0x35d584='(',_0xedb8a6=0x0;_0xedb8a6<_0xfdf7a6[_0x1f81('0x60')][_0x1f81('0x42')];_0xedb8a6+=0x1){_0xedb8a6>0x0&&(_0x35d584+='\x20'+_0xfdf7a6[_0x1f81('0x61')]+'\x20');_0x35d584+=_0xfdf7a6[_0x1f81('0x60')][_0xedb8a6]['group']?_0x2bfea9(_0xfdf7a6['rules'][_0xedb8a6]['group']):_0xfdf7a6[_0x1f81('0x60')][_0xedb8a6][_0x1f81('0x36')]+'\x20'+_0xfdf7a6['rules'][_0xedb8a6][_0x1f81('0x63')]+(_0x125dd2(_0xfdf7a6[_0x1f81('0x60')][_0xedb8a6]['condition'])?'\x20'+_0xfdf7a6[_0x1f81('0x60')][_0xedb8a6][_0x1f81('0x82')]:'');}return _0x35d584+')';};var _0x501b38={'where':{'id':_0xa7d00a[_0x1f81('0x41')]['id']}},_0x3f752f,_0x2ac191,_0x483f67,_0x3483d6,_0x3083f9;_0x501b38=_[_0x1f81('0x38')]({},_0x501b38,_0xa7d00a[_0x1f81('0x39')]);return db['AnalyticDefaultReport'][_0x1f81('0x43')](_0x501b38)[_0x1f81('0x1f')](function(_0x4ec169){if(_0x4ec169){_0x3f752f=_0x4ec169;return _0x4ec169[_0x1f81('0x9f')]();}return null;})['then'](function(_0x21cce5){if(_0x21cce5){if(!_0x21cce5[_0x1f81('0x42')]){throw new db[(_0x1f81('0x64'))][(_0x1f81('0x65'))](_0x1f81('0x83'),0x193);}_0x2ac191=_0x21cce5;return db[_0x1f81('0x84')][_0x1f81('0x3d')]({'raw':!![]});}return null;})[_0x1f81('0x1f')](function(_0x407b33){if(_0x407b33){_0x483f67=_['keyBy'](_0x407b33,'id');var _0x4f5d9e;var _0x4405f8=squel[_0x1f81('0x67')]();_0x4405f8['from'](_0x3f752f[_0x1f81('0x76')]);for(var _0xa609b0=0x0;_0xa609b0<_0x2ac191[_0x1f81('0x42')];_0xa609b0+=0x1){_0x4f5d9e=_0x2ac191[_0xa609b0][_0x1f81('0x69')]?_0x483f67[_0x2ac191[_0xa609b0][_0x1f81('0x69')]]['metric']:_0x2ac191[_0xa609b0][_0x1f81('0x36')];_0x4f5d9e=_0x2ac191[_0xa609b0]['function']?_[_0x1f81('0x6c')](_0x38907f[_0x2ac191[_0xa609b0][_0x1f81('0x6b')]],'%s',_0x4f5d9e):_0x4f5d9e;_0x4f5d9e=_0x2ac191[_0xa609b0][_0x1f81('0x6d')]?_['replace'](_0x46c6a2[_0x2ac191[_0xa609b0][_0x1f81('0x6d')]],'%s',_0x4f5d9e):_0x4f5d9e;if(_0x2ac191[_0xa609b0][_0x1f81('0x85')]){_0x4405f8[_0x1f81('0x62')](_0x4f5d9e);}_0x2ac191[_0xa609b0]['orderBy']&&_0x4405f8['order'](_0x4f5d9e,_0x2ac191[_0xa609b0][_0x1f81('0x6e')]===_0x1f81('0x6f')?![]:!![]);_0x4405f8[_0x1f81('0x36')](_0x4f5d9e,_0x2ac191[_0xa609b0][_0x1f81('0xa0')]||_0x4f5d9e);}if(_0x3f752f[_0x1f81('0x70')]){_0x3f752f[_0x1f81('0x70')]=JSON[_0x1f81('0x71')](_0x3f752f[_0x1f81('0x70')]);for(var _0x103ffe=0x0;_0x103ffe<_0x3f752f['joins'][_0x1f81('0x42')];_0x103ffe+=0x1){if(_0x3f752f[_0x1f81('0x70')][_0x103ffe][_0x1f81('0x35')]&&_[_0x1f81('0x5b')](_0x1fef19,_0x3f752f[_0x1f81('0x70')][_0x103ffe][_0x1f81('0x35')])&&_0x3f752f['joins'][_0x103ffe][_0x1f81('0x72')]&&_0x3f752f['joins'][_0x103ffe][_0x1f81('0x73')]&&_0x3f752f[_0x1f81('0x70')][_0x103ffe][_0x1f81('0x74')]){_0x4405f8[_0x3f752f[_0x1f81('0x70')][_0x103ffe][_0x1f81('0x35')]](_0x3f752f['joins'][_0x103ffe][_0x1f81('0x73')],null,util[_0x1f81('0x6d')](_0x1f81('0x75'),_0x3f752f[_0x1f81('0x76')],_0x3f752f[_0x1f81('0x70')][_0x103ffe][_0x1f81('0x72')],_0x3f752f[_0x1f81('0x70')][_0x103ffe][_0x1f81('0x73')],_0x3f752f[_0x1f81('0x70')][_0x103ffe]['foreignKey']));}}}if(_0x3f752f[_0x1f81('0x77')]){_0x3f752f[_0x1f81('0x77')]=JSON[_0x1f81('0x71')](_0x3f752f[_0x1f81('0x77')]);_0x4405f8[_0x1f81('0x3a')](_0x2bfea9(_0x3f752f['conditions'][_0x1f81('0x62')]));}_0x3083f9=_0x4405f8[_0x1f81('0x78')]();_0x4405f8['limit'](_0x50710e);_0x4405f8[_0x1f81('0x1b')](0x0);logger[_0x1f81('0x79')](_0x1f81('0x7a'),_0x4405f8[_0x1f81('0x7b')]());return db[_0x1f81('0x7c')][_0x1f81('0x2c')](_0x4405f8[_0x1f81('0x7b')](),{'type':db[_0x1f81('0x7c')][_0x1f81('0x7d')][_0x1f81('0x86')]});}})[_0x1f81('0x1f')](function(){return{'sql':_0x3083f9['toString']()};})[_0x1f81('0x1f')](respondWithResult(_0x3b5a88,null))['catch'](handleError(_0x3b5a88,null));};