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: ReportGenerator.pm,v 1.1 2010-12-27 00:03:37 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20 # Package for reports generation
21 # Classes should inherit Torrus::ReportGenerator
23 package Torrus::ReportGenerator;
29 use Torrus::SQL::Reports;
30 use Torrus::SQL::SrvExport;
37 if( not ref( $options ) or
38 not defined( $options->{'Date'} ) or
39 not defined( $options->{'Time'} ) or
40 not defined( $options->{'Name'} ) )
42 Error('Missing options in Torrus::Report constructor');
47 bless ($self, $class);
49 # For monthly reports, adjust date and time for the first day of the month
50 if( $self->isMonthly() )
52 $options->{'Time'} = '00:00';
53 my ($ss,$mm,$hh,$day,$month,$year,$zone) =
54 strptime( $options->{'Date'} );
57 $self->{'StartDate'} = sprintf('%.4d-%.2d-01', $year, $month);
58 $options->{'Date'} = $self->{'StartDate'};
59 $self->{'StartUnixTime'} = str2time( $self->{'StartDate'} );
60 $self->{'Year'} = $year;
61 $self->{'Month'} = $month;
63 # Count the number of seconds in the month and define the end date
65 my $endmonth = $month + 1;
73 my $enddate = sprintf('%.4d-%.2d-01', $endyear, $endmonth);
74 $self->{'EndDate'} = $enddate;
75 $self->{'EndUnixTime'} = str2time( $self->{'EndDate'} );
77 $self->{'RangeSeconds'} =
78 $self->{'EndUnixTime'} - $self->{'StartUnixTime'};
81 if( $self->usesSrvExport() )
84 Torrus::SQL::SrvExport->new( $options->{'SrvExportSqlSubtype'} );
85 if( not defined( $srvExp ) )
87 Error('Cannot connect to the database');
90 $self->{'srvexport'} = $srvExp;
93 $self->{'options'} = $options;
95 my $sqlRep = Torrus::SQL::Reports->new( $options->{'ReportsSqlSubtype'} );
96 if( not defined( $sqlRep ) )
98 Error('Cannot connect to the database');
101 $self->{'backend'} = $sqlRep;
103 my $reportId = $sqlRep->reportId( $options->{'Date'},
105 $options->{'Name'} );
106 $self->{'reportId'} = $reportId;
108 if( $sqlRep->isComplete( $reportId ) )
110 Error('Report already exists');
120 die('Virtual method called');
139 # indent-tabs-mode: nil
140 # perl-indent-level: 4