File: graph.js

package info (click to toggle)
darkstat 3.0.708-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 708 kB
  • ctags: 592
  • sloc: ansic: 5,326; sh: 322; makefile: 131
file content (182 lines) | stat: -rw-r--r-- 5,132 bytes parent folder | download
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
171
172
173
174
175
176
177
178
179
180
181
182
/* darkstat 3
 * copyright (c) 2006, 2007 Emil Mikulic.
 *
 * graph.js: graph renderer
 *
 * You may use, modify and redistribute this file under the terms of the
 * GNU General Public License version 2. (see COPYING.GPL)
 *
 * ---------------------------------------------------------------------
 * In my testing, this script worked correctly in:
 *  - Firefox 1.5.0.4, 2.0.0.1
 *  - IE 6.0
 *  - Opera 8.53
 */

/* need:
 *  - graph_width
 *  - graph_height
 *  - bar_gap
 *
 *  - graphs [ {id, name, title, bar_secs} ]
 *  - graphs_uri
 *
 *  - window.onload = graph_loader
 *
 * in CSS:
 *  - div.graph
 *  - div.graph div.bar_in
 *  - div.graph div.bar_out
 *  - div.graphtitle
 */

function killChildren(elem) {
 while (elem.childNodes.length > 0)
  elem.removeChild( elem.childNodes.item(0) );
}

function thousands(n) {
 var s = String(n);
 var out = "";
 while (s.length > 3) {
  out = "," + s.substr(-3) + out;
  s = s.substr(0, s.length - 3);
 }
 return s+out;
}

var xh;

function graph_loader() {
 for (var i=0; i<graphs.length; i++) {
  graphs[i].graph = document.getElementById( graphs[i].id );
  killChildren(graphs[i].graph);
  graphs[i].graph.appendChild(
   document.createTextNode("Loading..."));
 }

 /* create buttons */
 var b_reload = document.createElement("a");
 b_reload.setAttribute("id", "graph_reload");
 b_reload.setAttribute("href", "javascript:graph_reload()");
 b_reload.appendChild(
  document.createTextNode("reload graphs"));
 document.getElementById("graph_buttons").appendChild(b_reload);

 graph_reload();
}

function graph_reload() {
 document.getElementById("graph_reload").innerHTML = "loading...";
 xh = (window.ActiveXObject)
  ? new ActiveXObject("Microsoft.XMLHTTP")
  : new XMLHttpRequest();
 var asyncFlag = true;
 xh.open("GET", graphs_uri, asyncFlag);
 // try to nerf caching:
 xh.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
 xh.onreadystatechange = poll;
 xh.send(null);
}

function poll() {
 var STATE_COMPLETE = 4;
 if (xh && xh.readyState == STATE_COMPLETE) {
  for (var i=0; i<graphs.length; i++)
  {
   g = xh.responseXML.getElementsByTagName(graphs[i].name);
   buildGraph(graphs[i].graph, graphs[i].title, graphs[i].bar_secs,
    g[0].getElementsByTagName("e"));
  }
  document.getElementById("graph_reload").innerHTML = "reload graphs";
  head = xh.responseXML.childNodes[0];
  document.getElementById("tb").innerHTML = head.getAttribute("tb");
  document.getElementById("tp").innerHTML = head.getAttribute("tp");
  document.getElementById("rf").innerHTML = head.getAttribute("rf");
 }
}

function addBar(graph, title, barclass, width, height, left, bottom) {
 if (height == 0) return; /* not visible */
 var bar = document.createElement("div");
 bar.setAttribute("title", title);
 bar.setAttribute("class", barclass);
 bar.setAttribute("className", barclass); //IE
 bar.style.cssText =
  "width:"+width+"px; "+
  "height:"+height+"px; "+
  "position: absolute; "+
  "left:"+left+"px; "+
  "bottom:"+bottom+"px;";
 bar.setAttribute("style", bar.style.cssText);
 graph.appendChild(bar);
}

function kbps(bps) {
   bps /= 1024;
   if (bps < 1) return bps.toPrecision(2);
   else return bps.toFixed(1);
}

function buildGraph(graph, title, bar_secs, elems) {
 var total_max = 0;
 var data = []; /* list of [in, out] */
 for (var i=0; i<elems.length; i++) {
   var elem = elems.item(i);
   var b_pos = Number( elem.getAttribute("p") );
   var b_in = Number( elem.getAttribute("i") );
   var b_out = Number( elem.getAttribute("o") );
   var b_total = b_in + b_out;
/* FIXME: what happens when a bar's value is >4G? */
   if (b_total > total_max)
    total_max = b_total;
   data.push( [b_pos, b_in, b_out] );
 }

 var igraph = document.createElement("div"); // inner graph
 igraph.setAttribute("class", "graph");
 igraph.setAttribute("className", "graph"); //IE
 igraph.style.cssText =
  "width:"+graph_width+"px; "+
  "height:"+graph_height+"px; "+
  "position:relative;";
 igraph.setAttribute("style", igraph.style.cssText);

 var nbars = data.length;
 var b_width = (graph_width - bar_gap * (nbars-1)) / nbars;
 var next_xofs = 0;

 for (var i=0; i<nbars; i++) {
  var b_p = data[i][0];
  var b_i = data[i][1];
  var b_o = data[i][2];

  var xofs = next_xofs;

  next_xofs = Math.round((b_width + bar_gap) * (i+1));
  var curr_w = next_xofs - xofs - bar_gap;

  var h_i = Math.round( b_i * graph_height / total_max );
  var h_o = Math.round( b_o * graph_height / total_max );

  var label = b_p+": "+
   thousands(b_i)+" bytes in, "+
   thousands(b_o)+" bytes out | "+
   kbps(b_i/bar_secs)+" KB/s in, "+
   kbps(b_o/bar_secs)+" KB/s out";
  addBar(igraph, label, "bar_in", curr_w, h_i, xofs, 0);
  addBar(igraph, label, "bar_out", curr_w, h_o, xofs, h_i);
 }

 var gtitle = document.createElement("div");
 gtitle.setAttribute("class", "graphtitle");
 gtitle.setAttribute("className", "graphtitle"); //IE
 gtitle.style.cssText =
  "width:"+graph_width+"px;";
 gtitle.setAttribute("style", gtitle.style.cssText);
 gtitle.appendChild(document.createTextNode(title));

 killChildren(graph);
 graph.appendChild(igraph);
 graph.appendChild(gtitle);
}