<commit_before>var config = require("config");
var rest = require("restler");
var dateformat = require("dateformat");

var debug=config.get("debug");
var baseUrl = config.get("url");
var access = config.get("access");
var timeout = config.get("timeoutMilliSeconds");
var normal = config.get("normalStatus");
var limit = config.get("limit");
var headers = {
  "Authorization":"Token token="+config.get("access")
}

var alarm = null;
Gpio = require("onoff").Gpio,
alarmSpeaker = new Gpio(config.get("alarmSpeaker"), "out");
heartbeatLed = new Gpio(config.get("heartbeatLed"), "out");
alarmLed = new Gpio(config.get("alarmLed"), "out");
alarmSpeaker.writeSync(0);
heartbeatLed.writeSync(0);
alarmLed.writeSync(0);

checkIncident();

function checkIncident() {
  // wait for one minute and check again
  setTimeout(checkIncident, timeout);

  var now = new Date()
  var until = dateformat(now, "isoDateTime");
  now.setDate(now.getDate()-1);
  var since = dateformat(now, "isoDateTime");

  var url = baseUrl + "?since=" + since + "&until=" + until + "&limit=" + limit;
  if(debug) console.log(url);
  rest.get(url, {headers: headers}).on("success", function(result) {
    if (result.total > limit) {
      if(debug) console.log("Skip to the last page.");
      // try again
      var offset = result.total - 1;
      url += "&offset=" + offset;
      if(debug) console.log(url);
      rest.get(url, {headers:headers}).on("success", function(result) {
        processIncident(result.incidents[result.incidents.length-1]);
      });
    } else {
        processIncident(result.incidents[result.incidents.length-1]);
    }
  });
}

function processIncident(incident) {
  var now = new Date()
  var timestamp = dateformat(now, "isoDateTime");
  if (incident.status == normal) {
    console.log("@" + timestamp + " status: normal");
    heartbeatLed.writeSync(1);
    alarmLed.writeSync(0);
    alarmSpeaker.writeSync(0);
  } else {
    console.log("@" + timestamp + " status: " + incident.status);
    heartbeatLed.writeSync(0);
    alarmLed.writeSync(1);
    alarmSpeaker.writeSync(1);
    setTimeout(turnOffAlarm, 10);
  }
}

function turnOffAlarm() {
  alarmSpeaker.writeSync(0);
}<commit_msg>Set alarm for 10 seconds<commit_after>var config = require("config");
var rest = require("restler");
var dateformat = require("dateformat");

var debug=config.get("debug");
var baseUrl = config.get("url");
var access = config.get("access");
var timeout = config.get("timeoutMilliSeconds");
var normal = config.get("normalStatus");
var limit = config.get("limit");
var headers = {
  "Authorization":"Token token="+config.get("access")
}

var alarm = null;
Gpio = require("onoff").Gpio,
alarmSpeaker = new Gpio(config.get("alarmSpeaker"), "out");
heartbeatLed = new Gpio(config.get("heartbeatLed"), "out");
alarmLed = new Gpio(config.get("alarmLed"), "out");
alarmSpeaker.writeSync(0);
heartbeatLed.writeSync(0);
alarmLed.writeSync(0);

checkIncident();

function checkIncident() {
  // wait for one minute and check again
  setTimeout(checkIncident, timeout);

  var now = new Date()
  var until = dateformat(now, "isoDateTime");
  now.setDate(now.getDate()-1);
  var since = dateformat(now, "isoDateTime");

  var url = baseUrl + "?since=" + since + "&until=" + until + "&limit=" + limit;
  if(debug) console.log(url);
  rest.get(url, {headers: headers}).on("success", function(result) {
    if (result.total > limit) {
      if(debug) console.log("Skip to the last page.");
      // try again
      var offset = result.total - 1;
      url += "&offset=" + offset;
      if(debug) console.log(url);
      rest.get(url, {headers:headers}).on("success", function(result) {
        processIncident(result.incidents[result.incidents.length-1]);
      });
    } else {
        processIncident(result.incidents[result.incidents.length-1]);
    }
  });
}

function processIncident(incident) {
  var now = new Date()
  var timestamp = dateformat(now, "isoDateTime");
  if (incident.status == normal) {
    console.log("@" + timestamp + " status: normal");
    heartbeatLed.writeSync(1);
    alarmLed.writeSync(0);
    alarmSpeaker.writeSync(0);
  } else {
    console.log("@" + timestamp + " status: " + incident.status);
    heartbeatLed.writeSync(0);
    alarmLed.writeSync(1);
    alarmSpeaker.writeSync(1);
    setTimeout(turnOffAlarm, 10000);
  }
}

function turnOffAlarm() {
  alarmSpeaker.writeSync(0);
}
{'fraction_non_alphanumeric': 0.17076502732240437, 'fraction_numerical': 0.008424408014571949, 'mean_word_length': 4.337788578371811, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, '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': '3759456', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1473, 'n_words': 302}