split status-bar ranges on noncotiguous testcase numbers
[buildfarm-server.git] / cgi-bin / show_status.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 my $query = new CGI;
22 my @members = $query->multi_param('member');
23 map { s/[^a-zA-Z0-9_ -]//g; } @members;
24
25 my $dsn="dbi:Pg:dbname=$dbname";
26 $dsn .= ";host=$dbhost" if $dbhost;
27 $dsn .= ";port=$dbport" if $dbport;
28
29
30 my $sort_clause = "";
31 my $sortby = $query->param('sortby') || 'nosort';
32 if ($sortby eq 'name')
33 {
34         $sort_clause = 'lower(sysname),';
35 }
36 elsif ($sortby eq 'os')
37 {
38         $sort_clause = 'lower(operating_system), os_version desc,'; 
39 }
40 elsif ($sortby eq 'compiler')
41 {
42         $sort_clause = "lower(compiler), compiler_version,";
43 }
44
45 my $db = DBI->connect($dsn,$dbuser,$dbpass,{pg_expand_array => 0}) 
46     or die("$dsn,$dbuser,$dbpass,$!");
47
48 my $statement =<<EOS;
49
50
51   SELECT timezone('GMT'::text, now())::timestamp(0) without time zone - b.snapshot AS when_ago,
52         b.*,
53         s.log_text
54   FROM dashboard_mat b LEFT JOIN build_status_log s
55   ON    b.sysname = s.sysname
56     AND b.snapshot = s.snapshot
57     AND s.log_stage = 'test-results.log'
58   ORDER BY b.branch = 'master' desc,
59         b.branch desc, $sort_clause 
60         b.snapshot desc
61
62 EOS
63 ;
64
65 my $statrows=[];
66 my $sth=$db->prepare($statement);
67 $sth->execute;
68
69 # walk the set of result rows from the SQL query above
70 while (my $row = $sth->fetchrow_hashref)
71 {
72     next if (@members && ! grep {$_ eq $row->{sysname} } @members);
73
74     $row->{build_flags}  =~ s/^\{(.*)\}$/$1/;
75     $row->{build_flags}  =~ s/,/ /g;
76     $row->{build_flags}  =~ s/_PC\b//g;
77     $row->{build_flags} = lc($row->{build_flags});
78
79     if (defined($row->{log_text}))
80       {         # convert to a hash, find ranges, output list of ranges
81       my $h = { split /\s+/, $row->{log_text} };
82       my ($i, $start, $last);
83       my @ranges;
84
85       foreach my $k (sort {$a<=>$b} keys %$h)
86         {
87         if (defined $start)
88           {
89           if ($h->{$k} ne $h->{$start} || $k != $last + 1)
90             {
91             # The result (skiped, Passed, Failed) for this testcase number
92             # is different to the start one of the range,
93             # or the range became non-contiguous.
94             # Add text for the range to list of ranges
95             # (these three elements get used by "status.tt" BLOCK colourbar)
96             # Reset the start and the count, for a new range.
97
98             push @ranges, sprintf("%s %s %s", $h->{$start},  $start,  $i);
99             $start = $k;
100             $i = 1;
101             }
102           else
103             {
104             # bump the size of this range
105             $i++;
106             }
107           }
108         else
109           {
110           # First ever range for this row
111           $start = $k;
112           $i = 1;
113           }
114         $last = $k;
115         }
116       if (defined $start)
117         {
118         # close out the final range
119         push @ranges, sprintf("%s %s %s", $h->{$start},  $start,  $i);
120         }
121
122       $row->{log_text} = \@ranges;
123       }
124     push(@$statrows,$row);
125 }
126 $sth->finish;
127
128
129 $db->disconnect;
130
131
132 my $template_opts = { INCLUDE_PATH => $template_dir };
133 my $template = new Template($template_opts);
134
135 print "Content-Type: text/html\n\n";
136
137 $template->process('status.tt',
138                 {statrows=>$statrows});
139
140 exit;
141