averageComputer/handler.js
// helper function to compute back averages
helper function to compute back averages
helper function to compute back averages
function computeAverages(callback) {
  var now = (new Date).getTime();
  var maxAge = now - (60 * 24 * 60 * 60 * 1000);
  const params = {
    TableName: "PriceUpdates",
    ProjectionExpression: "#ts, price",
    KeyConditionExpression: "#typ = :type AND #ts BETWEEN :then AND :now",
    ScanIndexForward: false, 
    ExpressionAttributeNames: {
      "#ts": "timestamp",
      "#typ": "type"
    },
    ExpressionAttributeValues: {
      ":type": "BTC/USD",
      ":then": maxAge,
      ":now": now
    }
  };
  docClient.query(params, function(err, data) {
    if (err) {
      console.log("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
      console.log("Query succeeded.");
      var prices = new Array();
      data.Items.forEach(function(item) {
        item.timestamp = new Date(item.timestamp).toISOString();
        prices.push(item);
      });
      var arr = new Object();
      var period = 60;
      var result = 0.0;
      while (result != null) {
        if (period > prices.length || period >= MAX_PERIOD) {
          result = null;
        } else {
          arr[(period / 60)] = calculate(period, prices);
          period = period + 60;
        }
      }
      saveAverages(arr).then(function() {
        const response = {
          statusCode: 200,
          body: JSON.stringify({
            message: 'sucessfully updated averages',
            output: JSON.stringify(arr)
          }),
        };
        callback(null, response);
      });
    }
  });
}
function computeAverages ( callback ) { var now = ( new Date ) . getTime ( ) ; var maxAge = now - ( 60 * 24 * 60 * 60 * 1000 ) ; const params = { TableName : "PriceUpdates" , ProjectionExpression : "#ts, price" , KeyConditionExpression : "#typ = :type AND #ts BETWEEN :then AND :now" , ScanIndexForward : false , ExpressionAttributeNames : { "#ts" : "timestamp" , "#typ" : "type" } , ExpressionAttributeValues : { ":type" : "BTC/USD" , ":then" : maxAge , ":now" : now } } ; docClient . query ( params , function ( err , data ) { if ( err ) { console . log ( "Unable to query. Error:" , JSON . stringify ( err , null , 2 ) ) ; } else { console . log ( "Query succeeded." ) ; var prices = new Array ( ) ; data . Items . forEach ( function ( item ) { item . timestamp = new Date ( item . timestamp ) . toISOString ( ) ; prices . push ( item ) ; } ) ; var arr = new Object ( ) ; var period = 60 ; var result = 0.0 ; while ( result != null ) { if ( period > prices . length || period >= MAX_PERIOD ) { result = null ; } else { arr [ ( period / 60 ) ] = calculate ( period , prices ) ; period = period + 60 ; } } saveAverages ( arr ) . then ( function ( ) { const response = { statusCode : 200 , body : JSON . stringify ( { message : 'sucessfully updated averages' , output : JSON . stringify ( arr ) } ) , } ; callback ( null , response ) ; } ) ; } } ) ; }
helper function to compute back averages
helper function to compute back averages
// true = ascending, false = descending // scan price table, retrieve prices up to max age //  compute average for period // persist the computed average to the table