change layout
[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 get_url_params()
78 {
79     var vars, hashes, i;
80
81     vars = [];
82     hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
83
84     for(i = 0; i < hashes.length; i++) {
85         hash = hashes[i].split('=');
86         vars.push(hash[0]);
87         vars[hash[0]] = hash[1];
88     }
89
90     return vars;
91 }
92
93 function update_chart(chart_id, title, data) {
94     var min_date, max_date, min, max, value;
95     var measures, data_chart, date, entry;
96     var style;
97
98     measures = data["measures"];
99     data_chart = [];
100     
101     $("h1").append(data["name"]);
102     $("title").append(data["name"]);
103     
104     $.each(measures, function(i, item) {
105         value = item["value"];
106         date = new Date(item["time"]*1000);
107         entry = [date, item["value"]];
108         
109         data_chart.push(entry);
110         
111         if (!max_date || max_date < date)
112             max_date = date;        
113         if (!min_date || min_date > date)
114             min_date = date;
115         
116         if (!min || value < min)
117             min = value;
118         if (!max || value > max)
119             max = value;        
120     });
121     
122     style = { 
123         title: title,
124         axes: { 
125             xaxis: {
126                 renderer: $.jqplot.DateAxisRenderer,
127                 tickOptions: {
128                     formatString:'%H:%M:%S'
129                 },
130                 min: min_date,
131                 max: max_date
132             },
133             yaxis: {
134                 min: min-1,
135                 max: max+1
136             }
137         },
138         series: [ { 
139             lineWidth: 1, 
140             showMarker:false 
141         } ]
142     };
143
144     $.jqplot (chart_id, [data_chart], style);
145 }
146
147 function update_menu() {
148     var name, link, url, str;
149
150     $.getJSON("/api/1.0/sensors", function(data) {
151         str = "<li>Sensors\n<ul>";
152
153         $.each(data, function(i, item) {
154             name = item["name"];
155             url = "details.html?id="+escape("/api/1.0/sensors/"+item["id"]);
156             link = "<a href='"+url+"'>"+name+"</a>";
157             str += "<li>"+link+"</li>";
158         });
159
160         str += "</ul>";
161
162         $("#menu-list").append(str);
163     });
164
165 }