<commit_before>var CloudWatchAlarm = require('./lib/cw-alarm.js');
var async = require('async');
var sleep = require('sleep');


exports.handler = function(event, context){

  console.log('Received event:', JSON.stringify(event, null, 2));
  // Event stops triggering within a few minutes without a delay
  sleep.sleep(1);


  var message = JSON.parse(event.Records[0].Sns.Message);
  var cw = new CloudWatchAlarm(message);

  //Reset alarm state to OK
  cw.resetAlarm(function(err, data){
    if(err){
      console.log("Error setting alarm state", err, err.stack);
    }else{
      console.log("Successfully set alarm state");
      setMetrics(cw, event, context);
    }
  });
}

function setMetrics(cw, event, context){
  async.parallel({
    errors: function(callback){ cw.getMetrics('Errors', callback) },
    invocations: function(callback){ cw.getMetrics('Invocations', callback) }
    },
    function(err, results){
      if(err){
        console.log("ERROR fetching metrics", err, err.stack);
        context.fail("Unable to retrieve metrics");
      }else{
        var errors = results.errors;
        var invocations = results.invocations;
        console.log("Metrics retrieved");
        console.log("Errors: ",errors," Invocations: ",invocations);
        if(invocations != 0){
          var percentFailure = (errors / invocations) * 100
          console.log("Percent failure: ", percentFailure);
          cw.putCustomMetrics(percentFailure, function(err, data){
            if(err){
              console.log("ERROR - unable to post custom metric", err, err.stack);
              context.fail("Unable to post custom metric");
            }else{
              console.log("Successfully posted custom metric");
              console.log(data);
              context.succeed("Successfully posted custom metric");
            }
          });
        }else{
          console.log("ERROR - no invocations found");
          context.fail("No invocations to process");
        }
      }
    });
}<commit_msg>No longer considers finding no invocations an error. Will now output a warning instead of an error, and finish execution successfully<commit_after>var CloudWatchAlarm = require('./lib/cw-alarm.js');
var async = require('async');
var sleep = require('sleep');


exports.handler = function(event, context){

  console.log('Received event:', JSON.stringify(event, null, 2));
  // Event stops triggering within a few minutes without a delay
  sleep.sleep(1);


  var message = JSON.parse(event.Records[0].Sns.Message);
  var cw = new CloudWatchAlarm(message);

  //Reset alarm state to OK
  cw.resetAlarm(function(err, data){
    if(err){
      console.log("Error setting alarm state", err, err.stack);
    }else{
      console.log("Successfully set alarm state");
      setMetrics(cw, event, context);
    }
  });
}

function setMetrics(cw, event, context){
  async.parallel({
    errors: function(callback){ cw.getMetrics('Errors', callback) },
    invocations: function(callback){ cw.getMetrics('Invocations', callback) }
    },
    function(err, results){
      if(err){
        console.log("ERROR fetching metrics", err, err.stack);
        context.fail("Unable to retrieve metrics");
      }else{
        var errors = results.errors;
        var invocations = results.invocations;
        console.log("Metrics retrieved");
        console.log("Errors: ",errors," Invocations: ",invocations);
        if(invocations != 0){
          var percentFailure = (errors / invocations) * 100
          console.log("Percent failure: ", percentFailure);
          cw.putCustomMetrics(percentFailure, function(err, data){
            if(err){
              console.log("ERROR - unable to post custom metric", err, err.stack);
              context.fail("Unable to post custom metric");
            }else{
              console.log("Successfully posted custom metric");
              console.log(data);
              context.succeed("Successfully posted custom metric");
            }
          });
        }else{
          console.log("Warning - no invocations found");
          context.succeed("No invocations to process");
        }
      }
    });
}
{'fraction_non_alphanumeric': 0.131281558066843, 'fraction_numerical': 0.003366193796585718, 'mean_word_length': 2.7545126353790614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6460672', 'n_tokens_mistral': 1163, 'n_tokens_neox': 1113, 'n_words': 302}