Detail on the per-animal history page
[buildfarm-server.git] / cgi-bin / show_history.pl
1 #!/usr/bin/perl
2
3 =comment
4
5 Copyright (c) 2003-2010, Andrew Dunstan
6
7 See accompanying License file for license details
8
9 =cut 
10
11 use strict;
12 use DBI;
13 use Template;
14 use CGI;
15
16 use vars qw($dbhost $dbname $dbuser $dbpass $dbport $template_dir);
17
18 use FindBin qw($RealBin);
19 require "$RealBin/../BuildFarmWeb.pl";
20
21 die "no dbname" unless $dbname;
22 die "no dbuser" unless $dbuser;
23
24 my $dsn="dbi:Pg:dbname=$dbname";
25 $dsn .= ";host=$dbhost" if $dbhost;
26 $dsn .= ";port=$dbport" if $dbport;
27
28 my $db = DBI->connect($dsn,$dbuser,$dbpass);
29
30 die $DBI::errstr unless $db;
31
32 my $query = new CGI;
33 my $member = $query->param('nm'); $member =~ s/[^a-zA-Z0-9_ -]//g;
34 my $branch = $query->param('br'); $branch =~ s/[^a-zA-Z0-9_ -]//g;
35 my $hm = $query->param('hm');  $hm =~ s/[^a-zA-Z0-9_ -]//g;
36 $hm = '240' unless $hm =~ /^\d+$/;
37
38 my $latest_personality = $db->selectrow_arrayref(q{
39             select os_version, compiler_version
40             from personality
41             where name = ?
42             order by effective_date desc limit 1
43         }, undef, $member);
44
45 my $systemdata = q{
46     select operating_system, os_version, compiler, compiler_version, architecture,
47       owner_email, sys_notes_ts::date AS sys_notes_date, sys_notes
48     from buildsystems b
49     where b.status = 'approved'
50         and name = ?
51 };
52
53 my $statement = qq{
54    WITH x AS 
55    (  select * 
56       from build_status_recent_500
57       where sysname = ? 
58          and branch = ?
59    )
60    SELECT (now() at time zone 'GMT')::timestamp(0) - x.snapshot as when_ago, 
61             x.sysname, x.snapshot, x.status, x.stage, s.log_text
62    FROM x 
63    LEFT JOIN build_status_log s
64    ON x.snapshot = s.snapshot AND s.log_stage = 'test-results.log'
65    ORDER BY x.snapshot desc  
66    LIMIT $hm
67 }
68 ;
69
70 my $sth = $db->prepare($systemdata);
71 $sth->execute($member);
72 my $sysrow = $sth->fetchrow_hashref;
73 my $statrows=[];
74 $sth=$db->prepare($statement);
75 $sth->execute($member,$branch);
76 while (my $row = $sth->fetchrow_hashref)
77 {
78     last unless $sysrow;
79     while (my($k,$v) = each %$sysrow) { $row->{$k} = $v; }
80     $row->{owner_email} =~ s/\@/ [ a t ] /;
81     if ($latest_personality)
82     {
83         $row->{os_version} = $latest_personality->[0];
84         $row->{compiler_version} = $latest_personality->[1];
85     }
86     if (defined($row->{log_text}))
87       {         # convert to a hash, find ranges, output list of ranges
88       my $h = { split /\s+/, $row->{log_text} };
89       my $i;
90       my $start;
91       my @ranges;
92
93       foreach my $k (sort {$a<=>$b} keys %$h)
94         {
95         if (defined $start)
96           {
97           if ($h->{$k} ne $h->{$start})
98             {
99             push @ranges, sprintf("%s %s %s", $h->{$start},  $start,  $i);
100             $start = $k;
101             $i = 1;
102             }
103           else
104             {
105             $i++;
106             }
107           }
108         else
109           {
110           $start = $k;
111           $i = 1;
112           }
113         }
114       if (defined $start)
115         {
116         push @ranges, sprintf("%s %s %s", $h->{$start},  $start,  $i);
117         }
118
119       $row->{log_text} = \@ranges;
120       }
121
122     push(@$statrows,$row);
123 }
124
125 $sth->finish;
126
127 $db->disconnect;
128
129 my $template_opts = { INCLUDE_PATH => $template_dir, EVAL_PERL => 1 };
130 my $template = new Template($template_opts);
131
132 print "Content-Type: text/html\n\n";
133
134 $template->process('history.tt',
135                    {statrows=>$statrows, 
136                     branch=>$branch, 
137                     member => $member,
138                     hm => $hm
139                     });
140
141 exit;