01ab6b3a4aec4b9df9b70acc3cbe32d1f65ffa1c
[psensor.git] / www / psensor.js
1 /*
2  * Copyright (C) 2010-2011 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 function format_mem_size(s) {
21     var mo_bytes, go_bytes, o, k, m, g;
22
23     mo_bytes = 1024 * 1024;
24     go_bytes = 1024 * mo_bytes;
25
26     o = s % 1024;
27     k = Math.round((s / 1024) % 1024);
28     m = Math.round((s / (1024*1024)) % 1024);
29     g = Math.round(s / (1024*1024*1024));
30
31     if (g >= 1)
32         return g+"Go ";
33
34     if (m >= 1)
35         return m+"Mo";
36
37     if (k >= 1)
38         return k+"Ko";
39     
40     if (o > 0)
41         return o+"o";
42
43     return "0";
44 }
45
46 function type_to_str(stype) {
47     var stype_str;
48
49     stype_str = "N/A";
50
51     if (stype & 0x0100)
52         stype_str = "Sensor";
53     else if (stype & 0x0200) 
54         stype_str = "NVidia";
55     else if (stype & 0x0400)
56         stype_str = "HDD";
57     else if (stype & 0x0800)
58         stype_str = "CPU Usage Percentage";
59     else if (stype & 0x1000) 
60         stype_str = "AMD";
61  
62    if (stype & 0x0001)
63        stype_str += " Temperature";
64     else if (stype & 0x0002)
65        stype_str += " Fan";
66
67     return stype_str;
68 }
69
70 function type_to_unit(stype) {
71     if (stype & 0x0001)
72         unit = "C";
73     else if (stype & 0x0002)
74         unit = "RPM";
75
76     return unit;
77 }
78
79 function value_to_str(value, type) {
80     return value+type_to_unit(type);
81 }
82
83 function get_url_params() {
84     var vars, hashes, i;
85
86     vars = [];
87     hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
88
89     for(i = 0; i < hashes.length; i++) {
90         hash = hashes[i].split('=');
91         vars.push(hash[0]);
92         vars[hash[0]] = hash[1];
93     }
94
95     return vars;
96 }
97
98 function update_chart(chart_id, title, data) {
99     var min_date, max_date, min, max, value;
100     var measures, data_chart, date, entry;
101     var style;
102
103     measures = data["measures"];
104     data_chart = [];
105     
106     $("h1").append(data["name"]);
107     $("title").append(data["name"]);
108     
109     $.each(measures, function(i, item) {
110         value = item["value"];
111         date = new Date(item["time"]*1000);
112         entry = [date, item["value"]];
113         
114         data_chart.push(entry);
115         
116         if (!max_date || max_date < date)
117             max_date = date;        
118         if (!min_date || min_date > date)
119             min_date = date;
120         
121         if (!min || value < min)
122             min = value;
123         if (!max || value > max)
124             max = value;        
125     });
126     
127     style = { 
128         title: title,
129         axes: { 
130             xaxis: {
131                 renderer: $.jqplot.DateAxisRenderer,
132                 tickOptions: {
133                     formatString:'%H:%M:%S'
134                 },
135                 min: min_date,
136                 max: max_date
137             },
138             yaxis: {
139                 min: min-1,
140                 max: max+1
141             }
142         },
143         series: [ { 
144             lineWidth: 1, 
145             showMarker: false 
146         } ]
147     };
148
149     $.jqplot (chart_id, [data_chart], style);
150 }
151
152 function update_menu() {
153     var name, link, url, str;
154
155     str = "";
156
157     $.getJSON("/api/1.0/sensors", function(data) {
158         str += "<li><em>Sensors</em>\n<ul>";
159
160         $.each(data, function(i, item) {
161             name = item["name"];
162             url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
163             link = "<a href='"+url+"'>"+name+"</a>";
164             str += "<li>"+link+"</li>";
165         });
166         str += "</li></ul>";
167
168         str += "<li><em>CPU</em><ul>";
169         url = "details.html?id="+escape("/api/1.0/cpu/usage");
170         link = "<a href='"+url+"'>usage</a>";
171         str += "<li>"+link+"</li>";
172         
173         str += "</li></ul>";
174         
175         $("#menu-list").append(str);
176
177     });
178
179 }
180
181 function update_summary_sensors() {
182     var name, value_str, min_str, max_str, type, type_str, url;
183
184     $.getJSON("/api/1.0/sensors", function(data) {
185         $("#sensors tbody").html("");
186
187         $.each(data, function(i, item) {            
188             name = item["name"];
189             type = item["type"];
190             value_str = value_to_str(item["last_measure"]["value"], type);
191             min_str = value_to_str(item["min"], type);
192             max_str = value_to_str(item["max"], type);
193             type_str = type_to_str(type);
194             url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
195
196             $("#sensors tbody").append("<tr>"
197                                  +"<td><a href='"+url+"'>"+name+"</a></td>"
198                                  +"<td>"+value_str+"</td>"
199                                  +"<td>"+min_str+"</td>"
200                                  +"<td>"+max_str+"</td>"
201                                  +"<td>"+type_str+"</td>"
202                                  +"</tr>");                 
203         });          
204     });
205 }
206
207 function update_summary_sysinfo() {
208     $.getJSON("/api/1.0/sysinfo", function(data) {
209         $("#uptime").html("");
210         $("#cpu tbody").html("");
211         $("#memory").html("");
212         $("#swap").html("");
213         $("#net tbody").html("");
214
215         var load = Math.round(data["load"] * 100);
216         var load_1 = Math.round(data["load_1"]*1000)/1000;
217         var load_5 = Math.round(data["load_5"]*1000)/1000;
218         var load_15 = Math.round(data["load_15"]*1000)/1000;
219         var uptime = data["uptime"];
220         var uptime_s = uptime % 60;
221         var uptime_mn = Math.floor((uptime / 60) % 60);
222         var uptime_h = Math.floor((uptime / (60*60)) % 24);
223         var uptime_d = Math.floor(uptime / (60*60*24));
224         
225         $("#cpu").append("<tr><td><a href='details.html?id=/api/1.0/cpu/usage'>"+load+"%</a></td><td>"
226                          +load_1+"</td><td>"
227                          +load_5+"</td><td>"
228                          +load_15+"</td></tr>");
229         
230         $("#uptime").append(uptime_d+"d "+uptime_h+"h "+uptime_mn+"mn");
231         
232         var ram = data["ram"];
233         var swap = data["swap"];
234         var mu = data["mem_unit"];
235         
236         var ramtotal = ram["total"]*mu;
237         var ramfree = ram["free"]*mu;
238         var ramused = (ram["total"] - ram["free"])*mu;
239         var ramshared = ram["shared"]*mu;
240         var rambuffer = ram["buffer"]*mu;
241         
242         
243         $("#memory").append("<td>Memory</td>"
244                             +"<td>"+format_mem_size(ramtotal)+"</td>"
245                             +"<td>"+format_mem_size(ramused)+"</td>"
246                             +"<td>"+format_mem_size(ramfree)+"</td>"
247                             +"<td>"+format_mem_size(ramshared)+"</td>"
248                             +"<td>"+format_mem_size(rambuffer)+"</td>");
249         
250         $("#swap").append("<td>Swap</td>"
251                           +"<td>"+format_mem_size(swap["total"]*mu)+"</td>"
252                           +"<td>"+format_mem_size(swap["total"]*mu-swap["free"]*mu)+"</td>"
253                           +"<td>"+format_mem_size(swap["free"]*mu)+"</td>");
254         
255         var netdata = data["net"];
256         $.each(netdata, function(i, item) {
257             $("#net").append("<tr><td>"+item["name"]+"</td>"
258                              +"<td>"+format_mem_size(item["bytes_in"])+"</td>"
259                              +"<td>"+format_mem_size(item["bytes_out"])+"</td></tr>");
260         });
261     });
262 }