| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use oEdtk::Main; | ||
| 7 | use oEdtk::Config qw(config_read); | ||
| 8 | use oEdtk::DBAdmin qw(db_connect); | ||
| 9 | use oEdtk::Outmngr 0.8011 qw(omgr_stats); | ||
| 10 | use Text::CSV; | ||
| 11 | |||
| 12 | if ( @ARGV < 1 or $ARGV[0] =~ /-h/i ) { | ||
| 13 | warn "Usage: $0 <day|week|all|value> <idlot|idgplot> [file]\n"; | ||
| 14 | warn "\n\t where 'value' has the format ywwd (one or more numbers like 2521 or 252)\n"; | ||
| 15 | exit 1; | ||
| 16 | } | ||
| 17 | |||
| 18 | my $cfg = config_read('EDTK_STATS'); | ||
| 19 | my $dbh = db_connect( $cfg, 'EDTK_DBI_STATS', { AutoCommit => 1, RaiseError => 1 } ); | ||
| 20 | my $pdbh = db_connect( $cfg, 'EDTK_DBI_STATS' ); | ||
| 21 | |||
| 22 | my $rows = omgr_stats( $dbh, $pdbh, $ARGV[0], $ARGV[1] || "idlot" ); | ||
| 23 | my $fmt = shift(@$rows); | ||
| 24 | my $head = shift(@$rows); | ||
| 25 | |||
| 26 | if ( !scalar @{$rows} ) { | ||
| 27 | warn "INFO: no statistics for this period.\n"; | ||
| 28 | exit; | ||
| 29 | } | ||
| 30 | |||
| 31 | #my @cols; | ||
| 32 | #if (defined($ARGV[1]) && $ARGV[1]!~/idlot/i) { | ||
| 33 | # @cols = ("LOT", "CORP", "PLIS", "DOCS", "FEUILLES", "PAGES", "FACES", "MODEDI "); | ||
| 34 | #} else { | ||
| 35 | # @cols = ("LOT", "CORP", "ID_LOT", "PLIS", "DOCS", "FEUILLES", "PAGES", "FACES", "FIL."); | ||
| 36 | #} | ||
| 37 | |||
| 38 | # If an output file was given on the command line, we dump the data in | ||
| 39 | # the given file in CSV format. | ||
| 40 | if ( defined( $ARGV[2] ) && length( $ARGV[2] ) > 0 ) { | ||
| 41 | open( my $fh, ">$ARGV[2]" ) or die "ERROR: can't open $ARGV[2] : $!"; | ||
| 42 | my $csv = Text::CSV->new( { binary => 1, eol => "\n" } ); | ||
| 43 | $csv->print( $fh, $head ); | ||
| 44 | foreach my $row (@$rows) { | ||
| 45 | $csv->print( $fh, $row ); | ||
| 46 | } | ||
| 47 | close($fh); | ||
| 48 | exit; | ||
| 49 | } | ||
| 50 | |||
| 51 | # Otherwise, we output in a human-readable way. | ||
| 52 | printf( $$fmt, @$head ); | ||
| 53 | foreach my $row (@$rows) { | ||
| 54 | printf( $$fmt, @$row ); | ||
| 55 | } |