862aa1d26dae0de65906d24886ca185111ceafcd
[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 {
83     var vars, hashes, i;
84
85     vars = [];
86     hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
87
88     for(i = 0; i < hashes.length; i++) {
89         hash = hashes[i].split('=');
90         vars.push(hash[0]);
91         vars[hash[0]] = hash[1];
92     }
93
94     return vars;
95 }
96
97 function update_chart(chart_id, title, data) {
98     var min_date, max_date, min, max, value;
99     var measures, data_chart, date, entry;
100     var style;
101
102     measures = data["measures"];
103     data_chart = [];
104     
105     $("h1").append(data["name"]);
106     $("title").append(data["name"]);
107     
108     $.each(measures, function(i, item) {
109         value = item["value"];
110         date = new Date(item["time"]*1000);
111         entry = [date, item["value"]];
112         
113         data_chart.push(entry);
114         
115         if (!max_date || max_date < date)
116             max_date = date;        
117         if (!min_date || min_date > date)
118             min_date = date;
119         
120         if (!min || value < min)
121             min = value;
122         if (!max || value > max)
123             max = value;        
124     });
125     
126     style = { 
127         title: title,
128         axes: { 
129             xaxis: {
130                 renderer: $.jqplot.DateAxisRenderer,
131                 tickOptions: {
132                     formatString:'%H:%M:%S'
133                 },
134                 min: min_date,
135                 max: max_date
136             },
137             yaxis: {
138                 min: min-1,
139                 max: max+1
140             }
141         },
142         series: [ { 
143             lineWidth: 1, 
144             showMarker: false 
145         } ]
146     };
147
148     $.jqplot (chart_id, [data_chart], style);
149 }
150
151 function update_menu() {
152     var name, link, url, str;
153
154     $.getJSON("/api/1.0/sensors", function(data) {
155         str = "<li><em>Sensors</em>\n<ul>";
156
157         $.each(data, function(i, item) {
158             name = item["name"];
159             url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
160             link = "<a href='"+url+"'>"+name+"</a>";
161             str += "<li>"+link+"</li>";
162         });
163
164         str += "</ul>";
165
166         $("#menu-list").append(str);
167     });
168
169 }
170
171 function update_summary_sensors() {
172     var name, value_str, min_str, max_str, type, type_str, url;
173
174     $.getJSON("/api/1.0/sensors", function(data) {
175         $.each(data, function(i, item) {
176             name = item["name"];
177             type = item["type"];
178             value_str = value_to_str(item["last_measure"]["value"], type);
179             min_str = value_to_str(item["min"], type);
180             max_str = value_to_str(item["max"], type);
181             type_str = type_to_str(type);
182             url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
183
184             $("#sensors").append("<tr>"
185                                  +"<td><a href='"+url+"'>"+name+"</a></td>"
186                                  +"<td>"+value_str+"</td>"
187                                  +"<td>"+min_str+"</td>"
188                                  +"<td>"+max_str+"</td>"
189                                  +"<td>"+type_str+"</td>"
190                                  +"</tr>");                 
191         });          
192     });
193 }