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