import torrus 1.0.9
[freeside.git] / torrus / perllib / Torrus / SQL / Reports.pm
1 #  Copyright (C) 2005  Stanislav Sinyagin
2 #
3 #  This program is free software; you can redistribute it and/or modify
4 #  it under the terms of the GNU General Public License as published by
5 #  the Free Software Foundation; either version 2 of the License, or
6 #  (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software
15 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16
17 # $Id: Reports.pm,v 1.1 2010-12-27 00:03:59 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
19
20 # Class for Reporter data manipulation
21 package Torrus::SQL::ReportFields;
22
23 package Torrus::SQL::Reports;
24
25 use strict;
26
27 use Torrus::SQL;
28 use base 'Torrus::SQL';
29
30 use Torrus::Log;
31 # use Torrus::SQL::ReportFields;
32
33 # The name of the table and columns 
34 # defaults configured in torrus-config.pl
35 our $tableName;
36 our %columns;
37
38
39 sub new
40 {
41     my $class = shift;
42     my $subtype = shift;
43
44     my $self  = $class->SUPER::new( $subtype );
45
46     $self->{'fields'} = Torrus::SQL::ReportFields->new( $subtype );
47     
48     bless ($self, $class);
49     return $self;
50 }
51     
52
53 # Find or create a new row in reports table
54
55 sub reportId
56 {
57     my $self = shift;
58     my $repdate = shift;
59     my $reptime = shift;
60     my $repname = shift;
61
62     my $result = $self->{'sql'}->select_one_to_arrayref({
63         'fields' => [ $columns{'id'}, $columns{'iscomplete'} ],
64         'table' => $tableName,
65         'where' => { $columns{'rep_date'}   => $repdate,
66                      $columns{'rep_time'}   => $reptime,
67                      $columns{'reportname'} => $repname } });
68     
69     if( defined( $result ) )
70     {
71         if( not $result->[1] ) 
72         {
73             # iscomplete is zero - the report is unfinished
74             Warn('Found unfinished report ' . $repname . ' for ' .
75                  $repdate . ' ' . $reptime .
76                  '. Deleting the previous report data');
77             $self->{'fields'}->removeAll( $result->[0] );
78         }
79             
80         return $result->[0];
81     }
82     else
83     {
84         my $id = $self->sequenceNext();
85
86         $self->{'sql'}->insert({
87             'table' => $tableName,
88             'fields' => { $columns{'id'} => $id,
89                           $columns{'rep_date'}   => $repdate,
90                           $columns{'rep_time'}   => $reptime,
91                           $columns{'reportname'} => $repname,
92                           $columns{'iscomplete'} => 0 } });
93         
94         return $id;
95     }
96 }
97
98
99
100 # Add a new field to a report. The field is a hash array reference
101 # with keys: 'name', 'serviceid', 'value', 'units'
102
103 sub addField
104 {
105     my $self = shift;
106     my $reportId = shift;
107     my $field = shift;
108
109     if( isDebug() )
110     {
111         Debug('Adding report field: ' . $field->{'name'} .
112               ':' . $field->{'serviceid'} . ' = ' . $field->{'value'} .
113               ' ' . $field->{'units'});
114     }
115     $self->{'fields'}->add( $reportId, $field );
116 }
117
118
119 sub getFields
120 {
121     my $self = shift;
122     my $reportId = shift;
123
124     return $self->{'fields'}->getAll( $reportId );
125 }
126
127
128 sub isComplete
129 {
130     my $self = shift;
131     my $reportId = shift;
132     
133     my $result = $self->{'sql'}->select_one_to_arrayref({
134         'fields' => [ $columns{'iscomplete'} ],
135         'table' => $tableName,
136         'where' => { $columns{'id'}   => $reportId } });
137     
138     if( defined( $result ) )
139     {
140         return $result->[0];
141     }
142     else
143     {
144         Error('Cannot find the report record for ID=' . $reportId);
145     }
146
147     return 0;
148 }
149
150
151 sub finalize
152 {
153     my $self = shift;
154     my $reportId = shift;
155
156     $self->{'sql'}->update({
157         'table' => $tableName,
158         'where' => { $columns{'id'}   => $reportId },
159         'fields' => { $columns{'iscomplete'} => 1 } });
160
161     $self->{'sql'}->commit();
162 }
163
164
165 sub getAllReports
166 {
167     my $self = shift;
168     my $srvIdList = shift;
169     my $limitDate = shift;
170
171     my $where = { $columns{'iscomplete'} => 1 };
172     
173     if( defined( $limitDate ) )
174     {
175         $where->{$columns{'rep_date'}} = ['>=', $limitDate];
176     }
177     
178     $self->{'sql'}->select({
179         'table' => $tableName,
180         'where' => $where,
181         'fields' => [ $columns{'id'},
182                       $columns{'rep_date'},
183                       $columns{'rep_time'},
184                       $columns{'reportname'} ] });
185     
186     my $reports =
187         $self->fetchall([ 'id', 'rep_date', 'rep_time', 'reportname' ]);
188
189     my $ret = {};
190     foreach my $report ( @{$reports} )
191     {
192         my($year, $month, $day) = split('-', $report->{'rep_date'});
193
194         my $fields = $self->getFields( $report->{'id'} );
195         my $fieldsref = {};
196         
197         foreach my $field ( @{$fields} )
198         {
199             if( not ref( $srvIdList ) or
200                 grep {$field->{'serviceid'} eq $_} @{$srvIdList} )
201             {
202                 $fieldsref->{$field->{'serviceid'}}->{$field->{'name'}} = {
203                     'value' => $field->{'value'},
204                     'units' => $field->{'units'} };
205             }
206         }
207         
208         $ret->{$year}{$month}{$day}{$report->{'reportname'}} = $fieldsref;
209     }
210     return $ret;    
211 }
212
213         
214         
215     
216     
217
218         
219 ################################################
220 ## Class for report fields table
221
222 package Torrus::SQL::ReportFields;
223 use strict;
224
225 use Torrus::SQL;
226 use base 'Torrus::SQL';
227
228 use Torrus::Log;
229
230 # The name of the table and columns 
231 # defaults configured in torrus-config.pl
232 our $tableName;
233 our %columns;
234
235 sub add
236 {
237     my $self = shift;
238     my $reportId = shift;
239     my $attrs = shift;
240     
241     my $id = $self->sequenceNext();
242     
243     $self->{'sql'}->insert({
244         'table' => $tableName,
245         'fields' => { $columns{'id'}         => $id,
246                       $columns{'rep_id'}     => $reportId,
247                       $columns{'name'}       => $attrs->{'name'},
248                       $columns{'serviceid'}  => $attrs->{'serviceid'},
249                       $columns{'value'}      => $attrs->{'value'},
250                       $columns{'units'}      => $attrs->{'units'} } });
251 }
252
253
254 sub getAll
255 {
256     my $self = shift;
257     my $reportId = shift;
258        
259     $self->{'sql'}->select({
260         'table' => $tableName,
261         'where' => { $columns{'rep_id'} => $reportId },
262         'fields' => [ $columns{'name'},
263                       $columns{'serviceid'},
264                       $columns{'value'},
265                       $columns{'units'}] });
266
267     return $self->fetchall([ 'name', 'serviceid', 'value', 'units' ]);
268 }
269
270
271 sub removeAll
272 {
273     my $self = shift;
274     my $reportId = shift;
275        
276     $self->{'sql'}->delete({
277         'table' => $tableName,
278         'where' => { $columns{'rep_id'} => $reportId }});
279 }    
280     
281     
282     
283     
284 1;
285
286
287 # Local Variables:
288 # mode: perl
289 # indent-tabs-mode: nil
290 # perl-indent-level: 4
291 # End: