]> nos-oignons.net Git - website.git/blobdiff - assets/bw_graphs.js
Merge branch 'master' of ssh://bulbe.nos-oignons.net/website
[website.git] / assets / bw_graphs.js
index 567d43a3ac0368d0d0eca541ca5e6d5b8b257fc6..8091271c302c3b659a7f415a075c28477025fa32 100644 (file)
@@ -7,34 +7,38 @@ BwDrawer.margin = {top: 50, right: 10, bottom: 90, left: 130};
 BwDrawer.width = 600 - BwDrawer.margin.left - BwDrawer.margin.right;
 BwDrawer.height = 400 - BwDrawer.margin.top - BwDrawer.margin.bottom;
 
-BwDrawer.parseTime = d3.timeFormat("%Y-%m-%d %H:%M:%S").parse;
-BwDrawer.bwFormatter = d3.format("f");
+BwDrawer.parseTime = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
+BwDrawer.bwFormatter = d3.format(".f");
 
-BwDrawer.x = d3.timeDay
+BwDrawer.x = d3.time.scale()
     .range([0, BwDrawer.width]);
 
-BwDrawer.y = d3.scaleLinear()
+BwDrawer.y = d3.scale.linear()
     .range([BwDrawer.height, 0]);
 
-BwDrawer.xAxis = d3.axisBottom(BwDrawer.x);
+BwDrawer.xAxis = d3.svg.axis()
+    .scale(BwDrawer.x)
+    .orient("bottom");
 
-BwDrawer.yAxis = d3.axisLeft(BwDrawer.y)
+BwDrawer.yAxis = d3.svg.axis()
+    .scale(BwDrawer.y)
+    .orient("left")
     .tickFormat(function(d) { return (d == 0) ? "" : BwDrawer.bwFormatter(Math.abs(d)) + " Mbit/s " + ((d > 0) ? "in" : "out"); });
 
-BwDrawer.area = d3.area()
+BwDrawer.area = d3.svg.area()
     .x(function(d) { return BwDrawer.x(d.date); })
     .y0(function(d) { return BwDrawer.y(d.y0); })
     .y1(function(d) { return BwDrawer.y(d.y0 + d.y); });
 
-BwDrawer.read_stack = d3.stack()
-    .value(function(d) { return d.read_values; });
-BwDrawer.write_stack = d3.stack()
-    .value(function(d) { return d.write_values; });
+BwDrawer.read_stack = d3.layout.stack()
+    .values(function(d) { return d.read_values; });
+BwDrawer.write_stack = d3.layout.stack()
+    .values(function(d) { return d.write_values; });
 
-BwDrawer.onionoo_url = "https://onionoo.torproject.org/bandwidth?type=relay&contact=adminsys@nos-oignons.net";
+BwDrawer.onionoo_url = "https://onionoo.torproject.org/bandwidth?type=relay&contact=%20%20%20%200x9F29C15D42A8B6F3";
 
 BwDrawer.periods = [
-    { id: "3_months", label: L10n.t_3_months },
+    { id: "6_months", label: L10n.t_6_months },
     { id: "1_year", label: L10n.t_1_year },
     { id: "5_years", label: L10n.t_5_years },
   ];
@@ -44,7 +48,7 @@ BwDrawer.extract_values = function(history, interval, minTime, maxTime) {
   var first = history ? BwDrawer.parseTime(history.first) : maxTime;
   var last = history ? BwDrawer.parseTime(history.last) : minTime;
   var i = 0;
-  for (var current = minTime; current <= maxTime; current = d3.timeSecond.offset(current, interval)) {
+  for (var current = minTime; current <= maxTime; current = d3.time.second.offset(current, interval)) {
     values.push({ date: current,
                   y: (first <= current && current <= last) ? history.factor * history.values[i++] * 8 / 1000000 : 0
                 });
@@ -52,21 +56,24 @@ BwDrawer.extract_values = function(history, interval, minTime, maxTime) {
   return values;
 }
 
-BwDrawer.color = d3.scaleOrdinal();
-BwDrawer.color.domain(nos_oignons_relays.map(function(r) {return r.fingerprint}));
-BwDrawer.color.range(nos_oignons_relays.map(function(r) {return r.color}));
+BwDrawer.color = d3.scale.ordinal();
+BwDrawer.color.domain(nos_oignons_groups.map(function(g) {return g.name}));
+BwDrawer.color.range(nos_oignons_groups.map(function(g) {return g.color}));
 
 BwDrawer.draw_bandwidth_graph = function(raw_data, selector, period) {
   // Purge non running relays
-  raw_data.relays.forEach(function(r, i) {
-    if (typeof r.read_history === 'undefined' || typeof r.write_history === 'undefined') {
-      raw_data.relays.splice(i, 1);
-    }
+  raw_data.relays = raw_data.relays.filter(function(r) {
+    return typeof r.read_history !== 'undefined' && typeof r.write_history !== 'undefined';
   });
 
+
   var update_period;
 
-  var svg = d3.select(selector).append("svg")
+  var wrapper = d3.select(selector).append("div")
+      .style("display", "flex")
+      .style("align-items", "flex-start");
+
+  var svg = wrapper.append("svg")
       .attr("width", BwDrawer.width + BwDrawer.margin.left + BwDrawer.margin.right)
       .attr("height", BwDrawer.height + BwDrawer.margin.top + BwDrawer.margin.bottom)
     .append("g")
@@ -90,6 +97,8 @@ BwDrawer.draw_bandwidth_graph = function(raw_data, selector, period) {
     }
   });
 
+  var relayIndex = buildRelayIndex(raw_data.relays);
+
   var bw_data = {};
   BwDrawer.periods.map(function(p) { return p.id; }).forEach(function(period) {
     var interval = d3.max(raw_data.relays, function(d) {
@@ -113,20 +122,19 @@ BwDrawer.draw_bandwidth_graph = function(raw_data, selector, period) {
     var maxReadBandwidth = 0;
     var maxWriteBandwidth = 0;
 
-    var values = BwDrawer.color.domain().map(function(fingerprint) {
-      var relay_data = raw_data["relays"].filter(function(d) { return d.fingerprint == fingerprint; })[0];
-      var read_history = relay_data['read_history'][period];
-      var write_history = relay_data['write_history'][period];
-      var read_values = BwDrawer.extract_values(read_history, interval, minTime, maxTime);
-      var write_values = BwDrawer.extract_values(write_history, interval, minTime, maxTime);
-
-      maxReadBandwidth = maxReadBandwidth + d3.max(read_values, function(d) { return d.y; });
-      maxWriteBandwidth = maxWriteBandwidth + d3.max(write_values, function(d) { return d.y; });
-
+    var values = nos_oignons_groups.map(function(group) {
+      var group_read = sumGroupRelayValues(group, relayIndex, function(r) {
+        return BwDrawer.extract_values(r['read_history'][period], interval, minTime, maxTime);
+      });
+      var group_write = sumGroupRelayValues(group, relayIndex, function(r) {
+        return BwDrawer.extract_values(r['write_history'][period], interval, minTime, maxTime);
+      });
+      maxReadBandwidth += (d3.max(group_read, function(d) { return d.y; }) || 0);
+      maxWriteBandwidth += (d3.max(group_write, function(d) { return d.y; }) || 0);
       return {
-        fingerprint: fingerprint,
-        read_values: read_values,
-        write_values: write_values.map(function(d) { d.y = -d.y; return d })
+        group: group.name,
+        read_values: group_read,
+        write_values: group_write.map(function(d) { d.y = -d.y; return d })
       };
     });
     bw_data[period] = { minTime: minTime,
@@ -148,14 +156,14 @@ BwDrawer.draw_bandwidth_graph = function(raw_data, selector, period) {
     .enter().append("path")
       .attr("class", "read_graph area")
       .attr("d", function(d) { return BwDrawer.area(d.read_values); })
-      .style("fill", function(d) { return BwDrawer.color(d.fingerprint); });
+      .style("fill", function(d) { return BwDrawer.color(d.group); });
 
   var write_graph = svg.selectAll(".write_graph")
       .data(BwDrawer.write_stack(bw_data[initial_period].values))
     .enter().append("path")
       .attr("class", "write_graph area")
       .attr("d", function(d) { return BwDrawer.area(d.write_values); })
-      .style("fill", function(d) { return BwDrawer.color(d.fingerprint); });
+      .style("fill", function(d) { return BwDrawer.color(d.group); });
 
   update_period = function(period) {
     BwDrawer.x.domain([bw_data[period].minTime, bw_data[period].maxTime]);
@@ -190,26 +198,31 @@ BwDrawer.draw_bandwidth_graph = function(raw_data, selector, period) {
       .attr("class", "y axis")
       .call(BwDrawer.yAxis);
 
-  var legend = svg.selectAll(".legend")
-      .data(BwDrawer.color.domain().slice().reverse())
-    .enter().append("g")
-      .attr("class", "legend")
-      .attr("transform", function(d, i) { return "translate(0," + ((i * 20) - BwDrawer.margin.top) + ")"; });
-
-  legend.append("rect")
-      .attr("x", BwDrawer.width - 18)
-      .attr("width", 18)
-      .attr("height", 18)
-      .style("fill", BwDrawer.color);
-
-  legend.append("text")
-      .attr("x", BwDrawer.width - 24)
-      .attr("y", 9)
-      .attr("dy", ".35em")
-      .style("text-anchor", "end")
-      .text(function(d) {
-        return nos_oignons_relays.filter(function(r) { return r.fingerprint == d; })[0].name;
-      });
+  var legendDiv = wrapper.append("div")
+      .style("overflow-y", "auto")
+      .style("max-height", (BwDrawer.height + BwDrawer.margin.top + BwDrawer.margin.bottom) + "px")
+      .style("min-width", "90px")
+      .style("padding-left", "10px")
+      .style("padding-top", BwDrawer.margin.top + "px")
+      .style("box-sizing", "border-box");
+
+  BwDrawer.color.domain().slice().reverse().forEach(function(name) {
+    var item = legendDiv.append("div")
+        .style("display", "flex")
+        .style("align-items", "center")
+        .style("margin-bottom", "3px")
+        .style("white-space", "nowrap");
+    item.append("span")
+        .style("display", "inline-block")
+        .style("width", "14px")
+        .style("min-width", "14px")
+        .style("height", "14px")
+        .style("background-color", BwDrawer.color(name))
+        .style("margin-right", "5px");
+    item.append("span")
+        .style("font-size", "11px")
+        .text(name);
+  });
 };
 
 BwDrawer.prototype.draw = function() {