Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* eslint-disable no-console, no-shadow, valid-jsdoc */
- var _ = require('underscore');
- var schedule = require('node-schedule');
- var moment = require('moment');
- var async = require('async');
- var mysql = require('mysql');
- var e164 = require('e164');
- var dateFormat = 'YYYY-MM-DD HH';
- var cronSchedules = {
- everyOneMinute: '*/1 * * * *',
- everyTenSeconds: '*/9 * * * *',
- everyFiveSeconds: '*/5 * * * * *',
- everyOneHour: '* */1 * * *',
- everyTwoSeconds: '*/2 * * * * *',
- everyOneSecond: '*/1 * * * * *',
- onceToday: '0 5 11 28 6 *' // testing - One time Cron Job
- };
- // var pool = mysql.createPool({
- // connectionLimit: 100,
- // waitForConnections: true,
- // queueLimit: 0,
- // host: 'localhost',
- // port: '3306',
- // user: 'root',
- // password: ')(*&*((*&^',
- // database: 'radius',
- // acquireTimeout: 1000000,
- // supportBigNumbers: true,
- // insecureAuth: true,
- // debug: false,
- // waitTimeOut: 28800
- // });
- var pool = (process.env.NODE_ENV !== 'production')
- ? mysql.createPool({
- connectionLimit: 100,
- waitForConnections: true,
- queueLimit: 0,
- host: '10.10.0.100',
- port: '3306',
- user: 'b2bloguser',
- password: '(*(*(*(*(*(*(*(*(',
- database: 'campaign_logs',
- acquireTimeout: 1000000,
- supportBigNumbers: true,
- insecureAuth: true,
- debug: false,
- waitTimeOut: 28800,
- useTransaction: {
- connectionLimit: 1
- }
- })
- : mysql.createPool({
- connectionLimit: 100,
- waitForConnections: true,
- queueLimit: 0,
- host: '10.50.0.102',
- port: '3306',
- user: 'b2bloguser',
- password: '()()()()()()()()()()(',
- database: 'campaign_logs',
- acquireTimeout: 1000000,
- supportBigNumbers: true,
- insecureAuth: true,
- debug: false,
- waitTimeOut: 28800,
- useTransaction: {
- connectionLimit: 1
- }
- });
- // recurrence rule so CRON JOB Executes every hour at 6 minutes after the hour
- // var rule = new schedule.RecurrenceRule();
- // // rule.minute = 6;
- // rule.second = 6;
- console.log('Queued CRON Job and will execute on the 6th minute past the hour');
- schedule.scheduleJob(cronSchedules.everyFiveSeconds, function () {
- var queryHour = moment.utc(new Date()).valueOf();
- var hourBefore = subtractHourToMilli(queryHour);
- return queryAll(hourBefore, queryHour, pool);
- });
- /**
- * @param {moment obj} hourBefore
- * @param {moment obj} queryHour
- * @param {mysql obj} pool
- */
- function queryAll(hourBefore, queryHour, pool) {
- var CLQ = new CampaignLogQueries(hourBefore, queryHour, pool);
- async.series({
- impressionsPaid: function (callback) {
- CLQ.queryImpressionsPaid(function (results) {
- return callback(null, results);
- });
- },
- impressionsFree: function (callback) {
- CLQ.queryImpressionsFree(function (results) {
- callback(null, results);
- });
- },
- expressions: function (callback) {
- CLQ.queryExpressions(function (results) {
- callback(null, results);
- });
- },
- share: function (callback) {
- CLQ.queryShare(function (results) {
- callback(null, results);
- });
- },
- shareSN: function (callback) {
- CLQ.queryShareSN(function (results) {
- callback(null, results);
- });
- },
- comments: function (callback) {
- CLQ.queryComments(function (results) {
- callback(null, results);
- });
- },
- viewedTimeAvg: function (callback) {
- CLQ.queryViewedTimeAvg(function (results) {
- callback(null, results);
- });
- },
- callToAction: function (callback) {
- CLQ.queryCallToAction(function (results) {
- callback(null, results);
- });
- }
- },
- function (err, results) {
- if (err) return console.log(err);
- console.log(results);
- return console.log(JSON.stringify(CLQ.parseData(results), null, 2));
- });
- }
- /**
- * Query Factory Constructor
- * @param {moment obj} hourBefore
- * @param {moment obj} queryHour
- * @param {mysql obj} pool
- */
- function CampaignLogQueries(hourBefore, queryHour, pool) {
- this.hourBefore = hourBefore;
- this.queryHour = queryHour;
- this.pool = pool;
- /**
- * @param {func} outerCallback
- */
- this.queryImpressionsPaid = function (outerCallback) {
- var msg = {
- queryHour: this.queryHour,
- hourBefore: this.hourBefore,
- pool: this.pool
- };
- this.pool.getConnection(function (err, connection) {
- if (err) return outerCallback(err);
- var storedProcedure = 'CALL statistics_be_count_all_impressions_paid_grouped_by_country(\'2016-07-21 22:00:00\', \'2016-07-21 23:00:00\')';
- return connection.query(storedProcedure,
- function (err, rows, field) {
- if (err) return outerCallback(err);
- connection.release();
- return outerCallback(rows[0]);
- });
- });
- };
- /**
- * @param {func} outerCallback
- */
- this.queryImpressionsFree = function (outerCallback) {
- var msg = {
- queryHour: this.queryHour,
- hourBefore: this.hourBefore,
- pool: this.pool
- };
- this.pool.getConnection(function (err, connection) {
- if (err) return outerCallback(err);
- var storedProcedure = 'CALL statistics_be_count_all_impressions_free_grouped_by_country(\'2016-07-21 22:00:00\', \'2016-07-21 23:00:00\')';
- return connection.query(storedProcedure,
- function (err, rows, field) {
- if (err) return outerCallback(err);
- connection.release();
- return outerCallback(rows[0]);
- });
- });
- };
- /**
- * @param {func} outerCallback
- */
- this.queryExpressions = function (outerCallback) {
- var msg = {
- queryHour: this.queryHour,
- hourBefore: this.hourBefore,
- pool: this.pool
- };
- this.pool.getConnection(function (err, connection) {
- if (err) return outerCallback(err);
- var storedProcedure = 'CALL statistics_be_count_all_expressions_grouped_by_country(\'2016-07-01 13:00:00\', \'2016-07-01 16:00:00\')';
- return connection.query(storedProcedure,
- function (err, rows, field) {
- if (err) return outerCallback(err);
- connection.release();
- return outerCallback(rows[0]);
- });
- });
- };
- /**
- * @param {func} outerCallback
- */
- this.queryShare = function (outerCallback) {
- var msg = {
- queryHour: this.queryHour,
- hourBefore: this.hourBefore,
- pool: this.pool
- };
- this.pool.getConnection(function (err, connection) {
- if (err) return outerCallback(err);
- var storedProcedure = 'CALL statistics_be_count_all_share_grouped_by_country(\'2016-07-01 13:00:00\', \'2016-07-01 16:00:00\')';
- return connection.query(storedProcedure,
- function (err, rows, field) {
- if (err) return outerCallback(err);
- connection.release();
- return outerCallback(rows[0]);
- });
- });
- // return outerCallback(msg);
- };
- /**
- * @param {func} outerCallback
- */
- this.queryShareSN = function (outerCallback) {
- var msg = {
- queryHour: this.queryHour,
- hourBefore: this.hourBefore,
- pool: this.pool
- };
- this.pool.getConnection(function (err, connection) {
- if (err) return outerCallback(err);
- var storedProcedure = 'CALL statistics_be_count_all_share_sn_grouped_by_country(\'2016-07-01 13:00:00\', \'2016-07-01 16:00:00\')';
- return connection.query(storedProcedure,
- function (err, rows, field) {
- if (err) return outerCallback(err);
- connection.release();
- return outerCallback(rows[0]);
- });
- });
- };
- /**
- * @param {func} outerCallback
- */
- this.queryComments = function (outerCallback) {
- var msg = {
- queryHour: this.queryHour,
- hourBefore: this.hourBefore,
- pool: this.pool
- };
- this.pool.getConnection(function (err, connection) {
- if (err) return outerCallback(err);
- return connection.query('CALL statistics_be_count_all_comments_grouped_by_country(\'2016-04-10 00:00:00\', \'2016-04-10 05:00:00\')',
- function (err, rows, field) {
- if (err) return outerCallback(err);
- connection.release();
- return outerCallback(rows[0]);
- });
- });
- };
- /**
- * @param {func} outerCallback
- */
- this.queryViewedTimeAvg = function (outerCallback) {
- var msg = {
- queryHour: this.queryHour,
- hourBefore: this.hourBefore,
- pool: this.pool
- };
- this.pool.getConnection(function (err, connection) {
- if (err) return outerCallback(err);
- 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\')',
- function (err, rows, field) {
- if (err) return outerCallback(err);
- connection.release();
- return outerCallback(rows[0]);
- });
- });
- };
- /**
- * @param {func} outerCallback
- */
- this.queryCallToAction = function (outerCallback) {
- var msg = {
- queryHour: this.queryHour,
- hourBefore: this.hourBefore,
- pool: this.pool
- };
- this.pool.getConnection(function (err, connection) {
- if (err) return outerCallback(err);
- 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\')',
- function (err, rows, field) {
- if (err) return outerCallback(err);
- connection.release();
- return outerCallback(rows[0]);
- });
- });
- this.parseData = function (data) {
- var fromMilliToUTCMomentKey = moment.utc(this.queryHour).format('YYYY-MM-DD HH');
- // treat created as the key by which we will store to DB
- var groupImpressionsPaidByCreated = _.groupBy(data.impressionsPaid, 'created');
- var groupImpressionsFreeByCreated = _.groupBy(data.impressionsFree, 'created');
- var groupExpressionsByCreated = _.groupBy(data.impressionsFree, 'created');
- var groupViewedTimeAvgByCreated = _.groupBy(data.viewedTimeAvg, 'created');
- var groupCallToActionByCreated = _.groupBy(data.callToAction, 'created');
- var groupedCommentsByCreated = _.groupBy(data.comments, 'created');
- var groupShareSNByCreated = _.groupBy(data.shareSN, 'created');
- var groupShareByCreated = _.groupBy(data.share, 'created');
- var finalObj = {};
- finalObj[fromMilliToUTCMomentKey] = {
- impressions_paid: [],
- impressions_free: [],
- share: [],
- share_sn: [],
- express: [],
- comments: [],
- viewedTimeAvg: [],
- callToActions: []
- };
- var refToFinalObjKey = finalObj[fromMilliToUTCMomentKey];
- if (groupImpressionsPaidByCreated.length > 1) {
- groupImpressionsPaidByCreated.map(function (item) {
- refToFinalObjKey.impressions_paid.push(item);
- });
- }
- if (groupImpressionsFreeByCreated.length > 1) {
- groupImpressionsFreeByCreated.map(function (item) {
- refToFinalObjKey.impressions_free.push(item);
- });
- }
- if (groupShareByCreated.length > 1) {
- groupShareByCreated.map(function (item) {
- refToFinalObjKey.share.push(item);
- });
- }
- if (groupShareSNByCreated.length > 1) {
- groupShareByCreated.map(function (item) {
- refToFinalObjKey.share_sn.push(item);
- });
- }
- if (groupExpressionsByCreated.length > 1) {
- groupShareByCreated.map(function (item) {
- refToFinalObjKey.express.push(item);
- });
- }
- if (groupedCommentsByCreated.length > 1) {
- groupedCommentsByCreated.map(function (item) {
- refToFinalObjKey.comments.push(item);
- });
- }
- if (groupViewedTimeAvgByCreated.length > 1) {
- groupViewedTimeAvgByCreated.map(function (item) {
- refToFinalObjKey.viewedTimeAvg.push(item);
- });
- }
- if (groupCallToActionByCreated.length > 1) {
- groupCallToActionByCreated.map(function (item) {
- refToFinalObjKey.viewedTimeAvg.push(item);
- });
- }
- return finalObj;
- };
- };
- }
- /**
- * @param {moment obj} _moment
- * @returns
- */
- function subtractHourToMilli(_moment) {
- return moment.utc(_moment).subtract(1, 'hour').valueOf();
- }
- /**
- * @param {moment obj} _moment
- * @returns
- */
- function addHourToMilli(_moment) {
- return moment.utc(_moment).add(1, 'hour').valueOf();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement