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