Imported Upstream version 0.6.2.17
[psensor-pkg-debian.git] / www / psensor.js
1 /*
2  * Copyright (C) 2010-2012 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     $("#"+chart_id).html("");
104
105     measures = data["measures"];
106     data_chart = [];
107     
108     $("h1").html("");
109     $("h1").append(data["name"]);
110
111     try {
112         $("title").html(data["name"]);
113     } catch(ignore) {
114         // IE8 doesn't allow to modify the page title
115     }
116     
117     $.each(measures, function(i, item) {
118         value = item["value"];
119         date = new Date(item["time"]*1000);
120         entry = [date, item["value"]];
121         
122         data_chart.push(entry);
123         
124         if (!max_date || max_date < date)
125             max_date = date;        
126         if (!min_date || min_date > date)
127             min_date = date;
128         
129         if (!min || value < min)
130             min = value;
131         if (!max || value > max)
132             max = value;        
133     });
134     
135     style = { 
136         title: title,
137         axes: { 
138             xaxis: {
139                 renderer: $.jqplot.DateAxisRenderer,
140                 tickOptions: {
141                     formatString:'%H:%M:%S'
142                 },
143                 min: min_date,
144                 max: max_date
145             },
146             yaxis: {
147                 min: min-1,
148                 max: max+1
149             }
150         },
151         series: [ { 
152             lineWidth: 1, 
153             showMarker: false 
154         } ]
155     };
156
157
158     $.jqplot (chart_id, [data_chart], style);
159 }
160
161 function update_menu() {
162     var name, link, url, str;
163
164     str = "";
165
166     $.getJSON("/api/1.0/sensors", function(data) {
167         str += "<li><em>Sensors</em>\n<ul>";
168
169         $.each(data, function(i, item) {
170             name = item["name"];
171             url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
172             link = "<a href='"+url+"'>"+name+"</a>";
173             str += "<li>"+link+"</li>";
174         });
175         str += "</li></ul>";
176
177         str += "<li><em>CPU</em><ul>";
178         url = "details.html?id="+escape("/api/1.0/cpu/usage");
179         link = "<a href='"+url+"'>usage</a>";
180         str += "<li>"+link+"</li>";
181         
182         str += "</li></ul>";
183         
184         $("#menu-list").append(str);
185
186     });
187
188 }
189
190 function update_summary_sensors() {
191     var name, value_str, min_str, max_str, type, type_str, url;
192
193     $.getJSON("/api/1.0/sensors", function(data) {
194         $("#sensors tbody").html("");
195
196         $.each(data, function(i, item) {            
197             name = item["name"];
198             type = item["type"];
199             value_str = value_to_str(item["last_measure"]["value"], type);
200             min_str = value_to_str(item["min"], type);
201             max_str = value_to_str(item["max"], type);
202             type_str = type_to_str(type);
203             url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
204
205             $("#sensors tbody").append("<tr>"
206                                  +"<td><a href='"+url+"'>"+name+"</a></td>"
207                                  +"<td>"+value_str+"</td>"
208                                  +"<td>"+min_str+"</td>"
209                                  +"<td>"+max_str+"</td>"
210                                  +"<td>"+type_str+"</td>"
211                                  +"</tr>");                 
212         });          
213     });
214 }
215
216 function update_summary_sysinfo() {
217     $.getJSON("/api/1.0/sysinfo", function(data) {
218         $("#uptime").html("");
219         $("#cpu tbody").html("");
220         $("#memory").html("");
221         $("#swap").html("");
222         $("#net tbody").html("");
223
224         var load = Math.round(data["load"] * 100);
225         var load_1 = Math.round(data["load_1"]*1000)/1000;
226         var load_5 = Math.round(data["load_5"]*1000)/1000;
227         var load_15 = Math.round(data["load_15"]*1000)/1000;
228         var uptime = data["uptime"];
229         var uptime_s = uptime % 60;
230         var uptime_mn = Math.floor((uptime / 60) % 60);
231         var uptime_h = Math.floor((uptime / (60*60)) % 24);
232         var uptime_d = Math.floor(uptime / (60*60*24));
233         
234         $("#cpu").append("<tr><td><a href='details.html?id=/api/1.0/cpu/usage'>"+load+"%</a></td><td>"
235                          +load_1+"</td><td>"
236                          +load_5+"</td><td>"
237                          +load_15+"</td></tr>");
238         
239         $("#uptime").append(uptime_d+"d "+uptime_h+"h "+uptime_mn+"mn");
240         
241         var ram = data["ram"];
242         var swap = data["swap"];
243         var mu = data["mem_unit"];
244         
245         var ramtotal = ram["total"]*mu;
246         var ramfree = ram["free"]*mu;
247         var ramused = (ram["total"] - ram["free"])*mu;
248         var ramshared = ram["shared"]*mu;
249         var rambuffer = ram["buffer"]*mu;
250         
251         
252         $("#memory").append("<td>Memory</td>"
253                             +"<td>"+format_mem_size(ramtotal)+"</td>"
254                             +"<td>"+format_mem_size(ramused)+"</td>"
255                             +"<td>"+format_mem_size(ramfree)+"</td>"
256                             +"<td>"+format_mem_size(ramshared)+"</td>"
257                             +"<td>"+format_mem_size(rambuffer)+"</td>");
258         
259         $("#swap").append("<td>Swap</td>"
260                           +"<td>"+format_mem_size(swap["total"]*mu)+"</td>"
261                           +"<td>"+format_mem_size(swap["total"]*mu-swap["free"]*mu)+"</td>"
262                           +"<td>"+format_mem_size(swap["free"]*mu)+"</td>");
263         
264         var netdata = data["net"];
265         $.each(netdata, function(i, item) {
266             $("#net").append("<tr><td>"+item["name"]+"</td>"
267                              +"<td>"+format_mem_size(item["bytes_in"])+"</td>"
268                              +"<td>"+format_mem_size(item["bytes_out"])+"</td></tr>");
269         });
270     });
271 }