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