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