source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Stat.pm @ 1427

Last change on this file since 1427 was 1427, checked in by nanardon, 9 years ago

Add a statistics collecting functions

File size: 4.5 KB
Line 
1package LATMOS::Accounts::Bases::Sql::Stat;
2
3use 5.010000;
4use strict;
5use warnings;
6use LATMOS::Accounts::Log;
7
8use base qw(LATMOS::Accounts::Bases::Sql::objects);
9
10our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
11
12=head1 NAME
13
14LATMOS::Accounts::Bases::Sql::Site - Common Location/Address object
15
16=head1 DESCRIPTION
17
18Store common to many people office address (typically building location).
19
20=cut
21
22sub _object_table { 'stat' }
23
24sub _key_field { 'name' }
25
26sub _has_extended_attributes { 1 }
27
28sub _get_attr_schema {
29    my ($class, $base) = @_;
30
31    $class->SUPER::_get_attr_schema($base,
32        {
33            name   => { ro => 1, inline => 1, },
34            cn     => { ro => 1, inline => 1, iname => 'name' },
35            date   => { ro => 1, inline => 1, },
36            create => { ro => 1, inline => 1, },
37            filter => { multiple => 1 },
38            refFilter => { multiple => 1 },
39            refall => { },
40            otype  => { },
41            attribute => { },
42            description => { },
43            delay => { },
44            lastStatId => { },
45        }
46    )
47}
48
49=head2 compute()
50
51=cut
52
53sub compute {
54    my ($self) = @_;
55
56    my $otype = $self->_get_attributes('otype') or return;
57    my $attribute = $self->_get_attributes('attribute') or return;
58
59    my %stats;
60    my $reffiltered = undef;
61
62    if (my $attr = $self->base->attribute($otype, $attribute)) {
63        if (my $obj = $attr->reference) {
64            if (my @reffilter = $self->_get_attributes('refFilter')) {
65                $reffiltered = {};
66                foreach ($self->base->search_objects($obj, @reffilter)) {
67                    $reffiltered->{$_} = 1;
68                }
69            }
70        }
71        if ($self->_get_attributes('refall') && $attr->has_values_list) {
72            foreach ($attr->can_values) {
73                if ($reffiltered && !$reffiltered->{$_}) {
74                    next;
75                }
76                $stats{$_} = 0;
77            } 
78        }
79    } else {
80        $self->base->log(LA_ERR, "Cannot find attribute %s for object type %s", $attribute, $otype);
81    }
82
83    my %filtered;
84    if (my @fil = $self->_get_attributes('filter')) {
85        %filtered = map { $_ => 1 } $self->base->search_objects($otype, @fil);
86    } else {
87        %filtered = map { $_ => 1 } $self->base->list_objects($otype);
88    }
89
90    my %results = $self->base->attributes_summary_by_object($otype, $attribute);
91    foreach my $id (keys %results) {
92        $filtered{ $id } or next;
93        foreach (@{ $results{ $id }}) {
94            if ($reffiltered && !$reffiltered->{$_}) {
95                next;
96            }
97            $stats{ $_ } ||= 0;
98            $stats{ $_ }++;
99        }
100    }
101
102    return %stats;
103}
104
105=head2 collect()
106
107Store statistics for $attribute on $otype. @filters allow to limit result on
108$otype object matching the filters
109
110=cut
111
112sub collect {
113    my ($self) = @_;
114
115    my $otype = $self->_get_attributes('otype') or return;
116    my $attribute = $self->_get_attributes('attribute') or return;
117
118    if (my $days = $self->_get_attributes('delay')) {
119        my $sth = $self->db->prepare(
120            q{
121            select tstamp from statsentry where okey = ?
122                and tstamp > now() - (? * '1 day'::interval)
123                order by tstamp desc
124                limit 1
125            }
126        );
127        $sth->execute($self->Iid, $days);
128        if (my $res = $sth->fetchrow_hashref) {
129            $self->base->log(LA_DEBUG, "Stat %s/%s done recently: %s", $self->id, $otype, $res->{tstamp});
130            return 1;
131        }
132    }
133
134    my %stats = $self->compute();
135
136    my $sthr = $self->db->prepare(
137        q{ INSERT INTO statsentry (okey) values (?) }
138    );
139    $sthr->execute($self->Iid);
140
141    my $id = $self->db->last_insert_id(undef,undef,'statsentry',undef);
142
143    my $sthv =  $self->db->prepare(
144        q{
145        INSERT INTO statvalues (sid, value, count) values (?,?,?)
146        }
147    );
148    foreach (keys %stats) {
149        $sthv->execute($id, $_, $stats{$_});
150    }
151    $self->_set_c_fields('lastStatId' => $id) or do {
152        $self->base->log(LA_ERR, "Cannot update Stat object");
153        return;
154    };
155
156    return 1;
157}
158
159
1601;
161
162__END__
163
164=head1 SEE ALSO
165
166L<LATMOS::Accounts::Bases::Sql>
167
168=head1 AUTHOR
169
170Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
171
172=head1 COPYRIGHT AND LICENSE
173
174Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
175
176This library is free software; you can redistribute it and/or modify
177it under the same terms as Perl itself, either Perl version 5.10.0 or,
178at your option, any later version of Perl 5 you may have available.
179
180
181=cut
Note: See TracBrowser for help on using the repository browser.