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) /* {{{ */
31 var abs_value = Math.abs (value);
33 if ((abs_value < 10000) && (abs_value >= 0.1))
35 else if (abs_value > 1)
37 if (abs_value < 10000000)
38 return ("" + (value / 1000) + "k");
39 else if (abs_value < 10000000000)
40 return ("" + (value / 1000000) + "M");
41 else if (abs_value < 10000000000000)
42 return ("" + (value / 1000000000) + "G");
44 return ("" + (value / 1000000000000) + "T");
48 if (abs_value >= 0.001)
49 return ("" + (value * 1000) + "m");
50 else if (abs_value >= 0.000001)
51 return ("" + (value * 1000000) + "u");
53 return ("" + (value * 1000000000) + "n");
55 } /* }}} function value_to_string */
57 function instance_get_params (inst) /* {{{ */
59 var graph_selector = inst.graph_selector;
60 var inst_selector = inst.instance_selector;
63 if (graph_selector.host == inst_selector.host)
65 selector.host = graph_selector.host;
69 selector.graph_host = graph_selector.host;
70 selector.inst_host = inst_selector.host;
73 if (graph_selector.plugin == inst_selector.plugin)
75 selector.plugin = graph_selector.plugin;
79 selector.graph_plugin = graph_selector.plugin;
80 selector.inst_plugin = inst_selector.plugin;
83 if (graph_selector.plugin_instance == inst_selector.plugin_instance)
85 selector.plugin_instance = graph_selector.plugin_instance;
89 selector.graph_plugin_instance = graph_selector.plugin_instance;
90 selector.inst_plugin_instance = inst_selector.plugin_instance;
93 if (graph_selector.type == inst_selector.type)
95 selector.type = graph_selector.type;
99 selector.graph_type = graph_selector.type;
100 selector.inst_type = inst_selector.type;
103 if (graph_selector.type_instance == inst_selector.type_instance)
105 selector.type_instance = graph_selector.type_instance;
109 selector.graph_type_instance = graph_selector.type_instance;
110 selector.inst_type_instance = inst_selector.type_instance;
114 } /* }}} instance_get_params */
116 function ident_clone (ident) /* {{{ */
120 ret.host = ident.host;
121 ret.plugin = ident.plugin;
122 ret.plugin_instance = ident.plugin_instance;
123 ret.type = ident.type;
124 ret.type_instance = ident.type_instance;
127 } /* }}} ident_clone */
129 function inst_get_defs (inst) /* {{{ */
133 var params = instance_get_params (inst);
134 params.action = "graph_def_json";
137 url: "collection.fcgi",
141 success: function (data)
153 } /* }}} inst_get_defs */
155 function ident_matches (selector, ident) /* {{{ */
157 var part_matches = function (s,p)
162 if ((s == "/any/") || (s == "/all/"))
174 if (!part_matches (selector.host, ident.host))
177 if (!part_matches (selector.plugin, ident.plugin))
180 if (!part_matches (selector.plugin_instance, ident.plugin_instance))
183 if (!part_matches (selector.type, ident.type))
186 if (!part_matches (selector.type_instance, ident.type_instance))
190 } /* }}} function ident_matches */
192 function ident_describe (ident, selector) /* {{{ */
195 var check_field = function (field)
197 if (ident[field].toLowerCase () != selector[field].toLowerCase ())
205 check_field ("host");
206 check_field ("plugin");
207 check_field ("plugin_instance");
208 check_field ("type");
209 check_field ("type_instance");
214 } /* }}} function ident_describe */
216 function def_draw_one (def, data, chart_opts) /* {{{ */
218 var chart_series = new Object ();
220 chart_series.type = 'line';
221 chart_series.name = def.legend || def.data_source;
222 chart_series.pointInterval = data.interval * 1000;
223 chart_series.pointStart = data.first_value_time * 1000;
224 chart_series.data = data.data;
225 chart_series.lineWidth = 1;
226 chart_series.shadow = false;
227 chart_series.marker = { enabled: false };
230 chart_series.type = 'area';
233 chart_series.stacking = 'normal';
235 if ((def.color) && (def.color != 'random'))
236 chart_series.color = def.color;
238 chart_opts.series.push (chart_series);
239 } /* }}} function def_draw_one */
241 function def_draw (def, data_list, chart_opts) /* {{{ */
245 for (i = 0; i < data_list.length; i++)
247 if ((def.ds_name) && (def.ds_name != data_list[i].data_source))
249 if (!ident_matches (def.select, data_list[i].file))
252 def_draw_one (def, data_list[i], chart_opts);
254 } /* }}} function def_draw */
256 function instance_draw (inst, def, data_list) /* {{{ */
262 var chart_opts = new Object ();
264 if (!inst || !def || !data_list)
269 renderTo: inst.container,
275 maxZoom: 300, // five minutes
276 title: { text: null }
282 formatter: function () { return (value_to_string (this.value)); }
286 chart_opts.series = new Array ();
289 chart_opts.title = { text: def.title };
291 for (i = def.defs.length - 1; i >= 0; i--)
292 def_draw (def.defs[i], data_list, chart_opts);
294 // if ((def.defs.length == 0) && (data_list.length == 1))
295 // def_draw_one (null, data_list[0], chart_opts);
297 inst.chart = new Highcharts.Chart (chart_opts);
298 } /* }}} function instance_draw */
300 function json_graph_update (index) /* {{{ */
306 inst = c4.instances[index];
310 def = inst_get_defs (inst);
315 inst.container = "c4-graph" + index;
317 params = instance_get_params (inst);
318 params.action = "instance_data_json";
319 params.begin = inst.begin;
320 params.end = inst.end;
322 $.getJSON ("collection.fcgi", params,
325 instance_draw (inst, def, data);
327 } /* }}} json_graph_update */
329 function format_instance(inst)
331 return ("<li class=\"instance\"><a href=\"" + location.pathname
332 + "?action=show_instance;" + inst.params + "\">" + inst.description
336 function format_instance_list(instances)
338 var ret = "<ul class=\"instance_list\">";
341 if (instances.length == 0)
344 for (i = 0; i < instances.length; i++)
345 ret += format_instance (instances[i]);
352 function format_graph(graph)
354 return ("<li class=\"graph\">" + graph.title + format_instance_list (graph.instances) + "</li>");
357 function update_search_suggestions ()
359 var term = $("#search-input").val ();
362 $("#search-suggest").hide ();
366 $("#search-suggest").show ();
367 $.getJSON ("collection.fcgi",
368 { "action": "search_json", "q": term},
372 $("#search-suggest").html ("");
373 for (i = 0; i < data.length; i++)
376 $("#search-suggest").append (format_graph (graph));
380 } /* update_search_suggestions */
382 function zoom_redraw (jq_obj) /* {{{ */
384 var url = jq_obj.data ("base_url");
386 if ((jq_obj == null) || (url == null))
389 if (jq_obj.data ('begin') != null)
390 url += ";begin=" + jq_obj.data ('begin');
391 if (jq_obj.data ('end') != null)
392 url += ";end=" + jq_obj.data ('end');
394 jq_obj.attr ("src", url);
396 } /* }}} function zoom_redraw */
398 function zoom_reset (graph_id, diff) /* {{{ */
404 jq_obj = $("#" + graph_id);
408 end = new Number ((new Date ()).getTime () / 1000);
409 begin = new Number (end - diff);
411 jq_obj.data ('begin', begin.toFixed (0));
412 jq_obj.data ('end', end.toFixed (0));
414 return (zoom_redraw (jq_obj));
415 } /* }}} function zoom_reset */
417 function zoom_hour (graph_id) /* {{{ */
419 zoom_reset (graph_id, 3600);
420 } /* }}} function zoom_hour */
422 function zoom_day (graph_id) /* {{{ */
424 zoom_reset (graph_id, 86400);
425 } /* }}} function zoom_day */
427 function zoom_week (graph_id) /* {{{ */
429 zoom_reset (graph_id, 7 * 86400);
430 } /* }}} function zoom_week */
432 function zoom_month (graph_id) /* {{{ */
434 zoom_reset (graph_id, 31 * 86400);
435 } /* }}} function zoom_month */
437 function zoom_year (graph_id) /* {{{ */
439 zoom_reset (graph_id, 366 * 86400);
440 } /* }}} function zoom_year */
442 function zoom_relative (graph_id, factor_begin, factor_end) /* {{{ */
449 jq_obj = $("#" + graph_id);
453 begin = jq_obj.data ('begin');
454 end = jq_obj.data ('end');
455 if ((begin == null) || (end == null))
456 return (zoom_day (graph_id));
458 begin = new Number (begin);
459 end = new Number (end);
462 if ((diff <= 300) && (factor_begin > 0.0) && (factor_end < 0.0))
465 jq_obj.data ('begin', begin + (diff * factor_begin));
466 jq_obj.data ('end', end + (diff * factor_end));
468 return (zoom_redraw (jq_obj));
469 } /* }}} function zoom_relative */
471 function zoom_reference (graph_id) /* {{{ */
477 jq_obj = $("#" + graph_id);
481 begin = jq_obj.data ('begin');
482 end = jq_obj.data ('end');
483 if ((begin == null) || (end == null))
486 $(".graph-img img").each (function ()
488 $(this).data ('begin', begin);
489 $(this).data ('end', end);
490 zoom_redraw ($(this));
492 } /* }}} function zoom_reference */
494 function zoom_earlier (graph_id) /* {{{ */
496 return (zoom_relative (graph_id, -0.2, -0.2));
497 } /* }}} function zoom_earlier */
499 function zoom_later (graph_id) /* {{{ */
501 return (zoom_relative (graph_id, +0.2, +0.2));
502 } /* }}} function zoom_later */
504 function zoom_in (graph_id) /* {{{ */
506 return (zoom_relative (graph_id, +0.2, -0.2));
507 } /* }}} function zoom_earlier */
509 function zoom_out (graph_id) /* {{{ */
511 return (zoom_relative (graph_id, (-1.0 / 3.0), (1.0 / 3.0)));
512 } /* }}} function zoom_earlier */
514 $(document).ready(function() {
515 /* $("#layout-middle-right").html ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>"); */
516 $("#search-form").append ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>");
517 $("#search-suggest").hide ();
519 $("#search-input").blur (function()
521 window.setTimeout (function ()
523 $("#search-suggest").hide ();
527 $("#search-input").focus (function()
529 var term = $("#search-input").val ();
532 $("#search-suggest").hide ();
536 $("#search-suggest").show ();
540 $("#search-input").keyup (function()
542 update_search_suggestions ();
546 $(".graph-img").each (function (index, elem)
548 var id = "graph" + graph_count;
551 $(this).find ("img").each (function (img_index, img_elem)
555 $(this).attr ("id", id);
557 base_url = $(this).attr ("src").replace (/;(begin|end)=[^;]*/g, '');
558 $(this).data ("base_url", base_url);
561 $(this).append ("<div class=\"graph-buttons presets\">"
562 + "<div class=\"graph-button\" onClick=\"zoom_hour ('"+id+"');\">H</div>"
563 + "<div class=\"graph-button\" onClick=\"zoom_day ('"+id+"');\">D</div>"
564 + "<div class=\"graph-button\" onClick=\"zoom_week ('"+id+"');\">W</div>"
565 + "<div class=\"graph-button\" onClick=\"zoom_month ('"+id+"');\">M</div>"
566 + "<div class=\"graph-button\" onClick=\"zoom_year ('"+id+"');\">Y</div>"
567 + "<div class=\"graph-button\" onClick=\"zoom_reference ('"+id+"');\">!</div>"
569 + "<div class=\"graph-buttons navigation\">"
570 + "<div class=\"graph-button\" onClick=\"zoom_earlier ('"+id+"');\">←</div>"
571 + "<div class=\"graph-button\" onClick=\"zoom_out ('"+id+"');\">−</div>"
572 + "<div class=\"graph-button\" onClick=\"zoom_in ('"+id+"');\">+</div>"
573 + "<div class=\"graph-button\" onClick=\"zoom_later ('"+id+"');\">→</div>"
579 for (i = 0; i < c4.instances.length; i++)
581 json_graph_update (i);
585 /* vim: set sw=2 sts=2 et fdm=marker : */