cleanup, data of the monitor page refreshed automaticly (not using page refresh)
[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             
178
179             name = item["name"];
180             type = item["type"];
181             value_str = value_to_str(item["last_measure"]["value"], type);
182             min_str = value_to_str(item["min"], type);
183             max_str = value_to_str(item["max"], type);
184             type_str = type_to_str(type);
185             url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
186
187             $("#sensors tbody").append("<tr>"
188                                  +"<td><a href='"+url+"'>"+name+"</a></td>"
189                                  +"<td>"+value_str+"</td>"
190                                  +"<td>"+min_str+"</td>"
191                                  +"<td>"+max_str+"</td>"
192                                  +"<td>"+type_str+"</td>"
193                                  +"</tr>");                 
194         });          
195     });
196 }
197
198 function update_summary_sysinfo() {
199     $.getJSON("/api/1.0/sysinfo", function(data) {
200         $("#uptime").html("");
201         $("#cpu tbody").html("");
202         $("#memory").html("");
203         $("#swap").html("");
204         $("#net tbody").html("");
205
206         var load = Math.round(data["load"] * 100);
207         var load_1 = Math.round(data["load_1"]*1000)/1000;
208         var load_5 = Math.round(data["load_5"]*1000)/1000;
209         var load_15 = Math.round(data["load_15"]*1000)/1000;
210         var uptime = data["uptime"];
211         var uptime_s = uptime % 60;
212         var uptime_mn = Math.floor((uptime / 60) % 60);
213         var uptime_h = Math.floor((uptime / (60*60)) % 24);
214         var uptime_d = Math.floor(uptime / (60*60*24));
215         
216         $("#cpu").append("<tr><td>"+load+"%</td><td>"
217                          +load_1+"</td><td>"
218                          +load_5+"</td><td>"
219                          +load_15+"</td></tr>");
220         
221         $("#uptime").append(uptime_d+"d "+uptime_h+"h "+uptime_mn+"mn");
222         
223         var ram = data["ram"];
224         var swap = data["swap"];
225         var mu = data["mem_unit"];
226         
227         var ramtotal = ram["total"]*mu;
228         var ramfree = ram["free"]*mu;
229         var ramused = (ram["total"] - ram["free"])*mu;
230         var ramshared = ram["shared"]*mu;
231         var rambuffer = ram["buffer"]*mu;
232         
233         
234         $("#memory").append("<td>Memory</td>"
235                             +"<td>"+format_mem_size(ramtotal)+"</td>"
236                             +"<td>"+format_mem_size(ramused)+"</td>"
237                             +"<td>"+format_mem_size(ramfree)+"</td>"
238                             +"<td>"+format_mem_size(ramshared)+"</td>"
239                             +"<td>"+format_mem_size(rambuffer)+"</td>");
240         
241         $("#swap").append("<td>Swap</td>"
242                           +"<td>"+format_mem_size(swap["total"]*mu)+"</td>"
243                           +"<td>"+format_mem_size(swap["total"]*mu-swap["free"]*mu)+"</td>"
244                           +"<td>"+format_mem_size(swap["free"]*mu)+"</td>");
245         
246         var netdata = data["net"];
247         $.each(netdata, function(i, item) {
248             $("#net").append("<tr><td>"+item["name"]+"</td>"
249                              +"<td>"+format_mem_size(item["bytes_in"])+"</td>"
250                              +"<td>"+format_mem_size(item["bytes_out"])+"</td></tr>");
251         });
252     });
253 }