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 = {};
8 WeightsDrawer.prototype = new WeightsDrawer();
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;
14 WeightsDrawer.parseTime = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
15 WeightsDrawer.percentFormatter = d3.format(".2%");
17 WeightsDrawer.x = d3.time.scale()
18 .range([0, WeightsDrawer.width]);
20 WeightsDrawer.y = d3.scale.linear()
21 .range([WeightsDrawer.height, 0]);
23 WeightsDrawer.xAxis = d3.svg.axis()
24 .scale(WeightsDrawer.x)
27 WeightsDrawer.yAxis = d3.svg.axis()
28 .scale(WeightsDrawer.y)
30 .tickFormat(function(d) { return (d == 0) ? "" : WeightsDrawer.percentFormatter(d); });
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); });
37 WeightsDrawer.stack = d3.layout.stack()
38 .values(function(d) { return d.values; });
40 WeightsDrawer.onionoo_url = "https://onionoo.torproject.org/weights?type=relay&contact=adminsys@nos-oignons.net";
42 WeightsDrawer.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 { id: "5_years", label: L10n.t_5_years },
49 WeightsDrawer.current_period = WeightsDrawer.periods[0].id;
51 WeightsDrawer.sources = [
52 { id: "consensus_weight_fraction", label: L10n.consensus_weight },
53 { id: "exit_probability", label: L10n.exit_probability },
56 WeightsDrawer.current_source = WeightsDrawer.sources[0].id;
58 WeightsDrawer.extract_values = function(history, interval, minTime, maxTime) {
60 var first = history ? WeightsDrawer.parseTime(history.first) : maxTime;
61 var last = history ? WeightsDrawer.parseTime(history.last) : minTime;
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
71 WeightsDrawer.color = d3.scale.ordinal();
72 WeightsDrawer.color.domain(nos_oignons_relays.map(function(r) {return r.fingerprint}));
73 WeightsDrawer.color.range(nos_oignons_relays.map(function(r) {return r.color}));
75 WeightsDrawer.prototype.draw_weights_graph = function(raw_data) {
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);
85 var form_source = d3.select(drawer.selector).append("form")
87 WeightsDrawer.sources.forEach(function(s) {
88 var div = form_source.append("div");
89 var radio = div.append("input")
90 .attr("type", "radio")
91 .attr("name", "source")
92 .attr("id", "source_" + s.id)
93 .on("click", function() { drawer.update_source(s.id); });
95 .attr("for", "source_" + s.id)
97 if (s.id == WeightsDrawer.sources[0].id) {
98 radio.attr("checked", true);
102 drawer.svg = d3.select(drawer.selector).append("svg")
103 .attr("width", WeightsDrawer.width + WeightsDrawer.margin.left + WeightsDrawer.margin.right)
104 .attr("height", WeightsDrawer.height + WeightsDrawer.margin.top + WeightsDrawer.margin.bottom)
106 .attr("transform", "translate(" + WeightsDrawer.margin.left + "," + WeightsDrawer.margin.top + ")");
108 var form_period = d3.select(drawer.selector).append("form")
109 .attr("action", "#");
110 WeightsDrawer.periods.forEach(function(p) {
111 var div = form_period.append("div");
112 var radio = div.append("input")
113 .attr("type", "radio")
114 .attr("name", "period")
115 .attr("id", "period_" + p.id)
116 .on("click", function() { drawer.update_period(p.id); });
118 .attr("for", "period_" + p.id)
120 if (p.id == WeightsDrawer.periods[0].id) {
121 radio.attr("checked", true);
125 WeightsDrawer.sources.map(function(s) { return s.id; }).forEach(function(source) {
126 drawer.weights_data[source] = {};
127 WeightsDrawer.periods.map(function(p) { return p.id; }).forEach(function(period) {
128 var interval = d3.max(raw_data.relays, function(d) {
129 return d[source][period] && d[source][period].interval;
131 raw_data.relays.forEach(function(d) {
132 if ((d[source][period] && d[source][period].interval != interval)) {
133 throw "PANIC: Different interval for different relays in the same time period.";
136 var minTime = d3.min(raw_data.relays, function(d) {
137 return d[source][period] && WeightsDrawer.parseTime(d[source][period].first);
139 var maxTime = d3.min(raw_data.relays, function(d) {
140 return d[source][period] && WeightsDrawer.parseTime(d[source][period].last);
145 var relays = WeightsDrawer.color.domain().map(function(fingerprint) {
146 var relay_data = raw_data["relays"].filter(function(d) { return d.fingerprint == fingerprint; })[0];
148 var history = relay_data[source][period];
149 var values = WeightsDrawer.extract_values(history, interval, minTime, maxTime);
150 maxValue = maxValue + d3.max(values, function(d) { return d.y; });
153 fingerprint: fingerprint,
157 drawer.weights_data[source][period] = {
166 WeightsDrawer.y.domain([0, drawer.weights_data[WeightsDrawer.current_source][WeightsDrawer.current_period].maxValue]);
168 WeightsDrawer.x.domain([drawer.weights_data[WeightsDrawer.current_source][WeightsDrawer.current_period].minTime, drawer.weights_data[WeightsDrawer.current_source][WeightsDrawer.current_period].maxTime]);
170 var weight_graph = drawer.svg.selectAll(".weight_graph")
171 .data(WeightsDrawer.stack(drawer.weights_data[WeightsDrawer.current_source][WeightsDrawer.current_period].relays))
172 .enter().append("path")
173 .attr("class", "weight_graph area")
174 .attr("d", function(d) { return WeightsDrawer.area(d.values); })
175 .style("fill", function(d) { return WeightsDrawer.color(d.fingerprint); });
177 drawer.svg.append("g")
178 .attr("class", "x axis")
179 .attr("transform", "translate(0," + WeightsDrawer.height + ")")
180 .call(WeightsDrawer.xAxis)
182 .style("text-anchor", "end")
183 .attr("transform", "rotate(-90) translate(-10, 0)");
185 drawer.svg.append("g")
186 .attr("class", "y axis")
187 .call(WeightsDrawer.yAxis);
189 var legend = drawer.svg.selectAll(".legend")
190 .data(WeightsDrawer.color.domain().slice().reverse())
192 .attr("class", "legend")
193 .attr("transform", function(d, i) { return "translate(0," + ((i * 20) - WeightsDrawer.margin.top) + ")"; });
195 legend.append("rect")
196 .attr("x", WeightsDrawer.width - 18)
199 .style("fill", WeightsDrawer.color);
201 legend.append("text")
202 .attr("x", WeightsDrawer.width - 24)
205 .style("text-anchor", "end")
207 return nos_oignons_relays.filter(function(r) { return r.fingerprint == d; })[0].name;
211 WeightsDrawer.prototype.refresh_graph = function() {
214 WeightsDrawer.x.domain([drawer.weights_data[drawer.current_source][drawer.current_period].minTime, drawer.weights_data[drawer.current_source][drawer.current_period].maxTime]);
215 WeightsDrawer.y.domain([0, drawer.weights_data[drawer.current_source][drawer.current_period].maxValue]);
216 var t = drawer.svg.transition().duration(300);
217 t.select(".x.axis").call(WeightsDrawer.xAxis);
218 t.select(".y.axis").call(WeightsDrawer.yAxis);
219 t.selectAll(".weight_graph").style("fill-opacity", 0);
220 t.each("end", function() {
221 d3.selectAll(".weight_graph").data(WeightsDrawer.stack(drawer.weights_data[drawer.current_source][drawer.current_period].relays));
222 d3.selectAll(".weight_graph").attr("d", function(d) { return WeightsDrawer.area(d.values); })
223 var t2 = drawer.svg.transition().duration(100);
224 t2.selectAll(".weight_graph").style("fill-opacity", 1);
226 d3.selectAll(".x.axis text")
227 .style("text-anchor", "end")
228 .attr("transform", "rotate(-90) translate(-10, 0)");
231 WeightsDrawer.prototype.update_source = function(source) {
232 this.current_source = source;
233 this.refresh_graph();
236 WeightsDrawer.prototype.update_period = function(period) {
237 this.current_period = period;
238 this.refresh_graph();
241 WeightsDrawer.prototype.draw = function() {
243 d3.json(WeightsDrawer.onionoo_url, function(error, raw_data) {
244 d3.select(drawer.selector).text("");
245 drawer.draw_weights_graph(raw_data);