Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const AWS = require('aws-sdk');
  4. const mysql = require('mysql');
  5. const async = require('async');
  6. const connection = mysql.createConnection({
  7.   host: process.env.MYSQL_HOST,
  8.   user: process.env.MYSQL_USER,
  9.   password: process.env.MYSQL_PASSWORD,
  10.   database: process.env.MYSQL_DATABASE_NAME
  11. });
  12. const dynamodb = new AWS.DynamoDB.DocumentClient();
  13.  
  14. function getResult(sql) {
  15.   return new Promise(function(resolve, reject){
  16.     connection.query(sql, function(err, result){
  17.       if(err){
  18.         console.log(err);
  19.         reject(err);
  20.       }else{
  21.         resolve(result);
  22.       }
  23.     })
  24.   })
  25. }
  26.  
  27. let updateDevAppUsage = function updateUser(appid, mcuIn, mcuOut, turnIn, turnOut) {
  28.   // apparently we sum turn usage on dynamo
  29.   let turn = parseInt(turnIn) + parseInt(turnOut);
  30.  
  31.   console.log('Updating ' + appid + ' with mcuIn:' + mcuIn + ' mcuOut: ' + mcuOut + ' turn: ' + turn);
  32.  
  33.   let params = {
  34.     TableName: 'devapps',
  35.     Key: { 'appid': appid },
  36.     UpdateExpression: 'set #mcuIn = :mcuIn, #mcuOut = :mcuOut, #turn = :turn',
  37.     ConditionExpression: '#appid = :appid',
  38.     ExpressionAttributeNames: {
  39.       '#appid': 'appid',
  40.       '#mcuIn': 'mcuInboundMonthly',
  41.       '#mcuOut': 'mcuOutboundMonthly',
  42.       '#turn': 'turnUsageMonthly'
  43.     },
  44.     ExpressionAttributeValues: {
  45.       ':appid': appid,
  46.       ':mcuIn': mcuIn,
  47.       ':mcuOut': mcuOut,
  48.       ':turn': turn
  49.     }
  50.   };
  51.  
  52.   console.log('Updating dynamo...');
  53.  
  54.   return dynamodb.update(params).promise();
  55. };
  56.  
  57. // --------------- Main handler -----------------------
  58.  
  59. exports.handler = (event, context, callback) => {
  60.   try {
  61.     // connect to MySQL
  62.     connection.connect();
  63.  
  64.     // we have 1,440 minutes in a day.
  65.     // we have around < 14,500 keys
  66.     // adjust number of keys depending on how much
  67.     // we want to process per minute
  68.     let numOfKeysToProcess = process.env.NUM_OF_KEYS_TO_PROCESS || 10;
  69.     let time = new Date();
  70.     let month = time.getMonth() + 1;
  71.     let year = time.getFullYear();
  72.  
  73.     let minutesFromMidnight = time.getMinutes() + (60 * time.getHours());
  74.     let lowerIdRange = 6708;//(numOfKeysToProcess * minutesFromMidnight) - numOfKeysToProcess + 1;
  75.     let higherIdRange = 6711; //(numOfKeysToProcess * minutesFromMidnight);
  76.  
  77.     console.log('Doing IDs from ' + lowerIdRange + ' to ' + higherIdRange);
  78.  
  79.     let appQuery = 'SELECT id, appid from dev_apps where id between ' + lowerIdRange + ' and ' + higherIdRange;
  80.  
  81.     getResult(appQuery).then(function (results) {
  82.       async.eachSeries(results, function(res, cb){
  83.         let appid = res.appid;
  84.  
  85.         console.log('Running for key: ' + appid);
  86.  
  87.         let mcuQuery = 'select sum(mcu_usages.inbound) as mcu_inbound, ' +
  88.           'sum(mcu_usages.outbound) as mcu_outbound ' +
  89.           'from mcu_usages ' +
  90.           'where appid="' + appid + '" ' +
  91.           'and month(created_at) = ' + month + ' ' +
  92.           'and year(created_at) = ' + year + ';';
  93.  
  94.         let turnQuery = 'select sum(turn_usages.inbound) as turn_inbound, ' +
  95.           'sum(turn_usages.outbound) as turn_outbound ' +
  96.           'from turn_usages ' +
  97.           'where appid="' + appid + '" ' +
  98.           'and month(created_at) = ' + month + ' ' +
  99.           'and year(created_at) = ' + year + ';';
  100.  
  101.         console.log(mcuQuery);
  102.         console.log(turnQuery);
  103.  
  104.         let mcuInbound = 0;
  105.         let mcuOutbound = 0;
  106.         let turnInbound = 0;
  107.         let turnOutbound = 0;
  108.  
  109.         getResult(turnQuery).then(function(res){
  110.           console.log(res);
  111.           turnInbound = res[0].turn_inbound || 0;
  112.           turnOutbound = res[0].turn_outbound || 0;
  113.         }).then(function(){
  114.           getResult(mcuQuery).then(function(res){
  115.             console.log(res);
  116.             mcuInbound = res[0].mcu_inbound || 0;
  117.             mcuOutbound = res[0].mcu_outbound || 0;
  118.           }).then(function(){
  119.             updateDevAppUsage(appid, mcuInbound, mcuOutbound, turnInbound, turnOutbound).catch(console.error.bind(console));
  120.             cb();
  121.           });
  122.         });
  123.       }, function(err){
  124.         console.log(err);
  125.         connection.end();
  126.       })
  127.     });
  128.   } catch (err) {
  129.     callback(err);
  130.   }
  131. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement