]> nos-oignons.net Git - website.git/blob - assets/bw_graphs.js
Elles sont belles mes urls, elles sont belles.
[website.git] / assets / bw_graphs.js
1 function BwDrawer(selector) {
2   this.selector = selector;
3 };
4 BwDrawer.prototype = new BwDrawer();
5
6 BwDrawer.margin = {top: 50, right: 10, bottom: 90, left: 130};
7 BwDrawer.width = 600 - BwDrawer.margin.left - BwDrawer.margin.right;
8 BwDrawer.height = 400 - BwDrawer.margin.top - BwDrawer.margin.bottom;
9
10 BwDrawer.parseTime = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
11 BwDrawer.bwFormatter = d3.format(".f");
12
13 BwDrawer.x = d3.time.scale()
14     .range([0, BwDrawer.width]);
15
16 BwDrawer.y = d3.scale.linear()
17     .range([BwDrawer.height, 0]);
18
19 BwDrawer.xAxis = d3.svg.axis()
20     .scale(BwDrawer.x)
21     .orient("bottom");
22
23 BwDrawer.yAxis = d3.svg.axis()
24     .scale(BwDrawer.y)
25     .orient("left")
26     .tickFormat(function(d) { return (d == 0) ? "" : BwDrawer.bwFormatter(Math.abs(d)) + " Mbit/s " + ((d > 0) ? "in" : "out"); });
27
28 BwDrawer.area = d3.svg.area()
29     .x(function(d) { return BwDrawer.x(d.date); })
30     .y0(function(d) { return BwDrawer.y(d.y0); })
31     .y1(function(d) { return BwDrawer.y(d.y0 + d.y); });
32
33 BwDrawer.read_stack = d3.layout.stack()
34     .values(function(d) { return d.read_values; });
35 BwDrawer.write_stack = d3.layout.stack()
36     .values(function(d) { return d.write_values; });
37
38 BwDrawer.onionoo_url = "https://onionoo.torproject.org/bandwidth?type=relay&contact=adminsys@nos-oignons.net";
39
40 BwDrawer.periods = [
41     { id: "1_month", label: L10n.t_1_month },
42     { id: "3_months", label: L10n.t_3_months },
43     { id: "1_year", label: L10n.t_1_year },
44   ];
45
46 BwDrawer.extract_values = function(history, interval, minTime, maxTime) {
47   var values = [];
48   var first = history ? BwDrawer.parseTime(history.first) : maxTime;
49   var last = history ? BwDrawer.parseTime(history.last) : minTime;
50   var i = 0;
51   for (var current = minTime; current <= maxTime; current = d3.time.second.offset(current, interval)) {
52     values.push({ date: current,
53                   y: (first <= current && current <= last) ? history.factor * history.values[i++] * 8 / 1000000 : 0
54                 });
55   }
56   return values;
57 }
58
59 BwDrawer.color = d3.scale.ordinal();
60 BwDrawer.color.domain(nos_oignons_relays.map(function(r) {return r.fingerprint}));
61 BwDrawer.color.range(nos_oignons_relays.map(function(r) {return r.color}));
62
63 BwDrawer.draw_bandwidth_graph = function(raw_data, selector, period) {
64   var update_period;
65
66   var svg = d3.select(selector).append("svg")
67       .attr("width", BwDrawer.width + BwDrawer.margin.left + BwDrawer.margin.right)
68       .attr("height", BwDrawer.height + BwDrawer.margin.top + BwDrawer.margin.bottom)
69     .append("g")
70       .attr("transform", "translate(" + BwDrawer.margin.left + "," + BwDrawer.margin.top + ")");
71
72   var form = d3.select(selector).append("form")
73       .attr("action", "#");
74   BwDrawer.periods.forEach(function(p) {
75     var div = form.append("div");
76     var radio = div.append("input")
77       .attr("type", "radio")
78       .attr("name", "period")
79       .attr("id", "period_" + p.id)
80       .on("click", function() { update_period(p.id); });
81     div.append("label")
82       .attr("for", "period_" + p.id)
83       .text(p.label);
84     if (p.id == BwDrawer.periods[0].id) {
85       radio.attr("checked", true);
86     }
87   });
88
89   var bw_data = {};
90   BwDrawer.periods.map(function(p) { return p.id; }).forEach(function(period) {
91     var interval = d3.max(raw_data.relays, function(d) {
92       return d['read_history'][period] && d['read_history'][period].interval;
93     });
94     raw_data.relays.forEach(function(d) {
95       if ((d['read_history'][period] && d['read_history'][period].interval != interval) ||
96           (d['write_history'][period] && d['write_history'][period].interval != interval)) {
97         throw "PANIC: Different interval for different relays in the same time period.";
98       }
99     });
100     var minTime = d3.min(raw_data.relays, function(d) {
101       return d['read_history'][period] && BwDrawer.parseTime(d['read_history'][period].first) &&
102              d['write_history'][period] && BwDrawer.parseTime(d['write_history'][period].first);
103     });
104     var maxTime = d3.min(raw_data.relays, function(d) {
105       return d['read_history'][period] && BwDrawer.parseTime(d['read_history'][period].last) &&
106              d['write_history'][period] && BwDrawer.parseTime(d['write_history'][period].last);
107     });
108
109     var maxReadBandwidth = 0;
110     var maxWriteBandwidth = 0;
111
112     var values = BwDrawer.color.domain().map(function(fingerprint) {
113       var relay_data = raw_data["relays"].filter(function(d) { return d.fingerprint == fingerprint; })[0];
114       var read_history = relay_data['read_history'][period];
115       var write_history = relay_data['write_history'][period];
116       var read_values = BwDrawer.extract_values(read_history, interval, minTime, maxTime);
117       var write_values = BwDrawer.extract_values(write_history, interval, minTime, maxTime);
118
119       maxReadBandwidth = maxReadBandwidth + d3.max(read_values, function(d) { return d.y; });
120       maxWriteBandwidth = maxWriteBandwidth + d3.max(write_values, function(d) { return d.y; });
121
122       return {
123         fingerprint: fingerprint,
124         read_values: read_values,
125         write_values: write_values.map(function(d) { d.y = -d.y; return d })
126       };
127     });
128     bw_data[period] = { minTime: minTime,
129                         maxTime: maxTime,
130                         maxReadBandwidth: maxReadBandwidth,
131                         maxWriteBandwidth: maxWriteBandwidth,
132                         values: values };
133   });
134
135   BwDrawer.y.domain([-d3.max(d3.values(bw_data), function(d) { return d.maxWriteBandwidth; }),
136             d3.max(d3.values(bw_data), function(d) { return d.maxReadBandwidth; })]);
137
138   var initial_period = BwDrawer.periods[0].id;
139
140   BwDrawer.x.domain([bw_data[initial_period].minTime, bw_data[initial_period].maxTime]);
141
142   var read_graph = svg.selectAll(".read_graph")
143       .data(BwDrawer.read_stack(bw_data[initial_period].values))
144     .enter().append("path")
145       .attr("class", "read_graph area")
146       .attr("d", function(d) { return BwDrawer.area(d.read_values); })
147       .style("fill", function(d) { return BwDrawer.color(d.fingerprint); });
148
149   var write_graph = svg.selectAll(".write_graph")
150       .data(BwDrawer.write_stack(bw_data[initial_period].values))
151     .enter().append("path")
152       .attr("class", "write_graph area")
153       .attr("d", function(d) { return BwDrawer.area(d.write_values); })
154       .style("fill", function(d) { return BwDrawer.color(d.fingerprint); });
155
156   update_period = function(period) {
157     BwDrawer.x.domain([bw_data[period].minTime, bw_data[period].maxTime]);
158     var t = svg.transition().duration(300);
159     t.select(".x.axis").call(BwDrawer.xAxis);
160     t.selectAll(".read_graph").style("fill-opacity", 0);
161     t.selectAll(".write_graph").style("fill-opacity", 0);
162     t.each("end", function() {
163       d3.selectAll(".read_graph").data(BwDrawer.read_stack(bw_data[period].values));
164       d3.selectAll(".write_graph").data(BwDrawer.write_stack(bw_data[period].values));
165       d3.selectAll(".read_graph").attr("d", function(d) { return BwDrawer.area(d.read_values); })
166       d3.selectAll(".write_graph")
167         .attr("d", function(d) { return BwDrawer.area(d.write_values); })
168       var t2 = svg.transition().duration(100);
169       t2.selectAll(".read_graph").style("fill-opacity", 1);
170       t2.selectAll(".write_graph").style("fill-opacity", 1);
171     });
172     d3.selectAll(".x.axis text")
173       .style("text-anchor", "end")
174       .attr("transform", "rotate(-90) translate(-10, 0)");
175   }
176
177   svg.append("g")
178       .attr("class", "x axis")
179       .attr("transform", "translate(0," + BwDrawer.height + ")")
180       .call(BwDrawer.xAxis)
181         .selectAll("text")
182         .style("text-anchor", "end")
183         .attr("transform", "rotate(-90) translate(-10, 0)");
184
185   svg.append("g")
186       .attr("class", "y axis")
187       .call(BwDrawer.yAxis);
188
189   var legend = svg.selectAll(".legend")
190       .data(BwDrawer.color.domain().slice().reverse())
191     .enter().append("g")
192       .attr("class", "legend")
193       .attr("transform", function(d, i) { return "translate(0," + ((i * 20) - BwDrawer.margin.top) + ")"; });
194
195   legend.append("rect")
196       .attr("x", BwDrawer.width - 18)
197       .attr("width", 18)
198       .attr("height", 18)
199       .style("fill", BwDrawer.color);
200
201   legend.append("text")
202       .attr("x", BwDrawer.width - 24)
203       .attr("y", 9)
204       .attr("dy", ".35em")
205       .style("text-anchor", "end")
206       .text(function(d) {
207         return nos_oignons_relays.filter(function(r) { return r.fingerprint == d; })[0].name;
208       });
209 };
210
211 BwDrawer.prototype.draw = function() {
212   var selector = this.selector;
213   d3.json(BwDrawer.onionoo_url, function(error, raw_data) {
214     d3.select(selector).text("");
215     BwDrawer.draw_bandwidth_graph(raw_data, selector);
216   });
217 };