| 1 | package Test::Chimps::Server::Lister; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Params::Validate qw<:all>; | ||
| 7 | use Test::Chimps::Report; | ||
| 8 | use HTML::Mason; | ||
| 9 | use DateTime; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Test::Chimps::Server::Lister - Format the list of smoke reports | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | This module encapsulates the formatting and output of the smoke | ||
| 18 | report list. You should not have to use this module directly | ||
| 19 | unless you need to customize listing output. To do so, subclass | ||
| 20 | C<Lister> and pass one to your C<Server>. | ||
| 21 | |||
| 22 | package MyLister; | ||
| 23 | |||
| 24 | use base 'Test::Chimps::Server::Lister'; | ||
| 25 | |||
| 26 | sub foo { ... } | ||
| 27 | |||
| 28 | package main; | ||
| 29 | |||
| 30 | use Test::Chimps::Server; | ||
| 31 | |||
| 32 | my $lister = MyLister->new(); | ||
| 33 | |||
| 34 | my $server = Test::Chimps::Server->new( | ||
| 35 | base_dir => '/var/www/smokes', | ||
| 36 | lister => $lister | ||
| 37 | ); | ||
| 38 | |||
| 39 | $server->handle_request; | ||
| 40 | |||
| 41 | =head1 METHODS | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | use base qw/Class::Accessor/; | ||
| 46 | |||
| 47 | __PACKAGE__->mk_ro_accessors( | ||
| 48 | qw/max_reports_per_subcategory list_template/ | ||
| 49 | ); | ||
| 50 | |||
| 51 | |||
| 52 | =head2 new | ||
| 53 | |||
| 54 | Returns a new Lister object | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub new { | ||
| 59 | my $class = shift; | ||
| 60 | my $obj = bless {}, $class; | ||
| 61 | $obj->_init(@_); | ||
| 62 | return $obj; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _init { | ||
| 66 | my $self = shift; | ||
| 67 | my %args = validate_with( | ||
| 68 | params => \@_, | ||
| 69 | called => 'The Test::Chimps::Server::Lister constructor', | ||
| 70 | spec => { | ||
| 71 | list_template => { | ||
| 72 | type => SCALAR, | ||
| 73 | optional => 0, | ||
| 74 | }, | ||
| 75 | max_reports_per_subcategory => { | ||
| 76 | type => SCALAR, | ||
| 77 | optional => 0 | ||
| 78 | } | ||
| 79 | } | ||
| 80 | ); | ||
| 81 | |||
| 82 | foreach my $key (keys %args) { | ||
| 83 | $self->{$key} = $args{$key}; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 output_list | ||
| 88 | |||
| 89 | Output the smoke report listing. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub output_list { | ||
| 94 | my ($self, $template_dir, $reports, $cgi) = @_; | ||
| 95 | |||
| 96 | my $interp = HTML::Mason::Interp->new(comp_root => $template_dir); | ||
| 97 | |||
| 98 | my $categories = $self->_build_heirarchy($reports); | ||
| 99 | |||
| 100 | $interp->exec(File::Spec->catfile(File::Spec->rootdir, | ||
| 101 | $self->list_template), | ||
| 102 | categories => $categories, | ||
| 103 | cgi => $cgi); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _build_heirarchy { | ||
| 107 | my $self = shift; | ||
| 108 | my $reports = shift; | ||
| 109 | |||
| 110 | my $categories = {}; | ||
| 111 | foreach my $report (@$reports) { | ||
| 112 | my $category = $self->_compute_category($report); | ||
| 113 | my $subcategory = $self->_compute_subcategory($report); | ||
| 114 | push @{$categories->{$category}->{$subcategory}}, $report; | ||
| 115 | } | ||
| 116 | $self->_sort_reports($categories); | ||
| 117 | $self->_prune_reports($categories); | ||
| 118 | return $categories; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _compute_category { | ||
| 122 | my $self = shift; | ||
| 123 | my $report = shift; | ||
| 124 | return $report->project; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _compute_subcategory { | ||
| 128 | my $self = shift; | ||
| 129 | my $report = shift; | ||
| 130 | return ''; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _sort_reports { | ||
| 134 | my $self = shift; | ||
| 135 | my $categories = shift; | ||
| 136 | |||
| 137 | foreach my $category (keys %$categories) { | ||
| 138 | foreach my $subcategory (keys %{$categories->{$category}}) { | ||
| 139 | @{$categories->{$category}->{$subcategory}} = | ||
| 140 | sort _by_revision_then_date @{$categories->{$category}->{$subcategory}}; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _by_revision_then_date { | ||
| 146 | my $res = $b->revision <=> $a->revision; | ||
| 147 | |||
| 148 | if ($res != 0) { | ||
| 149 | return $res; | ||
| 150 | } | ||
| 151 | |||
| 152 | return DateTime->compare($b->timestamp, $a->timestamp); | ||
| 153 | } | ||
| 154 | |||
| 155 | sub _prune_reports { | ||
| 156 | my $self = shift; | ||
| 157 | my $categories = shift; | ||
| 158 | |||
| 159 | foreach my $category (keys %$categories) { | ||
| 160 | foreach my $subcategory (keys %{$categories->{$category}}) { | ||
| 161 | if (scalar @{$categories->{$category}->{$subcategory}} > | ||
| 162 | $self->max_reports_per_subcategory) | ||
| 163 | { | ||
| 164 | @{$categories->{$category}->{$subcategory}} = | ||
| 165 | @{$categories->{$category}->{$subcategory}}[0 .. ($self->max_reports_per_subcategory - 1)]; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | =head1 AUTHOR | ||
| 172 | |||
| 173 | Zev Benjamin, C<< <zev at cpan.org> >> | ||
| 174 | |||
| 175 | =head1 BUGS | ||
| 176 | |||
| 177 | Please report any bugs or feature requests to | ||
| 178 | C<bug-test-chimps at rt.cpan.org>, or through the web interface at | ||
| 179 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Chimps>. | ||
| 180 | I will be notified, and then you'll automatically be notified of progress on | ||
| 181 | your bug as I make changes. | ||
| 182 | |||
| 183 | =head1 SUPPORT | ||
| 184 | |||
| 185 | You can find documentation for this module with the perldoc command. | ||
| 186 | |||
| 187 | perldoc Test::Chimps | ||
| 188 | |||
| 189 | You can also look for information at: | ||
| 190 | |||
| 191 | =over 4 | ||
| 192 | |||
| 193 | =item * Mailing list | ||
| 194 | |||
| 195 | Chimps has a mailman mailing list at | ||
| 196 | L<chimps@bestpractical.com>. You can subscribe via the web | ||
| 197 | interface at | ||
| 198 | L<http://lists.bestpractical.com/cgi-bin/mailman/listinfo/chimps>. | ||
| 199 | |||
| 200 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 201 | |||
| 202 | L<http://annocpan.org/dist/Test-Chimps> | ||
| 203 | |||
| 204 | =item * CPAN Ratings | ||
| 205 | |||
| 206 | L<http://cpanratings.perl.org/d/Test-Chimps> | ||
| 207 | |||
| 208 | =item * RT: CPAN's request tracker | ||
| 209 | |||
| 210 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Chimps> | ||
| 211 | |||
| 212 | =item * Search CPAN | ||
| 213 | |||
| 214 | L<http://search.cpan.org/dist/Test-Chimps> | ||
| 215 | |||
| 216 | =back | ||
| 217 | |||
| 218 | =head1 COPYRIGHT & LICENSE | ||
| 219 | |||
| 220 | Copyright 2006 Best Practical Solutions. | ||
| 221 | |||
| 222 | This program is free software; you can redistribute it and/or modify it | ||
| 223 | under the same terms as Perl itself. | ||
| 224 | |||
| 225 | =cut | ||
| 226 | |||
| 227 | 1; |