#!/usr/local/bin/perl
#
# cpan-heavyweights - ascii graph of top releasers on CPAN,
#                     optionally for a specific year
#
use 5.006;
use strict;
use warnings;
use CPAN::ReleaseHistory 0.03;
use Getopt::Long;

my $SHOW_TOP_N   = 10;
my $MAX_BAR_SIZE = 40;
my $BAR_CHAR     = '#';
my $iterator     = CPAN::ReleaseHistory->new()->release_iterator(well_formed => 1);
my $TARGET_YEAR;
my %counts;

GetOptions('top=i',     \$SHOW_TOP_N,
           'char|c=s',  \$BAR_CHAR,
           'width|w=i', \$MAX_BAR_SIZE,
           'year=i',    \$TARGET_YEAR,
          ) || die "usage: $0 [-top N] [-year YYYY] [-char <char>] [-width N]\n";

while (my $release = $iterator->next_release) {
    my $year = (gmtime($release->timestamp))[5] + 1900;
    next if defined($TARGET_YEAR) && $year != $TARGET_YEAR;
    $counts{ $release->distinfo->cpanid }++;
}
die "No releases seen in $TARGET_YEAR\n" if defined($TARGET_YEAR) && keys(%counts) == 0;

my $max         = (sort { $b <=> $a } values %counts)[0];
my $count_width = (sort { $b <=> $a } map { length($_) } values %counts)[0];
my $scale       = $max < $MAX_BAR_SIZE ? 1 : $max / $MAX_BAR_SIZE;
my @users       = (sort { $counts{$b} <=> $counts{$a} } keys %counts)[0..($SHOW_TOP_N-1)];
my $user_width  = (sort { $b <=> $a } map { length($_) } @users)[0];


foreach my $user (@users) {
    printf " %${user_width}s (%${count_width}d) %s\n",
           $user, $counts{$user}||0, $BAR_CHAR x (($counts{$user}||0) / $scale);
}

=head1 NAME

cpan-heavyweights - display top N releasers to CPAN, optionally for a specific year

=head1 SYNOPSIS

 cpan-heayweights [--year YYYY] [-top N] [--char =] [--width 40]

=head1 DESCRIPTION

B<cpan-heavyweights> generates a text graph of the top releasers to CPAN of all time.

 % cpan-heavyweights

      RJBS (2667) ########################################
 SHARYANTO (2519) #####################################
    BINGOS (2314) ##################################
     ADAMK (2312) ##################################
 TOKUHIROM (1871) ############################
  MIYAGAWA (1668) #########################
   DROLSKY (1650) ########################
  DAGOLDEN (1358) ####################
   TOBYINK (1348) ####################
  MLEHMANN (1244) ##################

You can limit the graph to a particular year, and also specify how many people you want to see:

 % cpan-heavyweights --year 2003 -top 8

  AUTRIJUS (229) ########################################
   DROLSKY (174) ##############################
      GENE (148) #########################
   SOFTDIA (115) ####################
     TBONE (110) ###################
    RCLAMP (107) ##################
      KAKE (107) ##################
 ELIZABETH (105) ##################

=head1 SEE ALSO

L<CPAN::ReleaseHistory> is used to get data for all CPAN releases.

=head1 REPOSITORY

L<https://github.com/neilbowers/CPAN-ReleaseHistory>

=head1 AUTHOR

Neil Bowers E<lt>neilb@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Neil Bowers <neilb@cpan.org>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

