]> nos-oignons.net Git - graphnion.git/blob - bw_graphs.js
Use 'width' setting in demo.
[graphnion.git] / bw_graphs.js
1 /* graphnion: display Tor relay graphs on a website
2  *
3  * Copyright © 2013-2015 Nos oignons <contact@nos-oignons.net>
4  *
5  * graphnion is licensed under the Creative Commons Attribution-Share
6  * Alike 3.0 Unported license.
7  *
8  * You are free:
9  *
10  * • to share – to copy, distribute and transmit the work
11  * • to remix – to adapt the work
12  *
13  * Under the following conditions:
14  * • attribution – You must attribute the work in the manner specified
15  *   by the author or licensor (but not in any way that suggests that they
16  *   endorse you or your use of the work).
17  * • share alike – If you alter, transform, or build upon this work,
18  *   you may distribute the resulting work only under the same or similar
19  *   license to this one.
20  */
21
22 function BwDrawer(selector) {
23   this.selector = selector;
24 };
25 BwDrawer.prototype = new BwDrawer();
26
27 BwDrawer.margin = {top: 50, right: 10, bottom: 90, left: 130};
28 BwDrawer.width = 600 - BwDrawer.margin.left - BwDrawer.margin.right;
29 BwDrawer.height = 400 - BwDrawer.margin.top - BwDrawer.margin.bottom;
30
31 BwDrawer.parseTime = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
32 BwDrawer.bwFormatter = d3.format(".f");
33
34 BwDrawer.x = d3.time.scale()
35     .range([0, BwDrawer.width]);
36
37 BwDrawer.y = d3.scale.linear()
38     .range([BwDrawer.height, 0]);
39
40 BwDrawer.color = d3.scale.category20();
41
42 BwDrawer.xAxis = d3.svg.axis()
43     .scale(BwDrawer.x)
44     .orient("bottom");
45
46 BwDrawer.yAxis = d3.svg.axis()
47     .scale(BwDrawer.y)
48     .orient("left")
49     .tickFormat(function(d) { return (d == 0) ? "" : BwDrawer.bwFormatter(Math.abs(d)) + " Mbit/s " + ((d > 0) ? "in" : "out"); });
50
51 BwDrawer.area = d3.svg.area()
52     .x(function(d) { return BwDrawer.x(d.date); })
53     .y0(function(d) { return BwDrawer.y(d.y0); })
54     .y1(function(d) { return BwDrawer.y(d.y0 + d.y); });
55
56 BwDrawer.read_stack = d3.layout.stack()
57     .values(function(d) { return d.read_values; });
58 BwDrawer.write_stack = d3.layout.stack()
59     .values(function(d) { return d.write_values; });
60
61 BwDrawer.onionoo_url = "https://onionoo.torproject.org/bandwidth?type=relay&contact=adminsys@nos-oignons.net";
62
63 BwDrawer.periods = [
64     { id: "3_days", label: L10n.t_3_days },
65     { id: "1_week", label: L10n.t_1_week },
66     { id: "1_month", label: L10n.t_1_month },
67     { id: "3_months", label: L10n.t_3_months },
68     { id: "1_year", label: L10n.t_1_year },
69   ];
70
71 BwDrawer.extract_values = function(history, interval, minTime, maxTime) {
72   var values = [];
73   var first = history ? BwDrawer.parseTime(history.first) : maxTime;
74   var last = history ? BwDrawer.parseTime(history.last) : minTime;
75   var i = 0;
76   for (var current = minTime; current <= maxTime; current = d3.time.second.offset(current, interval)) {
77     values.push({ date: current,
78                   y: (first <= current && current <= last) ? history.factor * history.values[i++] * 8 / 1000000 : 0
79                 });
80   }
81   return values;
82 }
83
84 BwDrawer.draw_bandwidth_graph = function(raw_data, selector, period) {
85   var update_period;
86
87   var svg = d3.select(selector).append("svg")
88       .attr("width", BwDrawer.width + BwDrawer.margin.left + BwDrawer.margin.right)
89       .attr("height", BwDrawer.height + BwDrawer.margin.top + BwDrawer.margin.bottom)
90     .append("g")
91       .attr("transform", "translate(" + BwDrawer.margin.left + "," + BwDrawer.margin.top + ")");
92
93   var form = d3.select(selector).append("form")
94       .attr("action", "#");
95   BwDrawer.periods.forEach(function(p) {
96     var div = form.append("div");
97     var radio = div.append("input")
98       .attr("type", "radio")
99       .attr("name", "period")
100       .attr("id", "period_" + p.id)
101       .on("click", function() { update_period(p.id); });
102     div.append("label")
103       .attr("for", "period_" + p.id)
104       .text(p.label);
105     if (p.id == BwDrawer.periods[0].id) {
106       radio.attr("checked", true);
107     }
108   });
109
110   var valid_fingerprints = [];
111   nos_oignons_relays.forEach(function(r) {
112     var relay_data = raw_data["relays"].filter(function(d) { return d.fingerprint == r.fingerprint; })[0];
113     valid_fingerprints.push(r.fingerprint);
114   });
115   BwDrawer.color.domain(valid_fingerprints);
116
117   var bw_data = {};
118   BwDrawer.periods.map(function(p) { return p.id; }).forEach(function(period) {
119     var interval = d3.max(raw_data.relays, function(d) {
120       return d['read_history'][period] && d['read_history'][period].interval;
121     });
122     raw_data.relays.forEach(function(d) {
123       if ((d['read_history'][period] && d['read_history'][period].interval != interval) ||
124           (d['write_history'][period] && d['write_history'][period].interval != interval)) {
125         throw "PANIC: Different interval for different relays in the same time period.";
126       }
127     });
128     var minTime = d3.max(raw_data.relays, function(d) {
129       return d['read_history'][period] && BwDrawer.parseTime(d['read_history'][period].first) &&
130              d['write_history'][period] && BwDrawer.parseTime(d['write_history'][period].first);
131     });
132     var maxTime = d3.min(raw_data.relays, function(d) {
133       return d['read_history'][period] && BwDrawer.parseTime(d['read_history'][period].last) &&
134              d['write_history'][period] && BwDrawer.parseTime(d['write_history'][period].last);
135     });
136
137     var maxReadBandwidth = 0;
138     var maxWriteBandwidth = 0;
139
140     var values = BwDrawer.color.domain().map(function(fingerprint) {
141       var relay_data = raw_data["relays"].filter(function(d) { return d.fingerprint == fingerprint; })[0];
142       var read_history = relay_data['read_history'][period];
143       var write_history = relay_data['write_history'][period];
144       var read_values = BwDrawer.extract_values(read_history, interval, minTime, maxTime);
145       var write_values = BwDrawer.extract_values(write_history, interval, minTime, maxTime);
146
147       maxReadBandwidth = maxReadBandwidth + d3.max(read_values, function(d) { return d.y; });
148       maxWriteBandwidth = maxWriteBandwidth + d3.max(write_values, function(d) { return d.y; });
149
150       return {
151         fingerprint: fingerprint,
152         read_values: read_values,
153         write_values: write_values.map(function(d) { d.y = -d.y; return d })
154       };
155     });
156     bw_data[period] = { minTime: minTime,
157                         maxTime: maxTime,
158                         maxReadBandwidth: maxReadBandwidth,
159                         maxWriteBandwidth: maxWriteBandwidth,
160                         values: values };
161   });
162
163   BwDrawer.y.domain([-d3.max(d3.values(bw_data), function(d) { return d.maxWriteBandwidth; }),
164             d3.max(d3.values(bw_data), function(d) { return d.maxReadBandwidth; })]);
165
166   var initial_period = BwDrawer.periods[0].id;
167
168   BwDrawer.x.domain([bw_data[initial_period].minTime, bw_data[initial_period].maxTime]);
169
170   var read_graph = svg.selectAll(".read_graph")
171       .data(BwDrawer.read_stack(bw_data[initial_period].values))
172     .enter().append("path")
173       .attr("class", "read_graph area")
174       .attr("d", function(d) { return BwDrawer.area(d.read_values); })
175       .style("fill", function(d) { return BwDrawer.color(d.fingerprint); });
176
177   var write_graph = svg.selectAll(".write_graph")
178       .data(BwDrawer.write_stack(bw_data[initial_period].values))
179     .enter().append("path")
180       .attr("class", "write_graph area")
181       .attr("d", function(d) { return BwDrawer.area(d.write_values); })
182       .style("fill", function(d) { return BwDrawer.color(d.fingerprint); });
183
184   update_period = function(period) {
185     BwDrawer.x.domain([bw_data[period].minTime, bw_data[period].maxTime]);
186     var t = svg.transition().duration(300);
187     t.select(".x.axis").call(BwDrawer.xAxis);
188     t.selectAll(".read_graph").style("fill-opacity", 0);
189     t.selectAll(".write_graph").style("fill-opacity", 0);
190     t.each("end", function() {
191       d3.selectAll(".read_graph").data(BwDrawer.read_stack(bw_data[period].values));
192       d3.selectAll(".write_graph").data(BwDrawer.write_stack(bw_data[period].values));
193       d3.selectAll(".read_graph").attr("d", function(d) { return BwDrawer.area(d.read_values); })
194       d3.selectAll(".write_graph")
195         .attr("d", function(d) { return BwDrawer.area(d.write_values); })
196       var t2 = svg.transition().duration(100);
197       t2.selectAll(".read_graph").style("fill-opacity", 1);
198       t2.selectAll(".write_graph").style("fill-opacity", 1);
199     });
200     d3.selectAll(".x.axis text")
201       .style("text-anchor", "end")
202       .attr("transform", "rotate(-90) translate(-10, 0)");
203   }
204
205   svg.append("g")
206       .attr("class", "x axis")
207       .attr("transform", "translate(0," + BwDrawer.height + ")")
208       .call(BwDrawer.xAxis)
209         .selectAll("text")
210         .style("text-anchor", "end")
211         .attr("transform", "rotate(-90) translate(-10, 0)");
212
213   svg.append("g")
214       .attr("class", "y axis")
215       .call(BwDrawer.yAxis);
216
217   var legend = svg.selectAll(".legend")
218       .data(BwDrawer.color.domain().slice().reverse())
219     .enter().append("g")
220       .attr("class", "legend")
221       .attr("transform", function(d, i) { return "translate(0," + ((i * 20) - BwDrawer.margin.top) + ")"; });
222
223   legend.append("rect")
224       .attr("x", BwDrawer.width - 18)
225       .attr("width", 18)
226       .attr("height", 18)
227       .style("fill", BwDrawer.color);
228
229   legend.append("text")
230       .attr("x", BwDrawer.width - 24)
231       .attr("y", 9)
232       .attr("dy", ".35em")
233       .style("text-anchor", "end")
234       .text(function(d) {
235         return nos_oignons_relays.filter(function(r) { return r.fingerprint == d; })[0].name;
236       });
237 };
238
239 BwDrawer.prototype.draw = function() {
240   var selector = this.selector;
241   d3.json(BwDrawer.onionoo_url, function(error, raw_data) {
242     d3.select(selector).text("");
243     BwDrawer.draw_bandwidth_graph(raw_data, selector);
244   });
245 };