1 /* graphnion: display Tor relay graphs on a website
3 * Copyright © 2013-2015 Nos oignons <contact@nos-oignons.net>
5 * graphnion is licensed under the Creative Commons Attribution-Share
6 * Alike 3.0 Unported license.
10 * • to share – to copy, distribute and transmit the work
11 * • to remix – to adapt the work
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.
22 function BwDrawer(selector) {
23 this.selector = selector;
25 BwDrawer.prototype = new BwDrawer();
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;
31 BwDrawer.parseTime = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
32 BwDrawer.bwFormatter = d3.format(".f");
34 BwDrawer.x = d3.time.scale()
35 .range([0, BwDrawer.width]);
37 BwDrawer.y = d3.scale.linear()
38 .range([BwDrawer.height, 0]);
40 BwDrawer.color = d3.scale.category20();
42 BwDrawer.xAxis = d3.svg.axis()
46 BwDrawer.yAxis = d3.svg.axis()
49 .tickFormat(function(d) { return (d == 0) ? "" : BwDrawer.bwFormatter(Math.abs(d)) + " Mbit/s " + ((d > 0) ? "in" : "out"); });
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); });
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; });
61 BwDrawer.onionoo_url = "https://onionoo.torproject.org/bandwidth?type=relay&contact=adminsys@nos-oignons.net";
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 },
71 BwDrawer.extract_values = function(history, interval, minTime, maxTime) {
73 var first = history ? BwDrawer.parseTime(history.first) : maxTime;
74 var last = history ? BwDrawer.parseTime(history.last) : minTime;
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
84 BwDrawer.draw_bandwidth_graph = function(raw_data, selector, period) {
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)
91 .attr("transform", "translate(" + BwDrawer.margin.left + "," + BwDrawer.margin.top + ")");
93 var form = d3.select(selector).append("form")
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); });
103 .attr("for", "period_" + p.id)
105 if (p.id == BwDrawer.periods[0].id) {
106 radio.attr("checked", true);
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);
115 BwDrawer.color.domain(valid_fingerprints);
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;
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.";
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);
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);
137 var maxReadBandwidth = 0;
138 var maxWriteBandwidth = 0;
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);
147 maxReadBandwidth = maxReadBandwidth + d3.max(read_values, function(d) { return d.y; });
148 maxWriteBandwidth = maxWriteBandwidth + d3.max(write_values, function(d) { return d.y; });
151 fingerprint: fingerprint,
152 read_values: read_values,
153 write_values: write_values.map(function(d) { d.y = -d.y; return d })
156 bw_data[period] = { minTime: minTime,
158 maxReadBandwidth: maxReadBandwidth,
159 maxWriteBandwidth: maxWriteBandwidth,
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; })]);
166 var initial_period = BwDrawer.periods[0].id;
168 BwDrawer.x.domain([bw_data[initial_period].minTime, bw_data[initial_period].maxTime]);
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); });
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); });
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);
200 d3.selectAll(".x.axis text")
201 .style("text-anchor", "end")
202 .attr("transform", "rotate(-90) translate(-10, 0)");
206 .attr("class", "x axis")
207 .attr("transform", "translate(0," + BwDrawer.height + ")")
208 .call(BwDrawer.xAxis)
210 .style("text-anchor", "end")
211 .attr("transform", "rotate(-90) translate(-10, 0)");
214 .attr("class", "y axis")
215 .call(BwDrawer.yAxis);
217 var legend = svg.selectAll(".legend")
218 .data(BwDrawer.color.domain().slice().reverse())
220 .attr("class", "legend")
221 .attr("transform", function(d, i) { return "translate(0," + ((i * 20) - BwDrawer.margin.top) + ")"; });
223 legend.append("rect")
224 .attr("x", BwDrawer.width - 18)
227 .style("fill", BwDrawer.color);
229 legend.append("text")
230 .attr("x", BwDrawer.width - 24)
233 .style("text-anchor", "end")
235 return nos_oignons_relays.filter(function(r) { return r.fingerprint == d; })[0].name;
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);