]> nos-oignons.net Git - website.git/blob - assets/weights_graphs.js
Les graphs sont maintenant groupés par noms!
[website.git] / assets / weights_graphs.js
1 function WeightsDrawer(selector) {
2   this.selector = selector;
3   this.current_source = "consensus_weight_fraction";
4   this.current_period = "1_month";
5   this.weights_data = {};
6   this.svg = null;
7 };
8 WeightsDrawer.prototype = new WeightsDrawer();
9
10 WeightsDrawer.margin = {top: 50, right: 10, bottom: 90, left: 130};
11 WeightsDrawer.width = 600 - WeightsDrawer.margin.left - WeightsDrawer.margin.right;
12 WeightsDrawer.height = 400 - WeightsDrawer.margin.top - WeightsDrawer.margin.bottom;
13
14 WeightsDrawer.parseTime = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
15 WeightsDrawer.percentFormatter = d3.format(".2%");
16
17 WeightsDrawer.x = d3.time.scale()
18     .range([0, WeightsDrawer.width]);
19
20 WeightsDrawer.y = d3.scale.linear()
21     .range([WeightsDrawer.height, 0]);
22
23 WeightsDrawer.xAxis = d3.svg.axis()
24     .scale(WeightsDrawer.x)
25     .orient("bottom");
26
27 WeightsDrawer.yAxis = d3.svg.axis()
28     .scale(WeightsDrawer.y)
29     .orient("left")
30     .tickFormat(function(d) { return (d == 0) ? "" : WeightsDrawer.percentFormatter(d); });
31
32 WeightsDrawer.area = d3.svg.area()
33     .x(function(d) { return WeightsDrawer.x(d.date); })
34     .y0(function(d) { return WeightsDrawer.y(d.y0); })
35     .y1(function(d) { return WeightsDrawer.y(d.y0 + d.y); });
36
37 WeightsDrawer.stack = d3.layout.stack()
38     .values(function(d) { return d.values; });
39
40 WeightsDrawer.onionoo_url = "https://onionoo.torproject.org/weights?type=relay&contact=%20%20%20%200x9F29C15D42A8B6F3";
41
42 WeightsDrawer.periods = [
43     { id: "1_month", label: L10n.t_1_month },
44     { id: "6_months", label: L10n.t_6_months },
45     { id: "1_year", label: L10n.t_1_year },
46     { id: "5_years", label: L10n.t_5_years },
47   ];
48
49 WeightsDrawer.current_period = WeightsDrawer.periods[0].id;
50
51 WeightsDrawer.sources = [
52     { id: "consensus_weight_fraction", label: L10n.consensus_weight },
53     { id: "exit_probability", label: L10n.exit_probability },
54   ];
55
56 WeightsDrawer.current_source = WeightsDrawer.sources[0].id;
57
58 WeightsDrawer.extract_values = function(history, interval, minTime, maxTime) {
59   var values = [];
60   var first = history ? WeightsDrawer.parseTime(history.first) : maxTime;
61   var last = history ? WeightsDrawer.parseTime(history.last) : minTime;
62   var i = 0;
63   for (var current = minTime; current <= maxTime; current = d3.time.second.offset(current, interval)) {
64     values.push({ date: current,
65                   y: (first <= current && current <= last) ? history.factor * history.values[i++] : 0
66                 });
67   }
68   return values;
69 }
70
71 WeightsDrawer.color = d3.scale.ordinal();
72 WeightsDrawer.color.domain(nos_oignons_groups.map(function(g) {return g.name}));
73 WeightsDrawer.color.range(nos_oignons_groups.map(function(g) {return g.color}));
74
75 WeightsDrawer.prototype.draw_weights_graph = function(raw_data) {
76   var drawer = this;
77
78   // Purge non running relays
79   raw_data.relays.forEach(function(r, i) {
80     if (typeof r.consensus_weight_fraction === 'undefined') {
81       raw_data.relays.splice(i, 1);
82     }
83   });
84
85   var form_source = d3.select(drawer.selector).append("form")
86       .attr("id", "weight-sources")
87       .attr("action", "#");
88   WeightsDrawer.sources.forEach(function(s) {
89     var div = form_source.append("div");
90     var radio = div.append("input")
91       .attr("type", "radio")
92       .attr("name", "source")
93       .attr("id", "source_" + s.id)
94       .on("click", function() { drawer.update_source(s.id); });
95     div.append("label")
96       .attr("for", "source_" + s.id)
97       .text(s.label);
98     if (s.id == WeightsDrawer.sources[0].id) {
99       radio.attr("checked", true);
100     }
101   });
102
103   var wrapper = d3.select(drawer.selector).append("div")
104       .style("display", "flex")
105       .style("align-items", "flex-start");
106
107   drawer.svg = wrapper.append("svg")
108       .attr("width", WeightsDrawer.width + WeightsDrawer.margin.left + WeightsDrawer.margin.right)
109       .attr("height", WeightsDrawer.height + WeightsDrawer.margin.top + WeightsDrawer.margin.bottom)
110     .append("g")
111       .attr("transform", "translate(" + WeightsDrawer.margin.left + "," + WeightsDrawer.margin.top + ")");
112
113   var form_period = d3.select(drawer.selector).append("form")
114       .attr("class", "graph-period")
115       .attr("action", "#");
116   WeightsDrawer.periods.forEach(function(p) {
117     var div = form_period.append("div");
118     var radio = div.append("input")
119       .attr("type", "radio")
120       .attr("name", "period")
121       .attr("id", "weights_period_" + p.id)
122       .on("click", function() { drawer.update_period(p.id); });
123     div.append("label")
124       .attr("for", "weights_period_" + p.id)
125       .text(p.label);
126     if (p.id == WeightsDrawer.periods[0].id) {
127       radio.attr("checked", true);
128     }
129   });
130
131   var relayIndex = buildRelayIndex(raw_data.relays);
132
133   WeightsDrawer.sources.map(function(s) { return s.id; }).forEach(function(source) {
134     drawer.weights_data[source] = {};
135     WeightsDrawer.periods.map(function(p) { return p.id; }).forEach(function(period) {
136       var interval = d3.max(raw_data.relays, function(d) {
137         return d[source][period] && d[source][period].interval;
138       });
139       raw_data.relays.forEach(function(d) {
140         if ((d[source][period] && d[source][period].interval != interval)) {
141           throw "PANIC: Different interval for different relays in the same time period.";
142         }
143       });
144       var minTime = d3.min(raw_data.relays, function(d) {
145         return d[source][period] && WeightsDrawer.parseTime(d[source][period].first);
146       });
147       var maxTime = d3.min(raw_data.relays, function(d) {
148         return d[source][period] && WeightsDrawer.parseTime(d[source][period].last);
149       });
150
151       var maxValue = 0;
152
153       var relays = nos_oignons_groups.map(function(group) {
154         var group_values = sumGroupRelayValues(group, relayIndex, function(r) {
155           return WeightsDrawer.extract_values(r[source][period], interval, minTime, maxTime);
156         });
157         maxValue += (d3.max(group_values, function(d) { return d.y; }) || 0);
158         return { group: group.name, values: group_values };
159       });
160       drawer.weights_data[source][period] = {
161         minTime: minTime,
162         maxTime: maxTime,
163         maxValue: maxValue,
164         relays: relays
165       };
166     });
167   });
168
169   WeightsDrawer.y.domain([0, drawer.weights_data[WeightsDrawer.current_source][WeightsDrawer.current_period].maxValue]);
170
171   WeightsDrawer.x.domain([drawer.weights_data[WeightsDrawer.current_source][WeightsDrawer.current_period].minTime, drawer.weights_data[WeightsDrawer.current_source][WeightsDrawer.current_period].maxTime]);
172
173   var weight_graph = drawer.svg.selectAll(".weight_graph")
174       .data(WeightsDrawer.stack(drawer.weights_data[WeightsDrawer.current_source][WeightsDrawer.current_period].relays))
175     .enter().append("path")
176       .attr("class", "weight_graph area")
177       .attr("d", function(d) { return WeightsDrawer.area(d.values); })
178       .style("fill", function(d) { return WeightsDrawer.color(d.group); });
179
180   drawer.svg.append("g")
181       .attr("class", "x axis")
182       .attr("transform", "translate(0," + WeightsDrawer.height + ")")
183       .call(WeightsDrawer.xAxis)
184         .selectAll("text")
185         .style("text-anchor", "end")
186         .attr("transform", "rotate(-90) translate(-10, 0)");
187
188   drawer.svg.append("g")
189       .attr("class", "y axis")
190       .call(WeightsDrawer.yAxis);
191
192   var legendDiv = wrapper.append("div")
193       .style("overflow-y", "auto")
194       .style("max-height", (WeightsDrawer.height + WeightsDrawer.margin.top + WeightsDrawer.margin.bottom) + "px")
195       .style("min-width", "90px")
196       .style("padding-left", "10px")
197       .style("padding-top", WeightsDrawer.margin.top + "px")
198       .style("box-sizing", "border-box");
199
200   WeightsDrawer.color.domain().slice().reverse().forEach(function(name) {
201     var item = legendDiv.append("div")
202         .style("display", "flex")
203         .style("align-items", "center")
204         .style("margin-bottom", "3px")
205         .style("white-space", "nowrap");
206     item.append("span")
207         .style("display", "inline-block")
208         .style("width", "14px")
209         .style("min-width", "14px")
210         .style("height", "14px")
211         .style("background-color", WeightsDrawer.color(name))
212         .style("margin-right", "5px");
213     item.append("span")
214         .style("font-size", "11px")
215         .text(name);
216   });
217 };
218
219 WeightsDrawer.prototype.refresh_graph = function() {
220   var drawer = this;
221
222   WeightsDrawer.x.domain([drawer.weights_data[drawer.current_source][drawer.current_period].minTime, drawer.weights_data[drawer.current_source][drawer.current_period].maxTime]);
223   WeightsDrawer.y.domain([0, drawer.weights_data[drawer.current_source][drawer.current_period].maxValue]);
224   var t = drawer.svg.transition().duration(300);
225   t.select(".x.axis").call(WeightsDrawer.xAxis);
226   t.select(".y.axis").call(WeightsDrawer.yAxis);
227   t.selectAll(".weight_graph").style("fill-opacity", 0);
228   t.each("end", function() {
229     d3.selectAll(".weight_graph").data(WeightsDrawer.stack(drawer.weights_data[drawer.current_source][drawer.current_period].relays));
230     d3.selectAll(".weight_graph").attr("d", function(d) { return WeightsDrawer.area(d.values); })
231     var t2 = drawer.svg.transition().duration(100);
232     t2.selectAll(".weight_graph").style("fill-opacity", 1);
233   });
234   d3.selectAll(".x.axis text")
235     .style("text-anchor", "end")
236     .attr("transform", "rotate(-90) translate(-10, 0)");
237 }
238
239 WeightsDrawer.prototype.update_source = function(source) {
240   this.current_source = source;
241   this.refresh_graph();
242 }
243
244 WeightsDrawer.prototype.update_period = function(period) {
245   this.current_period = period;
246   this.refresh_graph();
247 }
248
249 WeightsDrawer.prototype.draw = function() {
250   var drawer = this;
251   d3.json(WeightsDrawer.onionoo_url, function(error, raw_data) {
252     d3.select(drawer.selector).text("");
253     drawer.draw_weights_graph(raw_data);
254   });
255 };