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