#!/usr/bin/perl

use strict;
use warnings;
use LATMOS::Accounts::Maintenance;
use LATMOS::Accounts::Log;
use Getopt::Long;
use Pod::Usage;

=head1 NAME

    la-sql-runstat - Collect statistic data

=head1 SYNOPSIS

    la-expired-reminder [options] statname

=cut

GetOptions(
    'c|config=s' => \my $config,
    'b|base=s'   => \my $base,
    's|store'    => \my $store,
    'help'       => sub { pod2usage(0) },
) or pod2usage();

=head1 OPTIONS

=over 4

=item -c|--config configdir

Use this configuration directory instead of the default one.

=item -b|--base base

Use this specific base

=back

=cut

my $LA = LATMOS::Accounts->new($config, noacl => 1);
my $labase = $LA->base($base);
$labase && $labase->load or die "Cannot load base";
$labase->wexported(1);

my ($name) = @ARGV;

my $stat = $labase->get_object('stat', $name) or die "no stat name $name";

my $res;
if ($store) {
    $stat->collect and $labase->commit;
    $res = $stat->getStat;
} else {
    $res = $stat->compute;
}

foreach (sort { $res->{$b} <=> $res->{$a} } keys %$res) {
   print "$_: " . $res->{$_} . "\n";
} 
