]> nos-oignons.net Git - website.git/blob - assets/bw_graphs.js
màj composition membres du CA
[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=%20%20%20%200x9F29C15D42A8B6F3";
39
40 BwDrawer.periods = [
41     { id: "6_months", label: L10n.t_6_months },
42     { id: "1_year", label: L10n.t_1_year },
43     { id: "5_years", label: L10n.t_5_years },
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   // Purge non running relays
65   raw_data.relays = raw_data.relays.filter(function(r) {
66     return typeof r.read_history !== 'undefined' && typeof r.write_history !== 'undefined';
67   });
68
69
70   var update_period;
71
72   var wrapper = d3.select(selector).append("div")
73       .style("display", "flex")
74       .style("align-items", "flex-start");
75
76   var svg = wrapper.append("svg")
77       .attr("width", BwDrawer.width + BwDrawer.margin.left + BwDrawer.margin.right)
78       .attr("height", BwDrawer.height + BwDrawer.margin.top + BwDrawer.margin.bottom)
79     .append("g")
80       .attr("transform", "translate(" + BwDrawer.margin.left + "," + BwDrawer.margin.top + ")");
81
82   var form = d3.select(selector).append("form")
83       .attr("class", "graph-period")
84       .attr("action", "#");
85   BwDrawer.periods.forEach(function(p) {
86     var div = form.append("div");
87     var radio = div.append("input")
88       .attr("type", "radio")
89       .attr("name", "period")
90       .attr("id", "bw_period_" + p.id)
91       .on("click", function() { update_period(p.id); });
92     div.append("label")
93       .attr("for", "bw_period_" + p.id)
94       .text(p.label);
95     if (p.id == BwDrawer.periods[0].id) {
96       radio.attr("checked", true);
97     }
98   });
99
100   var bw_data = {};
101   BwDrawer.periods.map(function(p) { return p.id; }).forEach(function(period) {
102     var interval = d3.max(raw_data.relays, function(d) {
103       return d['read_history'][period] && d['read_history'][period].interval;
104     });
105     raw_data.relays.forEach(function(d) {
106       if ((d['read_history'][period] && d['read_history'][period].interval != interval) ||
107           (d['write_history'][period] && d['write_history'][period].interval != interval)) {
108         throw "PANIC: Different interval for different relays in the same time period.";
109       }
110     });
111     var minTime = d3.min(raw_data.relays, function(d) {
112       return d['read_history'][period] && BwDrawer.parseTime(d['read_history'][period].first) &&
113              d['write_history'][period] && BwDrawer.parseTime(d['write_history'][period].first);
114     });
115     var maxTime = d3.min(raw_data.relays, function(d) {
116       return d['read_history'][period] && BwDrawer.parseTime(d['read_history'][period].last) &&
117              d['write_history'][period] && BwDrawer.parseTime(d['write_history'][period].last);
118     });
119
120     var maxReadBandwidth = 0;
121     var maxWriteBandwidth = 0;
122
123     var values = BwDrawer.color.domain().map(function(fingerprint) {
124       var relay_data = raw_data["relays"].filter(function(d) { return d.fingerprint == fingerprint; })[0];
125       var read_history = relay_data['read_history'][period];
126       var write_history = relay_data['write_history'][period];
127       var read_values = BwDrawer.extract_values(read_history, interval, minTime, maxTime);
128       var write_values = BwDrawer.extract_values(write_history, interval, minTime, maxTime);
129
130       maxReadBandwidth = maxReadBandwidth + d3.max(read_values, function(d) { return d.y; });
131       maxWriteBandwidth = maxWriteBandwidth + d3.max(write_values, function(d) { return d.y; });
132
133       return {
134         fingerprint: fingerprint,
135         read_values: read_values,
136         write_values: write_values.map(function(d) { d.y = -d.y; return d })
137       };
138     });
139     bw_data[period] = { minTime: minTime,
140                         maxTime: maxTime,
141                         maxReadBandwidth: maxReadBandwidth,
142                         maxWriteBandwidth: maxWriteBandwidth,
143                         values: values };
144   });
145
146   BwDrawer.y.domain([-d3.max(d3.values(bw_data), function(d) { return d.maxWriteBandwidth; }),
147             d3.max(d3.values(bw_data), function(d) { return d.maxReadBandwidth; })]);
148
149   var initial_period = BwDrawer.periods[0].id;
150
151   BwDrawer.x.domain([bw_data[initial_period].minTime, bw_data[initial_period].maxTime]);
152
153   var read_graph = svg.selectAll(".read_graph")
154       .data(BwDrawer.read_stack(bw_data[initial_period].values))
155     .enter().append("path")
156       .attr("class", "read_graph area")
157       .attr("d", function(d) { return BwDrawer.area(d.read_values); })
158       .style("fill", function(d) { return BwDrawer.color(d.fingerprint); });
159
160   var write_graph = svg.selectAll(".write_graph")
161       .data(BwDrawer.write_stack(bw_data[initial_period].values))
162     .enter().append("path")
163       .attr("class", "write_graph area")
164       .attr("d", function(d) { return BwDrawer.area(d.write_values); })
165       .style("fill", function(d) { return BwDrawer.color(d.fingerprint); });
166
167   update_period = function(period) {
168     BwDrawer.x.domain([bw_data[period].minTime, bw_data[period].maxTime]);
169     var t = svg.transition().duration(300);
170     t.select(".x.axis").call(BwDrawer.xAxis);
171     t.selectAll(".read_graph").style("fill-opacity", 0);
172     t.selectAll(".write_graph").style("fill-opacity", 0);
173     t.each("end", function() {
174       d3.selectAll(".read_graph").data(BwDrawer.read_stack(bw_data[period].values));
175       d3.selectAll(".write_graph").data(BwDrawer.write_stack(bw_data[period].values));
176       d3.selectAll(".read_graph").attr("d", function(d) { return BwDrawer.area(d.read_values); })
177       d3.selectAll(".write_graph")
178         .attr("d", function(d) { return BwDrawer.area(d.write_values); })
179       var t2 = svg.transition().duration(100);
180       t2.selectAll(".read_graph").style("fill-opacity", 1);
181       t2.selectAll(".write_graph").style("fill-opacity", 1);
182     });
183     d3.selectAll(".x.axis text")
184       .style("text-anchor", "end")
185       .attr("transform", "rotate(-90) translate(-10, 0)");
186   }
187
188   svg.append("g")
189       .attr("class", "x axis")
190       .attr("transform", "translate(0," + BwDrawer.height + ")")
191       .call(BwDrawer.xAxis)
192         .selectAll("text")
193         .style("text-anchor", "end")
194         .attr("transform", "rotate(-90) translate(-10, 0)");
195
196   svg.append("g")
197       .attr("class", "y axis")
198       .call(BwDrawer.yAxis);
199
200   var legendDiv = wrapper.append("div")
201       .style("overflow-y", "auto")
202       .style("max-height", (BwDrawer.height + BwDrawer.margin.top + BwDrawer.margin.bottom) + "px")
203       .style("min-width", "90px")
204       .style("padding-left", "10px")
205       .style("padding-top", BwDrawer.margin.top + "px")
206       .style("box-sizing", "border-box");
207
208   BwDrawer.color.domain().slice().reverse().forEach(function(d) {
209     var name = nos_oignons_relays.filter(function(r) { return r.fingerprint == d; })[0].name;
210     var item = legendDiv.append("div")
211         .style("display", "flex")
212         .style("align-items", "center")
213         .style("margin-bottom", "3px")
214         .style("white-space", "nowrap");
215     item.append("span")
216         .style("display", "inline-block")
217         .style("width", "14px")
218         .style("min-width", "14px")
219         .style("height", "14px")
220         .style("background-color", BwDrawer.color(d))
221         .style("margin-right", "5px");
222     item.append("span")
223         .style("font-size", "11px")
224         .text(name);
225   });
226 };
227
228 BwDrawer.prototype.draw = function() {
229   var selector = this.selector;
230   d3.json(BwDrawer.onionoo_url, function(error, raw_data) {
231     d3.select(selector).text("");
232     BwDrawer.draw_bandwidth_graph(raw_data, selector);
233   });
234 };