-
Notifications
You must be signed in to change notification settings - Fork 0
/
termoview.cgi
105 lines (87 loc) · 3.54 KB
/
termoview.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/perl
#termoview.cgi - показ архивных данных темепратуры окр. воздуха
use utf8;
use open qw(:std :utf8);
use strict;
use lib qw(/var/www/cgi-bin/libs);
use DBI;
use CGI qw(:standard -utf8);
use CGI::Carp qw(fatalsToBrowser);
use Template;
use Time::Piece;
my $today = (Time::Piece->new)->strftime("%d-%m-%Y");
my $yesterday = (Time::Piece->new - (24 * 3600))->strftime("%d-%m-%Y");
my $day_3 = (Time::Piece->new - (3 *24 * 3600))->strftime("%d-%m-%Y");
my $day_7 = (Time::Piece->new - (7 *24 * 3600))->strftime("%d-%m-%Y");
my $day_10 = (Time::Piece->new - (10 *24 * 3600))->strftime("%d-%m-%Y");
my $form = new CGI;
my $date_start = $form->param("date_start") ? $form->param("date_start") : $yesterday;
my $date_end = $form->param("date_end") ? $form->param("date_end") : $today;
my $act = $form->param("act") ? $form->param("act") : 'noprint';
my $dt_end = Time::Piece->strptime($date_end, "%d-%m-%Y") + 24 * 3600;
$dt_end = $dt_end->strftime("%d-%m-%Y");
my $dt_start = $date_start;
my $dbh = DBI->connect("dbi:ODBC:DSN=TEPLO", 'user', 'password') or die "Couldn't connect to database: " . DBI->errstr;
my $sth = $dbh->prepare("set dateformat dmy; SELECT AVG(air) as air from air where date >= ? and date < ?");
$sth->execute($dt_start, $dt_end);
my $period;
while(my @result = $sth->fetchrow()){
$period = $result[0];
}
my %ds = ();
my %avg = ();
while ( Time::Piece->strptime($dt_start, "%d-%m-%Y") < Time::Piece->strptime($dt_end, "%d-%m-%Y") ) {
my $date = Time::Piece->strptime($dt_start, "%d-%m-%Y") + (24 * 3600);
my $date = $date->strftime("%d-%m-%Y");
$sth = $dbh->prepare("set dateformat dmy; SELECT date, FORMAT(air, 'N1') as air from air where date >= ? and date < ? order by date");
$sth->execute($dt_start, $date);
my $results = $sth->fetchall_arrayref(\{ 0 => 'date_p', 1 => 'air' });
$ds{Time::Piece->strptime($dt_start, "%d-%m-%Y")->strftime("%s")} = $results; # хеш с ключом в виде даты в формате UNIX time
$sth = $dbh->prepare("set dateformat dmy; SELECT FORMAT(AVG(air), 'N2') as air from air where date >= ? and date < ?");
$sth->execute($dt_start, $date);
$results = $sth->fetchall_arrayref(\{ 0 => 'avg_air' });
$avg{Time::Piece->strptime($dt_start, "%d-%m-%Y")->strftime("%s")} = $results; # хеш с ключом в виде даты в формате UNIX time
$dt_start = $date;
}
$sth->finish();
$dbh->disconnect();
my %pack = ();
foreach my $key ( keys %ds ) {
my @results = ();
for my $i ( 0 .. 23 ) {
my $date_p = Time::Piece->strptime($key, "%s") + $i * 3600 + 7 * 3600;
$date_p = $date_p->strftime("%Y-%m-%d %H:%M:%S.000");
(my $p) = grep { $date_p eq $_->{'date_p'} } @{ $ds{$key} };
if ($p) {
push @results, { date_p => $p->{'date_p'}, air => $p->{'air'} };
}
else {
push @results, { date_p => $date_p, air => '--' };
}
}
$pack{$key} = [ @results ];
}
my $file = 'termoview.tt';
my $vars = {
'rows' => \%pack,
'avgs' => \%avg,
'period' => $period,
'date_start' => $date_start,
'date_end' => $date_end,
'today' => $today,
'yesterday' => $yesterday,
'day_3' => $day_3,
'day_7' => $day_7,
'day_10' => $day_10,
'act' => $act,
};
my $template = Template->new({
INCLUDE_PATH => 'templates',
ENCODING => 'utf8',
POST_CHOMP => 1,
STRICT => 1,
});
$| = 1;
print "Content-type: text/html\n\n";
$template->process($file, $vars)
|| die "Template process failed: ", $template->error(), "\n";