minor fixes
[sgn.git] / cgi-bin / about / temp.pl
blob952e409b667b9289051b16d43330985d1f6ea58c
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use GD::Graph::lines;
5 use CXGN::Page;
6 use CatalystX::GlobalContext '$c';
8 my $page = CXGN::Page->new();
10 # get from url
11 my ($deg) = $page->get_encoded_arguments('deg') || 'F';
12 my ($mode) = $page->get_encoded_arguments('mode');
13 my ($detail) = $page->get_encoded_arguments('detail');
14 $detail||=1;
15 my ($days) = $page->get_encoded_arguments('days');
16 $days ||= 7;
17 my ($imgsize) = $page->get_encoded_arguments('imgsize');
19 # Get the digitemp data
20 my $data_file = $c->config->{"pucebaboon_file"};
22 open (my $fh, $data_file) or barf("Can't open file $data_file: $!");
23 my @lines = <$fh>;
24 close $fh;
26 # get only the last X days' worth
27 @lines = splice(@lines,(-576*$days));
28 my $l = @lines;
29 warn "using last $l days of data\n$lines[0]\n$lines[-1]\n";
31 # Parse it into something we can graph
32 my (@times, @temp_C, @temp_F);
33 for my $i (0..$#lines){
34 next unless ($i%$detail==0);
35 my @fields = split(/ /,$lines[$i]);
36 push(@times, $fields[2]);
37 push(@temp_C, $fields[6]);
38 push(@temp_F, $fields[8]);
39 # $times[$i] = $fields[2];
40 # $temp_C[$i] = $fields[6];
41 # $temp_F[$i] = $fields[8];
44 barf("Hey, where's the data?") unless @times && @temp_C;
46 my $random = "&r=" . int(rand(1)*1000000000);
47 my $current_C = $temp_C[-1];
48 my $current_F = $temp_F[-1];
50 sub _last_mod { (stat $_[0])[9] }
51 my $file_ts = _last_mod($data_file);
52 my $time = localtime($file_ts);
54 my $time_diff = time() - $file_ts;
56 my $day_diff = sprintf("%.1f", $time_diff/60/60/24);
58 my $temp_disp =<<HTML;
59 <font size="10">$current_C C ($current_F F)</font>
60 HTML
62 if($time_diff > 1800) { #30 minutes
63 $temp_disp=<<HTML;
64 <font style="color:#555" size="5">$current_C C ($current_F F)</font><br />
65 <font size="6"> OUT-OF-DATE: $day_diff days</font>
66 HTML
69 if ($mode eq "current") {
70 $page->send_content_type_header();
71 print <<HTML;
72 <html>
73 <head>
74 </head>
75 <body>
76 <!--
77 <table width="100%"><tr><td width="150">
78 <img src="/documents/img/sgn_logo_icon.png" border="0" />
79 </td>
80 <td style="text-align:center;vertical-align:middle">
81 <h2>Server Room Status</h2>
82 </td>
83 <td width="150" style="text-align:right">
84 <img src="/documents/img/sgn_logo_icon.png" border="0" />
85 </td>
86 </tr>
87 </table>
88 <hr>
89 -->
90 <table width="1200">
91 <tr>
92 <td width="600">
93 Temperature as of $time:<br />
94 $temp_disp<br /><br />
95 <span style="font-size:0.9em">
96 <a target="spong" href="cluster_services.pl?mode=html">Cluster QuickView</a>
97 </span>
98 </td>
99 <td width="600">
100 <a href="temp.pl" target="_TOP">
101 <img border="0" src="temp.pl?&imgsize=small&detail=10$random&days=$days" width="600" height="100"/>
102 </a>
103 </td>
104 </tr></table>
105 </body>
106 </html>
108 HTML
110 exit;
113 # graph settings
114 my $graph;
115 if ($imgsize eq 'small'){
116 $graph = GD::Graph::lines->new(600,100);
117 } else {
118 $graph = GD::Graph::lines->new(1000,300);
120 my $current = $current_F;
121 if ($deg eq "C") {
122 $current = $current_C;
124 chomp $current;
126 $graph->set(
127 'x_label' => 'time',
128 'y_label' => "degrees $deg",
129 'title' => "Temperature in the server room - currently $current$deg - Last $days days",
130 'y_max_value' => ($deg eq 'C' ? 32 : 90),
131 'y_min_value' => ($deg eq 'C' ? 21 : 70),
132 'x_label_skip' => @times/5,
133 'transparent' => 0
134 ) or barf($graph->error());
138 # assemble the array and make the graph
139 my $data;
140 if ($deg eq "C") {
141 $data = [\@times, \@temp_C ];
143 else {
144 $data = [\@times, \@temp_F];
146 my $gd = $graph->plot($data) or barf($graph->error());
147 print "Content-type: image/png\n\n".$gd->png();
149 sub barf {
151 my $message = shift;
153 print "Content-type: text/plain\n\n$message\n";
155 exit;