add filtering to failures page.
[buildfarm-server.git] / cgi-bin / show_log.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 
17                         $template_dir @log_file_names $local_git_clone);
18
19 require "$ENV{BFConfDir}/BuildFarmWeb.pl";
20
21 my $template_opts = { INCLUDE_PATH => $template_dir, EVAL_PERL => 1};
22 my $template = new Template($template_opts);
23
24 die "no dbname" unless $dbname;
25 die "no dbuser" unless $dbuser;
26
27 my $dsn="dbi:Pg:dbname=$dbname";
28 $dsn .= ";host=$dbhost" if $dbhost;
29 $dsn .= ";port=$dbport" if $dbport;
30
31 my $query = new CGI;
32
33 my $system = $query->param('nm'); $system =~ s/[^a-zA-Z0-9_ -]//g;
34 my $logdate = $query->param('dt'); $logdate =~ s/[^a-zA-Z0-9_ :-]//g;
35
36 my $log = "";
37 my $conf = "";
38 my ($stage,$changed_this_run,$changed_since_success,$sysinfo,$branch,$scmurl);
39 my $scm;
40 my ($git_head_ref, $last_build_git_ref, $last_success_git_ref);
41 my ($stage_times, $run_time);
42
43 use vars qw($info_row);
44
45 if ($system && $logdate)
46 {
47
48         my $db = DBI->connect($dsn,$dbuser,$dbpass,{pg_expand_array => 0});
49
50         die $DBI::errstr unless $db;
51
52         my $statement = q{
53
54                 select log,conf_sum,stage, changed_this_run, changed_since_success,
55                 branch, log_archive_filenames, scm, scmurl, git_head_ref
56                 from build_status
57                 where sysname = ? and snapshot = ?
58
59         };
60         my $last_build_statement = q{
61                 select distinct on (sysname) sysname, snapshot, stage, git_head_ref 
62         from build_status 
63         where sysname = ? and branch = ? and snapshot < ? 
64         order by sysname, snapshot desc limit 1
65         };
66         my $last_success_statement = q{
67                 select distinct on (sysname) sysname, snapshot, git_head_ref 
68         from build_status 
69         where sysname = ? and branch = ? and snapshot < ? and stage = 'OK' 
70         order by sysname, snapshot desc limit 1
71         };
72         my $sth=$db->prepare($statement);
73         $sth->execute($system,$logdate);
74         my $row=$sth->fetchrow_arrayref;
75         $branch = $row->[5];
76         $git_head_ref = $row->[9];
77         $sth->finish;
78         my $last_build_row;
79         if ($git_head_ref)
80         {
81                 $last_build_row = 
82                   $db->selectrow_hashref($last_build_statement,undef,
83                                                                  $system,$branch,$logdate);
84                 $last_build_git_ref = $last_build_row->{git_head_ref}
85                   if $last_build_row;
86                 
87         }
88         my $last_success_row;
89         if (ref $last_build_row && $last_build_row->{stage} ne 'OK')
90         {
91                 $last_success_row =
92                   $db->selectrow_hashref($last_success_statement,undef,
93                                                                  $system,$branch,$logdate);
94                 $last_success_git_ref = $last_success_row->{git_head_ref}
95                   if $last_success_row;
96         }
97         $log=$row->[0];
98         $conf=$row->[1] || "not recorded" ;
99         $stage=$row->[2] || "unknown";
100         $changed_this_run = $row->[3];
101         $changed_since_success = $row->[4];
102         my $log_file_names = $row->[6];
103         $scm = $row->[7];
104         $scm ||= 'cvs'; # legacy scripts
105         $scmurl = $row->[8];
106         $scmurl = 'http://git.postgresql.org/gitweb?p=postgresql.git;a=commit;h=' 
107             if ($scmurl eq 'http://git.postgresql.org/git/postgresql.git');
108         $log_file_names =~ s/^\{(.*)\}$/$1/;
109         @log_file_names=split(',',$log_file_names)
110             if $log_file_names;
111
112         $statement = q{
113
114           select operating_system, os_version, 
115                  compiler, compiler_version, 
116                  architecture,
117                  replace(owner_email,E'\@',' [ a t ] ') as owner_email,
118                  sys_notes_ts::date AS sys_notes_date, sys_notes
119           from buildsystems 
120           where status = 'approved'
121                 and name = ?
122
123         };
124         $sth=$db->prepare($statement);
125         $sth->execute($system);
126         $info_row=$sth->fetchrow_hashref;
127
128         my $latest_personality = $db->selectrow_arrayref(q{
129             select os_version, compiler_version
130             from personality
131             where effective_date < ?
132             and name = ?
133             order by effective_date desc limit 1
134         }, undef, $logdate, $system);
135         if ($latest_personality)
136         {
137             $info_row->{os_version} = $latest_personality->[0];
138             $info_row->{compiler_version} = $latest_personality->[1];
139         }
140         $sth->finish;
141         my $stage_times_query = q{
142            select log_stage, stage_duration
143            from build_status_log
144            where sysname = ? and snapshot = ?
145         };
146         $stage_times = 
147             $db->selectall_hashref($stage_times_query,'log_stage',undef,
148                                    $system,$logdate);
149         $stage_times_query = q{
150            select sum(stage_duration)
151            from build_status_log
152            where sysname = ? and snapshot = ?
153         };
154         ($run_time) = $db->selectrow_array($stage_times_query,undef,
155                                    $system,$logdate);
156         $db->disconnect;
157 }
158
159 my ($changed_this_run_logs, $changed_since_success_logs);
160 ($changed_this_run, $changed_this_run_logs) = 
161   process_changed($changed_this_run,
162                                   $git_head_ref,$last_build_git_ref);
163 ($changed_since_success, $changed_since_success_logs) = 
164   process_changed($changed_since_success,
165                                   $last_build_git_ref,$last_success_git_ref);
166
167 $conf =~ s/\@/ [ a t ] /g;
168
169 print "Content-Type: text/html\n\n";
170
171 $template->process('log.tt',
172         {
173                 scm => $scm,
174                 scmurl => $scmurl,
175                 system => $system,
176                 branch => $branch,
177                 stage => $stage,
178                 stage_times => $stage_times,
179                 run_time => $run_time,
180                 urldt => $logdate,
181                 log_file_names => \@log_file_names,
182                 conf => $conf,
183                 log => $log,
184                 changed_this_run => $changed_this_run,
185                 changed_since_success => $changed_since_success,
186                 changed_this_run_logs => $changed_this_run_logs,
187                 changed_since_success_logs => $changed_since_success_logs,
188                 info_row => $info_row,
189             git_head_ref => $git_head_ref,
190             last_build_git_ref => $last_build_git_ref,
191             last_success_git_ref => $last_success_git_ref,
192
193         });
194
195 exit;
196
197 ##########################################################
198
199 sub process_changed
200 {
201
202         my $chgd = shift;
203         my $git_to = shift;
204         my $git_from = shift;
205
206     my @lines = split(/!/,$chgd);
207     my @changed_rows;
208         my %commits;
209         my @commit_logs;
210         my $gitcmd = "TZ=UTC GIT_DIR=$local_git_clone git log --date=local";
211     foreach (@lines)
212     {
213                 next if ($scm eq 'cvs' and ! m!^(pgsql|master|REL\d_\d_STABLE)/!);
214                 push(@changed_rows,[$1,$3]) if (m!(^\S+)(\s+)(\S+)!);
215                 $commits{$3} = 1 if $scm eq 'git';
216     }
217         if ($git_from && $git_to)
218         {
219                 my $format = 'commit %h %cd UTC%w(160,2,2)%s';
220                 my $gitlog = `$gitcmd --pretty=format:"$format" $git_from..$git_to 2>&1`;
221                 @commit_logs = split(/(?=^commit)/m,$gitlog)
222         }
223         else
224         {
225                 # normally we expect to have the git refs. this is just a fallback.
226                 my $format = 'epoch: %at%ncommit %h %cd UTC%w(160,2,2)%s';
227                 foreach my $commit ( keys %commits )
228                 {
229                         my $commitlog = 
230                           `$gitcmd -n 1 --pretty=format:"$format" $commit 2>&1`;
231                         push(@commit_logs,$commitlog);
232                 }
233                 @commit_logs = reverse (sort @commit_logs);
234                 s/epoch:.*\n// for (@commit_logs);
235         }
236                 return (\@changed_rows,\@commit_logs);
237 }
238