1 # Copyright (C) 2005 Stanislav Sinyagin
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.
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.
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.
17 # $Id: Reports.pm,v 1.1 2010-12-27 00:03:59 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20 # Class for Reporter data manipulation
21 package Torrus::SQL::ReportFields;
23 package Torrus::SQL::Reports;
28 use base 'Torrus::SQL';
31 # use Torrus::SQL::ReportFields;
33 # The name of the table and columns
34 # defaults configured in torrus-config.pl
44 my $self = $class->SUPER::new( $subtype );
46 $self->{'fields'} = Torrus::SQL::ReportFields->new( $subtype );
48 bless ($self, $class);
53 # Find or create a new row in reports table
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 } });
69 if( defined( $result ) )
71 if( not $result->[1] )
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] );
84 my $id = $self->sequenceNext();
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 } });
100 # Add a new field to a report. The field is a hash array reference
101 # with keys: 'name', 'serviceid', 'value', 'units'
106 my $reportId = shift;
111 Debug('Adding report field: ' . $field->{'name'} .
112 ':' . $field->{'serviceid'} . ' = ' . $field->{'value'} .
113 ' ' . $field->{'units'});
115 $self->{'fields'}->add( $reportId, $field );
122 my $reportId = shift;
124 return $self->{'fields'}->getAll( $reportId );
131 my $reportId = shift;
133 my $result = $self->{'sql'}->select_one_to_arrayref({
134 'fields' => [ $columns{'iscomplete'} ],
135 'table' => $tableName,
136 'where' => { $columns{'id'} => $reportId } });
138 if( defined( $result ) )
144 Error('Cannot find the report record for ID=' . $reportId);
154 my $reportId = shift;
156 $self->{'sql'}->update({
157 'table' => $tableName,
158 'where' => { $columns{'id'} => $reportId },
159 'fields' => { $columns{'iscomplete'} => 1 } });
161 $self->{'sql'}->commit();
168 my $srvIdList = shift;
169 my $limitDate = shift;
171 my $where = { $columns{'iscomplete'} => 1 };
173 if( defined( $limitDate ) )
175 $where->{$columns{'rep_date'}} = ['>=', $limitDate];
178 $self->{'sql'}->select({
179 'table' => $tableName,
181 'fields' => [ $columns{'id'},
182 $columns{'rep_date'},
183 $columns{'rep_time'},
184 $columns{'reportname'} ] });
187 $self->fetchall([ 'id', 'rep_date', 'rep_time', 'reportname' ]);
190 foreach my $report ( @{$reports} )
192 my($year, $month, $day) = split('-', $report->{'rep_date'});
194 my $fields = $self->getFields( $report->{'id'} );
197 foreach my $field ( @{$fields} )
199 if( not ref( $srvIdList ) or
200 grep {$field->{'serviceid'} eq $_} @{$srvIdList} )
202 $fieldsref->{$field->{'serviceid'}}->{$field->{'name'}} = {
203 'value' => $field->{'value'},
204 'units' => $field->{'units'} };
208 $ret->{$year}{$month}{$day}{$report->{'reportname'}} = $fieldsref;
219 ################################################
220 ## Class for report fields table
222 package Torrus::SQL::ReportFields;
226 use base 'Torrus::SQL';
230 # The name of the table and columns
231 # defaults configured in torrus-config.pl
238 my $reportId = shift;
241 my $id = $self->sequenceNext();
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'} } });
257 my $reportId = shift;
259 $self->{'sql'}->select({
260 'table' => $tableName,
261 'where' => { $columns{'rep_id'} => $reportId },
262 'fields' => [ $columns{'name'},
263 $columns{'serviceid'},
265 $columns{'units'}] });
267 return $self->fetchall([ 'name', 'serviceid', 'value', 'units' ]);
274 my $reportId = shift;
276 $self->{'sql'}->delete({
277 'table' => $tableName,
278 'where' => { $columns{'rep_id'} => $reportId }});
289 # indent-tabs-mode: nil
290 # perl-indent-level: 4