| 1 | package Audio::DB::Reports; | ||
| 2 | # $Id: Reports.pm,v 1.2 2005/02/27 16:56:25 todd Exp $ | ||
| 3 | |||
| 4 | # THIS MODULE SHOULD REALLY ONLY HOLD COLLECTIVE METHODS THAT GENERATE REPORTS | ||
| 5 | |||
| 6 | use strict 'vars'; | ||
| 7 | use Carp 'croak','cluck'; | ||
| 8 | use CGI qw/:standard *table *TR -no_xhtml *center/; | ||
| 9 | use DBI; | ||
| 10 | use vars qw(@ISA $VERSION); | ||
| 11 | |||
| 12 | use Audio::DB; | ||
| 13 | use Audio::DB::Query; | ||
| 14 | use Audio::DB::Util::Warnings; | ||
| 15 | use Audio::DB::Util::DataAccess; | ||
| 16 | use Audio::DB::Util::Rearrange; | ||
| 17 | |||
| 18 | @ISA = qw/ | ||
| 19 | Audio::DB | ||
| 20 | Audio::DB::Query | ||
| 21 | Audio::DB::Util::DataAccess | ||
| 22 | Audio::DB::Util::Warnings/; | ||
| 23 | |||
| 24 | $VERSION = ''; | ||
| 25 | |||
| 26 | =pod | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Audio::DB::Reports - Generate quick reports of a Audio::DB database | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | use Audio::DB::Reports; | ||
| 35 | |||
| 36 | my $report = Audio::DB::Reports->new(-dsn => 'musicdb'); | ||
| 37 | |||
| 38 | my $albums = $report->album_list(-sort_by => 'artist'); | ||
| 39 | foreach my $album_obj (@$albums) { | ||
| 40 | my $album = $album_obj->album; | ||
| 41 | my $artist = $album_obj->artists; | ||
| 42 | my $year = $album_obj->years; | ||
| 43 | |||
| 44 | print join("\t",$album,$artist,$year),"\n"; | ||
| 45 | |||
| 46 | =head1 OVERVIEW | ||
| 47 | |||
| 48 | Audio::DB::Reports facilitates several different classes of reports. | ||
| 49 | These include full library reports, song, artist, album, genre and | ||
| 50 | bitrate based reports. In general, each report returns arrays of the | ||
| 51 | appropriate objects. | ||
| 52 | |||
| 53 | Associated accessor methods are provide full access to the underlying | ||
| 54 | data. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | ############################# | ||
| 59 | # ALBUM REPORTS | ||
| 60 | ############################# | ||
| 61 | |||
| 62 | =pod | ||
| 63 | |||
| 64 | =head2 Album Reports | ||
| 65 | |||
| 66 | Album reports aggregate songs into albums provide quick, decise views | ||
| 67 | of your collection. These include: | ||
| 68 | |||
| 69 | - listing all albums with associated artists and year released | ||
| 70 | - finding albums that fall below a given bitrate threshold | ||
| 71 | - finding albums with multiple bitrates | ||
| 72 | |||
| 73 | The following methods return a complex data structure. It is suggested | ||
| 74 | that you use provided methods to access the structure as it may change | ||
| 75 | in future releases. | ||
| 76 | |||
| 77 | =over 4 | ||
| 78 | |||
| 79 | =item $report->album_list(@options); | ||
| 80 | |||
| 81 | Generate a list of all albums. Returns a list of Audio::DB::Album | ||
| 82 | objects, each potentially containing Audio::DB::Song objects. | ||
| 83 | |||
| 84 | Options: | ||
| 85 | -sort_order A sort key. One of artist, album, or year | ||
| 86 | -include_songs Boolean true to return songs with the albums | ||
| 87 | |||
| 88 | The resulting list can be sorted by artist, album, or year released by | ||
| 89 | passing the sort_order option. In the event that multiple years or | ||
| 90 | artists are contained on the album, the placeholder "Various Artists" | ||
| 91 | or "Various Years" will be used as the search key. | ||
| 92 | |||
| 93 | In the interests of memory consumption, the album_list() method | ||
| 94 | returns only the album name, artist, year, and bitrates for each album | ||
| 95 | by default. If you also need access to the song list for each | ||
| 96 | individual album, pass a true value with the -include_songs option. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | # The internal data strcuture for album reports looks like this | ||
| 101 | # | ||
| 102 | # $self = [ { | ||
| 103 | # album => album name, | ||
| 104 | # album_id => album id, | ||
| 105 | # artists => [ all contributing artists ], | ||
| 106 | # songs => [ all contributing songs ], | ||
| 107 | # bitrates => [ bitrates seen for the album ], | ||
| 108 | # years => [ years seen for the album ], | ||
| 109 | # Also included in the hash ref but not necessarily accurate because of compilation CDs | ||
| 110 | # year => year released | ||
| 111 | # artist => primary artist | ||
| 112 | # } ] | ||
| 113 | # | ||
| 114 | |||
| 115 | |||
| 116 | sub album_list { | ||
| 117 | my ($self,@p) = @_; | ||
| 118 | my ($sort_by,$include) = rearrange([qw/SORT_BY INCLUDE/],@p); | ||
| 119 | my $albums = $self->fetch_class(-class=>'albums'); | ||
| 120 | my %include = map {$_ => 1 } @$include; | ||
| 121 | if (defined $include{songs}) { | ||
| 122 | foreach my $album (@$albums) { | ||
| 123 | my $songs = $self->fetch(-class=>'song',-query=>$album->album_id,-perspective=>'by_album_id'); | ||
| 124 | $album->add_songs($songs); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | if ($sort_by) { | ||
| 129 | my @sorted = _sort_albums($sort_by,$albums); | ||
| 130 | return \@sorted; | ||
| 131 | } | ||
| 132 | return $albums; | ||
| 133 | } | ||
| 134 | |||
| 135 | =pod | ||
| 136 | |||
| 137 | =item $report->albums_with_multiple_bitrates(@options) | ||
| 138 | |||
| 139 | Display all albums that have songs of different bitrates. Returns a | ||
| 140 | list of Audio::DB::Album objects, each potentially containing | ||
| 141 | Audio::DB::Song objects. | ||
| 142 | |||
| 143 | Options: | ||
| 144 | -sort_order A sort key. One of artist, album, or year | ||
| 145 | -include_songs Boolean true to return songs with the albums | ||
| 146 | |||
| 147 | Like album_list(), returned albums can be sorted by passing the | ||
| 148 | sort_order option, and can contain songs if passed the -include_songs | ||
| 149 | option. See album_list() for details. | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | sub albums_with_multiple_bitrates { | ||
| 154 | my ($self,@p) = @_; | ||
| 155 | my ($sort_by) = rearrange([qw/SORT_BY/],@p); | ||
| 156 | my $adaptor = $self->adaptor; | ||
| 157 | |||
| 158 | my $albums = $self->fetch_class(-class=>'album'); | ||
| 159 | foreach my $album (@$albums) { | ||
| 160 | my $songs = $self->fetch(-class=>'song',-query=>$album->album_id,-perspective=>'by_album_id'); | ||
| 161 | $album->add_songs($songs); | ||
| 162 | } | ||
| 163 | |||
| 164 | my @retain; | ||
| 165 | foreach (@$albums) { | ||
| 166 | my $bitrate = $_->bitrates; | ||
| 167 | next unless $bitrate eq 'various'; | ||
| 168 | push @retain,$_; | ||
| 169 | } | ||
| 170 | |||
| 171 | if ($sort_by) { | ||
| 172 | my @sorted = _sort_albums($sort_by,\@retain); | ||
| 173 | return \@sorted; | ||
| 174 | } | ||
| 175 | return \@retain; | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | =pod | ||
| 180 | |||
| 181 | =item $report->albums_below_bitrate_threshold(@options) | ||
| 182 | |||
| 183 | Display all albums that have ANY songs below a provided bitrate | ||
| 184 | threshold. Returns a list of Audio::DB::Album objects, each | ||
| 185 | potentially containing Audio::DB::Song objects. | ||
| 186 | |||
| 187 | Options: | ||
| 188 | -threshold The bitrate threshold | ||
| 189 | -sort_order A sort key. One of artist, album, or year | ||
| 190 | -include_songs Boolean true to return songs with the albums | ||
| 191 | |||
| 192 | Like album_list(), returned albums can be sorted by passing the | ||
| 193 | sort_order option, and can contain songs if passed the -include_songs | ||
| 194 | option. See album_list() for details. | ||
| 195 | |||
| 196 | =cut | ||
| 197 | |||
| 198 | |||
| 199 | # ATTEMPTING TO OPTIMIZE... | ||
| 200 | #sub albums_below_bitrate_threshold { | ||
| 201 | # my ($self,@p) = @_; | ||
| 202 | # my ($threshold,$sort_by,$include_songs) = rearrange([qw/THRESHOLD SORT_BY INCLUDE_SONGS/],@p); | ||
| 203 | # my $adaptor = $self->adaptor; | ||
| 204 | # my $sth = $adaptor->prepare('albums_below_bitrate_threshold'); | ||
| 205 | # $sth->execute($threshold); | ||
| 206 | # my @return; | ||
| 207 | # while (my $h = $sth->fetchrow_hashref) { | ||
| 208 | # # Create new album objects for each | ||
| 209 | # my $obj = Audio::DB::DataTypes::Album->new(-adaptor=>$adaptor,-data=>$h); | ||
| 210 | # my $id = $obj->album_id; | ||
| 211 | # # Need to touch songs to get artists and bitrates (could use seperate queries, too). | ||
| 212 | # my $songs = $self->fetch_songs(-album=>$id); | ||
| 213 | # @{$obj->{songs}} = @$songs if ($include_songs); # Stuff in the songs, too, if requested | ||
| 214 | # my %seen; | ||
| 215 | # push(@{$obj->{artists}},grep { !$seen{$_}++} map { $_->artist } @$songs); | ||
| 216 | # push(@{$obj->{artists}},grep { !$seen{$_}++} map { $_->bitrate } @$songs); | ||
| 217 | # push @return,$obj; | ||
| 218 | # } | ||
| 219 | # return \@return; | ||
| 220 | #} | ||
| 221 | |||
| 222 | |||
| 223 | sub albums_below_bitrate_threshold { | ||
| 224 | my ($self,@p) = @_; | ||
| 225 | my ($threshold,$sort_by,$include) = rearrange([qw/THRESHOLD SORT_BY INCLUDE/],@p); | ||
| 226 | $include = [qw/songs/] unless @$include; # We need to add in the songs in order to fetch all bitrates | ||
| 227 | my $albums = $self->fetch_class(-class=>'albums'); | ||
| 228 | my %include = map {$_ => 1 } @$include; | ||
| 229 | if (defined $include{songs}) { | ||
| 230 | foreach my $album (@$albums) { | ||
| 231 | my $songs = $self->fetch(-class=>'song',-query=>$album->album_id,-perspective=>'by_album_id'); | ||
| 232 | $album->add_songs($songs); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | my @retain; | ||
| 237 | foreach (@$albums) { | ||
| 238 | my $bitrate = $_->bitrates; | ||
| 239 | unless ($bitrate eq 'various') { | ||
| 240 | next if ($bitrate >= $threshold); | ||
| 241 | } | ||
| 242 | push @retain,$_; | ||
| 243 | } | ||
| 244 | |||
| 245 | if ($sort_by) { | ||
| 246 | my @sorted = _sort_albums($sort_by,\@retain); | ||
| 247 | return \@sorted; | ||
| 248 | } | ||
| 249 | return \@retain; | ||
| 250 | } | ||
| 251 | |||
| 252 | # Internal album sorting method (specific since I need to call methods that do not correspond to column names) | ||
| 253 | sub _sort_albums { | ||
| 254 | my ($sort_by,$albums) = @_; | ||
| 255 | my @sorted; | ||
| 256 | if ($sort_by eq 'artist') { | ||
| 257 | @sorted = sort { $a->artists cmp $b->artists } @$albums; | ||
| 258 | } elsif ($sort_by eq 'year') { | ||
| 259 | @sorted = sort {$a->years <=> $b->years } @$albums; | ||
| 260 | } elsif ($sort_by eq 'album') { | ||
| 261 | @sorted = sort {$a->album cmp $b->album } @$albums; | ||
| 262 | } | ||
| 263 | return @sorted; | ||
| 264 | } | ||
| 265 | |||
| 266 | ############################# | ||
| 267 | # ARTIST REPORTS | ||
| 268 | ############################# | ||
| 269 | |||
| 270 | =pod | ||
| 271 | |||
| 272 | =head2 Artist Reports | ||
| 273 | |||
| 274 | =over 4 | ||
| 275 | |||
| 276 | =item $report->artists_with_multi_genres(@options); | ||
| 277 | |||
| 278 | Generate a list of all artists with multiple genres associated to | ||
| 279 | them. Returns an array of Audio::DB::DataTypes::Artist objects, each of which | ||
| 280 | contains multiple Audio::DB::DataTypes::Genre objects. See the example | ||
| 281 | script eg/artists_with_multiple_genres for a demonstration on | ||
| 282 | accessing this data structure using provided methods. | ||
| 283 | |||
| 284 | =back | ||
| 285 | |||
| 286 | =cut | ||
| 287 | |||
| 288 | sub artists_with_multiple_genres { | ||
| 289 | my ($self,@p) = @_; | ||
| 290 | my $adaptor = $self->adaptor; | ||
| 291 | my $artists = $self->fetch(-class=>'artist',-perspective=>'artists_multiple_genres'); | ||
| 292 | foreach my $artist (@$artists) { | ||
| 293 | # Fetch the genres associated with each artist... | ||
| 294 | my $genres = $self->fetch(-class=>'genre',-perspective=>'by_artist_id',-query=>$artist->artist_id); | ||
| 295 | @{$artist->{genres}} = @$genres; | ||
| 296 | } | ||
| 297 | return $artists; | ||
| 298 | } | ||
| 299 | |||
| 300 | ############################# | ||
| 301 | # SONG REPORTS | ||
| 302 | ############################# | ||
| 303 | |||
| 304 | =pod | ||
| 305 | |||
| 306 | =head1 Song Reports | ||
| 307 | |||
| 308 | The following methods provide several song-level reports. | ||
| 309 | |||
| 310 | =over 4 | ||
| 311 | |||
| 312 | =item $report->song_list(@options) | ||
| 313 | |||
| 314 | Generate a list of all songs. Returns a list of | ||
| 315 | Audio::DB::DataTypes::Song objects. This is not particularly useful | ||
| 316 | for large collections and may consume a lot of memory. | ||
| 317 | |||
| 318 | Options: | ||
| 319 | -sort_order A sort key. One of artist, album, or year | ||
| 320 | |||
| 321 | The resulting list can be sorted by any of the available fields in the | ||
| 322 | song table. | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | sub song_list { | ||
| 327 | my ($self,@p) = @_; | ||
| 328 | my ($sort_by) = rearrange([qw/SORT_BY/],@p); | ||
| 329 | my $songs = $self->fetch_class(-class=>'songs'); | ||
| 330 | if ($sort_by) { | ||
| 331 | my @sorted = _generic_sort($sort_by,$songs); | ||
| 332 | return \@sorted; | ||
| 333 | } | ||
| 334 | return $songs; | ||
| 335 | } | ||
| 336 | |||
| 337 | # I NEED SOME WAY OF DISTINGUISHING THINGS THAT SHOULD BE | ||
| 338 | # NUMERIC SORT FROM ALPHABETIC SORT | ||
| 339 | sub _genric_sort { | ||
| 340 | my ($sort_by,$data) = @_; | ||
| 341 | my @sorted = sort { $a->$sort_by cmp $b->$sort_by } @$data; | ||
| 342 | return @sorted; | ||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | ############################# | ||
| 347 | # GENRE REPORTS | ||
| 348 | ############################# | ||
| 349 | # How many songs/albums/artists of each genre? | ||
| 350 | sub genre_report { | ||
| 351 | my ($self,@p) = @_; | ||
| 352 | my ($sort_by,$include) = rearrange([qw/SORT_BY INCLUDE/],@p); | ||
| 353 | my $sth; | ||
| 354 | |||
| 355 | my $genres = $self->fetch(-class=>'genre',-perspective=>'all_genres'); | ||
| 356 | my @return; | ||
| 357 | foreach my $genre (@$genres) { | ||
| 358 | # How many songs? | ||
| 359 | my $songs = $self->fetch(-class=>'song', -perspective=>'by_genre_id',-query=>$genre->genre_id); | ||
| 360 | my $albums = $self->fetch(-class=>'album', -perspective=>'by_genre_id',-query=>$genre->genre_id); | ||
| 361 | my $artists = $self->fetch(-class=>'artist',-perspective=>'by_genre_id',-query=>$genre->genre_id); | ||
| 362 | push (@{$genre->{stats}},scalar @$songs,scalar @$albums,scalar @$artists); | ||
| 363 | if ($include) { | ||
| 364 | @{$genre->{songs}} = @$songs; | ||
| 365 | @{$genre->{albums}} = @$albums; | ||
| 366 | @{$genre->{artists}} = @$artists; | ||
| 367 | } | ||
| 368 | push @return,$genre; | ||
| 369 | } | ||
| 370 | return wantarray ? @return : \@return; | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | |||
| 375 | ############################# | ||
| 376 | # GENERIC STATISTICS | ||
| 377 | ############################# | ||
| 378 | |||
| 379 | =pod | ||
| 380 | |||
| 381 | =item $report->distribution(@options) | ||
| 382 | |||
| 383 | Create an histogram showing distribution of albums or songs by | ||
| 384 | year. Requires that the GD module be installed. | ||
| 385 | |||
| 386 | Options: | ||
| 387 | -class one of albums or songs | ||
| 388 | -width desired width of the image in pixels | ||
| 389 | -height desired height of the image in pixels | ||
| 390 | -range array of year range to display (ie [1950..2004]). | ||
| 391 | If not provided, all years in the collection will be used | ||
| 392 | -background background color for the image as an [r g b] array reference | ||
| 393 | -foreground foreground color for the boxes as an [r g b] array reference | ||
| 394 | -omit_totals if provided, yearly totals will be omitted | ||
| 395 | |||
| 396 | =cut | ||
| 397 | |||
| 398 | sub distribution { | ||
| 399 | my ($self,@p) = @_; | ||
| 400 | my ($class,$width,$height,$range,$background,$foreground,$omit_totals) | ||
| 401 | = rearrange([qw/CLASS WIDTH HEIGHT RANGE BACKGROUND FOREGROUND OMIT_TOTALS/],@p); | ||
| 402 | |||
| 403 | eval { use GD; };# or die "You need to install the GD.pm module in order to use the album_distibution() method"; | ||
| 404 | |||
| 405 | # Set up some suitable defaults | ||
| 406 | use constant DEBUG => 0; | ||
| 407 | $width ||= 600; | ||
| 408 | $height ||= 550; | ||
| 409 | |||
| 410 | # Padding around all sides of the image | ||
| 411 | my $PADRIGHT = 20; | ||
| 412 | my $PADLEFT = 50; | ||
| 413 | my $PADTOP = 10; | ||
| 414 | my $PADBOTTOM = 50; | ||
| 415 | |||
| 416 | my $adaptor = $self->adaptor; | ||
| 417 | my $sth = ($class eq 'songs') | ||
| 418 | ? $adaptor->song_queries('songs_per_year') : $adaptor->album_queries('albums_per_year'); | ||
| 419 | |||
| 420 | # Calculate the totals for each year | ||
| 421 | my %years_seen; | ||
| 422 | foreach (@$range) { | ||
| 423 | $sth->execute($_); | ||
| 424 | my ($result) = $sth->fetchrow_array; | ||
| 425 | $years_seen{$_} = $result; | ||
| 426 | } | ||
| 427 | |||
| 428 | # Find the year which contains the most albums to dynamically set the y-scale | ||
| 429 | my @sorted_years = sort { $years_seen{$a} <=> $years_seen{$b} } @$range; | ||
| 430 | my $biggest_year = $years_seen{$sorted_years[-1]}; | ||
| 431 | my $yscale = ($height - ($PADTOP + $PADBOTTOM) - 30) / $biggest_year; | ||
| 432 | |||
| 433 | print STDERR "y-scale: $yscale\n" if DEBUG; | ||
| 434 | print STDERR "biggest year: $sorted_years[-1], $biggest_year\n" if DEBUG; | ||
| 435 | |||
| 436 | my $im = new GD::Image($width,$height); | ||
| 437 | |||
| 438 | push(@$background,255,255,0) unless @$background; | ||
| 439 | my ($bred,$bgreen,$bblue) = @{$background}; | ||
| 440 | my $bg = $im->colorAllocate($bred,$bgreen,$bblue); | ||
| 441 | |||
| 442 | push(@$foreground,0,255,255) unless @$foreground; | ||
| 443 | my ($fred,$fgreen,$fblue) = @$foreground; | ||
| 444 | my $fg = $im->colorAllocate($fred,$fgreen,$fblue); | ||
| 445 | |||
| 446 | my $black = $im->colorAllocate(0,0,0); | ||
| 447 | my $blue = $im->colorAllocate(0,0,255); | ||
| 448 | |||
| 449 | $im->transparent($bg); | ||
| 450 | $im->rectangle(0 + $PADLEFT,0 + $PADTOP, | ||
| 451 | $width - $PADRIGHT,$height - $PADBOTTOM,$black); | ||
| 452 | |||
| 453 | # Set up the axis, grid, and labels | ||
| 454 | # Create a suitable scale of y-axis tick marks | ||
| 455 | # if drawing 10 up the axis | ||
| 456 | my $division = int($biggest_year/10); | ||
| 457 | my $total = $division * 10; | ||
| 458 | for (my $i = 0;$i<=9;$i++) { | ||
| 459 | my $y1 = ($division * $i) * $yscale + $PADBOTTOM; | ||
| 460 | print STDERR $division . '-' . $y1,"\n" if DEBUG; | ||
| 461 | $im->line(0+$PADLEFT-5,$y1,0 + $PADLEFT,$y1,$black); | ||
| 462 | $im->line(0+$PADLEFT,$y1,$width - $PADRIGHT,$y1,$blue); | ||
| 463 | $im->string(gdTinyFont,$PADLEFT-20,$y1-(gdTinyFont->height/2),($total - ($division*$i)),$black); | ||
| 464 | } | ||
| 465 | |||
| 466 | $im->stringUp(gdSmallFont,0+5,((($height - $PADTOP - $PADBOTTOM)/2) + (gdSmallFont->height * 1)/2), | ||
| 467 | 'TOTAL ' . uc($class),$black); | ||
| 468 | $im->string(gdSmallFont, | ||
| 469 | (($width - $PADLEFT - $PADRIGHT)/2) + ((gdSmallFont->width * 5)/2),$height-20,'YEARS',$black); | ||
| 470 | |||
| 471 | # Now I need to iterate through the range drawing boxes | ||
| 472 | # Dynamically set the box width based on the number of years seen | ||
| 473 | my $boxwidth = ($width - ($PADLEFT + $PADRIGHT)) / scalar (@$range); | ||
| 474 | my $xoffset; | ||
| 475 | foreach my $year (@$range) { | ||
| 476 | my $total = $years_seen{$year} || 0; | ||
| 477 | |||
| 478 | # normalize the yoffset to the biggest column | ||
| 479 | my $y1 = int(($height - $PADBOTTOM) - ($total * $yscale)); | ||
| 480 | my $x1 = int($PADLEFT + $xoffset); | ||
| 481 | |||
| 482 | my $x2 = int($x1 + $boxwidth); | ||
| 483 | my $y2 = int($height - $PADBOTTOM); | ||
| 484 | $im->filledRectangle($x1,$y1,$x2,$y2,$fg); | ||
| 485 | $im->rectangle($x1,$y1,$x2,$y2,$black); | ||
| 486 | # $im->stringUp(gdTinyFont,$x1-2,$y2+25,$year,$black); | ||
| 487 | |||
| 488 | # Add the year and totals | ||
| 489 | $im->stringUp(gdTinyFont,$x1 + ((gdTinyFont->width * 1)/2),$y2+25,$year,$black); | ||
| 490 | unless ($omit_totals) { | ||
| 491 | $im->stringUp(gdTinyFont,$x1 + ((gdTinyFont->width * 1)/2),$y1-5,$total,$black); | ||
| 492 | } | ||
| 493 | $xoffset += $boxwidth; | ||
| 494 | print STDERR join("\t",$year,$total,$x1,$y1,$x2,$y2),"\n" if DEBUG | ||
| 495 | } | ||
| 496 | |||
| 497 | my $string = $im->png; | ||
| 498 | return $string; | ||
| 499 | } | ||
| 500 | |||
| 501 | |||
| 502 | =pod | ||
| 503 | |||
| 504 | =item $report->library_size() | ||
| 505 | |||
| 506 | Calculate the full size of your library. Returns a hash reference | ||
| 507 | containing the size in kilobytes (Kb), megabytes (MB), and the size in | ||
| 508 | gigabytes (GB). | ||
| 509 | |||
| 510 | =cut | ||
| 511 | |||
| 512 | sub library_size { | ||
| 513 | my $self = shift; | ||
| 514 | my $adaptor = $self->adaptor; | ||
| 515 | my $size = $adaptor->query_for_total('filesize'); | ||
| 516 | my %stats; | ||
| 517 | $stats{MB} = sprintf ('%.3f',($size / (1024 * 1024))); | ||
| 518 | $stats{GB} = sprintf ('%.3f',($size / (1024 * 1024 * 1024))); | ||
| 519 | $stats{Kb} = $size; | ||
| 520 | return \%stats; | ||
| 521 | } | ||
| 522 | |||
| 523 | =pod | ||
| 524 | |||
| 525 | =item $report->library_duration() | ||
| 526 | |||
| 527 | Calculate the full duration of your library. Returns a hash reference | ||
| 528 | containing the breakdown of the total duration using the hash keys | ||
| 529 | days, minutes, hours, and seconds. For convenience, the total time is | ||
| 530 | also returned as a formatted string of DD::HH::MM::SS under the hash | ||
| 531 | key "total_time". | ||
| 532 | |||
| 533 | =cut | ||
| 534 | |||
| 535 | sub library_duration { | ||
| 536 | my $self = shift; | ||
| 537 | my $adaptor = $self->adaptor; | ||
| 538 | my $milliseconds = $adaptor->query_for_total('seconds'); | ||
| 539 | my $seconds = int($milliseconds / 1000); | ||
| 540 | my $days = _save_remainder($seconds,60 * 60 * 24); | ||
| 541 | my $hours = _save_remainder($seconds,60 * 60); | ||
| 542 | my $min = _save_remainder($seconds,60); | ||
| 543 | my %stats; | ||
| 544 | $stats{days} = $days; | ||
| 545 | $stats{minutes} = $min; | ||
| 546 | $stats{hours} = $hours; | ||
| 547 | $stats{seconds} = int $seconds; | ||
| 548 | $stats{total_time} = join(':',$days,$hours,$min,$seconds); | ||
| 549 | return \%stats; | ||
| 550 | } | ||
| 551 | |||
| 552 | sub _save_remainder { | ||
| 553 | my ($val) = int($_[0] / $_[1]); | ||
| 554 | $_[0] -= $val * $_[1]; | ||
| 555 | return $val; | ||
| 556 | } | ||
| 557 | |||
| 558 | |||
| 559 | =pod | ||
| 560 | |||
| 561 | =item $report->counts() | ||
| 562 | |||
| 563 | Generate simple counts of the primary classes in the library. Returns | ||
| 564 | a hash reference with the following keys: songs, artists, albums, and | ||
| 565 | genres. | ||
| 566 | |||
| 567 | =cut | ||
| 568 | |||
| 569 | sub counts { | ||
| 570 | my $self = shift; | ||
| 571 | my %stats; | ||
| 572 | $stats{songs} = $self->count('songs'); | ||
| 573 | $stats{artists} = $self->count('artists'); | ||
| 574 | $stats{albums} = $self->count('albums'); | ||
| 575 | $stats{genres} = $self->count('genres'); | ||
| 576 | return \%stats; | ||
| 577 | } | ||
| 578 | |||
| 579 | |||
| 580 | |||
| 581 | 1; |