1 function BwDrawer(selector) {
2 this.selector = selector;
4 BwDrawer.prototype = new BwDrawer();
6 BwDrawer.margin = {top: 50, right: 10, bottom: 90, left: 130};
7 BwDrawer.width = 600 - BwDrawer.margin.left - BwDrawer.margin.right;
8 BwDrawer.height = 400 - BwDrawer.margin.top - BwDrawer.margin.bottom;
10 BwDrawer.parseTime = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
11 BwDrawer.bwFormatter = d3.format(".f");
13 BwDrawer.x = d3.time.scale()
14 .range([0, BwDrawer.width]);
16 BwDrawer.y = d3.scale.linear()
17 .range([BwDrawer.height, 0]);
19 BwDrawer.color = d3.scale.category20();
21 BwDrawer.xAxis = d3.svg.axis()
25 BwDrawer.yAxis = d3.svg.axis()
28 .tickFormat(function(d) { return (d == 0) ? "" : BwDrawer.bwFormatter(Math.abs(d)) + " Mbit/s " + ((d > 0) ? "in" : "out"); });
30 BwDrawer.area = d3.svg.area()
31 .x(function(d) { return BwDrawer.x(d.date); })
32 .y0(function(d) { return BwDrawer.y(d.y0); })
33 .y1(function(d) { return BwDrawer.y(d.y0 + d.y); });
35 BwDrawer.read_stack = d3.layout.stack()
36 .values(function(d) { return d.read_values; });
37 BwDrawer.write_stack = d3.layout.stack()
38 .values(function(d) { return d.write_values; });
40 BwDrawer.onionoo_url = "https://onionoo.torproject.org/bandwidth?type=relay&contact=adminsys@nos-oignons.net";
43 { id: "3_days", label: L10n.t_3_days },
44 { id: "1_week", label: L10n.t_1_week },
45 { id: "1_month", label: L10n.t_1_month },
46 { id: "3_months", label: L10n.t_3_months },
47 { id: "1_year", label: L10n.t_1_year },
50 BwDrawer.extract_values = function(history, interval, minTime, maxTime) {
52 var first = history ? BwDrawer.parseTime(history.first) : maxTime;
53 var last = history ? BwDrawer.parseTime(history.last) : minTime;
55 for (var current = minTime; current <= maxTime; current = d3.time.second.offset(current, interval)) {
56 values.push({ date: current,
57 y: (first <= current && current <= last) ? history.factor * history.values[i++] * 8 / 1000000 : 0
63 BwDrawer.draw_bandwidth_graph = function(raw_data, selector, period) {
66 var svg = d3.select(selector).append("svg")
67 .attr("width", BwDrawer.width + BwDrawer.margin.left + BwDrawer.margin.right)
68 .attr("height", BwDrawer.height + BwDrawer.margin.top + BwDrawer.margin.bottom)
70 .attr("transform", "translate(" + BwDrawer.margin.left + "," + BwDrawer.margin.top + ")");
72 var form = d3.select(selector).append("form")
74 BwDrawer.periods.forEach(function(p) {
75 var div = form.append("div");
76 var radio = div.append("input")
77 .attr("type", "radio")
78 .attr("name", "period")
79 .attr("id", "period_" + p.id)
80 .on("click", function() { update_period(p.id); });
82 .attr("for", "period_" + p.id)
84 if (p.id == BwDrawer.periods[0].id) {
85 radio.attr("checked", true);
89 var valid_fingerprints = [];
90 nos_oignons_relays.forEach(function(r) {
91 var relay_data = raw_data["relays"].filter(function(d) { return d.fingerprint == r.fingerprint; })[0];
92 valid_fingerprints.push(r.fingerprint);
94 BwDrawer.color.domain(valid_fingerprints);
97 BwDrawer.periods.map(function(p) { return p.id; }).forEach(function(period) {
98 var interval = d3.max(raw_data.relays, function(d) {
99 return d['read_history'][period] && d['read_history'][period].interval;
101 raw_data.relays.forEach(function(d) {
102 if ((d['read_history'][period] && d['read_history'][period].interval != interval) ||
103 (d['write_history'][period] && d['write_history'][period].interval != interval)) {
104 throw "PANIC: Different interval for different relays in the same time period.";
107 var minTime = d3.max(raw_data.relays, function(d) {
108 return d['read_history'][period] && BwDrawer.parseTime(d['read_history'][period].first) &&
109 d['write_history'][period] && BwDrawer.parseTime(d['write_history'][period].first);
111 var maxTime = d3.min(raw_data.relays, function(d) {
112 return d['read_history'][period] && BwDrawer.parseTime(d['read_history'][period].last) &&
113 d['write_history'][period] && BwDrawer.parseTime(d['write_history'][period].last);
116 var maxReadBandwidth = 0;
117 var maxWriteBandwidth = 0;
119 var values = BwDrawer.color.domain().map(function(fingerprint) {
120 var relay_data = raw_data["relays"].filter(function(d) { return d.fingerprint == fingerprint; })[0];
121 var read_history = relay_data['read_history'][period];
122 var write_history = relay_data['write_history'][period];
123 var read_values = BwDrawer.extract_values(read_history, interval, minTime, maxTime);
124 var write_values = BwDrawer.extract_values(write_history, interval, minTime, maxTime);
126 maxReadBandwidth = maxReadBandwidth + d3.max(read_values, function(d) { return d.y; });
127 maxWriteBandwidth = maxWriteBandwidth + d3.max(write_values, function(d) { return d.y; });
130 fingerprint: fingerprint,
131 read_values: read_values,
132 write_values: write_values.map(function(d) { d.y = -d.y; return d })
135 bw_data[period] = { minTime: minTime,
137 maxReadBandwidth: maxReadBandwidth,
138 maxWriteBandwidth: maxWriteBandwidth,
142 BwDrawer.y.domain([-d3.max(d3.values(bw_data), function(d) { return d.maxWriteBandwidth; }),
143 d3.max(d3.values(bw_data), function(d) { return d.maxReadBandwidth; })]);
145 var initial_period = BwDrawer.periods[0].id;
147 BwDrawer.x.domain([bw_data[initial_period].minTime, bw_data[initial_period].maxTime]);
149 var read_graph = svg.selectAll(".read_graph")
150 .data(BwDrawer.read_stack(bw_data[initial_period].values))
151 .enter().append("path")
152 .attr("class", "read_graph area")
153 .attr("d", function(d) { return BwDrawer.area(d.read_values); })
154 .style("fill", function(d) { return BwDrawer.color(d.fingerprint); });
156 var write_graph = svg.selectAll(".write_graph")
157 .data(BwDrawer.write_stack(bw_data[initial_period].values))
158 .enter().append("path")
159 .attr("class", "write_graph area")
160 .attr("d", function(d) { return BwDrawer.area(d.write_values); })
161 .style("fill", function(d) { return BwDrawer.color(d.fingerprint); });
163 update_period = function(period) {
164 BwDrawer.x.domain([bw_data[period].minTime, bw_data[period].maxTime]);
165 var t = svg.transition().duration(300);
166 t.select(".x.axis").call(BwDrawer.xAxis);
167 t.selectAll(".read_graph").style("fill-opacity", 0);
168 t.selectAll(".write_graph").style("fill-opacity", 0);
169 t.each("end", function() {
170 d3.selectAll(".read_graph").data(BwDrawer.read_stack(bw_data[period].values));
171 d3.selectAll(".write_graph").data(BwDrawer.write_stack(bw_data[period].values));
172 d3.selectAll(".read_graph").attr("d", function(d) { return BwDrawer.area(d.read_values); })
173 d3.selectAll(".write_graph")
174 .attr("d", function(d) { return BwDrawer.area(d.write_values); })
175 var t2 = svg.transition().duration(100);
176 t2.selectAll(".read_graph").style("fill-opacity", 1);
177 t2.selectAll(".write_graph").style("fill-opacity", 1);
179 d3.selectAll(".x.axis text")
180 .style("text-anchor", "end")
181 .attr("transform", "rotate(-90) translate(-10, 0)");
185 .attr("class", "x axis")
186 .attr("transform", "translate(0," + BwDrawer.height + ")")
187 .call(BwDrawer.xAxis)
189 .style("text-anchor", "end")
190 .attr("transform", "rotate(-90) translate(-10, 0)");
193 .attr("class", "y axis")
194 .call(BwDrawer.yAxis);
196 var legend = svg.selectAll(".legend")
197 .data(BwDrawer.color.domain().slice().reverse())
199 .attr("class", "legend")
200 .attr("transform", function(d, i) { return "translate(0," + ((i * 20) - BwDrawer.margin.top) + ")"; });
202 legend.append("rect")
203 .attr("x", BwDrawer.width - 18)
206 .style("fill", BwDrawer.color);
208 legend.append("text")
209 .attr("x", BwDrawer.width - 24)
212 .style("text-anchor", "end")
214 return nos_oignons_relays.filter(function(r) { return r.fingerprint == d; })[0].name;
218 BwDrawer.prototype.draw = function() {
219 var selector = this.selector;
220 d3.json(BwDrawer.onionoo_url, function(error, raw_data) {
221 d3.select(selector).text("");
222 BwDrawer.draw_bandwidth_graph(raw_data, selector);