package LATMOS::Accounts::Task::Stats;

use strict;
use warnings;
use base qw(LATMOS::Accounts::Task);
use LATMOS::Accounts;
use LATMOS::Accounts::Log;
use LATMOS::Accounts::Utils;

=head1 NAME

LATMOS::Accounts::Task::Stats - Store value to perform some statistics

=head1 DESCRIPTION

=cut

sub init {
    my ($self) = @_;
    my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1);
    my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base;
    $labase && $labase->load or die "Cannot load base";

    $self->{_la} = $LA;
    $self->{_base} = $labase;

    1;
}

sub order { 2 }

sub waitDelay { 5 * 60 }

sub needupd { 1; }

sub run {
    my ($self) = @_;

    $self->{_base}->temp_switch_unexported(
        sub {
            foreach my $stat ($self->{_base}->list_objects('stat')) {
                my $ostat = $self->{_base}->get_object('stat', $stat);
                # Don't process unexported stat objects
                $ostat->_get_attributes('unexported') and next;
                if ($ostat->collect) {
                    $self->{_base}->commit;
                } else {
                    $self->{_base}->rollback;
                }
            }
        },
        1
    );

    return 1;
}

1;

__END__

=head1 SEE ALSO

L<LATMOS::Accounts>, L<LATMOS::Accounts::Task>

=head1 AUTHOR

Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2012 CNRS SA/CETP/LATMOS

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.

=cut

