Imported Upstream version 0.8.0.4
[psensor-pkg-debian.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     if (stype & 0x0200) 
50         stype_str = "NVidia ";
51     else if (stype & 0x0800)
52         stype_str = "ATI/AMD ";
53     else 
54         stype_str = "";
55  
56     if (stype & 0x04000)
57         stype_str += "HDD ";
58     else if (stype & 0x08000)
59         stype_str += "CPU ";
60     else if (stype & 0x10000)
61         stype_str += "GPU ";
62     else if (stype & 0x20000)
63         stype_str += "Fan ";
64
65     if (stype & 0x0001)
66        stype_str += "Temperature";
67     else if (stype & 0x0002)
68        stype_str += "RPM";
69     else if (stype & 0x0004)
70        stype_str += "Load";
71
72     return stype_str;
73 }
74
75 function type_to_unit(stype) {
76     if (stype & 0x0001)
77         unit = "C";
78     else if (stype & 0x0002)
79         unit = "RPM";
80
81     return unit;
82 }
83
84 function value_to_str(value, type) {
85     return value+type_to_unit(type);
86 }
87
88 function get_url_params() {
89     var vars, hashes, i;
90
91     vars = [];
92     hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
93
94     for(i = 0; i < hashes.length; i++) {
95         hash = hashes[i].split('=');
96         vars.push(hash[0]);
97         vars[hash[0]] = hash[1];
98     }
99
100     return vars;
101 }
102
103 function update_chart(chart_id, title, data) {
104     var min_date, max_date, min, max, value;
105     var measures, data_chart, date, entry;
106     var style;
107
108     $("#"+chart_id).html("");
109
110     measures = data["measures"];
111     data_chart = [];
112     
113     $("h1").html("");
114     $("h1").append(data["name"]);
115
116     try {
117         $("title").html(data["name"]);
118     } catch(ignore) {
119         // IE8 doesn't allow to modify the page title
120     }
121     
122     $.each(measures, function(i, item) {
123         value = item["value"];
124         date = new Date(item["time"]*1000);
125         entry = [date, item["value"]];
126         
127         data_chart.push(entry);
128         
129         if (!max_date || max_date < date)
130             max_date = date;        
131         if (!min_date || min_date > date)
132             min_date = date;
133         
134         if (!min || value < min)
135             min = value;
136         if (!max || value > max)
137             max = value;        
138     });
139     
140     style = { 
141         title: title,
142         axes: { 
143             xaxis: {
144                 renderer: $.jqplot.DateAxisRenderer,
145                 tickOptions: {
146                     formatString:'%H:%M:%S'
147                 },
148                 min: min_date,
149                 max: max_date
150             },
151             yaxis: {
152                 min: min-1,
153                 max: max+1
154             }
155         },
156         series: [ { 
157             lineWidth: 1, 
158             showMarker: false 
159         } ]
160     };
161
162
163     $.jqplot (chart_id, [data_chart], style);
164 }
165
166 function update_menu() {
167     var name, link, url, str;
168
169     str = "";
170
171     $.getJSON("/api/1.1/sensors", function(data) {
172         str += "<li><em>Sensors</em>\n<ul>";
173
174         $.each(data, function(i, item) {
175             name = item["name"];
176             url = "details.html?id="+escape("/api/1.1/sensors/"+item["id"]);
177             link = "<a href='"+url+"'>"+name+"</a>";
178             str += "<li>"+link+"</li>";
179         });
180         str += "</li></ul>";
181
182         str += "<li><em>CPU</em><ul>";
183         url = "details.html?id="+escape("/api/1.1/cpu/usage");
184         link = "<a href='"+url+"'>usage</a>";
185         str += "<li>"+link+"</li>";
186         
187         str += "</li></ul>";
188         
189         $("#menu-list").append(str);
190
191     });
192
193 }
194
195 function update_summary_sensors() {
196     var name, value_str, min_str, max_str, type, type_str, url;
197
198     $.getJSON("/api/1.1/sensors", function(data) {
199         $("#sensors tbody").html("");
200
201         $.each(data, function(i, item) {            
202             name = item["name"];
203             type = item["type"];
204             value_str = value_to_str(item["last_measure"]["value"], type);
205             min_str = value_to_str(item["min"], type);
206             max_str = value_to_str(item["max"], type);
207             type_str = type_to_str(type);
208             url = "details.html?id="+escape("/api/1.1/sensors/"+item["id"]);
209
210             $("#sensors tbody").append("<tr>"
211                                  +"<td><a href='"+url+"'>"+name+"</a></td>"
212                                  +"<td>"+value_str+"</td>"
213                                  +"<td>"+min_str+"</td>"
214                                  +"<td>"+max_str+"</td>"
215                                  +"<td>"+type_str+"</td>"
216                                  +"</tr>");                 
217         });          
218     });
219 }
220
221 function update_summary_sysinfo() {
222     $.getJSON("/api/1.1/sysinfo", function(data) {
223         $("#uptime").html("");
224         $("#cpu tbody").html("");
225         $("#memory").html("");
226         $("#swap").html("");
227         $("#net tbody").html("");
228
229         var load = Math.round(data["load"] * 100);
230         var load_1 = Math.round(data["load_1"]*1000)/1000;
231         var load_5 = Math.round(data["load_5"]*1000)/1000;
232         var load_15 = Math.round(data["load_15"]*1000)/1000;
233         var uptime = data["uptime"];
234         var uptime_s = uptime % 60;
235         var uptime_mn = Math.floor((uptime / 60) % 60);
236         var uptime_h = Math.floor((uptime / (60*60)) % 24);
237         var uptime_d = Math.floor(uptime / (60*60*24));
238         
239         $("#cpu").append("<tr><td><a href='details.html?id=/api/1.1/cpu/usage'>"+load+"%</a></td><td>"
240                          +load_1+"</td><td>"
241                          +load_5+"</td><td>"
242                          +load_15+"</td></tr>");
243         
244         $("#uptime").append(uptime_d+"d "+uptime_h+"h "+uptime_mn+"mn");
245         
246         var ram = data["ram"];
247         var swap = data["swap"];
248         var mu = data["mem_unit"];
249         
250         var ramtotal = ram["total"]*mu;
251         var ramfree = ram["free"]*mu;
252         var ramused = (ram["total"] - ram["free"])*mu;
253         var ramshared = ram["shared"]*mu;
254         var rambuffer = ram["buffer"]*mu;
255         
256         
257         $("#memory").append("<td>Memory</td>"
258                             +"<td>"+format_mem_size(ramtotal)+"</td>"
259                             +"<td>"+format_mem_size(ramused)+"</td>"
260                             +"<td>"+format_mem_size(ramfree)+"</td>"
261                             +"<td>"+format_mem_size(ramshared)+"</td>"
262                             +"<td>"+format_mem_size(rambuffer)+"</td>");
263         
264         $("#swap").append("<td>Swap</td>"
265                           +"<td>"+format_mem_size(swap["total"]*mu)+"</td>"
266                           +"<td>"+format_mem_size(swap["total"]*mu-swap["free"]*mu)+"</td>"
267                           +"<td>"+format_mem_size(swap["free"]*mu)+"</td>");
268         
269         var netdata = data["net"];
270         $.each(netdata, function(i, item) {
271             $("#net").append("<tr><td>"+item["name"]+"</td>"
272                              +"<td>"+format_mem_size(item["bytes_in"])+"</td>"
273                              +"<td>"+format_mem_size(item["bytes_out"])+"</td></tr>");
274         });
275     });
276 }