much-improved graphs
[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             .rate {
20                 stroke: #0000FF;
21                 stroke-width: 1.4;
22                 fill: none;
23             }
24             
25             .deadrate {
26                 stroke: #FF0000;
27                 stroke-width: 1.4;
28                 fill: none;
29             }
30             
31             text {
32                 font-family: Sans-serif;
33                 font-size: 12px;
34             }
35         </style>
36     </head>
37     
38     <body>
39         <h1>P2Pool Graphs</h1>
40         
41         <p>Periods: <span id="period_chooser"></span> Current: <span id="period_current"></span></p>
42         
43         <h2>Local rate - Blue=All, Red=Dead (hash/second)</h2>
44         <svg id="local"></svg>
45         
46         <h2>Current payout (BTC)</h2>
47         <svg id="payout"></svg>
48         
49         <h2>Pool rate - Blue=All, Red=Stale (hash/second)</h2>
50         <svg id="pool"></svg>
51         
52         <script type="text/javascript">
53             function compose() {
54                 var funcs = arguments;
55                 return function(x) {
56                     for(var i = funcs.length-1; i >= 0; i--) {
57                         x = funcs[i](x);
58                     }
59                     return x;
60                 }
61             }
62             function itemgetter(i) { return function(x) { return x[i]; } }
63             function as_date(x) { return new Date(1000*x); }
64             function not_null(x) { return x != null; }
65             
66             function getData(url) {
67                 var xmlhttp = new XMLHttpRequest();
68                 xmlhttp.open("GET",  url, false);
69                 xmlhttp.send();
70                 return JSON.parse(xmlhttp.responseText);
71             }
72             
73             function plot(g, blue, red) {
74                 var data = getData("/web/graph_data/" + blue);
75                 if(red)
76                     var dead_data = getData("/web/graph_data/" + red);
77                 else
78                     var dead_data = [];
79                 
80                 var w = 640;
81                 var h = 300;
82                 var margin = 50;
83                 var margin_top = 10;
84                 
85                 var x = d3.time.scale().domain([as_date(d3.min(data, itemgetter(0))), as_date(d3.max(data, itemgetter(0)))]).range([0 + margin * 2, w - margin * 2]);
86                 var y = d3.scale.linear().domain([0, d3.max(data, itemgetter(1))]).range([h - margin, margin_top]);
87                 
88                 var line = d3.svg.line().x(compose(x, as_date, itemgetter(0))).y(compose(y, itemgetter(1))).defined(compose(not_null, itemgetter(1)));
89                 
90                 g.attr("width", w).attr("height", h);
91                 g.selectAll("*").remove();
92                 
93                 g.append("svg:path")
94                     .attr("d", line(data))
95                     .attr("class", "rate");
96                 
97                 if(dead_data)
98                     g.append("svg:path")
99                         .attr("d", line(dead_data))
100                         .attr("class", "deadrate");
101                 
102                 
103                 g.append("svg:line")
104                     .attr("x1", margin * 2)
105                     .attr("y1", h - margin)
106                     .attr("x2", w - margin * 2)
107                     .attr("y2", h - margin);
108                 
109                 g.selectAll()
110                     .data(x.ticks(13))
111                     .enter().append("svg:g")
112                     .attr("transform", function(d) { return "translate(" + x(d) + "," + (h-margin/2) + ")"; })
113                     .append("svg:text")
114                     .attr("transform", "rotate(45)")
115                     .attr("text-anchor", "middle")
116                     .attr("dominant-baseline", "central")
117                     .text(x.tickFormat(13));
118                 
119                 g.selectAll()
120                     .data(x.ticks(13))
121                     .enter().append("svg:line")
122                     .attr("x1", x)
123                     .attr("y1", h - margin)
124                     .attr("x2", x)
125                     .attr("y2", h - margin + 5);
126                 
127                 
128                 g.append("svg:line")
129                     .attr("x1", margin * 2)
130                     .attr("y1", h - margin)
131                     .attr("x2", margin * 2)
132                     .attr("y2", margin_top);
133                 
134                 g.selectAll()
135                     .data(y.ticks(6))
136                     .enter().append("svg:line")
137                     .attr("x1", margin * 2 - 5)
138                     .attr("y1", y)
139                     .attr("x2", margin * 2)
140                     .attr("y2", y);
141                 
142                 g.selectAll()
143                     .data(y.ticks(6))
144                     .enter().append("svg:text")
145                     .text(d3.format(".3s"))
146                     .attr("x", margin - 2.5)
147                     .attr("y", y)
148                     .attr("dominant-baseline", "central")
149                     .attr("text-anchor", "middle");
150                 
151                 function get_avg(data) {
152                     var top = 0;
153                     var bottom = 0;
154                     for(var i = 0; i < data.length; i++) {
155                         if(data[i][1] == null) continue; // no data for bin
156                         top += data[i][1] * data[i][2];
157                         bottom += data[i][2];
158                     }
159                     if(bottom == 0) return null; // no data at all
160                     return top/bottom;
161                 }
162                 
163                 var mean = get_avg(data);
164                 if(mean != null) {
165                     g.append("svg:text")
166                         .text("-Mean: " + d3.format(".3s")(mean))
167                         .attr("text-anchor", "start")
168                         .attr("dominant-baseline", "central")
169                         .attr("fill", "#0000FF")
170                         .attr("x", w - 2*margin)
171                         .attr("y", y(mean));
172                 }
173                 
174                 var mean2 = get_avg(dead_data);
175                 if(mean2 != null) {
176                     g.append("svg:text")
177                         .text("-Mean: " + d3.format(".3s")(mean2))
178                         .attr("text-anchor", "start")
179                         .attr("dominant-baseline", "central")
180                         .attr("fill", "#FF0000")
181                         .attr("x", w - 2*margin)
182                         .attr("y", y(mean2));
183                 }
184             }
185             
186             function attachGraph(e, blue, red) {
187                 plot(e, blue, red);
188                 //setInterval(function() { plot(e, blue, red); }, 5000);
189             }
190             
191             periods = ["Hour", "Day", "Week", "Month", "Year"];
192             d3.select("#period_chooser").selectAll().data(periods).enter().append("span")
193                 .text(function(x){return x;})
194                 .on("click", change_period)
195                 .attr("style", function(d, i) { return (i == 0 ? "" : "margin-left:.4em;") + "color:blue;text-decoration:underline;cursor:pointer" });
196             function change_period(period) {
197                 var lowerperiod = period.toLowerCase();
198                 attachGraph(d3.select("#local"), "local_hash_rate/last_" + lowerperiod, "local_dead_hash_rate/last_" + lowerperiod);
199                 attachGraph(d3.select("#payout"), "current_payout/last_" + lowerperiod);
200                 attachGraph(d3.select("#pool"), "pool_rate/last_" + lowerperiod, "pool_stale_rate/last_" + lowerperiod);
201                 d3.select("#period_current").text(period);
202             }
203             change_period(periods[1]);
204         </script>
205     </body>
206 </html>