c961c7317d5daf5d75aba15e3c0d771de6acc395
[motion2.git] / server / config / schedule / transcribe.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 _0xc893=['findOne','Negative','Neutral','name','SequelizeValidationError','error','inspect','VoiceRecording','Setting','transcribeAccountId','bucket','Cloud\x20Provider\x20not\x20configured','all','catch','aws-sdk','request-promise','../../mysqldb','../../config/logger','transcribe','TranscribeService','getTranscriptionJob','Comprehend','detectSentiment','substring','info','format','transcribeName','data1','data2','transcribeRegion','then','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s','fileText','TranscriptionJob','TranscriptionJobStatus','TranscriptFileUri','[RECORDING:%d]\x20Get\x20transcript','Transcript','[RECORDING:%d]\x20Transcript\x20downloaded','results','transcripts','length','transcript','update','Sequelize','ValidationError','Sentiment\x20Analysis\x20not\x20requested','sentimentRegion','language','Sentiment\x20Analysis\x20not\x20configured','Transcript\x20not\x20available','sentimentAccountId','CloudProvider','Cloud\x20Provider\x20not\x20available','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','SentimentScore','Positive','Mixed','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','FAILED','resolve','tempSentiment','sentiment'];(function(_0xeaa3bd,_0x166691){var _0x2e1174=function(_0x310580){while(--_0x310580){_0xeaa3bd['push'](_0xeaa3bd['shift']());}};_0x2e1174(++_0x166691);}(_0xc893,0x194));var _0x3c89=function(_0x2fda09,_0x1518d5){_0x2fda09=_0x2fda09-0x0;var _0x371576=_0xc893[_0x2fda09];return _0x371576;};var _=require('lodash');var AWS=require(_0x3c89('0x0'));var rp=require(_0x3c89('0x1'));var util=require('util');var BPromise=require('bluebird');var db=require(_0x3c89('0x2'))['db'];var logger=require(_0x3c89('0x3'))(_0x3c89('0x4'));function getTranscriptionJob(_0xedc679,_0x144af5,_0x120117,_0x1629c8){return new Promise(function(_0x5f2803,_0x2e156d){var _0x1a0ec4=new AWS[(_0x3c89('0x5'))]({'accessKeyId':_0x144af5,'secretAccessKey':_0xedc679,'region':_0x120117});_0x1a0ec4[_0x3c89('0x6')]({'TranscriptionJobName':_0x1629c8},function(_0x524125,_0x251b0c){if(_0x524125){return _0x2e156d(_0x524125);}else{return _0x5f2803(_0x251b0c);}});});}function sentiment(_0x5d09d7,_0x4ffa29,_0x3cfe79,_0x4b8ce9,_0x2c026e){return new Promise(function(_0x161ed1,_0x53ed5c){var _0x182415=new AWS[(_0x3c89('0x7'))]({'accessKeyId':_0x4ffa29,'secretAccessKey':_0x5d09d7,'region':_0x3cfe79});_0x182415[_0x3c89('0x8')]({'Text':_0x4b8ce9,'LanguageCode':_0x2c026e[_0x3c89('0x9')](0x0,0x2)},function(_0xd06b18,_0x3c7c6a){if(_0xd06b18){return _0x53ed5c(_0xd06b18);}else{return _0x161ed1(_0x3c7c6a);}});});}function checkTranscribeJob(_0x3a56df,_0x3bfe2b,_0xda4e85){var _0x13d423;return new BPromise(function(_0x1a9c0b,_0x1e486d){logger[_0x3c89('0xa')](util[_0x3c89('0xb')]('[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)',_0x3a56df['id'],_0x3a56df[_0x3c89('0xc')]));return getTranscriptionJob(_0xda4e85[_0x3c89('0xd')],_0xda4e85[_0x3c89('0xe')],_0x3bfe2b[_0x3c89('0xf')],_0x3a56df[_0x3c89('0xc')])[_0x3c89('0x10')](function(_0xeffc20){logger[_0x3c89('0xa')](util[_0x3c89('0xb')](_0x3c89('0x11'),_0x3a56df['id'],_0x3a56df[_0x3c89('0xc')],_0xeffc20['TranscriptionJob']['TranscriptionJobStatus']));if(!_0x3a56df[_0x3c89('0x12')]){switch(_0xeffc20[_0x3c89('0x13')]['TranscriptionJobStatus']){case'COMPLETED':var _0x425d5c={'transcribeStatus':_0xeffc20[_0x3c89('0x13')][_0x3c89('0x14')],'fileUri':_0xeffc20[_0x3c89('0x13')]['Transcript'][_0x3c89('0x15')]};logger[_0x3c89('0xa')](util[_0x3c89('0xb')](_0x3c89('0x16'),_0x3a56df['id']));return rp({'uri':_0xeffc20[_0x3c89('0x13')][_0x3c89('0x17')][_0x3c89('0x15')],'json':!![]})[_0x3c89('0x10')](function(_0x247732){logger[_0x3c89('0xa')](util[_0x3c89('0xb')](_0x3c89('0x18'),_0x3a56df['id']));if(_0x247732&&_0x247732[_0x3c89('0x19')]&&_0x247732[_0x3c89('0x19')][_0x3c89('0x1a')]&&_0x247732[_0x3c89('0x19')][_0x3c89('0x1a')][_0x3c89('0x1b')]){_0x425d5c[_0x3c89('0x12')]='';for(var _0x462552=0x0;_0x462552<_0x247732['results'][_0x3c89('0x1a')]['length'];_0x462552++){_0x425d5c['fileText']+=_0x247732['results'][_0x3c89('0x1a')][_0x462552][_0x3c89('0x1c')]+'';}}return _0x3a56df[_0x3c89('0x1d')](_0x425d5c);})['then'](function(){logger[_0x3c89('0xa')](util[_0x3c89('0xb')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x3a56df['id']));if(!_0x3a56df['tempSentiment']){throw new db[(_0x3c89('0x1e'))][(_0x3c89('0x1f'))](_0x3c89('0x20'));}if(!_0x3bfe2b['sentiment']||!_0x3bfe2b['sentimentAccountId']||!_0x3bfe2b[_0x3c89('0x21')]||!_0x3bfe2b[_0x3c89('0x22')]||!_0x3bfe2b['bucket']){throw new db[(_0x3c89('0x1e'))][(_0x3c89('0x1f'))](_0x3c89('0x23'));}if(!_0x425d5c[_0x3c89('0x12')]){throw new Error(_0x3c89('0x24'));}if(_0x3bfe2b[_0x3c89('0x25')]===_0xda4e85['id']){return _0xda4e85;}else{return db[_0x3c89('0x26')]['findOne']({'where':{'id':_settings[_0x3c89('0x25')]}});}})[_0x3c89('0x10')](function(_0x3fcb76){if(!_0x3fcb76){throw new db[(_0x3c89('0x1e'))]['ValidationError'](_0x3c89('0x27'));}if(!_0x3fcb76['data1']||!_0x3fcb76[_0x3c89('0xe')]){throw new db[(_0x3c89('0x1e'))][(_0x3c89('0x1f'))]('Cloud\x20Provider\x20not\x20configured');}_0x13d423=_0x3fcb76;logger[_0x3c89('0xa')](util['format'](_0x3c89('0x28'),_0x3a56df['id']));return sentiment(_0x13d423[_0x3c89('0xd')],_0x13d423['data2'],_0x3bfe2b[_0x3c89('0x21')],_0x425d5c[_0x3c89('0x12')],_0x3bfe2b[_0x3c89('0x22')]);})[_0x3c89('0x10')](function(_0x151072){logger[_0x3c89('0xa')](util['format'](_0x3c89('0x29'),_0x3a56df['id']));return _0x3a56df[_0x3c89('0x1d')]({'sentiment':_0x151072[_0x3c89('0x2a')],'sPositive':_0x151072[_0x3c89('0x2b')][_0x3c89('0x2c')],'sNegative':_0x151072[_0x3c89('0x2b')]['Negative'],'sMixed':_0x151072[_0x3c89('0x2b')][_0x3c89('0x2d')],'sNeutral':_0x151072[_0x3c89('0x2b')]['Neutral']});})[_0x3c89('0x10')](function(){logger[_0x3c89('0xa')](util[_0x3c89('0xb')](_0x3c89('0x2e'),_0x3a56df['id']));_0x1a9c0b();});case _0x3c89('0x2f'):return _0x3a56df[_0x3c89('0x1d')]({'transcribeStatus':_0x3c89('0x2f'),'failureReason':_0xeffc20[_0x3c89('0x13')]['FailureReason']})['then'](function(){logger[_0x3c89('0xa')](util[_0x3c89('0xb')](_0x3c89('0x2e'),_0x3a56df['id']));_0x1a9c0b();});default:logger['info'](util[_0x3c89('0xb')]('[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed',_0x3a56df['id']));break;}}else{return BPromise[_0x3c89('0x30')]()[_0x3c89('0x10')](function(){if(!_0x3a56df[_0x3c89('0x31')]){throw new db[(_0x3c89('0x1e'))]['ValidationError'](_0x3c89('0x20'));}if(!_0x3bfe2b[_0x3c89('0x32')]||!_0x3bfe2b['sentimentAccountId']||!_0x3bfe2b['sentimentRegion']||!_0x3bfe2b['language']||!_0x3bfe2b['bucket']){throw new db['Sequelize'][(_0x3c89('0x1f'))](_0x3c89('0x23'));}if(!_0x3a56df[_0x3c89('0x12')]){throw new Error(_0x3c89('0x24'));}if(_0x3bfe2b[_0x3c89('0x25')]===_0xda4e85['id']){return _0xda4e85;}else{return db[_0x3c89('0x26')][_0x3c89('0x33')]({'where':{'id':_settings[_0x3c89('0x25')]}});}})[_0x3c89('0x10')](function(_0x5ee1da){if(!_0x5ee1da){throw new db[(_0x3c89('0x1e'))][(_0x3c89('0x1f'))]('Cloud\x20Provider\x20not\x20available');}if(!_0x5ee1da[_0x3c89('0xd')]||!_0x5ee1da[_0x3c89('0xe')]){throw new db['Sequelize'][(_0x3c89('0x1f'))]('Cloud\x20Provider\x20not\x20configured');}_0x13d423=_0x5ee1da;logger[_0x3c89('0xa')](util['format'](_0x3c89('0x28'),_0x3a56df['id']));return sentiment(_0x13d423[_0x3c89('0xd')],_0x13d423[_0x3c89('0xe')],_0x3bfe2b[_0x3c89('0x21')],_0x3a56df['fileText'],_0x3bfe2b[_0x3c89('0x22')]);})[_0x3c89('0x10')](function(_0x2f1097){logger['info'](util['format'](_0x3c89('0x29'),_0x3a56df['id']));return _0x3a56df['update']({'sentiment':_0x2f1097['Sentiment'],'sPositive':_0x2f1097[_0x3c89('0x2b')]['Positive'],'sNegative':_0x2f1097[_0x3c89('0x2b')][_0x3c89('0x34')],'sMixed':_0x2f1097[_0x3c89('0x2b')][_0x3c89('0x2d')],'sNeutral':_0x2f1097['SentimentScore'][_0x3c89('0x35')],'transcribeStatus':'COMPLETED'});})[_0x3c89('0x10')](function(){logger[_0x3c89('0xa')](util[_0x3c89('0xb')](_0x3c89('0x2e'),_0x3a56df['id']));_0x1a9c0b();});}})['catch'](function(_0x3c4548){if(_0x3c4548){if(!_0x3c4548[_0x3c89('0x36')]||_0x3c4548[_0x3c89('0x36')]!==_0x3c89('0x37')){logger[_0x3c89('0x38')](util[_0x3c89('0xb')]('[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis',_0x3a56df['id']),util[_0x3c89('0x39')](_0x3c4548,{'showHidden':![],'depth':null}));}}_0x1a9c0b();});});}function checkTranscribe(){var _0x54e1a7;var _0x33bd3c;var _0x598817;return db[_0x3c89('0x3a')]['findAll']({'where':{'transcribeStatus':'IN_PROGRESS'}})[_0x3c89('0x10')](function(_0x49d356){if(!_0x49d356||!_0x49d356[_0x3c89('0x1b')]){throw new db[(_0x3c89('0x1e'))]['ValidationError']('No\x20transcribe\x20job\x20to\x20check');}_0x54e1a7=_0x49d356;return db[_0x3c89('0x3b')][_0x3c89('0x33')]({'where':{'id':0x1}});})[_0x3c89('0x10')](function(_0x39cf2b){if(!_0x39cf2b){throw new db[(_0x3c89('0x1e'))][(_0x3c89('0x1f'))]('Settings\x20not\x20available');}_0x33bd3c=_0x39cf2b;if(!_0x33bd3c['transcribe']||!_0x33bd3c[_0x3c89('0x3c')]||!_0x33bd3c[_0x3c89('0xf')]||!_0x33bd3c[_0x3c89('0x22')]||!_0x33bd3c[_0x3c89('0x3d')]){throw new db[(_0x3c89('0x1e'))]['ValidationError']('Transcribe\x20not\x20configured');}return db['CloudProvider'][_0x3c89('0x33')]({'where':{'id':_0x33bd3c[_0x3c89('0x3c')]}});})[_0x3c89('0x10')](function(_0x49ec7e){if(!_0x49ec7e){throw new db[(_0x3c89('0x1e'))]['ValidationError']('Cloud\x20Provider\x20not\x20available');}if(!_0x49ec7e[_0x3c89('0xd')]||!_0x49ec7e['data2']){throw new db[(_0x3c89('0x1e'))][(_0x3c89('0x1f'))](_0x3c89('0x3e'));}_0x598817=_0x49ec7e;var _0x46d61b=[];_0x54e1a7['forEach'](function(_0x1c2caa){_0x46d61b['push'](checkTranscribeJob(_0x1c2caa,_0x33bd3c,_0x598817));});return BPromise[_0x3c89('0x3f')](_0x46d61b);})[_0x3c89('0x40')](function(_0x4388ca){if(_0x4388ca){if(!_0x4388ca[_0x3c89('0x36')]||_0x4388ca['name']!==_0x3c89('0x37')){logger['error'](util['inspect'](_0x4388ca,{'showHidden':![],'depth':null}));}}});}exports['start']=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};