| 1 | package Smolder::DB::Project; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Smolder::DB'; | ||
| 5 | use Smolder::DB::Developer; | ||
| 6 | use Smolder::Conf qw(DataDir); | ||
| 7 | use File::Path; | ||
| 8 | use File::Spec::Functions qw(catdir); | ||
| 9 | |||
| 10 | __PACKAGE__->set_up_table('project'); | ||
| 11 | __PACKAGE__->has_many('project_developers' => 'Smolder::DB::ProjectDeveloper'); | ||
| 12 | __PACKAGE__->has_many('smoke_reports' => 'Smolder::DB::SmokeReport'); | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Smolder::DB::Project | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | L<Class::DBI> based model class for the 'project' table in the database. | ||
| 21 | |||
| 22 | =head1 METHODS | ||
| 23 | |||
| 24 | =head2 ACCESSSOR/MUTATORS | ||
| 25 | |||
| 26 | Each column in the borough table has a method with the same name | ||
| 27 | that can be used as an accessor and mutator. | ||
| 28 | |||
| 29 | The following columns will return objects instead of the value contained in the table: | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | __PACKAGE__->has_a( | ||
| 34 | start_date => 'DateTime', | ||
| 35 | inflate => sub { __PACKAGE__->parse_datetime(shift) }, | ||
| 36 | deflate => sub { __PACKAGE__->format_datetime(shift) }, | ||
| 37 | ); | ||
| 38 | |||
| 39 | # make sure we delete any test_report directories associated with us | ||
| 40 | __PACKAGE__->add_trigger( | ||
| 41 | after_delete => sub { | ||
| 42 | my $self = shift; | ||
| 43 | my $dir = catdir(DataDir, 'smoke_reports', $self->id); | ||
| 44 | rmtree($dir) if (-d $dir); | ||
| 45 | } | ||
| 46 | ); | ||
| 47 | |||
| 48 | =over | ||
| 49 | |||
| 50 | =item start_date | ||
| 51 | |||
| 52 | This is a L<DateTime> object representing the datetime stored. | ||
| 53 | |||
| 54 | =back | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | =head2 OBJECT METHODS | ||
| 59 | |||
| 60 | =head3 developers | ||
| 61 | |||
| 62 | Returns an array of all L<Smolder::DB::Developer> objects associated with this | ||
| 63 | Project (using the C<project_developer> join table. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | sub developers { | ||
| 68 | my $self = shift; | ||
| 69 | my $sth = $self->db_Main->prepare_cached( | ||
| 70 | qq( | ||
| 71 | SELECT developer.* FROM developer, project_developer | ||
| 72 | WHERE project_developer.project = ? AND project_developer.developer = developer.id | ||
| 73 | ORDER BY project_developer.added | ||
| 74 | ) | ||
| 75 | ); | ||
| 76 | $sth->execute($self->id); | ||
| 77 | return Smolder::DB::Developer->sth_to_objects($sth); | ||
| 78 | } | ||
| 79 | |||
| 80 | =head3 has_developer | ||
| 81 | |||
| 82 | Return true if the given L<Smolder::DB::Developer> object is considered a member | ||
| 83 | of this Project. | ||
| 84 | |||
| 85 | if( ! $project->has_developer($dev) ) { | ||
| 86 | return "Unauthorized!"; | ||
| 87 | } | ||
| 88 | |||
| 89 | =cut | ||
| 90 | |||
| 91 | sub has_developer { | ||
| 92 | my ($self, $developer) = @_; | ||
| 93 | my $sth = $self->db_Main->prepare_cached( | ||
| 94 | qq( | ||
| 95 | SELECT COUNT(*) FROM project_developer | ||
| 96 | WHERE project = ? AND developer = ? | ||
| 97 | ) | ||
| 98 | ); | ||
| 99 | $sth->execute($self->id, $developer->id); | ||
| 100 | my $row = $sth->fetchrow_arrayref(); | ||
| 101 | $sth->finish(); | ||
| 102 | return $row->[0]; | ||
| 103 | } | ||
| 104 | |||
| 105 | =head3 admins | ||
| 106 | |||
| 107 | Returns a list of L<Smolder::DB::Developer> objects who are considered 'admins' | ||
| 108 | for this Project | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub admins { | ||
| 113 | my $self = shift; | ||
| 114 | my $sth = $self->db_Main->prepare_cached( | ||
| 115 | qq( | ||
| 116 | SELECT d.* FROM project_developer pd, developer d | ||
| 117 | WHERE pd.project = ? AND pd.developer = d.id AND pd.admin = 1 | ||
| 118 | ORDER BY d.id | ||
| 119 | ) | ||
| 120 | ); | ||
| 121 | $sth->execute($self->id); | ||
| 122 | my @admins = Smolder::DB::Developer->sth_to_objects($sth); | ||
| 123 | return @admins; | ||
| 124 | } | ||
| 125 | |||
| 126 | =head3 is_admin | ||
| 127 | |||
| 128 | Returns true if the given L<Smolder::DB::Developer> is considered an 'admin' | ||
| 129 | for this Project. | ||
| 130 | |||
| 131 | if( $project->is_admin($developer) { | ||
| 132 | ... | ||
| 133 | } | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | sub is_admin { | ||
| 138 | my ($self, $developer) = @_; | ||
| 139 | if ($developer) { | ||
| 140 | my $sth = $self->db_Main->prepare_cached( | ||
| 141 | qq( | ||
| 142 | SELECT admin FROM project_developer | ||
| 143 | WHERE developer = ? AND project = ? | ||
| 144 | ) | ||
| 145 | ); | ||
| 146 | $sth->execute($developer->id, $self->id); | ||
| 147 | my $row = $sth->fetchrow_arrayref(); | ||
| 148 | $sth->finish(); | ||
| 149 | return $row->[0]; | ||
| 150 | } else { | ||
| 151 | return; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | =head3 clear_admins | ||
| 156 | |||
| 157 | Removes the 'admin' flag from any Developers associated with this Project. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub clear_admins { | ||
| 162 | my ($self, @admins) = @_; | ||
| 163 | my $sth; | ||
| 164 | if (@admins) { | ||
| 165 | my $place_holders = join(', ', ('?') x scalar @admins); | ||
| 166 | $sth = $self->db_Main->prepare_cached( | ||
| 167 | qq( | ||
| 168 | UPDATE project_developer SET admin = 0 | ||
| 169 | WHERE project = ? AND developer IN ($place_holders) | ||
| 170 | ) | ||
| 171 | ); | ||
| 172 | } else { | ||
| 173 | $sth = $self->db_Main->prepare_cached( | ||
| 174 | qq( | ||
| 175 | UPDATE project_developer SET admin = 0 | ||
| 176 | WHERE project_developer.project = ? | ||
| 177 | ) | ||
| 178 | ); | ||
| 179 | } | ||
| 180 | $sth->execute($self->id, @admins); | ||
| 181 | } | ||
| 182 | |||
| 183 | =head3 set_admins | ||
| 184 | |||
| 185 | Given a list of Developer id's, this method will set each Developer | ||
| 186 | to be an admin of the Project. | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | sub set_admins { | ||
| 191 | my ($self, @admins) = @_; | ||
| 192 | my $place_holders = join(', ', ('?') x scalar @admins); | ||
| 193 | my $sql = qq( | ||
| 194 | UPDATE project_developer SET admin = 1 | ||
| 195 | WHERE project = ? AND developer IN ($place_holders) | ||
| 196 | ); | ||
| 197 | my $sth = $self->db_Main->prepare_cached($sql); | ||
| 198 | $sth->execute($self->id, @admins); | ||
| 199 | } | ||
| 200 | |||
| 201 | =head3 all_reports | ||
| 202 | |||
| 203 | Returns a list of L<Smolder::DB::SmokeReport> objects that are associate with this | ||
| 204 | Project in descending order (by default). You can provide optional 'limit' and 'offset' parameters | ||
| 205 | which will control which reports (and how many) are returned. | ||
| 206 | |||
| 207 | You can additionally specify a 'direction' parameter to specify the order in which they | ||
| 208 | are returned. | ||
| 209 | |||
| 210 | # all of them | ||
| 211 | my @reports = $project->all_reports(); | ||
| 212 | |||
| 213 | # just 5 most recent | ||
| 214 | @reports = $project->all_reports( | ||
| 215 | limit => 5 | ||
| 216 | ); | ||
| 217 | |||
| 218 | # the next 5 | ||
| 219 | @reports = $project->all_reports( | ||
| 220 | limit => 5, | ||
| 221 | offset => 5, | ||
| 222 | ); | ||
| 223 | |||
| 224 | # in ascendig order | ||
| 225 | @reports = $project->all_reports( | ||
| 226 | direction => 'ASC', | ||
| 227 | ); | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | sub all_reports { | ||
| 232 | my ($self, %args) = @_; | ||
| 233 | my $limit = $args{limit} || 0; | ||
| 234 | my $offset = $args{offset} || 0; | ||
| 235 | my $direction = $args{direction} || 'DESC'; | ||
| 236 | my $tag = $args{tag}; | ||
| 237 | my @bind_vars = ($self->id); | ||
| 238 | |||
| 239 | my $sql; | ||
| 240 | if ($tag) { | ||
| 241 | $sql = q/SELECT sr.* FROM smoke_report sr | ||
| 242 | JOIN project p ON (sr.project = p.id) | ||
| 243 | JOIN smoke_report_tag srt ON (srt.smoke_report = sr.id) | ||
| 244 | WHERE p.id = ? AND srt.tag = ?/; | ||
| 245 | push(@bind_vars, $tag); | ||
| 246 | } else { | ||
| 247 | $sql = q/SELECT sr.* FROM smoke_report sr | ||
| 248 | JOIN project p ON (sr.project = p.id) | ||
| 249 | WHERE p.id = ?/; | ||
| 250 | } | ||
| 251 | |||
| 252 | $sql .= " ORDER BY added $direction, sr.id DESC"; | ||
| 253 | $sql .= " LIMIT $offset, $limit " if ($limit); | ||
| 254 | |||
| 255 | my $sth = $self->db_Main->prepare_cached($sql); | ||
| 256 | $sth->execute(@bind_vars); | ||
| 257 | return Smolder::DB::SmokeReport->sth_to_objects($sth); | ||
| 258 | } | ||
| 259 | |||
| 260 | =head3 report_count | ||
| 261 | |||
| 262 | The number of reports associated with this Project. Can also provide an | ||
| 263 | optional tag to use as well | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub report_count { | ||
| 268 | my ($self, $tag) = @_; | ||
| 269 | my @bind_vars = ($self->id); | ||
| 270 | |||
| 271 | my $sql; | ||
| 272 | if ($tag) { | ||
| 273 | $sql = q/SELECT COUNT(*) FROM smoke_report sr | ||
| 274 | JOIN project p ON (sr.project = p.id) | ||
| 275 | JOIN smoke_report_tag srt ON (srt.smoke_report = sr.id) | ||
| 276 | WHERE p.id = ? AND srt.tag = ?/; | ||
| 277 | push(@bind_vars, $tag); | ||
| 278 | } else { | ||
| 279 | $sql = q/SELECT COUNT(*) FROM smoke_report sr | ||
| 280 | JOIN project p ON (sr.project = p.id) | ||
| 281 | WHERE p.id = ?/; | ||
| 282 | } | ||
| 283 | |||
| 284 | my $sth = $self->db_Main->prepare_cached($sql); | ||
| 285 | $sth->execute(@bind_vars); | ||
| 286 | my $row = $sth->fetchrow_arrayref(); | ||
| 287 | $sth->finish(); | ||
| 288 | return $row->[0]; | ||
| 289 | } | ||
| 290 | |||
| 291 | =head3 report_graph_data | ||
| 292 | |||
| 293 | Will return an array of arrays (based on the given fields) that | ||
| 294 | is suitable for feeding to GD::Graph. To limit the date range | ||
| 295 | used to build the data, you can also pass a 'start' and 'stop' | ||
| 296 | L<DateTime> parameter. | ||
| 297 | |||
| 298 | my $data = $project->report_graph_data( | ||
| 299 | fields => [qw(total pass fail)], | ||
| 300 | start => $start, | ||
| 301 | stop => DateTime->today(), | ||
| 302 | ); | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | sub report_graph_data { | ||
| 307 | my ($self, %args) = @_; | ||
| 308 | my $fields = $args{fields}; | ||
| 309 | my $start = $args{start}; | ||
| 310 | my $stop = $args{stop}; | ||
| 311 | my $tag = $args{tag}; | ||
| 312 | my @data; | ||
| 313 | my @bind_cols = ($self->id); | ||
| 314 | |||
| 315 | # we need the date before anything else | ||
| 316 | my $sql; | ||
| 317 | if ($tag) { | ||
| 318 | $sql = | ||
| 319 | "SELECT " | ||
| 320 | . join(', ', "added", @$fields) | ||
| 321 | . " FROM smoke_report sr" | ||
| 322 | . " JOIN smoke_report_tag srt ON (sr.id = srt.smoke_report)" | ||
| 323 | . " WHERE sr.project = ? AND sr.invalid = 0 AND srt.tag = ?"; | ||
| 324 | push(@bind_cols, $tag); | ||
| 325 | } else { | ||
| 326 | $sql = | ||
| 327 | "SELECT " | ||
| 328 | . join(', ', "added", @$fields) | ||
| 329 | . " FROM smoke_report sr" | ||
| 330 | . " WHERE sr.project = ? AND sr.invalid = 0 "; | ||
| 331 | } | ||
| 332 | |||
| 333 | # if we need to limit by date | ||
| 334 | if ($start) { | ||
| 335 | $sql .= " AND DATE(sr.added) >= ? "; | ||
| 336 | push(@bind_cols, $start->strftime('%Y-%m-%d')); | ||
| 337 | } | ||
| 338 | if ($stop) { | ||
| 339 | $sql .= " AND DATE(sr.added) <= ? "; | ||
| 340 | push(@bind_cols, $stop->strftime('%Y-%m-%d')); | ||
| 341 | } | ||
| 342 | |||
| 343 | # add optional args | ||
| 344 | foreach my $extra_param qw(architecture platform) { | ||
| 345 | if ($args{$extra_param}) { | ||
| 346 | $sql .= " AND sr.$extra_param = ? "; | ||
| 347 | push(@bind_cols, $args{$extra_param}); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | # add the ORDER BY | ||
| 352 | $sql .= " ORDER BY sr.added "; | ||
| 353 | |||
| 354 | my $sth = $self->db_Main->prepare_cached($sql); | ||
| 355 | $sth->execute(@bind_cols); | ||
| 356 | while (my $row = $sth->fetchrow_arrayref()) { | ||
| 357 | |||
| 358 | # reformat added - used to do this in SQL with DATE_FORMAT(), | ||
| 359 | # but SQLite don't play that game | ||
| 360 | my ($year, $month, $day) = $row->[0] =~ /(\d{4})-(\d{2})-(\d{2})/; | ||
| 361 | $row->[0] = "$month/$day/$year"; | ||
| 362 | |||
| 363 | for my $i (0 .. scalar(@$row) - 1) { | ||
| 364 | push(@{$data[$i]}, $row->[$i]); | ||
| 365 | } | ||
| 366 | } | ||
| 367 | return \@data; | ||
| 368 | } | ||
| 369 | |||
| 370 | =head3 platforms | ||
| 371 | |||
| 372 | Returns an arrayref of all the platforms that have been associated with | ||
| 373 | smoke tests uploaded for this project. | ||
| 374 | |||
| 375 | =cut | ||
| 376 | |||
| 377 | sub platforms { | ||
| 378 | my $self = shift; | ||
| 379 | my $sth = $self->db_Main->prepare_cached( | ||
| 380 | q( | ||
| 381 | SELECT DISTINCT platform FROM smoke_report | ||
| 382 | WHERE platform != '' AND project = ? ORDER BY platform | ||
| 383 | ) | ||
| 384 | ); | ||
| 385 | $sth->execute($self->id); | ||
| 386 | my @plats; | ||
| 387 | while (my $row = $sth->fetchrow_arrayref) { | ||
| 388 | push(@plats, $row->[0]); | ||
| 389 | } | ||
| 390 | return \@plats; | ||
| 391 | } | ||
| 392 | |||
| 393 | =head3 architectures | ||
| 394 | |||
| 395 | Returns a list of all the architectures that have been associated with | ||
| 396 | smoke tests uploaded for this project. | ||
| 397 | |||
| 398 | =cut | ||
| 399 | |||
| 400 | sub architectures { | ||
| 401 | my $self = shift; | ||
| 402 | my $sth = $self->db_Main->prepare_cached( | ||
| 403 | q( | ||
| 404 | SELECT DISTINCT architecture FROM smoke_report | ||
| 405 | WHERE architecture != '' AND project = ? ORDER BY architecture | ||
| 406 | ) | ||
| 407 | ); | ||
| 408 | $sth->execute($self->id); | ||
| 409 | my @archs; | ||
| 410 | while (my $row = $sth->fetchrow_arrayref) { | ||
| 411 | push(@archs, $row->[0]); | ||
| 412 | } | ||
| 413 | return \@archs; | ||
| 414 | } | ||
| 415 | |||
| 416 | =head3 tags | ||
| 417 | |||
| 418 | Returns a list of all of tags that have been added to smoke reports for | ||
| 419 | this project (in the smoke_report_tag table). | ||
| 420 | |||
| 421 | # returns a simple list of scalars | ||
| 422 | my @tags = $project->tags(); | ||
| 423 | |||
| 424 | # returns a hash of the tag value and count, ie { tag => 'foo', count => 20 } | ||
| 425 | my @tags = $project->tags(with_counts => 1); | ||
| 426 | |||
| 427 | =cut | ||
| 428 | |||
| 429 | sub tags { | ||
| 430 | my ($self, %args) = @_; | ||
| 431 | my @tags; | ||
| 432 | if ($args{with_counts}) { | ||
| 433 | my $sth = $self->db_Main->prepare_cached( | ||
| 434 | q/ | ||
| 435 | SELECT srt.tag, COUNT(*) FROM smoke_report_tag srt | ||
| 436 | JOIN smoke_report sr ON (sr.id = srt.smoke_report) | ||
| 437 | WHERE sr.project = ? GROUP BY srt.tag ORDER BY srt.tag/ | ||
| 438 | ); | ||
| 439 | $sth->execute($self->id); | ||
| 440 | while (my $row = $sth->fetchrow_arrayref()) { | ||
| 441 | push(@tags, {tag => $row->[0], count => $row->[1]}); | ||
| 442 | } | ||
| 443 | } else { | ||
| 444 | my $sth = $self->db_Main->prepare_cached( | ||
| 445 | q/ | ||
| 446 | SELECT DISTINCT(srt.tag) FROM smoke_report_tag srt | ||
| 447 | JOIN smoke_report sr ON (sr.id = srt.smoke_report) | ||
| 448 | WHERE sr.project = ? ORDER BY srt.tag/ | ||
| 449 | ); | ||
| 450 | $sth->execute($self->id); | ||
| 451 | while (my $row = $sth->fetchrow_arrayref()) { | ||
| 452 | push(@tags, $row->[0]); | ||
| 453 | } | ||
| 454 | } | ||
| 455 | return @tags; | ||
| 456 | |||
| 457 | } | ||
| 458 | |||
| 459 | =head3 delete_tag | ||
| 460 | |||
| 461 | Deletes a tag in the smoke_report_tag table for Smoke Reports associated with this Project. | ||
| 462 | |||
| 463 | $project->delete_tag("Something Old"); | ||
| 464 | |||
| 465 | =cut | ||
| 466 | |||
| 467 | sub delete_tag { | ||
| 468 | my ($self, $tag) = @_; | ||
| 469 | |||
| 470 | # because SQL doesn't support multi-table deletes (with a USING clause) | ||
| 471 | # we need to resort to doing this in 2 steps | ||
| 472 | my $sth = $self->db_Main->prepare_cached( | ||
| 473 | q/ | ||
| 474 | SELECT id FROM smoke_report_tag WHERE tag = ? | ||
| 475 | / | ||
| 476 | ); | ||
| 477 | $sth->execute($tag); | ||
| 478 | my $tag_ids = $sth->fetchall_arrayref([0]); | ||
| 479 | |||
| 480 | my $placeholders = join(', ', ('?') x scalar(@$tag_ids)); | ||
| 481 | $sth = $self->db_Main->prepare_cached( | ||
| 482 | qq/ | ||
| 483 | DELETE FROM smoke_report_tag WHERE id IN ($placeholders) | ||
| 484 | / | ||
| 485 | ); | ||
| 486 | $sth->execute(map { $_->[0] } @$tag_ids); | ||
| 487 | } | ||
| 488 | |||
| 489 | =head3 change_tag | ||
| 490 | |||
| 491 | This method will change a tag of project's smoke reports into some other tag | ||
| 492 | |||
| 493 | $project->change_tag('Something', 'Something Else'); | ||
| 494 | |||
| 495 | =cut | ||
| 496 | |||
| 497 | sub change_tag { | ||
| 498 | my ($self, $tag, $repl) = @_; | ||
| 499 | |||
| 500 | # because SQL doesn't support multi-table updates (with a USING clause) | ||
| 501 | # we need to resort to doing this in 2 steps | ||
| 502 | my $sth = $self->db_Main->prepare_cached('SELECT id FROM smoke_report_tag WHERE tag = ?'); | ||
| 503 | $sth->execute($tag); | ||
| 504 | my $tag_ids = $sth->fetchall_arrayref([0]); | ||
| 505 | |||
| 506 | my $placeholders = join(', ', ('?') x scalar(@$tag_ids)); | ||
| 507 | $sth = $self->db_Main->prepare_cached( | ||
| 508 | "UPDATE smoke_report_tag SET tag = ? WHERE id IN ($placeholders)"); | ||
| 509 | $sth->execute($repl, map { $_->[0] } @$tag_ids); | ||
| 510 | } | ||
| 511 | |||
| 512 | =head3 graph_start_datetime | ||
| 513 | |||
| 514 | Returns a L<DateTime> object that represents the real date for the value | ||
| 515 | stored in the 'graph_start' column. For example, if the current date | ||
| 516 | were March 17, 2006 and the project was started on Feb 20th, 2006 | ||
| 517 | then the following values would become the following dates: | ||
| 518 | |||
| 519 | project => Feb 20th, 2006 | ||
| 520 | year => Jan 1st, 2006 | ||
| 521 | month => Mar 1st, 2006 | ||
| 522 | week => Mar 13th, 2006 | ||
| 523 | day => Mar 17th, 2006 | ||
| 524 | |||
| 525 | =cut | ||
| 526 | |||
| 527 | sub graph_start_datetime { | ||
| 528 | my $self = shift; | ||
| 529 | my $dt; | ||
| 530 | |||
| 531 | # the project's start date | ||
| 532 | if ($self->graph_start eq 'project') { | ||
| 533 | $dt = $self->start_date; | ||
| 534 | |||
| 535 | # the first day of this year | ||
| 536 | } elsif ($self->graph_start eq 'year') { | ||
| 537 | $dt = DateTime->today()->set( | ||
| 538 | month => 1, | ||
| 539 | day => 1, | ||
| 540 | ); | ||
| 541 | |||
| 542 | # the first day of this month | ||
| 543 | } elsif ($self->graph_start eq 'month') { | ||
| 544 | $dt = DateTime->today()->set(day => 1); | ||
| 545 | |||
| 546 | # the first day of this week | ||
| 547 | } elsif ($self->graph_start eq 'week') { | ||
| 548 | $dt = DateTime->today; | ||
| 549 | my $day_diff = $dt->day_of_week - 1; | ||
| 550 | $dt->subtract(days => $day_diff) if ($day_diff); | ||
| 551 | |||
| 552 | # today | ||
| 553 | } elsif ($self->graph_start eq 'day') { | ||
| 554 | $dt = DateTime->today(); | ||
| 555 | } | ||
| 556 | return $dt; | ||
| 557 | } | ||
| 558 | |||
| 559 | =head3 purge_old_reports | ||
| 560 | |||
| 561 | This method will check to see if the C<max_reports> limit has been reached | ||
| 562 | for this project and delete the tap archive files associated with those | ||
| 563 | reports, also marking the reports as C<purged>. | ||
| 564 | |||
| 565 | =cut | ||
| 566 | |||
| 567 | sub purge_old_reports { | ||
| 568 | my $self = shift; | ||
| 569 | if ($self->max_reports) { | ||
| 570 | |||
| 571 | # Delete any non-purged reports that pass the above limit | ||
| 572 | my $sth = $self->db_Main->prepare_cached( | ||
| 573 | q( | ||
| 574 | SELECT id FROM smoke_report | ||
| 575 | WHERE project = ? AND purged = 0 | ||
| 576 | ORDER BY added DESC | ||
| 577 | LIMIT 1000000 OFFSET | ||
| 578 | ) . $self->max_reports | ||
| 579 | ); | ||
| 580 | $sth->execute($self->id); | ||
| 581 | my (@ids, $id); | ||
| 582 | $sth->bind_col(1, \$id); | ||
| 583 | push(@ids, $id) while ($sth->fetch); | ||
| 584 | $sth->finish(); | ||
| 585 | |||
| 586 | foreach my $id (@ids) { | ||
| 587 | my $report = Smolder::DB::SmokeReport->retrieve($id); | ||
| 588 | $report->delete_files(); | ||
| 589 | $report->purged(1); | ||
| 590 | $report->update(); | ||
| 591 | } | ||
| 592 | } | ||
| 593 | } | ||
| 594 | |||
| 595 | =head3 most_recent_report | ||
| 596 | |||
| 597 | Returns the most recent L<Smolder::DB::SmokeReport> object that was added. | ||
| 598 | |||
| 599 | =cut | ||
| 600 | |||
| 601 | sub most_recent_report { | ||
| 602 | my $self = shift; | ||
| 603 | my $sth = $self->db_Main->prepare_cached( | ||
| 604 | q/ | ||
| 605 | SELECT * FROM smoke_report | ||
| 606 | WHERE project = ? | ||
| 607 | ORDER BY added DESC | ||
| 608 | LIMIT 1 | ||
| 609 | / | ||
| 610 | ); | ||
| 611 | $sth->execute($self->id); | ||
| 612 | my ($report) = Smolder::DB::SmokeReport->sth_to_objects($sth); | ||
| 613 | return $report; | ||
| 614 | } | ||
| 615 | |||
| 616 | =head2 CLASS METHODS | ||
| 617 | |||
| 618 | =head3 all_names | ||
| 619 | |||
| 620 | Returns an array containing all the names of all existing projects. | ||
| 621 | Can receive an extra arg that is the id of a project who's name should | ||
| 622 | not be returned. | ||
| 623 | |||
| 624 | =cut | ||
| 625 | |||
| 626 | sub all_names { | ||
| 627 | my ($class, $id) = @_; | ||
| 628 | my $sql = "SELECT NAME FROM project"; | ||
| 629 | $sql .= " WHERE id != $id" if ($id); | ||
| 630 | my $sth = $class->db_Main->prepare_cached($sql); | ||
| 631 | $sth->execute(); | ||
| 632 | my @names; | ||
| 633 | while (my $row = $sth->fetchrow_arrayref()) { | ||
| 634 | push(@names, $row->[0]); | ||
| 635 | } | ||
| 636 | return @names; | ||
| 637 | } | ||
| 638 | |||
| 639 | 1; |