Changeset 1428


Ignore:
Timestamp:
10/29/15 15:00:00 (9 years ago)
Author:
nanardon
Message:

Add functions to fetch statistics

Location:
trunk/LATMOS-Accounts
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Stat.pm

    r1427 r1428  
    100100    } 
    101101 
    102     return %stats; 
     102    return \%stats; 
    103103} 
    104104 
     
    132132    } 
    133133 
    134     my %stats = $self->compute(); 
     134    my $stats = $self->compute(); 
    135135 
    136136    my $sthr = $self->db->prepare( 
     
    146146        } 
    147147    ); 
    148     foreach (keys %stats) { 
    149         $sthv->execute($id, $_, $stats{$_}); 
     148    foreach (keys %$stats) { 
     149        $sthv->execute($id, $_, $stats->{$_}); 
    150150    } 
    151151    $self->_set_c_fields('lastStatId' => $id) or do { 
     
    157157} 
    158158 
     159=head2 getStat($id) 
     160 
     161Return the statistics value for this stat entry. 
     162 
     163If not given, C<$id> will be the last entry found 
     164 
     165=cut 
     166 
     167sub getStat { 
     168    my ($self, $id) = @_; 
     169 
     170    ($id) ||= $self->_attributes('lastStatId'); 
     171 
     172    if (!$id) { 
     173        $self->base->log(LA_ERR, "No Stat Id given for stat name %s", $self->id); 
     174        return; 
     175    } 
     176 
     177    my $sthentry = $self->db->prepare('SELECT * from statsentry where okey = ? and id = ?'); 
     178    $sthentry->execute($self->Iid, $id); 
     179    if (!(my $res = $sthentry->fetchrow_hashref)) { 
     180        $self->base->log(LA_ERR, "No stat entry %d found for stat %s", $id, $self->id); 
     181        return; 
     182    } 
     183 
     184    my $sthvalues = $self->db->prepare("SELECT * FROM statvalues where sid = ?"); 
     185    $sthvalues->execute($id); 
     186 
     187    my $stats = {}; 
     188    while (my $res = $sthvalues->fetchrow_hashref) { 
     189        $stats->{$res->{value}} = $res->{count}; 
     190    } 
     191 
     192    $stats 
     193} 
     194 
     195sub getAllStat { 
     196    my ($self) = @_; 
     197 
     198    my $sth = $self->db->prepare(q{ 
     199        SELECT count, value, to_char(tstamp, 'YYYY-MM-DD') as d from statsentry join 
     200                      statvalues 
     201            on statsentry.id = statvalues.sid 
     202            where statsentry.okey = ? 
     203                order by statsentry.tstamp, value 
     204    }); 
     205    $sth->execute($self->Iid); 
     206 
     207    my $stats = {}; 
     208    while(my $res = $sth->fetchrow_hashref) { 
     209        $stats->{$res->{d}}{$res->{value}} = $res->{count}; 
     210    } 
     211 
     212    return $stats; 
     213} 
    159214 
    1602151; 
Note: See TracChangeset for help on using the changeset viewer.