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 instance_get_params (graph) /* {{{ */
31 var graph_selector = graph.graph_selector;
32 var inst_selector = graph.instance_selector;
35 if (graph_selector.host == inst_selector.host)
37 selector.host = graph_selector.host;
41 selector.graph_host = graph_selector.host;
42 selector.inst_host = inst_selector.host;
45 if (graph_selector.plugin == inst_selector.plugin)
47 selector.plugin = graph_selector.plugin;
51 selector.graph_plugin = graph_selector.plugin;
52 selector.inst_plugin = inst_selector.plugin;
55 if (graph_selector.plugin_instance == inst_selector.plugin_instance)
57 selector.plugin_instance = graph_selector.plugin_instance;
61 selector.graph_plugin_instance = graph_selector.plugin_instance;
62 selector.inst_plugin_instance = inst_selector.plugin_instance;
65 if (graph_selector.type == inst_selector.type)
67 selector.type = graph_selector.type;
71 selector.graph_type = graph_selector.type;
72 selector.inst_type = inst_selector.type;
75 if (graph_selector.type_instance == inst_selector.type_instance)
77 selector.type_instance = graph_selector.type_instance;
81 selector.graph_type_instance = graph_selector.type_instance;
82 selector.inst_type_instance = inst_selector.type_instance;
86 } /* }}} instance_get_params */
88 function ident_clone (ident) /* {{{ */
92 ret.host = ident.host;
93 ret.plugin = ident.plugin;
94 ret.plugin_instance = ident.plugin_instance;
95 ret.type = ident.type;
96 ret.type_instance = ident.type_instance;
99 } /* }}} ident_clone */
101 function graph_get_defs (graph)
105 var params = ident_clone (graph.graph_selector);
106 params.action = "graph_def_json";
109 url: "collection.fcgi",
113 success: function (data)
125 } /* graph_get_defs */
127 function instance_draw (inst, def, data)
133 if (!inst || !def || !data)
136 for (i = 0; i < data.length; i++)
145 x_val = ds.first_value_time;
147 for (j = 0; j < ds.data.length; j++)
149 var y_val = ds.data[j];
154 x_val += ds.interval;
161 inst.raphael.clear ();
163 inst.raphael.g.text (250, 15, def.title);
164 if (def.vertical_label)
165 inst.raphael.g.text (5, 100, def.vertical_label).rotate (270);
166 inst.raphael.g.linechart(50, 25, 500, 150, x_data, y_data, {axis: "0 0 1 1"});
169 function json_graph_update (index)
175 inst = c4.instances[index];
179 def = graph_get_defs (inst);
184 inst.raphael = Raphael ("c4-graph" + index);
186 params = instance_get_params (inst);
187 params.action = "instance_data_json";
188 params.begin = inst.begin;
189 params.end = inst.end;
191 $.getJSON ("collection.fcgi", params,
194 instance_draw (inst, def, data);
196 } /* json_graph_update */
198 function format_instance(inst)
200 return ("<li class=\"instance\"><a href=\"" + location.pathname
201 + "?action=show_instance;" + inst.params + "\">" + inst.description
205 function format_instance_list(instances)
207 var ret = "<ul class=\"instance_list\">";
210 if (instances.length == 0)
213 for (i = 0; i < instances.length; i++)
214 ret += format_instance (instances[i]);
221 function format_graph(graph)
223 return ("<li class=\"graph\">" + graph.title + format_instance_list (graph.instances) + "</li>");
226 function update_search_suggestions ()
228 var term = $("#search-input").val ();
231 $("#search-suggest").hide ();
235 $("#search-suggest").show ();
236 $.getJSON ("collection.fcgi",
237 { "action": "search_json", "q": term},
241 $("#search-suggest").html ("");
242 for (i = 0; i < data.length; i++)
245 $("#search-suggest").append (format_graph (graph));
249 } /* update_search_suggestions */
251 function zoom_redraw (jq_obj) /* {{{ */
253 var url = jq_obj.data ("base_url");
255 if ((jq_obj == null) || (url == null))
258 if (jq_obj.data ('begin') != null)
259 url += ";begin=" + jq_obj.data ('begin');
260 if (jq_obj.data ('end') != null)
261 url += ";end=" + jq_obj.data ('end');
263 jq_obj.attr ("src", url);
265 } /* }}} function zoom_redraw */
267 function zoom_reset (graph_id, diff) /* {{{ */
273 jq_obj = $("#" + graph_id);
277 end = new Number ((new Date ()).getTime () / 1000);
278 begin = new Number (end - diff);
280 jq_obj.data ('begin', begin.toFixed (0));
281 jq_obj.data ('end', end.toFixed (0));
283 return (zoom_redraw (jq_obj));
284 } /* }}} function zoom_reset */
286 function zoom_hour (graph_id) /* {{{ */
288 zoom_reset (graph_id, 3600);
289 } /* }}} function zoom_hour */
291 function zoom_day (graph_id) /* {{{ */
293 zoom_reset (graph_id, 86400);
294 } /* }}} function zoom_day */
296 function zoom_week (graph_id) /* {{{ */
298 zoom_reset (graph_id, 7 * 86400);
299 } /* }}} function zoom_week */
301 function zoom_month (graph_id) /* {{{ */
303 zoom_reset (graph_id, 31 * 86400);
304 } /* }}} function zoom_month */
306 function zoom_year (graph_id) /* {{{ */
308 zoom_reset (graph_id, 366 * 86400);
309 } /* }}} function zoom_year */
311 function zoom_relative (graph_id, factor_begin, factor_end) /* {{{ */
318 jq_obj = $("#" + graph_id);
322 begin = jq_obj.data ('begin');
323 end = jq_obj.data ('end');
324 if ((begin == null) || (end == null))
325 return (zoom_day (graph_id));
327 begin = new Number (begin);
328 end = new Number (end);
331 if ((diff <= 300) && (factor_begin > 0.0) && (factor_end < 0.0))
334 jq_obj.data ('begin', begin + (diff * factor_begin));
335 jq_obj.data ('end', end + (diff * factor_end));
337 return (zoom_redraw (jq_obj));
338 } /* }}} function zoom_relative */
340 function zoom_reference (graph_id) /* {{{ */
346 jq_obj = $("#" + graph_id);
350 begin = jq_obj.data ('begin');
351 end = jq_obj.data ('end');
352 if ((begin == null) || (end == null))
355 $(".graph-img img").each (function ()
357 $(this).data ('begin', begin);
358 $(this).data ('end', end);
359 zoom_redraw ($(this));
361 } /* }}} function zoom_reference */
363 function zoom_earlier (graph_id) /* {{{ */
365 return (zoom_relative (graph_id, -0.2, -0.2));
366 } /* }}} function zoom_earlier */
368 function zoom_later (graph_id) /* {{{ */
370 return (zoom_relative (graph_id, +0.2, +0.2));
371 } /* }}} function zoom_later */
373 function zoom_in (graph_id) /* {{{ */
375 return (zoom_relative (graph_id, +0.2, -0.2));
376 } /* }}} function zoom_earlier */
378 function zoom_out (graph_id) /* {{{ */
380 return (zoom_relative (graph_id, (-1.0 / 3.0), (1.0 / 3.0)));
381 } /* }}} function zoom_earlier */
383 $(document).ready(function() {
384 /* $("#layout-middle-right").html ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>"); */
385 $("#search-form").append ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>");
386 $("#search-suggest").hide ();
388 $("#search-input").blur (function()
390 window.setTimeout (function ()
392 $("#search-suggest").hide ();
396 $("#search-input").focus (function()
398 var term = $("#search-input").val ();
401 $("#search-suggest").hide ();
405 $("#search-suggest").show ();
409 $("#search-input").keyup (function()
411 update_search_suggestions ();
415 $(".graph-img").each (function (index, elem)
417 var id = "graph" + graph_count;
420 $(this).find ("img").each (function (img_index, img_elem)
424 $(this).attr ("id", id);
426 base_url = $(this).attr ("src").replace (/;(begin|end)=[^;]*/g, '');
427 $(this).data ("base_url", base_url);
430 $(this).append ("<div class=\"graph-buttons presets\">"
431 + "<div class=\"graph-button\" onClick=\"zoom_hour ('"+id+"');\">H</div>"
432 + "<div class=\"graph-button\" onClick=\"zoom_day ('"+id+"');\">D</div>"
433 + "<div class=\"graph-button\" onClick=\"zoom_week ('"+id+"');\">W</div>"
434 + "<div class=\"graph-button\" onClick=\"zoom_month ('"+id+"');\">M</div>"
435 + "<div class=\"graph-button\" onClick=\"zoom_year ('"+id+"');\">Y</div>"
436 + "<div class=\"graph-button\" onClick=\"zoom_reference ('"+id+"');\">!</div>"
438 + "<div class=\"graph-buttons navigation\">"
439 + "<div class=\"graph-button\" onClick=\"zoom_earlier ('"+id+"');\">←</div>"
440 + "<div class=\"graph-button\" onClick=\"zoom_out ('"+id+"');\">−</div>"
441 + "<div class=\"graph-button\" onClick=\"zoom_in ('"+id+"');\">+</div>"
442 + "<div class=\"graph-button\" onClick=\"zoom_later ('"+id+"');\">→</div>"
448 for (i = 0; i < c4.instances.length; i++)
450 json_graph_update (i);
454 /* vim: set sw=2 sts=2 et fdm=marker : */