X-Git-Url: https://git.octo.it/?p=collection4.git;a=blobdiff_plain;f=share%2Fcollection.js;h=0f41089223d7aa88cef72c103039048db9447e9e;hp=86ba87d36a1d11ea1fc7613334448751d86a31e6;hb=110247dd90ceaec4d9fee197fce64cfd63101519;hpb=4f4a330557182d024ed5854bd9c79880b6fdaeb6 diff --git a/share/collection.js b/share/collection.js index 86ba87d..0f41089 100644 --- a/share/collection.js +++ b/share/collection.js @@ -1,91 +1,381 @@ +/** + * collection4 - collection.js + * Copyright (C) 2010-2013 Florian octo Forster + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + * + * Authors: + * Florian octo Forster + **/ + var c4 = { - graphs: [] + instances: new Array (), + config: + { + width: 324, + height: 200 + } }; -function json_graph_get_def (graph) +function value_to_string (value) /* {{{ */ +{ + var abs_value; + var v2s = function (value) + { + var tmp = Math.round (100.0 * value) / 100.0; + return ("" + tmp); + } + + if (value == null) + return ('NaN'); + else if (value == 0) + return ('0'); + + abs_value = Math.abs (value); + + if ((abs_value < 10000) && (abs_value >= 0.1)) + return (v2s (value)); + else if (abs_value > 1) + { + if (abs_value < 10000000) + return (v2s (value / 1000) + "k"); + else if (abs_value < 10000000000) + return (v2s (value / 1000000) + "M"); + else if (abs_value < 10000000000000) + return (v2s (value / 1000000000) + "G"); + else + return (v2s (value / 1000000000000) + "T"); + } + else + { + if (abs_value >= 0.001) + return (v2s (value * 1000) + "m"); + else if (abs_value >= 0.000001) + return (v2s (value * 1000000) + "u"); + else + return (v2s (value * 1000000000) + "n"); + } +} /* }}} function value_to_string */ + +function instance_get_params (inst) /* {{{ */ +{ + var graph_selector = inst.graph_selector; + var inst_selector = inst.instance_selector; + var selector = {}; + + if (graph_selector.host == inst_selector.host) + { + selector.host = graph_selector.host; + } + else + { + selector.graph_host = graph_selector.host; + selector.inst_host = inst_selector.host; + } + + if (graph_selector.plugin == inst_selector.plugin) + { + selector.plugin = graph_selector.plugin; + } + else + { + selector.graph_plugin = graph_selector.plugin; + selector.inst_plugin = inst_selector.plugin; + } + + if (graph_selector.plugin_instance == inst_selector.plugin_instance) + { + selector.plugin_instance = graph_selector.plugin_instance; + } + else + { + selector.graph_plugin_instance = graph_selector.plugin_instance; + selector.inst_plugin_instance = inst_selector.plugin_instance; + } + + if (graph_selector.type == inst_selector.type) + { + selector.type = graph_selector.type; + } + else + { + selector.graph_type = graph_selector.type; + selector.inst_type = inst_selector.type; + } + + if (graph_selector.type_instance == inst_selector.type_instance) + { + selector.type_instance = graph_selector.type_instance; + } + else + { + selector.graph_type_instance = graph_selector.type_instance; + selector.inst_type_instance = inst_selector.type_instance; + } + + return (selector); +} /* }}} instance_get_params */ + +function ident_clone (ident) /* {{{ */ +{ + var ret = {}; + + ret.host = ident.host; + ret.plugin = ident.plugin; + ret.plugin_instance = ident.plugin_instance; + ret.type = ident.type; + ret.type_instance = ident.type_instance; + + return (ret); +} /* }}} ident_clone */ + +function inst_get_defs (inst) /* {{{ */ { - if (!graph.def) + if (!inst.def) { + var params = instance_get_params (inst); + params.action = "graph_def_json"; + $.ajax({ - url: "collection.fcgi?action=graph_def_json;" + graph.params, + url: "collection.fcgi", async: false, dataType: 'json', + data: params, success: function (data) { if (!data) return; - graph.def = data; + inst.def = data; }}); } - if (graph.def) - return (graph.def); + if (inst.def) + return (inst.def); return; -} /* json_graph_get_def */ +} /* }}} inst_get_defs */ -function json_graph_update(index) +function ident_matches (selector, ident) /* {{{ */ { - var graph; - var def; + var part_matches = function (s,p) + { + if (s == null) + return (false); - graph = c4.graphs[index]; - if (!graph) - return; + if ((s == "/any/") || (s == "/all/")) + return (true); - def = json_graph_get_def (graph); - if (!def) - return; + if (p == null) + return (false); + + if (s == p) + return (true); + + return (false); + }; + + if (!part_matches (selector.host, ident.host)) + return (false); + + if (!part_matches (selector.plugin, ident.plugin)) + return (false); + + if (!part_matches (selector.plugin_instance, ident.plugin_instance)) + return (false); + + if (!part_matches (selector.type, ident.type)) + return (false); + + if (!part_matches (selector.type_instance, ident.type_instance)) + return (false); + + return (true); +} /* }}} function ident_matches */ + +function ident_describe (ident, selector) /* {{{ */ +{ + var ret = ""; + var check_field = function (field) + { + if (ident[field].toLowerCase () != selector[field].toLowerCase ()) + { + if (ret != "") + ret += "/"; + ret += ident[field]; + } + }; - if (!graph.raphael) + check_field ("host"); + check_field ("plugin"); + check_field ("plugin_instance"); + check_field ("type"); + check_field ("type_instance"); + + if (ret == "") + return (null); + return (ret); +} /* }}} function ident_describe */ + +/* + * Given one metric definition, returns the appropriate metric data from the + * graph data (data list). */ +function metric_def_get_data (metric_def, graph_data) +{ + var i; + + for (i = 0; i < graph_data.length; i++) + { + if ((metric_def.ds_name) && (metric_def.ds_name != graph_data[i].data_source)) + continue; + if (!ident_matches (metric_def.select, graph_data[i].file)) + continue; + + return (graph_data[i]); + } + return; +} + +function metric_def_to_rickshaw_series (metric_def, metric_data) +{ + var series = { + data: [] + }; + var i; + + if (metric_def.legend) + series.name = metric_def.legend; + + if (metric_def.color) + series.color = metric_def.color; + + for (i = 0; i < metric_data.data.length; i++) + { + var x = metric_data.first_value_time + (i * metric_data.interval); + var y = metric_data.data[i]; + + series.data.push ({'x': x, 'y': y}); + } + + return (series); +} + +function graph_def_to_rickshaw_config (root_element, graph_def, data_list) +{ + var graph_config = { + element: root_element, + renderer: 'line', + series: [] + }; + var graph; + var i; + + for (i = 0; i < graph_def.defs.length; i++) { - graph.raphael = Raphael ("c4-graph" + index); + var metric_def = graph_def.defs[i]; + var metric_data = metric_def_get_data (metric_def, data_list); + var series; + + if (!metric_data) + continue; + + series = metric_def_to_rickshaw_series (metric_def, metric_data); + if (series) + graph_config.series.push (series); } - $.getJSON ("collection.fcgi?action=graph_data_json;" + graph.params + ";begin=-3600;end=0", + return (graph_config); +} + +function graph_def_to_rickshaw_graph (root_element, graph_def, graph_data) +{ + var graph_config = graph_def_to_rickshaw_config (root_element, graph_def, graph_data); + + var graph = new Rickshaw.Graph (graph_config); + graph.render (); + + var x_axis = new Rickshaw.Graph.Axis.Time({ + graph: graph + }); + x_axis.render (); + + var y_axis = new Rickshaw.Graph.Axis.Y({ + graph: graph + }); + y_axis.render (); +} + +function inst_draw_rickshaw (inst, graph_def, graph_data) +{ + var root_element = document.getElementById (inst.container); + + inst.chart = graph_def_to_rickshaw_graph (root_element, graph_def, graph_data); +} + +function inst_draw (inst, graph_def, graph_data) +{ + inst_draw_rickshaw (inst, graph_def, graph_data); +} + +function inst_redraw (inst, graph_def, graph_data) /* {{{ */ +{ + var series_array; + var i; + + if (!inst.chart) + return (inst_draw (inst, graph_def, graph_data)); + else + return; /* TODO: Insert new data into the graph */ +} /* }}} function inst_redraw */ + +function inst_fetch_data (inst, begin, end) /* {{{ */ +{ + var graph_def; + var params; + + graph_def = inst_get_defs (inst); + if (!graph_def) + return; + + params = instance_get_params (inst); + params.action = "instance_data_json"; + params.begin = begin || inst.begin; + params.end = end || inst.end; + params.resolution = (params.end - params.begin) / c4.config.width; + + $.getJSON ("collection.fcgi", params, function (data) { - var x_data = []; - var y_data = []; - var i; + inst_redraw (inst, graph_def, data); + }); /* getJSON */ +} /* }}} inst_fetch_data */ - if (!data) - return; +function json_graph_update (index) /* {{{ */ +{ + var inst; - for (i = 0; i < data.length; i++) - { - var ds = data[i]; - - var j; - var x = []; - var y = []; - - for (j = 0; j < ds.data.length; j++) - { - var dp = ds.data[j]; - var t = dp[0]; - var v = dp[1]; - - if (v == null) - continue; - - x.push (t); - y.push (v); - } - - x_data.push (x); - y_data.push (y); - } - - graph.raphael.clear (); - if (def.title) - graph.raphael.g.text (250, 15, def.title); - if (def.vertical_label) - graph.raphael.g.text (5, 100, def.vertical_label).rotate (270); - graph.raphael.g.linechart(50, 25, 500, 150, x_data, y_data, {axis: "0 0 1 1"}); - }); /* getJSON */ -} /* json_graph_update */ + inst = c4.instances[index]; + if (!inst) + return; + + if (!inst.container) + inst.container = "c4-graph" + index; + + inst_fetch_data (inst); +} /* }}} json_graph_update */ function format_instance(inst) { @@ -272,6 +562,26 @@ function zoom_out (graph_id) /* {{{ */ return (zoom_relative (graph_id, (-1.0 / 3.0), (1.0 / 3.0))); } /* }}} function zoom_earlier */ +function graph_recalc_width () /* {{{ */ +{ + var tmp; + + tmp = $("#layout-middle-center").width (); + if (!tmp) + return; + + if (tmp < 324) + tmp = 324; + + c4.config.width = tmp; + c4.config.height = Math.round (tmp / 1.61803398874989484820); + $(".graph-json").each (function () + { + $(this).width (c4.config.width); + $(this).height (c4.config.height); + }); +} /* }}} function graph_recalc_width */ + $(document).ready(function() { /* $("#layout-middle-right").html (""); */ $("#search-form").append (""); @@ -328,16 +638,18 @@ $(document).ready(function() { + "
!
" + "" + "
" - + "
←
" - + "
−
" + + "
\u2190
" + + "
\u2212
" + "
+
" - + "
→
" + + "
\u2192
" + "
" ); }); + graph_recalc_width (); + var i; - for (i = 0; i < c4.graphs.length; i++) + for (i = 0; i < c4.instances.length; i++) { json_graph_update (i); }