7d5219c3d9773b6a2a0dd96cda16cb08cffc43f3
[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
87     # convert list of individual testresults to list of ranges of equal results
88     # for speed of display
89     if (defined($row->{log_text}))
90       {         # convert to a hash, find ranges, output list of ranges
91       my $h = { split /\s+/, $row->{log_text} };
92       my $i;
93       my $start;
94       my @ranges;
95
96       foreach my $k (sort {$a<=>$b} keys %$h)
97         {
98         if (defined $start)
99           {
100           if ($h->{$k} ne $h->{$start})
101             {
102             push @ranges, sprintf("%s %s %s", $h->{$start},  $start,  $i);
103             $start = $k;
104             $i = 1;
105             }
106           else
107             {
108             $i++;
109             }
110           }
111         else
112           {
113           $start = $k;
114           $i = 1;
115           }
116         }
117       if (defined $start)
118         {
119         push @ranges, sprintf("%s %s %s", $h->{$start},  $start,  $i);
120         }
121
122       $row->{log_text} = \@ranges;
123       }
124
125     push(@$statrows,$row);
126 }
127
128 $sth->finish;
129
130 $db->disconnect;
131
132 my $template_opts = { INCLUDE_PATH => $template_dir, EVAL_PERL => 1 };
133 my $template = new Template($template_opts);
134
135 print "Content-Type: text/html\n\n";
136
137 $template->process('history.tt',
138                    {statrows=>$statrows, 
139                     branch=>$branch, 
140                     member => $member,
141                     hm => $hm
142                     });
143
144 exit;