Advertisement
Guest User

Untitled

a guest
Aug 1st, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable no-console, no-shadow, valid-jsdoc */
  2.  
  3. var _ = require('underscore');
  4. var schedule = require('node-schedule');
  5. var moment = require('moment');
  6. var async = require('async');
  7. var mysql = require('mysql');
  8. var e164 = require('e164');
  9.  
  10. var dateFormat = 'YYYY-MM-DD HH';
  11.  
  12. var cronSchedules = {
  13.   everyOneMinute: '*/1 * * * *',
  14.   everyTenSeconds: '*/9 * * * *',
  15.   everyFiveSeconds: '*/5 * * * * *',
  16.   everyOneHour: '* */1 * * *',
  17.   everyTwoSeconds: '*/2 * * * * *',
  18.   everyOneSecond: '*/1 * * * * *',
  19.   onceToday: '0 5 11 28 6 *' // testing - One time Cron Job
  20. };
  21.  
  22. // var pool = mysql.createPool({
  23. //   connectionLimit: 100,
  24. //   waitForConnections: true,
  25. //   queueLimit: 0,
  26. //   host: 'localhost',
  27. //   port: '3306',
  28. //   user: 'root',
  29. //   password: ')(*&*((*&^',
  30. //   database: 'radius',
  31. //   acquireTimeout: 1000000,
  32. //   supportBigNumbers: true,
  33. //   insecureAuth: true,
  34. //   debug: false,
  35. //   waitTimeOut: 28800
  36. // });
  37.  
  38. var pool = (process.env.NODE_ENV !== 'production')
  39.   ? mysql.createPool({
  40.     connectionLimit: 100,
  41.     waitForConnections: true,
  42.     queueLimit: 0,
  43.     host: '10.10.0.100',
  44.     port: '3306',
  45.     user: 'b2bloguser',
  46.     password: '(*(*(*(*(*(*(*(*(',
  47.     database: 'campaign_logs',
  48.     acquireTimeout: 1000000,
  49.     supportBigNumbers: true,
  50.     insecureAuth: true,
  51.     debug: false,
  52.     waitTimeOut: 28800,
  53.     useTransaction: {
  54.       connectionLimit: 1
  55.     }
  56.   })
  57.   : mysql.createPool({
  58.     connectionLimit: 100,
  59.     waitForConnections: true,
  60.     queueLimit: 0,
  61.     host: '10.50.0.102',
  62.     port: '3306',
  63.     user: 'b2bloguser',
  64.     password: '()()()()()()()()()()(',
  65.     database: 'campaign_logs',
  66.     acquireTimeout: 1000000,
  67.     supportBigNumbers: true,
  68.     insecureAuth: true,
  69.     debug: false,
  70.     waitTimeOut: 28800,
  71.     useTransaction: {
  72.       connectionLimit: 1
  73.     }
  74.   });
  75.  
  76. // recurrence rule so CRON JOB Executes every hour at 6 minutes after the hour
  77. // var rule = new schedule.RecurrenceRule();
  78. // // rule.minute = 6;
  79. // rule.second = 6;
  80.  
  81. console.log('Queued CRON Job and will execute on the 6th minute past the hour');
  82. schedule.scheduleJob(cronSchedules.everyFiveSeconds, function () {
  83.   var queryHour = moment.utc(new Date()).valueOf();
  84.   var hourBefore = subtractHourToMilli(queryHour);
  85.   return queryAll(hourBefore, queryHour, pool);
  86. });
  87.  
  88. /**
  89.  * @param {moment obj} hourBefore
  90.  * @param {moment obj} queryHour
  91.  * @param {mysql obj}  pool
  92.  */
  93. function queryAll(hourBefore, queryHour, pool) {
  94.   var CLQ = new CampaignLogQueries(hourBefore, queryHour, pool);
  95.  
  96.   async.series({
  97.     impressionsPaid: function (callback) {
  98.       CLQ.queryImpressionsPaid(function (results) {
  99.         return callback(null, results);
  100.       });
  101.     },
  102.     impressionsFree: function (callback) {
  103.       CLQ.queryImpressionsFree(function (results) {
  104.         callback(null, results);
  105.       });
  106.     },
  107.     expressions: function (callback) {
  108.       CLQ.queryExpressions(function (results) {
  109.         callback(null, results);
  110.       });
  111.     },
  112.     share: function (callback) {
  113.       CLQ.queryShare(function (results) {
  114.         callback(null, results);
  115.       });
  116.     },
  117.     shareSN: function (callback) {
  118.       CLQ.queryShareSN(function (results) {
  119.         callback(null, results);
  120.       });
  121.     },
  122.     comments: function (callback) {
  123.       CLQ.queryComments(function (results) {
  124.         callback(null, results);
  125.       });
  126.     },
  127.     viewedTimeAvg: function (callback) {
  128.       CLQ.queryViewedTimeAvg(function (results) {
  129.         callback(null, results);
  130.       });
  131.     },
  132.     callToAction: function (callback) {
  133.       CLQ.queryCallToAction(function (results) {
  134.         callback(null, results);
  135.       });
  136.     }
  137.   },
  138.     function (err, results) {
  139.       if (err) return console.log(err);
  140.       console.log(results);
  141.       return console.log(JSON.stringify(CLQ.parseData(results), null, 2));
  142.     });
  143. }
  144.  
  145.  
  146. /**
  147.  * Query Factory Constructor
  148.  * @param {moment obj} hourBefore
  149.  * @param {moment obj} queryHour
  150.  * @param {mysql obj}  pool
  151.  */
  152. function CampaignLogQueries(hourBefore, queryHour, pool) {
  153.   this.hourBefore = hourBefore;
  154.   this.queryHour = queryHour;
  155.   this.pool = pool;
  156.  
  157.   /**
  158.    * @param {func} outerCallback
  159.    */
  160.   this.queryImpressionsPaid = function (outerCallback) {
  161.     var msg = {
  162.       queryHour: this.queryHour,
  163.       hourBefore: this.hourBefore,
  164.       pool: this.pool
  165.     };
  166.  
  167.     this.pool.getConnection(function (err, connection) {
  168.       if (err) return outerCallback(err);
  169.       var storedProcedure = 'CALL statistics_be_count_all_impressions_paid_grouped_by_country(\'2016-07-21 22:00:00\', \'2016-07-21 23:00:00\')';
  170.       return connection.query(storedProcedure,
  171.         function (err, rows, field) {
  172.           if (err) return outerCallback(err);
  173.           connection.release();
  174.           return outerCallback(rows[0]);
  175.         });
  176.     });
  177.   };
  178.  
  179.   /**
  180.    * @param {func} outerCallback
  181.    */
  182.   this.queryImpressionsFree = function (outerCallback) {
  183.     var msg = {
  184.       queryHour: this.queryHour,
  185.       hourBefore: this.hourBefore,
  186.       pool: this.pool
  187.     };
  188.  
  189.     this.pool.getConnection(function (err, connection) {
  190.       if (err) return outerCallback(err);
  191.       var storedProcedure = 'CALL statistics_be_count_all_impressions_free_grouped_by_country(\'2016-07-21 22:00:00\', \'2016-07-21 23:00:00\')';
  192.       return connection.query(storedProcedure,
  193.         function (err, rows, field) {
  194.           if (err) return outerCallback(err);
  195.           connection.release();
  196.           return outerCallback(rows[0]);
  197.         });
  198.     });
  199.   };
  200.   /**
  201.    * @param {func} outerCallback
  202.    */
  203.   this.queryExpressions = function (outerCallback) {
  204.     var msg = {
  205.       queryHour: this.queryHour,
  206.       hourBefore: this.hourBefore,
  207.       pool: this.pool
  208.     };
  209.  
  210.     this.pool.getConnection(function (err, connection) {
  211.       if (err) return outerCallback(err);
  212.       var storedProcedure = 'CALL statistics_be_count_all_expressions_grouped_by_country(\'2016-07-01 13:00:00\', \'2016-07-01 16:00:00\')';
  213.       return connection.query(storedProcedure,
  214.         function (err, rows, field) {
  215.           if (err) return outerCallback(err);
  216.           connection.release();
  217.           return outerCallback(rows[0]);
  218.         });
  219.     });
  220.   };
  221.  
  222.   /**
  223.    * @param {func} outerCallback
  224.    */
  225.   this.queryShare = function (outerCallback) {
  226.     var msg = {
  227.       queryHour: this.queryHour,
  228.       hourBefore: this.hourBefore,
  229.       pool: this.pool
  230.     };
  231.     this.pool.getConnection(function (err, connection) {
  232.       if (err) return outerCallback(err);
  233.       var storedProcedure = 'CALL statistics_be_count_all_share_grouped_by_country(\'2016-07-01 13:00:00\', \'2016-07-01 16:00:00\')';
  234.       return connection.query(storedProcedure,
  235.         function (err, rows, field) {
  236.           if (err) return outerCallback(err);
  237.           connection.release();
  238.           return outerCallback(rows[0]);
  239.         });
  240.     });
  241.     // return outerCallback(msg);
  242.   };
  243.  
  244.   /**
  245.    * @param {func} outerCallback
  246.    */
  247.   this.queryShareSN = function (outerCallback) {
  248.     var msg = {
  249.       queryHour: this.queryHour,
  250.       hourBefore: this.hourBefore,
  251.       pool: this.pool
  252.     };
  253.  
  254.     this.pool.getConnection(function (err, connection) {
  255.       if (err) return outerCallback(err);
  256.       var storedProcedure = 'CALL statistics_be_count_all_share_sn_grouped_by_country(\'2016-07-01 13:00:00\', \'2016-07-01 16:00:00\')';
  257.       return connection.query(storedProcedure,
  258.         function (err, rows, field) {
  259.           if (err) return outerCallback(err);
  260.           connection.release();
  261.           return outerCallback(rows[0]);
  262.         });
  263.     });
  264.   };
  265.  
  266.   /**
  267.    * @param {func} outerCallback
  268.    */
  269.   this.queryComments = function (outerCallback) {
  270.     var msg = {
  271.       queryHour: this.queryHour,
  272.       hourBefore: this.hourBefore,
  273.       pool: this.pool
  274.     };
  275.  
  276.     this.pool.getConnection(function (err, connection) {
  277.       if (err) return outerCallback(err);
  278.       return connection.query('CALL statistics_be_count_all_comments_grouped_by_country(\'2016-04-10 00:00:00\', \'2016-04-10 05:00:00\')',
  279.         function (err, rows, field) {
  280.           if (err) return outerCallback(err);
  281.           connection.release();
  282.           return outerCallback(rows[0]);
  283.         });
  284.     });
  285.   };
  286.  
  287.   /**
  288.    * @param {func} outerCallback
  289.    */
  290.   this.queryViewedTimeAvg = function (outerCallback) {
  291.     var msg = {
  292.       queryHour: this.queryHour,
  293.       hourBefore: this.hourBefore,
  294.       pool: this.pool
  295.     };
  296.  
  297.     this.pool.getConnection(function (err, connection) {
  298.       if (err) return outerCallback(err);
  299.       return connection.query('CALL statistics_be_calculate_all_viewtime_avg_grouped_by_country(\'2016-07-21 22:00:00\', \'2016-07-21 23:00:00\')',
  300.         function (err, rows, field) {
  301.           if (err) return outerCallback(err);
  302.           connection.release();
  303.           return outerCallback(rows[0]);
  304.         });
  305.     });
  306.   };
  307.  
  308.   /**
  309.    * @param {func} outerCallback
  310.    */
  311.   this.queryCallToAction = function (outerCallback) {
  312.     var msg = {
  313.       queryHour: this.queryHour,
  314.       hourBefore: this.hourBefore,
  315.       pool: this.pool
  316.     };
  317.  
  318.     this.pool.getConnection(function (err, connection) {
  319.       if (err) return outerCallback(err);
  320.       return connection.query('CALL statistics_be_count_all_call_to_actions_grouped_by_country(\'2016-03-24 12:43:18\', \'2016-03-24 20:00:00\')',
  321.         function (err, rows, field) {
  322.           if (err) return outerCallback(err);
  323.           connection.release();
  324.           return outerCallback(rows[0]);
  325.         });
  326.     });
  327.  
  328.     this.parseData = function (data) {
  329.       var fromMilliToUTCMomentKey = moment.utc(this.queryHour).format('YYYY-MM-DD HH');
  330.       // treat created as the key by which we will store to DB
  331.       var groupImpressionsPaidByCreated = _.groupBy(data.impressionsPaid, 'created');
  332.       var groupImpressionsFreeByCreated = _.groupBy(data.impressionsFree, 'created');
  333.       var groupExpressionsByCreated = _.groupBy(data.impressionsFree, 'created');
  334.       var groupViewedTimeAvgByCreated = _.groupBy(data.viewedTimeAvg, 'created');
  335.       var groupCallToActionByCreated = _.groupBy(data.callToAction, 'created');
  336.       var groupedCommentsByCreated = _.groupBy(data.comments, 'created');
  337.       var groupShareSNByCreated = _.groupBy(data.shareSN, 'created');
  338.       var groupShareByCreated = _.groupBy(data.share, 'created');
  339.       var finalObj = {};
  340.  
  341.       finalObj[fromMilliToUTCMomentKey] = {
  342.         impressions_paid: [],
  343.         impressions_free: [],
  344.         share: [],
  345.         share_sn: [],
  346.         express: [],
  347.         comments: [],
  348.         viewedTimeAvg: [],
  349.         callToActions: []
  350.       };
  351.  
  352.       var refToFinalObjKey = finalObj[fromMilliToUTCMomentKey];
  353.  
  354.       if (groupImpressionsPaidByCreated.length > 1) {
  355.         groupImpressionsPaidByCreated.map(function (item) {
  356.           refToFinalObjKey.impressions_paid.push(item);
  357.         });
  358.       }
  359.  
  360.       if (groupImpressionsFreeByCreated.length > 1) {
  361.         groupImpressionsFreeByCreated.map(function (item) {
  362.           refToFinalObjKey.impressions_free.push(item);
  363.         });
  364.       }
  365.  
  366.       if (groupShareByCreated.length > 1) {
  367.         groupShareByCreated.map(function (item) {
  368.           refToFinalObjKey.share.push(item);
  369.         });
  370.       }
  371.  
  372.       if (groupShareSNByCreated.length > 1) {
  373.         groupShareByCreated.map(function (item) {
  374.           refToFinalObjKey.share_sn.push(item);
  375.         });
  376.       }
  377.  
  378.       if (groupExpressionsByCreated.length > 1) {
  379.         groupShareByCreated.map(function (item) {
  380.           refToFinalObjKey.express.push(item);
  381.         });
  382.       }
  383.  
  384.       if (groupedCommentsByCreated.length > 1) {
  385.         groupedCommentsByCreated.map(function (item) {
  386.           refToFinalObjKey.comments.push(item);
  387.         });
  388.       }
  389.  
  390.       if (groupViewedTimeAvgByCreated.length > 1) {
  391.         groupViewedTimeAvgByCreated.map(function (item) {
  392.           refToFinalObjKey.viewedTimeAvg.push(item);
  393.         });
  394.       }
  395.  
  396.       if (groupCallToActionByCreated.length > 1) {
  397.         groupCallToActionByCreated.map(function (item) {
  398.           refToFinalObjKey.viewedTimeAvg.push(item);
  399.         });
  400.       }
  401.  
  402.       return finalObj;
  403.     };
  404.   };
  405. }
  406.  
  407.  
  408. /**
  409.  * @param {moment obj} _moment
  410.  * @returns
  411.  */
  412. function subtractHourToMilli(_moment) {
  413.   return moment.utc(_moment).subtract(1, 'hour').valueOf();
  414. }
  415.  
  416. /**
  417.  * @param {moment obj} _moment
  418.  * @returns
  419.  */
  420. function addHourToMilli(_moment) {
  421.   return moment.utc(_moment).add(1, 'hour').valueOf();
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement