1 | package LATMOS::Accounts::Bases::Sql::Stat; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | use LATMOS::Accounts::Log; |
---|
7 | |
---|
8 | use base qw(LATMOS::Accounts::Bases::Sql::objects); |
---|
9 | |
---|
10 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
11 | |
---|
12 | =head1 NAME |
---|
13 | |
---|
14 | LATMOS::Accounts::Bases::Sql::Site - Common Location/Address object |
---|
15 | |
---|
16 | =head1 DESCRIPTION |
---|
17 | |
---|
18 | Store common to many people office address (typically building location). |
---|
19 | |
---|
20 | =cut |
---|
21 | |
---|
22 | sub _object_table { 'stat' } |
---|
23 | |
---|
24 | sub _key_field { 'name' } |
---|
25 | |
---|
26 | sub _has_extended_attributes { 1 } |
---|
27 | |
---|
28 | sub _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 | |
---|
53 | sub 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 | |
---|
107 | Store statistics for $attribute on $otype. @filters allow to limit result on |
---|
108 | $otype object matching the filters |
---|
109 | |
---|
110 | =cut |
---|
111 | |
---|
112 | sub 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 | =head2 getStat($id) |
---|
160 | |
---|
161 | Return the statistics value for this stat entry. |
---|
162 | |
---|
163 | If not given, C<$id> will be the last entry found |
---|
164 | |
---|
165 | =cut |
---|
166 | |
---|
167 | sub 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 | |
---|
195 | sub 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 | } |
---|
214 | |
---|
215 | 1; |
---|
216 | |
---|
217 | __END__ |
---|
218 | |
---|
219 | =head1 SEE ALSO |
---|
220 | |
---|
221 | L<LATMOS::Accounts::Bases::Sql> |
---|
222 | |
---|
223 | =head1 AUTHOR |
---|
224 | |
---|
225 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
226 | |
---|
227 | =head1 COPYRIGHT AND LICENSE |
---|
228 | |
---|
229 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
230 | |
---|
231 | This library is free software; you can redistribute it and/or modify |
---|
232 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
233 | at your option, any later version of Perl 5 you may have available. |
---|
234 | |
---|
235 | |
---|
236 | =cut |
---|