2 * collection4 - collection.js
3 * Copyright (C) 2010 Florian octo Forster
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.
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.
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
21 * Florian octo Forster <ff at octo.it>
29 function value_to_string (value) /* {{{ */
32 var v2s = function (value)
34 var tmp = Math.round (100.0 * value) / 100.0;
43 abs_value = Math.abs (value);
45 if ((abs_value < 10000) && (abs_value >= 0.1))
47 else if (abs_value > 1)
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");
56 return (v2s (value / 1000000000000) + "T");
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");
65 return (v2s (value * 1000000000) + "n");
67 } /* }}} function value_to_string */
69 function instance_get_params (inst) /* {{{ */
71 var graph_selector = inst.graph_selector;
72 var inst_selector = inst.instance_selector;
75 if (graph_selector.host == inst_selector.host)
77 selector.host = graph_selector.host;
81 selector.graph_host = graph_selector.host;
82 selector.inst_host = inst_selector.host;
85 if (graph_selector.plugin == inst_selector.plugin)
87 selector.plugin = graph_selector.plugin;
91 selector.graph_plugin = graph_selector.plugin;
92 selector.inst_plugin = inst_selector.plugin;
95 if (graph_selector.plugin_instance == inst_selector.plugin_instance)
97 selector.plugin_instance = graph_selector.plugin_instance;
101 selector.graph_plugin_instance = graph_selector.plugin_instance;
102 selector.inst_plugin_instance = inst_selector.plugin_instance;
105 if (graph_selector.type == inst_selector.type)
107 selector.type = graph_selector.type;
111 selector.graph_type = graph_selector.type;
112 selector.inst_type = inst_selector.type;
115 if (graph_selector.type_instance == inst_selector.type_instance)
117 selector.type_instance = graph_selector.type_instance;
121 selector.graph_type_instance = graph_selector.type_instance;
122 selector.inst_type_instance = inst_selector.type_instance;
126 } /* }}} instance_get_params */
128 function ident_clone (ident) /* {{{ */
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;
139 } /* }}} ident_clone */
141 function inst_get_defs (inst) /* {{{ */
145 var params = instance_get_params (inst);
146 params.action = "graph_def_json";
149 url: "collection.fcgi",
153 success: function (data)
165 } /* }}} inst_get_defs */
167 function ident_matches (selector, ident) /* {{{ */
169 var part_matches = function (s,p)
174 if ((s == "/any/") || (s == "/all/"))
186 if (!part_matches (selector.host, ident.host))
189 if (!part_matches (selector.plugin, ident.plugin))
192 if (!part_matches (selector.plugin_instance, ident.plugin_instance))
195 if (!part_matches (selector.type, ident.type))
198 if (!part_matches (selector.type_instance, ident.type_instance))
202 } /* }}} function ident_matches */
204 function ident_describe (ident, selector) /* {{{ */
207 var check_field = function (field)
209 if (ident[field].toLowerCase () != selector[field].toLowerCase ())
217 check_field ("host");
218 check_field ("plugin");
219 check_field ("plugin_instance");
220 check_field ("type");
221 check_field ("type_instance");
226 } /* }}} function ident_describe */
228 function def_draw_one (def, data, series_array) /* {{{ */
230 var chart_series = new Object ();
232 chart_series.type = 'line';
233 chart_series.pointInterval = data.interval * 1000.0;
234 chart_series.pointStart = data.first_value_time * 1000.0;
235 chart_series.data = data.data;
236 chart_series.lineWidth = 1;
237 chart_series.shadow = false;
238 chart_series.marker = { enabled: false };
241 chart_series.name = def.legend;
242 else if (def.ds_name)
243 chart_series.name = def.ds_name;
246 chart_series.type = 'area';
249 chart_series.stacking = 'normal';
251 if ((def.color) && (def.color != 'random'))
252 chart_series.color = def.color;
254 series_array.push (chart_series);
255 } /* }}} function def_draw_one */
257 function def_draw (def, data_list, series_array) /* {{{ */
261 for (i = 0; i < data_list.length; i++)
263 if ((def.ds_name) && (def.ds_name != data_list[i].data_source))
265 if (!ident_matches (def.select, data_list[i].file))
268 def_draw_one (def, data_list[i], series_array);
270 } /* }}} function def_draw */
272 function inst_get_chart_opts (inst, def) /* {{{ */
274 var chart_opts = new Object ();
278 renderTo: inst.container,
284 maxZoom: 300, // five minutes
285 title: { text: null },
288 setExtremes: function (event)
293 if ((event.min) && (event.max))
295 begin = event.min / 1000.0;
296 end = event.max / 1000.0;
298 inst_fetch_data (inst, begin, end);
306 formatter: function () { return (value_to_string (this.value)); }
313 labelFormatter: function ()
316 var min = Number.MAX_VALUE;
317 var max = Number.NEGATIVE_INFINITY;
323 for (i = 0; i < this.data.length; i++)
351 return (this.name + " (" + value_to_string (min) + " min, "
352 + value_to_string (avg) + " avg, "
353 + value_to_string (max) + " max)");
356 chart_opts.series = new Array ();
359 chart_opts.title = { text: def.title };
361 if (def.vertical_label)
362 chart_opts.yAxis.title = { text: def.vertical_label };
365 } /* }}} function chart_opts_get */
367 function inst_draw (inst, def, data_list) /* {{{ */
372 if (!inst || !def || !data_list)
375 chart_opts = inst_get_chart_opts (inst, def);
377 for (i = def.defs.length - 1; i >= 0; i--)
378 def_draw (def.defs[i], data_list, chart_opts.series);
380 inst.chart = new Highcharts.Chart (chart_opts);
381 } /* }}} function inst_draw */
383 function inst_redraw (inst, def, data_list) /* {{{ */
389 return (inst_draw (inst, def, data_list));
391 series_array = new Array ();
392 for (i = def.defs.length - 1; i >= 0; i--)
393 def_draw (def.defs[i], data_list, series_array);
395 if (inst.chart.series.length != series_array.length)
397 alert ("inst.chart.series.length != series_array.length");
401 for (i = inst.chart.series.length - 1; i >= 0; i--)
403 series_array[i].visible = inst.chart.series[i].visible;
404 inst.chart.series[i].remove (/* redraw = */ false);
407 for (i = 0; i < series_array.length; i++)
408 inst.chart.addSeries (series_array[i], /* redraw = */ false);
410 inst.chart.redraw ();
411 } /* }}} function inst_redraw */
413 function inst_fetch_data (inst, begin, end) /* {{{ */
418 def = inst_get_defs (inst);
422 params = instance_get_params (inst);
423 params.action = "instance_data_json";
424 params.begin = begin || inst.begin;
425 params.end = end || inst.end;
427 $.getJSON ("collection.fcgi", params,
430 inst_redraw (inst, def, data);
432 } /* }}} inst_fetch_data */
434 function json_graph_update (index) /* {{{ */
438 inst = c4.instances[index];
443 inst.container = "c4-graph" + index;
445 inst_fetch_data (inst);
446 } /* }}} json_graph_update */
448 function format_instance(inst)
450 return ("<li class=\"instance\"><a href=\"" + location.pathname
451 + "?action=show_instance;" + inst.params + "\">" + inst.description
455 function format_instance_list(instances)
457 var ret = "<ul class=\"instance_list\">";
460 if (instances.length == 0)
463 for (i = 0; i < instances.length; i++)
464 ret += format_instance (instances[i]);
471 function format_graph(graph)
473 return ("<li class=\"graph\">" + graph.title + format_instance_list (graph.instances) + "</li>");
476 function update_search_suggestions ()
478 var term = $("#search-input").val ();
481 $("#search-suggest").hide ();
485 $("#search-suggest").show ();
486 $.getJSON ("collection.fcgi",
487 { "action": "search_json", "q": term},
491 $("#search-suggest").html ("");
492 for (i = 0; i < data.length; i++)
495 $("#search-suggest").append (format_graph (graph));
499 } /* update_search_suggestions */
501 function zoom_redraw (jq_obj) /* {{{ */
503 var url = jq_obj.data ("base_url");
505 if ((jq_obj == null) || (url == null))
508 if (jq_obj.data ('begin') != null)
509 url += ";begin=" + jq_obj.data ('begin');
510 if (jq_obj.data ('end') != null)
511 url += ";end=" + jq_obj.data ('end');
513 jq_obj.attr ("src", url);
515 } /* }}} function zoom_redraw */
517 function zoom_reset (graph_id, diff) /* {{{ */
523 jq_obj = $("#" + graph_id);
527 end = new Number ((new Date ()).getTime () / 1000);
528 begin = new Number (end - diff);
530 jq_obj.data ('begin', begin.toFixed (0));
531 jq_obj.data ('end', end.toFixed (0));
533 return (zoom_redraw (jq_obj));
534 } /* }}} function zoom_reset */
536 function zoom_hour (graph_id) /* {{{ */
538 zoom_reset (graph_id, 3600);
539 } /* }}} function zoom_hour */
541 function zoom_day (graph_id) /* {{{ */
543 zoom_reset (graph_id, 86400);
544 } /* }}} function zoom_day */
546 function zoom_week (graph_id) /* {{{ */
548 zoom_reset (graph_id, 7 * 86400);
549 } /* }}} function zoom_week */
551 function zoom_month (graph_id) /* {{{ */
553 zoom_reset (graph_id, 31 * 86400);
554 } /* }}} function zoom_month */
556 function zoom_year (graph_id) /* {{{ */
558 zoom_reset (graph_id, 366 * 86400);
559 } /* }}} function zoom_year */
561 function zoom_relative (graph_id, factor_begin, factor_end) /* {{{ */
568 jq_obj = $("#" + graph_id);
572 begin = jq_obj.data ('begin');
573 end = jq_obj.data ('end');
574 if ((begin == null) || (end == null))
575 return (zoom_day (graph_id));
577 begin = new Number (begin);
578 end = new Number (end);
581 if ((diff <= 300) && (factor_begin > 0.0) && (factor_end < 0.0))
584 jq_obj.data ('begin', begin + (diff * factor_begin));
585 jq_obj.data ('end', end + (diff * factor_end));
587 return (zoom_redraw (jq_obj));
588 } /* }}} function zoom_relative */
590 function zoom_reference (graph_id) /* {{{ */
596 jq_obj = $("#" + graph_id);
600 begin = jq_obj.data ('begin');
601 end = jq_obj.data ('end');
602 if ((begin == null) || (end == null))
605 $(".graph-img img").each (function ()
607 $(this).data ('begin', begin);
608 $(this).data ('end', end);
609 zoom_redraw ($(this));
611 } /* }}} function zoom_reference */
613 function zoom_earlier (graph_id) /* {{{ */
615 return (zoom_relative (graph_id, -0.2, -0.2));
616 } /* }}} function zoom_earlier */
618 function zoom_later (graph_id) /* {{{ */
620 return (zoom_relative (graph_id, +0.2, +0.2));
621 } /* }}} function zoom_later */
623 function zoom_in (graph_id) /* {{{ */
625 return (zoom_relative (graph_id, +0.2, -0.2));
626 } /* }}} function zoom_earlier */
628 function zoom_out (graph_id) /* {{{ */
630 return (zoom_relative (graph_id, (-1.0 / 3.0), (1.0 / 3.0)));
631 } /* }}} function zoom_earlier */
633 $(document).ready(function() {
634 /* $("#layout-middle-right").html ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>"); */
635 $("#search-form").append ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>");
636 $("#search-suggest").hide ();
638 $("#search-input").blur (function()
640 window.setTimeout (function ()
642 $("#search-suggest").hide ();
646 $("#search-input").focus (function()
648 var term = $("#search-input").val ();
651 $("#search-suggest").hide ();
655 $("#search-suggest").show ();
659 $("#search-input").keyup (function()
661 update_search_suggestions ();
665 $(".graph-img").each (function (index, elem)
667 var id = "graph" + graph_count;
670 $(this).find ("img").each (function (img_index, img_elem)
674 $(this).attr ("id", id);
676 base_url = $(this).attr ("src").replace (/;(begin|end)=[^;]*/g, '');
677 $(this).data ("base_url", base_url);
680 $(this).append ("<div class=\"graph-buttons presets\">"
681 + "<div class=\"graph-button\" onClick=\"zoom_hour ('"+id+"');\">H</div>"
682 + "<div class=\"graph-button\" onClick=\"zoom_day ('"+id+"');\">D</div>"
683 + "<div class=\"graph-button\" onClick=\"zoom_week ('"+id+"');\">W</div>"
684 + "<div class=\"graph-button\" onClick=\"zoom_month ('"+id+"');\">M</div>"
685 + "<div class=\"graph-button\" onClick=\"zoom_year ('"+id+"');\">Y</div>"
686 + "<div class=\"graph-button\" onClick=\"zoom_reference ('"+id+"');\">!</div>"
688 + "<div class=\"graph-buttons navigation\">"
689 + "<div class=\"graph-button\" onClick=\"zoom_earlier ('"+id+"');\">←</div>"
690 + "<div class=\"graph-button\" onClick=\"zoom_out ('"+id+"');\">−</div>"
691 + "<div class=\"graph-button\" onClick=\"zoom_in ('"+id+"');\">+</div>"
692 + "<div class=\"graph-button\" onClick=\"zoom_later ('"+id+"');\">→</div>"
698 for (i = 0; i < c4.instances.length; i++)
700 json_graph_update (i);
704 /* vim: set sw=2 sts=2 et fdm=marker : */