share/collection.js: Improve number formatting.
[collection4.git] / share / collection.js
1 /**
2  * collection4 - collection.js
3  * Copyright (C) 2010  Florian octo Forster
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
23
24 var c4 =
25 {
26   instances: []
27 };
28
29 function value_to_string (value) /* {{{ */
30 {
31   var abs_value;
32   var v2s = function (value)
33   {
34     var tmp = Math.round (100.0 * value) / 100.0;
35     return ("" + tmp);
36   }
37
38   if (value == null)
39     return ('NaN');
40   else if (value == 0)
41     return ('0');
42
43   abs_value = Math.abs (value);
44
45   if ((abs_value < 10000) && (abs_value >= 0.1))
46     return (v2s (value));
47   else if (abs_value > 1)
48   {
49     if (abs_value < 10000000)
50       return (v2s (value / 1000) + "k");
51     else if (abs_value < 10000000000)
52       return (v2s (value / 1000000) + "M");
53     else if (abs_value < 10000000000000)
54       return (v2s (value / 1000000000) + "G");
55     else
56       return (v2s (value / 1000000000000) + "T");
57   }
58   else
59   {
60     if (abs_value >= 0.001)
61       return (v2s (value * 1000) + "m");
62     else if (abs_value >= 0.000001)
63       return (v2s (value * 1000000) + "u");
64     else
65       return (v2s (value * 1000000000) + "n");
66   }
67 } /* }}} function value_to_string */
68
69 function instance_get_params (inst) /* {{{ */
70 {
71   var graph_selector = inst.graph_selector;
72   var inst_selector = inst.instance_selector;
73   var selector = {};
74
75   if (graph_selector.host == inst_selector.host)
76   {
77     selector.host = graph_selector.host;
78   }
79   else
80   {
81     selector.graph_host = graph_selector.host;
82     selector.inst_host = inst_selector.host;
83   }
84
85   if (graph_selector.plugin == inst_selector.plugin)
86   {
87     selector.plugin = graph_selector.plugin;
88   }
89   else
90   {
91     selector.graph_plugin = graph_selector.plugin;
92     selector.inst_plugin = inst_selector.plugin;
93   }
94
95   if (graph_selector.plugin_instance == inst_selector.plugin_instance)
96   {
97     selector.plugin_instance = graph_selector.plugin_instance;
98   }
99   else
100   {
101     selector.graph_plugin_instance = graph_selector.plugin_instance;
102     selector.inst_plugin_instance = inst_selector.plugin_instance;
103   }
104
105   if (graph_selector.type == inst_selector.type)
106   {
107     selector.type = graph_selector.type;
108   }
109   else
110   {
111     selector.graph_type = graph_selector.type;
112     selector.inst_type = inst_selector.type;
113   }
114
115   if (graph_selector.type_instance == inst_selector.type_instance)
116   {
117     selector.type_instance = graph_selector.type_instance;
118   }
119   else
120   {
121     selector.graph_type_instance = graph_selector.type_instance;
122     selector.inst_type_instance = inst_selector.type_instance;
123   }
124
125   return (selector);
126 } /* }}} instance_get_params */
127
128 function ident_clone (ident) /* {{{ */
129 {
130   var ret = {};
131
132   ret.host = ident.host;
133   ret.plugin = ident.plugin;
134   ret.plugin_instance = ident.plugin_instance;
135   ret.type = ident.type;
136   ret.type_instance = ident.type_instance;
137
138   return (ret);
139 } /* }}} ident_clone */
140
141 function inst_get_defs (inst) /* {{{ */
142 {
143   if (!inst.def)
144   {
145     var params = instance_get_params (inst);
146     params.action = "graph_def_json";
147
148     $.ajax({
149       url: "collection.fcgi",
150       async: false,
151       dataType: 'json',
152       data: params,
153       success: function (data)
154       {
155         if (!data)
156           return;
157
158         inst.def = data;
159       }});
160   }
161
162   if (inst.def)
163     return (inst.def);
164   return;
165 } /* }}} inst_get_defs */
166
167 function ident_matches (selector, ident) /* {{{ */
168 {
169   var part_matches = function (s,p)
170   {
171     if (s == null)
172       return (false);
173
174     if ((s == "/any/") || (s == "/all/"))
175       return (true);
176
177     if (p == null)
178       return (false);
179
180     if (s == p)
181       return (true);
182
183     return (false);
184   };
185
186   if (!part_matches (selector.host, ident.host))
187     return (false);
188
189   if (!part_matches (selector.plugin, ident.plugin))
190     return (false);
191
192   if (!part_matches (selector.plugin_instance, ident.plugin_instance))
193     return (false);
194
195   if (!part_matches (selector.type, ident.type))
196     return (false);
197
198   if (!part_matches (selector.type_instance, ident.type_instance))
199     return (false);
200
201   return (true);
202 } /* }}} function ident_matches */
203
204 function ident_describe (ident, selector) /* {{{ */
205 {
206   var ret = "";
207   var check_field = function (field)
208   {
209     if (ident[field].toLowerCase () != selector[field].toLowerCase ())
210     {
211       if (ret != "")
212         ret += "/";
213       ret += ident[field];
214     }
215   };
216
217   check_field ("host");
218   check_field ("plugin");
219   check_field ("plugin_instance");
220   check_field ("type");
221   check_field ("type_instance");
222
223   if (ret == "")
224     return (null);
225   return (ret);
226 } /* }}} function ident_describe */
227
228 function def_draw_one (def, data, chart_opts) /* {{{ */
229 {
230   var chart_series = new Object ();
231
232   chart_series.type = 'line';
233   chart_series.pointInterval = data.interval * 1000;
234   chart_series.pointStart = data.first_value_time * 1000;
235   chart_series.data = data.data;
236   chart_series.lineWidth = 1;
237   chart_series.shadow = false;
238   chart_series.marker = { enabled: false };
239
240   if (def.legend)
241     chart_series.name = def.legend;
242   else if (def.ds_name)
243     chart_series.name = def.ds_name;
244
245   if (def.area)
246     chart_series.type = 'area';
247
248   if (def.stack)
249     chart_series.stacking = 'normal';
250
251   if ((def.color) && (def.color != 'random'))
252     chart_series.color = def.color;
253
254   chart_opts.series.push (chart_series);
255 } /* }}} function def_draw_one */
256
257 function def_draw (def, data_list, chart_opts) /* {{{ */
258 {
259   var i;
260
261   for (i = 0; i < data_list.length; i++)
262   {
263     if ((def.ds_name) && (def.ds_name != data_list[i].data_source))
264       continue;
265     if (!ident_matches (def.select, data_list[i].file))
266       continue;
267
268     def_draw_one (def, data_list[i], chart_opts);
269   }
270 } /* }}} function def_draw */
271
272 function instance_draw (inst, def, data_list) /* {{{ */
273 {
274   var x_data = [];
275   var y_data = [];
276   var i;
277
278   var chart_opts = new Object ();
279
280   if (!inst || !def || !data_list)
281     return;
282
283   chart_opts.chart =
284   {
285     renderTo: inst.container,
286     zoomType: 'x'
287   };
288   chart_opts.xAxis =
289   {
290     type: 'datetime',
291     maxZoom: 300, // five minutes
292     title: { text: null }
293   };
294   chart_opts.yAxis =
295   {
296     labels:
297     {
298       formatter: function () { return (value_to_string (this.value)); }
299     },
300     endOnTick: false
301   };
302   chart_opts.series = new Array ();
303
304   if (def.title)
305     chart_opts.title = { text: def.title };
306
307   for (i = def.defs.length - 1; i >= 0; i--)
308     def_draw (def.defs[i], data_list, chart_opts);
309
310 //  if ((def.defs.length == 0) && (data_list.length == 1))
311 //    def_draw_one (null, data_list[0], chart_opts);
312
313   inst.chart = new Highcharts.Chart (chart_opts);
314 } /* }}} function instance_draw */
315
316 function json_graph_update (index) /* {{{ */
317 {
318   var inst;
319   var def;
320   var params;
321
322   inst = c4.instances[index];
323   if (!inst)
324     return;
325
326   def = inst_get_defs (inst);
327   if (!def)
328     return;
329
330   if (!inst.container)
331     inst.container = "c4-graph" + index;
332
333   params = instance_get_params (inst);
334   params.action = "instance_data_json";
335   params.begin = inst.begin;
336   params.end = inst.end;
337
338   $.getJSON ("collection.fcgi", params,
339       function (data)
340       {
341         instance_draw (inst, def, data);
342       }); /* getJSON */
343 } /* }}} json_graph_update */
344
345 function format_instance(inst)
346 {
347   return ("<li class=\"instance\"><a href=\"" + location.pathname
348       + "?action=show_instance;" + inst.params + "\">" + inst.description
349       + "</a></li>");
350 }
351
352 function format_instance_list(instances)
353 {
354   var ret = "<ul class=\"instance_list\">";
355   var i;
356
357   if (instances.length == 0)
358     return ("");
359
360   for (i = 0; i < instances.length; i++)
361     ret += format_instance (instances[i]);
362   
363   ret += "</ul>";
364
365   return (ret);
366 }
367
368 function format_graph(graph)
369 {
370   return ("<li class=\"graph\">" + graph.title + format_instance_list (graph.instances) + "</li>");
371 }
372
373 function update_search_suggestions ()
374 {
375   var term = $("#search-input").val ();
376   if (term.length < 2)
377   {
378     $("#search-suggest").hide ();
379     return (true);
380   }
381
382   $("#search-suggest").show ();
383   $.getJSON ("collection.fcgi",
384     { "action": "search_json", "q": term},
385     function(data)
386     {
387       var i;
388       $("#search-suggest").html ("");
389       for (i = 0; i < data.length; i++)
390       {
391         var graph = data[i];
392         $("#search-suggest").append (format_graph (graph));
393       }
394     }
395   );
396 } /* update_search_suggestions */
397
398 function zoom_redraw (jq_obj) /* {{{ */
399 {
400   var url = jq_obj.data ("base_url");
401
402   if ((jq_obj == null) || (url == null))
403     return (false);
404
405   if (jq_obj.data ('begin') != null)
406     url += ";begin=" + jq_obj.data ('begin');
407   if (jq_obj.data ('end') != null)
408     url += ";end=" + jq_obj.data ('end');
409
410   jq_obj.attr ("src", url);
411   return (true);
412 } /* }}} function zoom_redraw */
413
414 function zoom_reset (graph_id, diff) /* {{{ */
415 {
416   var jq_obj;
417   var end;
418   var begin;
419
420   jq_obj = $("#" + graph_id);
421   if (jq_obj == null)
422     return (false);
423
424   end = new Number ((new Date ()).getTime () / 1000);
425   begin = new Number (end - diff);
426
427   jq_obj.data ('begin', begin.toFixed (0));
428   jq_obj.data ('end', end.toFixed (0));
429
430   return (zoom_redraw (jq_obj));
431 } /* }}} function zoom_reset */
432
433 function zoom_hour (graph_id) /* {{{ */
434 {
435   zoom_reset (graph_id, 3600);
436 } /* }}} function zoom_hour */
437
438 function zoom_day (graph_id) /* {{{ */
439 {
440   zoom_reset (graph_id, 86400);
441 } /* }}} function zoom_day */
442
443 function zoom_week (graph_id) /* {{{ */
444 {
445   zoom_reset (graph_id, 7 * 86400);
446 } /* }}} function zoom_week */
447
448 function zoom_month (graph_id) /* {{{ */
449 {
450   zoom_reset (graph_id, 31 * 86400);
451 } /* }}} function zoom_month */
452
453 function zoom_year (graph_id) /* {{{ */
454 {
455   zoom_reset (graph_id, 366 * 86400);
456 } /* }}} function zoom_year */
457
458 function zoom_relative (graph_id, factor_begin, factor_end) /* {{{ */
459 {
460   var jq_obj;
461   var end;
462   var begin;
463   var diff;
464
465   jq_obj = $("#" + graph_id);
466   if (jq_obj == null)
467     return (false);
468
469   begin = jq_obj.data ('begin');
470   end = jq_obj.data ('end');
471   if ((begin == null) || (end == null))
472     return (zoom_day (graph_id));
473
474   begin = new Number (begin);
475   end = new Number (end);
476
477   diff = end - begin;
478   if ((diff <= 300) && (factor_begin > 0.0) && (factor_end < 0.0))
479     return (true);
480
481   jq_obj.data ('begin', begin + (diff * factor_begin));
482   jq_obj.data ('end', end + (diff * factor_end));
483
484   return (zoom_redraw (jq_obj));
485 } /* }}} function zoom_relative */
486
487 function zoom_reference (graph_id) /* {{{ */
488 {
489   var jq_obj;
490   var end;
491   var begin;
492
493   jq_obj = $("#" + graph_id);
494   if (jq_obj == null)
495     return (false);
496
497   begin = jq_obj.data ('begin');
498   end = jq_obj.data ('end');
499   if ((begin == null) || (end == null))
500     return (false);
501
502   $(".graph-img img").each (function ()
503   {
504     $(this).data ('begin', begin);
505     $(this).data ('end', end);
506     zoom_redraw ($(this));
507   });
508 } /* }}} function zoom_reference */
509
510 function zoom_earlier (graph_id) /* {{{ */
511 {
512   return (zoom_relative (graph_id, -0.2, -0.2));
513 } /* }}} function zoom_earlier */
514
515 function zoom_later (graph_id) /* {{{ */
516 {
517   return (zoom_relative (graph_id, +0.2, +0.2));
518 } /* }}} function zoom_later */
519
520 function zoom_in (graph_id) /* {{{ */
521 {
522   return (zoom_relative (graph_id, +0.2, -0.2));
523 } /* }}} function zoom_earlier */
524
525 function zoom_out (graph_id) /* {{{ */
526 {
527   return (zoom_relative (graph_id, (-1.0 / 3.0), (1.0 / 3.0)));
528 } /* }}} function zoom_earlier */
529
530 $(document).ready(function() {
531     /* $("#layout-middle-right").html ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>"); */
532     $("#search-form").append ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>");
533     $("#search-suggest").hide ();
534
535     $("#search-input").blur (function()
536     {
537       window.setTimeout (function ()
538       {
539         $("#search-suggest").hide ();
540       }, 500);
541     });
542
543     $("#search-input").focus (function()
544     {
545       var term = $("#search-input").val ();
546       if (term.length < 2)
547       {
548         $("#search-suggest").hide ();
549       }
550       else
551       {
552         $("#search-suggest").show ();
553       }
554     });
555
556     $("#search-input").keyup (function()
557     {
558       update_search_suggestions ();
559     });
560
561     var graph_count = 0;
562     $(".graph-img").each (function (index, elem)
563     {
564       var id = "graph" + graph_count;
565       graph_count++;
566
567       $(this).find ("img").each (function (img_index, img_elem)
568       {
569         var base_url;
570
571         $(this).attr ("id", id);
572
573         base_url = $(this).attr ("src").replace (/;(begin|end)=[^;]*/g, '');
574         $(this).data ("base_url", base_url);
575       });
576
577       $(this).append ("<div class=\"graph-buttons presets\">"
578         + "<div class=\"graph-button\" onClick=\"zoom_hour  ('"+id+"');\">H</div>"
579         + "<div class=\"graph-button\" onClick=\"zoom_day   ('"+id+"');\">D</div>"
580         + "<div class=\"graph-button\" onClick=\"zoom_week  ('"+id+"');\">W</div>"
581         + "<div class=\"graph-button\" onClick=\"zoom_month ('"+id+"');\">M</div>"
582         + "<div class=\"graph-button\" onClick=\"zoom_year  ('"+id+"');\">Y</div>"
583         + "<div class=\"graph-button\" onClick=\"zoom_reference ('"+id+"');\">!</div>"
584         + "</div>"
585         + "<div class=\"graph-buttons navigation\">"
586         + "<div class=\"graph-button\" onClick=\"zoom_earlier ('"+id+"');\">←</div>"
587         + "<div class=\"graph-button\" onClick=\"zoom_out     ('"+id+"');\">−</div>"
588         + "<div class=\"graph-button\" onClick=\"zoom_in      ('"+id+"');\">+</div>"
589         + "<div class=\"graph-button\" onClick=\"zoom_later   ('"+id+"');\">→</div>"
590         + "</div>"
591         );
592     });
593
594     var i;
595     for (i = 0; i < c4.instances.length; i++)
596     {
597       json_graph_update (i);
598     }
599 });
600
601 /* vim: set sw=2 sts=2 et fdm=marker : */