Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const AWS = require('aws-sdk');
- const mysql = require('mysql');
- const async = require('async');
- const connection = mysql.createConnection({
- host: process.env.MYSQL_HOST,
- user: process.env.MYSQL_USER,
- password: process.env.MYSQL_PASSWORD,
- database: process.env.MYSQL_DATABASE_NAME
- });
- const dynamodb = new AWS.DynamoDB.DocumentClient();
- function getResult(sql) {
- return new Promise(function(resolve, reject){
- connection.query(sql, function(err, result){
- if(err){
- console.log(err);
- reject(err);
- }else{
- resolve(result);
- }
- })
- })
- }
- let updateDevAppUsage = function updateUser(appid, mcuIn, mcuOut, turnIn, turnOut) {
- // apparently we sum turn usage on dynamo
- let turn = parseInt(turnIn) + parseInt(turnOut);
- console.log('Updating ' + appid + ' with mcuIn:' + mcuIn + ' mcuOut: ' + mcuOut + ' turn: ' + turn);
- let params = {
- TableName: 'devapps',
- Key: { 'appid': appid },
- UpdateExpression: 'set #mcuIn = :mcuIn, #mcuOut = :mcuOut, #turn = :turn',
- ConditionExpression: '#appid = :appid',
- ExpressionAttributeNames: {
- '#appid': 'appid',
- '#mcuIn': 'mcuInboundMonthly',
- '#mcuOut': 'mcuOutboundMonthly',
- '#turn': 'turnUsageMonthly'
- },
- ExpressionAttributeValues: {
- ':appid': appid,
- ':mcuIn': mcuIn,
- ':mcuOut': mcuOut,
- ':turn': turn
- }
- };
- console.log('Updating dynamo...');
- return dynamodb.update(params).promise();
- };
- // --------------- Main handler -----------------------
- exports.handler = (event, context, callback) => {
- try {
- // connect to MySQL
- connection.connect();
- // we have 1,440 minutes in a day.
- // we have around < 14,500 keys
- // adjust number of keys depending on how much
- // we want to process per minute
- let numOfKeysToProcess = process.env.NUM_OF_KEYS_TO_PROCESS || 10;
- let time = new Date();
- let month = time.getMonth() + 1;
- let year = time.getFullYear();
- let minutesFromMidnight = time.getMinutes() + (60 * time.getHours());
- let lowerIdRange = 6708;//(numOfKeysToProcess * minutesFromMidnight) - numOfKeysToProcess + 1;
- let higherIdRange = 6711; //(numOfKeysToProcess * minutesFromMidnight);
- console.log('Doing IDs from ' + lowerIdRange + ' to ' + higherIdRange);
- let appQuery = 'SELECT id, appid from dev_apps where id between ' + lowerIdRange + ' and ' + higherIdRange;
- getResult(appQuery).then(function (results) {
- async.eachSeries(results, function(res, cb){
- let appid = res.appid;
- console.log('Running for key: ' + appid);
- let mcuQuery = 'select sum(mcu_usages.inbound) as mcu_inbound, ' +
- 'sum(mcu_usages.outbound) as mcu_outbound ' +
- 'from mcu_usages ' +
- 'where appid="' + appid + '" ' +
- 'and month(created_at) = ' + month + ' ' +
- 'and year(created_at) = ' + year + ';';
- let turnQuery = 'select sum(turn_usages.inbound) as turn_inbound, ' +
- 'sum(turn_usages.outbound) as turn_outbound ' +
- 'from turn_usages ' +
- 'where appid="' + appid + '" ' +
- 'and month(created_at) = ' + month + ' ' +
- 'and year(created_at) = ' + year + ';';
- console.log(mcuQuery);
- console.log(turnQuery);
- let mcuInbound = 0;
- let mcuOutbound = 0;
- let turnInbound = 0;
- let turnOutbound = 0;
- getResult(turnQuery).then(function(res){
- console.log(res);
- turnInbound = res[0].turn_inbound || 0;
- turnOutbound = res[0].turn_outbound || 0;
- }).then(function(){
- getResult(mcuQuery).then(function(res){
- console.log(res);
- mcuInbound = res[0].mcu_inbound || 0;
- mcuOutbound = res[0].mcu_outbound || 0;
- }).then(function(){
- updateDevAppUsage(appid, mcuInbound, mcuOutbound, turnInbound, turnOutbound).catch(console.error.bind(console));
- cb();
- });
- });
- }, function(err){
- console.log(err);
- connection.end();
- })
- });
- } catch (err) {
- callback(err);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement