fixed distrib name
[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><a href=\"index.html#cpu\">CPU</a><ul>";
173         url = "details.html?id="+escape("/api/1.1/cpu/usage");
174         link = "<a href='"+url+"'>usage</a>";
175         str += "<li>"+link+"</li>";
176         str += "</li></ul>";    
177
178         str += "<li><a href=\"index.html#network\">Network</a></li>";
179
180         str += "<li><a href=\"index.html#memory\">Memory</a></li>";
181
182         str += "<li><a href=\"index.html#sensors\">Sensors</a>\n<ul>";
183         $.each(data, function(i, item) {
184             name = item["name"];
185             url = "details.html?id="+escape("/api/1.1/sensors/"+item["id"]);
186             link = "<a href='"+url+"'>"+name+"</a>";
187             str += "<li>"+link+"</li>";
188         });
189         str += "</li></ul>";
190         
191         $("#menu-list").append(str);
192
193     });
194
195 }
196
197 function update_summary_sensors() {
198     var name, value_str, min_str, max_str, type, type_str, url;
199
200     $.getJSON("/api/1.1/sensors", function(data) {
201         $("#sensors tbody").html("");
202
203         $.each(data, function(i, item) {            
204             name = item["name"];
205             type = item["type"];
206             value_str = value_to_str(item["last_measure"]["value"], type);
207             min_str = value_to_str(item["min"], type);
208             max_str = value_to_str(item["max"], type);
209             type_str = type_to_str(type);
210             url = "details.html?id="+escape("/api/1.1/sensors/"+item["id"]);
211
212             $("#sensors tbody").append("<tr>"
213                                  +"<td><a href='"+url+"'>"+name+"</a></td>"
214                                  +"<td>"+value_str+"</td>"
215                                  +"<td>"+min_str+"</td>"
216                                  +"<td>"+max_str+"</td>"
217                                  +"<td>"+type_str+"</td>"
218                                  +"</tr>");                 
219         });          
220     });
221 }
222
223 function update_summary_sysinfo() {
224     $.getJSON("/api/1.1/sysinfo", function(data) {
225         $("#uptime").html("");
226         $("#cpu tbody").html("");
227         $("#memory").html("");
228         $("#swap").html("");
229         $("#net tbody").html("");
230
231         var load = Math.round(data["load"] * 100);
232         var load_1 = Math.round(data["load_1"]*1000)/1000;
233         var load_5 = Math.round(data["load_5"]*1000)/1000;
234         var load_15 = Math.round(data["load_15"]*1000)/1000;
235         var uptime = data["uptime"];
236         var uptime_s = uptime % 60;
237         var uptime_mn = Math.floor((uptime / 60) % 60);
238         var uptime_h = Math.floor((uptime / (60*60)) % 24);
239         var uptime_d = Math.floor(uptime / (60*60*24));
240         
241         $("#cpu").append("<tr><td><a href='details.html?id=/api/1.1/cpu/usage'>"+load+"%</a></td><td>"
242                          +load_1+"</td><td>"
243                          +load_5+"</td><td>"
244                          +load_15+"</td></tr>");
245         
246         $("#uptime").append(uptime_d+"d "+uptime_h+"h "+uptime_mn+"mn");
247         
248         var ram = data["ram"];
249         var swap = data["swap"];
250         var mu = data["mem_unit"];
251         
252         var ramtotal = ram["total"]*mu;
253         var ramfree = ram["free"]*mu;
254         var ramused = (ram["total"] - ram["free"])*mu;
255         var ramshared = ram["shared"]*mu;
256         var rambuffer = ram["buffer"]*mu;
257         
258         
259         $("#memory").append("<td>Memory</td>"
260                             +"<td>"+format_mem_size(ramtotal)+"</td>"
261                             +"<td>"+format_mem_size(ramused)+"</td>"
262                             +"<td>"+format_mem_size(ramfree)+"</td>"
263                             +"<td>"+format_mem_size(ramshared)+"</td>"
264                             +"<td>"+format_mem_size(rambuffer)+"</td>");
265         
266         $("#swap").append("<td>Swap</td>"
267                           +"<td>"+format_mem_size(swap["total"]*mu)+"</td>"
268                           +"<td>"+format_mem_size(swap["total"]*mu-swap["free"]*mu)+"</td>"
269                           +"<td>"+format_mem_size(swap["free"]*mu)+"</td>");
270         
271         var netdata = data["net"];
272         $.each(netdata, function(i, item) {
273             $("#net").append("<tr><td>"+item["name"]+"</td>"
274                              +"<td>"+format_mem_size(item["bytes_in"])+"</td>"
275                              +"<td>"+format_mem_size(item["bytes_out"])+"</td></tr>");
276         });
277     });
278 }