Changeset 1462


Ignore:
Timestamp:
11/04/15 09:30:22 (9 years ago)
Author:
nanardon
Message:

Add avg/count aggregate function to stat, allow unset value to be count

Location:
trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql
Files:
2 edited

Legend:

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

    r1453 r1462  
    4444            lastStatId => { }, 
    4545            aggregateFunction => { 
    46                 can_values => sub { qw(sum) }, 
    47             } 
     46                can_values => sub { qw(sum avg count) }, 
     47            }, 
     48            nullValue => { }, 
    4849        } 
    4950    ) 
     
    6364    my $reffiltered = undef; 
    6465 
     66    my $op = $self->_get_attributes('aggregateFunction'); 
    6567    if (my $attr = $self->base->attribute($otype, $attribute)) { 
    6668        if (my $obj = $attr->reference) { 
     
    7274            } 
    7375        } 
    74         if ($self->_get_attributes('refall') && $attr->has_values_list) { 
     76        if ($self->_get_attributes('refall') && $attr->has_values_list && !$op) { 
    7577            foreach ($attr->can_values) { 
    7678                if ($reffiltered && !$reffiltered->{$_}) { 
     
    9395    my %results = $self->base->attributes_summary_by_object($otype, $attribute); 
    9496 
    95     my $op = $self->_get_attributes('aggregateFunction'); 
     97    my $nullValue = $self->_get_attributes('nullValue'); 
     98    my %aggdata; 
    9699    foreach my $id (keys %results) { 
    97100        $filtered{ $id } or next; 
    98101        foreach (@{ $results{ $id }}) { 
    99             if ($reffiltered && !$reffiltered->{$_}) { 
     102            next unless (defined($_) || $nullValue); 
     103            if ($_ && $reffiltered && !$reffiltered->{$_}) { 
    100104                next; 
    101105            } 
     106            $_ ||= '(none)'; 
    102107            if ($op) { 
    103                 if ($op eq 'sum') { 
    104                     $stats{$id} ||= 0; 
    105                     $stats{$id} += $_; 
    106                 } 
     108                push(@{$aggdata{$id}}, $_); 
    107109            } else { 
    108110                $stats{ $_ } ||= 0; 
    109111                $stats{ $_ }++; 
     112            } 
     113        } 
     114    } 
     115    if ($op) { 
     116        foreach my $key (keys %aggdata) { 
     117            if ($op eq 'sum') { 
     118                my $sum = 0; 
     119                $sum += $_ foreach (@{ $aggdata{$key} }); 
     120                $stats{ $key } = $sum; 
     121            } 
     122            elsif ($op eq 'count') { 
     123                $stats{ $key } = scalar(@{ $aggdata{$key} }); 
     124            } 
     125            elsif ($op eq 'average') { 
     126                my $sum = 0; 
     127                $sum += $_ foreach (@{ $aggdata{$key} }); 
     128                $stats{ $key } = $sum / scalar(@{ $aggdata{$key} }); 
    110129            } 
    111130        } 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/objects.pm

    r1457 r1462  
    634634            ) 
    635635            : sprintf( 
    636                 q{select name, value from %s join %s on %s.ikey = %s.okey where attr = ?} . ($base->{wexported} ? '' : ' and "exported" = true'), 
     636                q{select name, value from %s left join %s on %s.ikey = %s.okey and attr = ?} . ($base->{wexported} ? '' : ' and "exported" = true'), 
    637637                $base->db->quote_identifier($class->_object_table), 
    638638                $base->db->quote_identifier($class->_object_table . 
Note: See TracChangeset for help on using the changeset viewer.