<commit_before>var pm_host = location.hostname;
var pm_root = "http://" + pm_host + ":44323/pmapi";
var pm_context = -1;

// ----------------------------------------------------------------------

// TODOXXX use actual pictures for the blinkenlights
var red_light = "(!)";
var green_light = "(X)";
var gray_light = "( )";

// ----------------------------------------------------------------------

function Predicate(name,index,operator,threshold) {
  this.name = name;
  this.index = index;
  this.operator = operator;
  this.threshold = threshold;
  this.inames = {}; // TODOXXX gather instance names for this metric, requires _indom support
}

Predicate.prototype.test = function(elt,data_dict,index) {
  if (this.index == "*" && typeof(index) == "undefined") {
    var predicate = this;
    $.each(data_dict[this.name].instances, function(i,_instance) {
      predicate.test(elt,data_dict,i);
    });
    return;
  }
  
  if (typeof(index) == "undefined") index == this.index;

  var metric = data_dict[this.name].instances[index].value;
  var result = 0, error = "";
  if (this.operator == "<") result = metric < this.threshold;
  else if (this.operator == ">") result = metric > this.threshold;
  else if (this.operator == "<=") result = metric <= this.threshold;
  else if (this.operator == ">=") result = metric >= this.threshold;
  else if (this.operator == "==") result = metric == this.threshold;
  else { error = "unknown operator '" + this.operator + "'"; result = -1; }
  
  // TODOXXX assign id to this li, avoid $("#blinkenlights").empty()
  // XXX var blinken_id = 
  var blinken = result < 0 ? red_light : result ? green_light : gray_light;

  // TODOXXX use instance names to display something nicer than an index
  var source = this.name + "[" + index + "] " + this.operator + " " + this.threshold;
  var content = blinken + " <span>" + source + "</span>"
              + (result < 0 ? " &ndash; error: " + error : "");

  // XXX if (elt.find("#" + blinken_id)) ...
  // XXX else elt.append("<li id=\"" + blinken_id + "\">" + content + "</li>")
  elt.append("<li>" + content + "</li>");
};

var predicates = [];

function parsePredicate(src) {
  var matches = /([^[]+)\s*(\[\*\]|\[\]|)\s*(<|>|<=|>=|==)\s*(\S+)/.exec(src);
  var name = matches[1];
  var index = matches[2]; index = index == "" ? "*" : index.substring(1,index.length-1);
  var operator = matches[3];
  var threshold = parseFloat(matches[4]); // TODOXXX what about other types?
  return new Predicate(name,index,operator,threshold);
}

// ----------------------------------------------------------------------

var updateInterval;

function updateBlinkenlights() {
  var pm_url;

  if (pm_context < 0) {
    pm_url = pm_root + "/context?hostname=" + pm_host;
    $.getJSON(pm_url, function(data, status) {
      // TODOXXX error handling
      pm_context = data.context;
      return; // will retry one cycle later
    });
  }

  // ajax request for JSON data
  pm_url = pm_root + "/" + pm_context + "/_fetch?names=";
  $.each(predicates, function(i, predicate) {
    if (i > 0) pm_url += ",";
    pm_url += predicate.name;
  });
  $.getJSON(pm_url, function(data, status) {<commit_msg>pmwebapi blinkenlights: fix to stop spurious _fetch calls before we have context<commit_after>var pm_host = location.hostname;
var pm_root = "http://" + pm_host + ":44323/pmapi";
var pm_context = -1;

// ----------------------------------------------------------------------

// TODOXXX use actual pictures for the blinkenlights
var red_light = "(!)";
var green_light = "(X)";
var gray_light = "( )";

// ----------------------------------------------------------------------

function Predicate(name,index,operator,threshold) {
  this.name = name;
  this.index = index;
  this.operator = operator;
  this.threshold = threshold;
  this.inames = {}; // TODOXXX gather instance names for this metric, requires _indom support
}

Predicate.prototype.test = function(elt,data_dict,index) {
  if (this.index == "*" && typeof(index) == "undefined") {
    var predicate = this;
    $.each(data_dict[this.name].instances, function(i,_instance) {
      predicate.test(elt,data_dict,i);
    });
    return;
  }
  
  if (typeof(index) == "undefined") index == this.index;

  var metric = data_dict[this.name].instances[index].value;
  var result = 0, error = "";
  if (this.operator == "<") result = metric < this.threshold;
  else if (this.operator == ">") result = metric > this.threshold;
  else if (this.operator == "<=") result = metric <= this.threshold;
  else if (this.operator == ">=") result = metric >= this.threshold;
  else if (this.operator == "==") result = metric == this.threshold;
  else { error = "unknown operator '" + this.operator + "'"; result = -1; }
  
  // TODOXXX assign id to this li, avoid $("#blinkenlights").empty()
  // XXX var blinken_id = 
  var blinken = result < 0 ? red_light : result ? green_light : gray_light;

  // TODOXXX use instance names to display something nicer than an index
  var source = this.name + "[" + index + "] " + this.operator + " " + this.threshold;
  var content = blinken + " <span>" + source + "</span>"
              + (result < 0 ? " &ndash; error: " + error : "");

  // XXX if (elt.find("#" + blinken_id)) ...
  // XXX else elt.append("<li id=\"" + blinken_id + "\">" + content + "</li>")
  elt.append("<li>" + content + "</li>");
};

var predicates = [];

function parsePredicate(src) {
  var matches = /([^[]+)\s*(\[\*\]|\[\]|)\s*(<|>|<=|>=|==)\s*(\S+)/.exec(src);
  var name = matches[1];
  var index = matches[2]; index = index == "" ? "*" : index.substring(1,index.length-1);
  var operator = matches[3];
  var threshold = parseFloat(matches[4]); // TODOXXX what about other types?
  return new Predicate(name,index,operator,threshold);
}

// ----------------------------------------------------------------------

var updateInterval;

function updateBlinkenlights() {
  var pm_url;

  if (pm_context < 0) {
    pm_url = pm_root + "/context?hostname=" + pm_host;
    $.getJSON(pm_url, function(data, status) {
      // TODOXXX error handling
      pm_context = data.context;
    });
    return; // will retry one cycle later
  }

  // ajax request for JSON data
  pm_url = pm_root + "/" + pm_context + "/_fetch?names=";
  $.each(predicates, function(i, predicate) {
    if (i > 0) pm_url += ",";
    pm_url += predicate.name;
  });
  $.getJSON(pm_url, function(data, status) {
{'fraction_non_alphanumeric': 0.23770875604807243, 'fraction_numerical': 0.005618854378024036, 'mean_word_length': 2.587905935050392, 'pattern_counts': {'":': 2, '<': 33, '<?xml version=': 0, '>': 29, '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': '6981764', 'n_tokens_mistral': 2041, 'n_tokens_neox': 1983, 'n_words': 503}