/**
* collection4 - collection.js
- * Copyright (C) 2010 Florian octo Forster
+ * 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
* Boston, MA 02110-1301 USA
*
* Authors:
- * Florian octo Forster <ff at octo.it>
+ * Florian octo Forster <octo at collectd.org>
**/
var c4 =
{
- instances: []
+ instances: new Array (),
+ config:
+ {
+ width: 324,
+ height: 200
+ }
};
function value_to_string (value) /* {{{ */
{
- var abs_value = Math.abs (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 ("" + value);
+ return (v2s (value));
else if (abs_value > 1)
{
if (abs_value < 10000000)
- return ("" + (value / 1000) + "k");
+ return (v2s (value / 1000) + "k");
else if (abs_value < 10000000000)
- return ("" + (value / 1000000) + "M");
+ return (v2s (value / 1000000) + "M");
else if (abs_value < 10000000000000)
- return ("" + (value / 1000000000) + "G");
+ return (v2s (value / 1000000000) + "G");
else
- return ("" + (value / 1000000000000) + "T");
+ return (v2s (value / 1000000000000) + "T");
}
else
{
if (abs_value >= 0.001)
- return ("" + (value * 1000) + "m");
+ return (v2s (value * 1000) + "m");
else if (abs_value >= 0.000001)
- return ("" + (value * 1000000) + "u");
+ return (v2s (value * 1000000) + "u");
else
- return ("" + (value * 1000000000) + "n");
+ return (v2s (value * 1000000000) + "n");
}
} /* }}} function value_to_string */
-function instance_get_params (graph) /* {{{ */
+function instance_get_params (inst) /* {{{ */
{
- var graph_selector = graph.graph_selector;
- var inst_selector = graph.instance_selector;
+ var graph_selector = inst.graph_selector;
+ var inst_selector = inst.instance_selector;
var selector = {};
if (graph_selector.host == inst_selector.host)
return (ret);
} /* }}} ident_clone */
-function graph_get_defs (graph) /* {{{ */
+function inst_get_defs (inst) /* {{{ */
{
- if (!graph.def)
+ if (!inst.def)
{
- var params = ident_clone (graph.graph_selector);
+ var params = instance_get_params (inst);
params.action = "graph_def_json";
$.ajax({
if (!data)
return;
- graph.def = data;
+ inst.def = data;
}});
}
- if (graph.def)
- return (graph.def);
+ if (inst.def)
+ return (inst.def);
return;
-} /* }}} graph_get_defs */
+} /* }}} inst_get_defs */
function ident_matches (selector, ident) /* {{{ */
{
return (ret);
} /* }}} function ident_describe */
-function def_draw_one (def, data, chart_opts) /* {{{ */
+/*
+ * 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 chart_series = new Object ();
+ 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;
+}
- chart_series.type = 'line';
- chart_series.name = def.legend || def.data_source;
- chart_series.pointInterval = data.interval * 1000;
- chart_series.pointStart = data.first_value_time * 1000;
- chart_series.data = data.data;
- chart_series.lineWidth = 1;
- chart_series.shadow = false;
- chart_series.marker = { enabled: false };
+function metric_def_to_rickshaw_series (metric_def, metric_data)
+{
+ var series = {
+ data: []
+ };
+ var i;
- if (def.area)
- chart_series.type = 'area';
+ if (metric_def.legend)
+ series.name = metric_def.legend;
- if (def.stack)
- chart_series.stacking = 'normal';
+ if (metric_def.color)
+ series.color = metric_def.color;
- if ((def.color) && (def.color != 'random'))
- chart_series.color = 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];
- chart_opts.series.push (chart_series);
-} /* }}} function def_draw_one */
+ series.data.push ({'x': x, 'y': y});
+ }
-function def_draw (def, data_list, chart_opts) /* {{{ */
+ 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 < data_list.length; i++)
+ for (i = 0; i < graph_def.defs.length; i++)
{
- if ((def.ds_name) && (def.ds_name != data_list[i].data_source))
- continue;
- if (!ident_matches (def.select, data_list[i].file))
+ var metric_def = graph_def.defs[i];
+ var metric_data = metric_def_get_data (metric_def, data_list);
+ var series;
+
+ if (!metric_data)
continue;
- def_draw_one (def, data_list[i], chart_opts);
+ series = metric_def_to_rickshaw_series (metric_def, metric_data);
+ if (series)
+ graph_config.series.push (series);
}
-} /* }}} function def_draw */
-function instance_draw (inst, def, data_list) /* {{{ */
+ return (graph_config);
+}
+
+function graph_def_to_rickshaw_graph (root_element, graph_def, graph_data)
{
- var x_data = [];
- var y_data = [];
- var i;
+ var graph_config = graph_def_to_rickshaw_config (root_element, graph_def, graph_data);
- var chart_opts = new Object ();
+ var graph = new Rickshaw.Graph (graph_config);
+ graph.render ();
- if (!inst || !def || !data_list)
- return;
+ var x_axis = new Rickshaw.Graph.Axis.Time({
+ graph: graph
+ });
+ x_axis.render ();
- chart_opts.chart =
- {
- renderTo: inst.container,
- zoomType: 'x'
- };
- chart_opts.xAxis =
- {
- type: 'datetime',
- maxZoom: 300, // five minutes
- title: { text: null }
- };
- chart_opts.yAxis =
- {
- labels:
- {
- formatter: function () { return (value_to_string (this.value)); }
- },
- endOnTick: false
- };
- chart_opts.series = new Array ();
+ var y_axis = new Rickshaw.Graph.Axis.Y({
+ graph: graph
+ });
+ y_axis.render ();
+}
- if (def.title)
- chart_opts.title = { text: def.title };
+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);
+}
- for (i = def.defs.length - 1; i >= 0; i--)
- def_draw (def.defs[i], data_list, chart_opts);
+function inst_draw (inst, graph_def, graph_data)
+{
+ inst_draw_rickshaw (inst, graph_def, graph_data);
+}
-// if ((def.defs.length == 0) && (data_list.length == 1))
-// def_draw_one (null, data_list[0], chart_opts);
+function inst_redraw (inst, graph_def, graph_data) /* {{{ */
+{
+ var series_array;
+ var i;
- inst.chart = new Highcharts.Chart (chart_opts);
-} /* }}} function instance_draw */
+ if (!inst.chart)
+ return (inst_draw (inst, graph_def, graph_data));
+ else
+ return; /* TODO: Insert new data into the graph */
+} /* }}} function inst_redraw */
-function json_graph_update (index) /* {{{ */
+function inst_fetch_data (inst, begin, end) /* {{{ */
{
- var inst;
- var def;
+ var graph_def;
var params;
- inst = c4.instances[index];
- if (!inst)
+ graph_def = inst_get_defs (inst);
+ if (!graph_def)
return;
- def = graph_get_defs (inst);
- if (!def)
- return;
-
- if (!inst.container)
- inst.container = "c4-graph" + index;
-
params = instance_get_params (inst);
params.action = "instance_data_json";
- params.begin = inst.begin;
- params.end = inst.end;
+ 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)
{
- instance_draw (inst, def, data);
+ inst_redraw (inst, graph_def, data);
}); /* getJSON */
+} /* }}} inst_fetch_data */
+
+function json_graph_update (index) /* {{{ */
+{
+ var inst;
+
+ 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)
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 ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>"); */
$("#search-form").append ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>");
+ "<div class=\"graph-button\" onClick=\"zoom_reference ('"+id+"');\">!</div>"
+ "</div>"
+ "<div class=\"graph-buttons navigation\">"
- + "<div class=\"graph-button\" onClick=\"zoom_earlier ('"+id+"');\">←</div>"
- + "<div class=\"graph-button\" onClick=\"zoom_out ('"+id+"');\">−</div>"
+ + "<div class=\"graph-button\" onClick=\"zoom_earlier ('"+id+"');\">\u2190</div>"
+ + "<div class=\"graph-button\" onClick=\"zoom_out ('"+id+"');\">\u2212</div>"
+ "<div class=\"graph-button\" onClick=\"zoom_in ('"+id+"');\">+</div>"
- + "<div class=\"graph-button\" onClick=\"zoom_later ('"+id+"');\">→</div>"
+ + "<div class=\"graph-button\" onClick=\"zoom_later ('"+id+"');\">\u2192</div>"
+ "</div>"
);
});
+ graph_recalc_width ();
+
var i;
for (i = 0; i < c4.instances.length; i++)
{