summaryrefslogtreecommitdiff
path: root/src/pmwebapi/jsdemos/blinkenlights/blinkenlights.js
blob: 03bb315ab1b60fb2743aa93fa869efa72a497b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var pm_root = "http://" + location.hostname + ":" + location.port + "/pmapi";
var pm_context = -1;

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

function Predicate(name,index,operator,threshold) {
  this.name = name;
  this.index = index;
  this.operator = operator;
  this.threshold = threshold;
  this.inames = {};
}

Predicate.prototype.to_string = function() {
  return this.name + "[" + this.index + "] "
    + this.operator + " " + this.threshold;
};

Predicate.prototype.get_iname = function(iid) {
  if (!(iid in this.inames)) {
    var pm_url = pm_root + "/" + pm_context + "/_indom?name=" + this.name + "&instance=" + iid;
    var predicate = this;
    $.getJSON(pm_url, function(data, status) {
      // TODOXXX error check: should return 1 instance
      predicate.inames[iid] = data.instances[0].name;
    });

    return "..."; // will be reloaded next cycle
  }

  return this.inames[iid];
}

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 iid = data_dict[this.name].instances[index].instance;
  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 avoid $("#blinkenlights").empty() by using existing li's??
  var bclass = result < 0 ? "error" : result ? "on" : "off";

  var source = "<strong>" + metric + "</strong> -- "
             + this.name + " ( " + this.get_iname(iid) + " : " + iid + " ) "
             + this.operator + " " + this.threshold;
  var content = "<span>" + source + "</span>"
              + (result < 0 ? " -- error: " + error : "");

  elt.append("<li class=\"" + bclass + "\">" + content + "</li>");
};

var predicates = [];

function parsePredicate(src) {
  var matches = /^([^[ ]+)\s*(\[\d+\]|\[\*\]|\[\]|)\s*(<=|>=|==|<|>)\s*(\S*)$/.exec(src);
  if (matches == null) return null;
  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?; accepts 40foobar
  if (isNaN(threshold)) return null;

  console.log ("create predicate " + name + " : " + index + " : " + operator + " : " + threshold)

  return new Predicate(name,index,operator,threshold);
}

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

var updateInterval = 10000; // milliseconds
var updateIntervalID = 1;

function setUpdateInterval(i) {
   if (updateIntervalID >= 0) { clearInterval(updateIntervalID); }
   if (i > updateInterval) { pm_context = -1; } // will likely need a new context
   updateInterval = i;
   updateIntervalID = setInterval(updateBlinkenlights, updateInterval); 
}

// default mode
var pm_context_type = "hostspec";
var pm_context_spec = "localhost";

function setPMContextType(k) {
   pm_context_type = k;
   pm_context = -1;
   updateBlinkenlights();
}
function setPMContextSpec(i) {
   pm_context_spec = i;
   pm_context = -1;
   updateBlinkenlights();
}


function updateBlinkenlights() {
  var pm_url;

  if (pm_context < 0) {
    pm_url = pm_root
          + "/context?"
          + pm_context_type + "=" + encodeURIComponent(pm_context_spec)
          + "&polltimeout=" + Math.floor(5*updateInterval/1000);
    $.getJSON(pm_url, function(data, status) {
      pm_context = data.context;
      setTimeout(updateBlinkenlights, 100); // retry soon
    }).error(function() { pm_context = -1; });
    return; // will retry one cycle later
  }

  if(predicates.length == 0) {
      $("#blinkenlights").html("<b>No predicates requested...</b>");
      return;
  }

  // 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) {
    // update data_dict
    var data_dict = {};
    $.each(data.values, function(i, value) {
      data_dict[value.name] = value;
    });
    // update status field
    theDate = new Date(0);
    theDate.setUTCSeconds(data.timestamp.s);
    theDate.setUTCMilliseconds(data.timestamp.us/1000);
    $("#status").html("Timestamp: " + theDate.toString());
    // update the view
    $("#blinkenlights").empty();
    $.each(predicates, function(i, predicate) {
      predicate.test($("#blinkenlights"), data_dict);
    });
  }).error(function() { 
      $("#blinkenlights").html("<b>error accessing server, retrying...</b>");
      pm_context = -1; });
}

function loadBlinkenlights() {
  $("#header").html("pcp blinkenlights demo");
  $("#content").html("<ul id=\"blinkenlights\"><li>loading...</li></ul>");

  // start timer for updateBlinkenlights
  updateBlinkenlights();
  setUpdateInterval(updateInterval);
}

$(document).ready(function() {
  loadBlinkenlights();
  // TODOXXX add support for editing mode
});