clarified current payout graph title
[p2pool.git] / web-static / graphs.html
1 <!DOCTYPE html>
2 <html>
3     <head>
4         <title>P2Pool Graphs</title>
5         <script type="text/javascript" src="d3.v2.min.js"></script>
6         
7         <style type="text/css">
8             body {
9                 font-family: Sans-serif;
10                 font-size: 12px;
11             }
12             
13             line {
14                 stroke: black;
15                 stroke-width: 1;
16                 shape-rendering: crispEdges;
17             }
18             
19             .plotline {
20                 stroke-width: 1.4;
21                 fill: none;
22             }
23             
24             text {
25                 font-family: Sans-serif;
26                 font-size: 12px;
27             }
28         </style>
29     </head>
30     
31     <body>
32         <h1>P2Pool Graphs</h1>
33         
34         <p>Periods: <span id="period_chooser"></span> Current: <span id="period_current"></span></p>
35         
36         <h2>Local rate</h2>
37         <svg id="local"></svg>
38         
39         <h2>Local rate reflected in shares</h2>
40         <svg id="local_shares"></svg>
41         
42         <h2>Current payout to default address</h2>
43         <svg id="payout"></svg>
44         
45         <h2>Pool rate</h2>
46         <svg id="pool"></svg>
47         
48         <h2>Peers</h2>
49         <svg id="peers"></svg>
50         
51         <h2>Miners</h2>
52         <div id="miners"></div>
53         
54         <script type="text/javascript">
55             function compose() {
56                 var funcs = arguments;
57                 return function(x) {
58                     for(var i = funcs.length-1; i >= 0; i--) {
59                         x = funcs[i](x);
60                     }
61                     return x;
62                 }
63             }
64             function itemgetter(i) { return function(x) { return x[i]; } }
65             function as_date(x) { return new Date(1000*x); }
66             function not_null(x) { return x != null; }
67             function identity(x) { return x; }
68             
69             function get_area_mean(data, value_getter) {
70                 var top = 0;
71                 var bottom = 0;
72                 for(var i = 0; i < data.length; i++) {
73                     var value = value_getter(data[i][1]);
74                     if(value == null) continue; // no data for bin
75                     top += value * data[i][2];
76                     bottom += data[i][2];
77                 }
78                 return {"area": top, "mean": bottom==0?null:top/bottom};
79             }
80             
81             function plot(g, unit, total_unit, lines) {
82                 // lines is a list of objects which have attributes data, value_getter, color, and optionally label
83                 
84                 var w = 640;
85                 var h = 300;
86                 var margin_v = 40;
87                 var margin_h = 120;
88                 
89                 var x = d3.time.scale().domain([
90                     as_date(d3.min(lines, function(line) { return d3.min(line.data, itemgetter(0)); })),
91                     as_date(d3.max(lines, function(line) { return d3.max(line.data, itemgetter(0)); }))
92                 ]).range([0 + margin_h, w - margin_h]);
93                 var y = d3.scale.linear().domain([
94                     0,
95                     d3.max(lines, function(line) { return d3.max(line.data, compose(line.value_getter, itemgetter(1))); } )
96                 ]).range([h - margin_v, margin_v]);
97                 
98                 
99                 g.attr("width", w).attr("height", h);
100                 g.selectAll("*").remove();
101                 
102                 for(var i = 0; i < lines.length; ++i) {
103                     var line = lines[i];
104                     var line_type = d3.svg.line().x(compose(x, as_date, itemgetter(0))).y(compose(y, line.value_getter, itemgetter(1))).defined(compose(not_null, line.value_getter, itemgetter(1)));
105                     g.append("svg:path")
106                         .attr("d", line_type(line.data))
107                         .attr("stroke", line.color)
108                         .attr("class", "plotline");
109                     var stats = get_area_mean(line.data, line.value_getter);
110                     if(stats.mean != null) {
111                         g.append("svg:text")
112                             .text(line.label)
113                             .attr("text-anchor", "start")
114                             .attr("dominant-baseline", "central")
115                             .attr("fill", line.color)
116                             .attr("x", w - margin_h + 10)
117                             .attr("y", y(stats.mean) - 12);
118                         g.append("svg:text")
119                             .text("-Mean: " + d3.format(".3s")(stats.mean) + unit)
120                             .attr("text-anchor", "start")
121                             .attr("dominant-baseline", "central")
122                             .attr("fill", line.color)
123                             .attr("x", w - margin_h)
124                             .attr("y", y(stats.mean));
125                         if(total_unit != null)
126                             g.append("svg:text")
127                                 .text("Area: " + d3.format(".3s")(stats.area) + total_unit)
128                                 .attr("text-anchor", "start")
129                                 .attr("dominant-baseline", "central")
130                                 .attr("fill", line.color)
131                                 .attr("x", w - margin_h + 10)
132                                 .attr("y", y(stats.mean) + 12);
133                     }
134                 }
135                 
136                 // x axis
137                 
138                 g.append("svg:line")
139                     .attr("x1", margin_h)
140                     .attr("y1", h - margin_v)
141                     .attr("x2", w - margin_h)
142                     .attr("y2", h - margin_v);
143                 
144                 g.selectAll()
145                     .data(x.ticks(13))
146                     .enter().append("svg:g")
147                     .attr("transform", function(d) { return "translate(" + x(d) + "," + (h-margin_v/2) + ")"; })
148                     .append("svg:text")
149                     .attr("transform", "rotate(45)")
150                     .attr("text-anchor", "middle")
151                     .attr("dominant-baseline", "central")
152                     .text(x.tickFormat(13));
153                 
154                 g.selectAll()
155                     .data(x.ticks(13))
156                     .enter().append("svg:line")
157                     .attr("x1", x)
158                     .attr("y1", h - margin_v)
159                     .attr("x2", x)
160                     .attr("y2", h - margin_v + 5);
161                 
162                 // y axis
163                 
164                 g.append("svg:line")
165                     .attr("x1", margin_h)
166                     .attr("y1", h - margin_v)
167                     .attr("x2", margin_h)
168                     .attr("y2", margin_v);
169                 
170                 g.selectAll()
171                     .data(y.ticks(6))
172                     .enter().append("svg:line")
173                     .attr("x1", margin_h - 5)
174                     .attr("y1", y)
175                     .attr("x2", margin_h)
176                     .attr("y2", y);
177                 
178                 g.selectAll()
179                     .data(y.ticks(6))
180                     .enter().append("svg:text")
181                     .text(compose(function(x) { return x + unit; }, d3.format(".2s")))
182                     .attr("x", margin_h/2)
183                     .attr("y", y)
184                     .attr("dominant-baseline", "central")
185                     .attr("text-anchor", "middle");
186             }
187             function plot_later(g, unit, total_unit, lines) { // takes lines with url attribute instead of data attribute
188                 var callbacks_left = lines.length;
189                 lines.map(function(line) {
190                     d3.json(line.url, function(line_data) {
191                         line.data = line_data;
192                         callbacks_left--;
193                         if(callbacks_left == 0)
194                             plot(g, unit, total_unit, lines);
195                     });
196                 });
197             }
198             
199             function change_period(period) {
200                 d3.select("#period_current").text(period);
201                 var lowerperiod = period.toLowerCase();
202                 plot_later(d3.select("#local"), "H/s", "H", [
203                     {"url": "/web/graph_data/local_hash_rate/last_" + lowerperiod, "value_getter": identity, "color": "#0000FF", "label": "Total"},
204                     {"url": "/web/graph_data/local_dead_hash_rate/last_" + lowerperiod, "value_getter": identity, "color": "#FF0000", "label": "Dead"}
205                 ]);
206                 plot_later(d3.select("#local_shares"), "H/s", "H", [
207                     {"url": "/web/graph_data/local_share_hash_rate/last_" + lowerperiod, "value_getter": identity, "color": "#0000FF", "label": "Total"},
208                     //{"url": getData("/web/graph_data/local_dead_share_hash_rate/last_" + lowerperiod, "value_getter": identity, "color": "#FF0000", "label": "Dead"}
209                 ]);
210                 plot_later(d3.select("#payout"), "BTC", null, [
211                     {"url": "/web/graph_data/current_payout/last_" + lowerperiod, "value_getter": identity, "color": "#0000FF"}
212                 ]);
213                 plot_later(d3.select("#pool"), "H/s", "H", [
214                     {"url": "/web/graph_data/pool_rate/last_" + lowerperiod, "value_getter": identity, "color": "#0000FF", "label": "Total"},
215                     {"url": "/web/graph_data/pool_stale_rate/last_" + lowerperiod, "value_getter": identity, "color": "#FF0000", "label": "Stale"}
216                 ]);
217                 plot_later(d3.select("#peers"), "", null, [
218                     {"url": "/web/graph_data/incoming_peers/last_" + lowerperiod, "value_getter": identity, "color": "#0000FF", "label": "Incoming"},
219                     {"url": "/web/graph_data/outgoing_peers/last_" + lowerperiod, "value_getter": identity, "color": "#FF0000", "label": "Outgoing"}
220                 ]);
221                 
222                 d3.json("/web/graph_data/miner_hash_rates/last_" + lowerperiod, function(data) {
223                     d3.json("/web/graph_data/miner_dead_hash_rates/last_" + lowerperiod, function(dead_data) {
224                         var users = {}; for(var i = 0; i < data.length; ++i) for(var u in data[i][1]) users[u] = null; for(var i = 0; i < dead_data.length; ++i) for(var u in dead_data[i][1]) users[u] = null;
225                         var userlist = []; for(var u in users) userlist.push(u);
226                         d3.select("#miners").selectAll("*").remove();
227                         var div = d3.select("#miners").selectAll().data(userlist).enter().append("div");
228                         div.append("h3").text(identity);
229                         div.append("svg:svg").each(function(u) {
230                             plot(d3.select(this), "H/s", "H", [
231                                 {"data": data, "value_getter": function(d) { return u in d ? d[u] : 0; }, "color": "#0000FF", "label": "Total"},
232                                 {"data": dead_data, "value_getter": function(d) { return u in d ? d[u] : 0; }, "color": "#FF0000", "label": "Dead"}
233                             ]);
234                         });
235                     });
236                 });
237             }
238             
239             periods = ["Hour", "Day", "Week", "Month", "Year"];
240             d3.select("#period_chooser").selectAll().data(periods).enter().append("span")
241                 .text(identity)
242                 .on("click", change_period)
243                 .attr("style", function(d, i) { return (i == 0 ? "" : "margin-left:.4em;") + "color:blue;text-decoration:underline;cursor:pointer" });
244             change_period(periods[1]);
245         </script>
246     </body>
247 </html>