added pool rate to 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         <h2>Local rate - Blue=All, Red=Dead</h2>
42         
43         <h3>Last hour</h3>
44         <div id="local_hour"></div>
45         
46         <h3>Last day</h3>
47         <div id="local_day"></div>
48         
49         <h3>Last week</h3>
50         <div id="local_week"></div>
51         
52         <h3>Last month</h3>
53         <div id="local_month"></div>
54         
55         <h2>Pool rate - Blue=All, Red=Stale</h2>
56         
57         <h3>Last hour</h3>
58         <div id="pool_hour"></div>
59         
60         <h3>Last day</h3>
61         <div id="pool_day"></div>
62         
63         <h3>Last week</h3>
64         <div id="pool_week"></div>
65         
66         <h3>Last month</h3>
67         <div id="pool_month"></div>
68         
69         <script type="text/javascript">
70             function getData(url) {
71                 xmlhttp = new XMLHttpRequest();
72                 xmlhttp.open("GET",  url, false);
73                 xmlhttp.send();
74                 var data = JSON.parse(xmlhttp.responseText);
75                 
76                 return data;
77             }
78             
79             function plot(e, blue, red) {
80                 data = getData("/web/graph_data/" + blue);
81                 dead_data = getData("/web/graph_data/" + red);
82                 
83                 var w = 640;
84                 var h = 300;
85                 var margin = 50;
86                 
87                 function itemgetter(i) {
88                     return function(x) {
89                         return x[i];
90                     }
91                 }
92                 function as_date(x) {
93                     return new Date(1000*x);
94                 }
95                 
96                 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]);
97                 var y = d3.scale.linear().domain([0, d3.max(data, itemgetter(1))]).range([h - margin, margin]);
98                 
99                 var line = d3.svg.line().x(function(d, i) { return x(as_date(d[0])); }).y(function(d) { return y(d[1]); });
100                 
101                 e.selectAll("svg").remove();
102                 var g = e.insert("svg:svg").attr("width", w).attr("height", h);
103                 
104                 g.append("svg:path")
105                     .attr("d", line(data))
106                     .attr("class", "rate");
107                 
108                 g.append("svg:path")
109                     .attr("d", line(dead_data))
110                     .attr("class", "deadrate");
111                 
112                 
113                 g.append("svg:line")
114                     .attr("x1", margin * 2)
115                     .attr("y1", h - margin)
116                     .attr("x2", w - margin * 2)
117                     .attr("y2", h - margin);
118                 
119                 g.selectAll()
120                     .data(x.ticks(13))
121                     .enter().append("svg:g")
122                     .attr("transform", function(d) { return "translate(" + x(d) + "," + (h-margin/2) + ")"; })
123                     .append("svg:text")
124                     .attr("transform", "rotate(45)")
125                     .attr("text-anchor", "middle")
126                     .text(x.tickFormat(13));
127                 
128                 g.selectAll()
129                     .data(x.ticks(13))
130                     .enter().append("svg:line")
131                     .attr("x1", x)
132                     .attr("y1", h - margin)
133                     .attr("x2", x)
134                     .attr("y2", h - margin + 5);
135                 
136                 
137                 g.append("svg:line")
138                     .attr("x1", margin * 2)
139                     .attr("y1", h - margin)
140                     .attr("x2", margin * 2)
141                     .attr("y2", margin);
142                 
143                 g.selectAll()
144                     .data(y.ticks(18))
145                     .enter().append("svg:line")
146                     .attr("x1", margin * 2 - 5)
147                     .attr("y1", y)
148                     .attr("x2", margin * 2)
149                     .attr("y2", y);
150                 
151                 g.selectAll()
152                     .data(y.ticks(6))
153                     .enter().append("svg:text")
154                     .text(d3.format(".3s"))
155                     .attr("x", margin - 2.5)
156                     .attr("y", function(d) { return y(d) + 4; })
157                     .attr("text-anchor", "middle");
158             }
159             
160             function attachGraph(e, blue, red) {
161                 plot(e, blue, red);
162                 setInterval(function() { plot(e, blue, red); }, 5000);
163             }
164             
165             periods = ["hour", "day", "week", "month"];
166             for(i = 0; i < periods.length; i++) {
167                 attachGraph(d3.select("#local_" + periods[i]), "local_hash_rate/last_" + periods[i], "local_dead_hash_rate/last_" + periods[i]);
168                 attachGraph(d3.select("#pool_" + periods[i]), "pool_rate/last_" + periods[i], "pool_stale_rate/last_" + periods[i]);
169             }
170         </script>
171     </body>
172 </html>