Merge remote-tracking branch 'upstream/master'
[torrus-plus.git] / src / lib / Torrus / ReportGenerator.pm
blob8eec3575d040a328ab6302b6f7e9c8314b0501a1
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$
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
20 # Package for reports generation
21 # Classes should inherit Torrus::ReportGenerator
23 package Torrus::ReportGenerator;
25 use strict;
26 use warnings;
28 use Date::Parse;
30 use Torrus::Log;
31 use Torrus::SQL::Reports;
32 use Torrus::SQL::SrvExport;
34 our $VERSION = 1.0;
36 sub new
38 my $class = shift;
39 my $options = shift;
41 if( not ref( $options ) or
42 not defined( $options->{'Date'} ) or
43 not defined( $options->{'Time'} ) or
44 not defined( $options->{'Name'} ) )
46 Error('Missing options in Torrus::Report constructor');
47 return
50 my $self = {};
51 bless ($self, $class);
53 # For monthly reports, adjust date and time for the first day of the month
54 if( $self->isMonthly() )
56 $options->{'Time'} = '00:00';
57 my ($ss,$mm,$hh,$day,$month,$year,$zone) =
58 strptime( $options->{'Date'} );
59 $year += 1900;
60 $month++;
61 $self->{'StartDate'} = sprintf('%.4d-%.2d-01', $year, $month);
62 $options->{'Date'} = $self->{'StartDate'};
63 $self->{'StartUnixTime'} = str2time( $self->{'StartDate'} );
64 $self->{'Year'} = $year;
65 $self->{'Month'} = $month;
67 # Count the number of seconds in the month and define the end date
68 my $endyear = $year;
69 my $endmonth = $month + 1;
71 if( $endmonth > 12 )
73 $endmonth = 1;
74 $endyear++;
77 my $enddate = sprintf('%.4d-%.2d-01', $endyear, $endmonth);
78 $self->{'EndDate'} = $enddate;
79 $self->{'EndUnixTime'} = str2time( $self->{'EndDate'} );
81 $self->{'RangeSeconds'} =
82 $self->{'EndUnixTime'} - $self->{'StartUnixTime'};
85 if( $self->usesSrvExport() )
87 my $srvExp =
88 Torrus::SQL::SrvExport->new( $options->{'SrvExportSqlSubtype'} );
89 if( not defined( $srvExp ) )
91 Error('Cannot connect to the database');
92 return
94 $self->{'srvexport'} = $srvExp;
97 $self->{'options'} = $options;
99 my $sqlRep = Torrus::SQL::Reports->new( $options->{'ReportsSqlSubtype'} );
100 if( not defined( $sqlRep ) )
102 Error('Cannot connect to the database');
103 return
105 $self->{'backend'} = $sqlRep;
107 my $reportId = $sqlRep->reportId( $options->{'Date'},
108 $options->{'Time'},
109 $options->{'Name'} );
110 $self->{'reportId'} = $reportId;
112 if( $sqlRep->isComplete( $reportId ) )
114 Error('Report already exists');
115 return
118 return $self;
122 sub generate
124 die('Virtual method called');
128 sub isMonthly
130 return 0;
133 sub usesSrvExport
135 return 0;
141 # Local Variables:
142 # mode: perl
143 # indent-tabs-mode: nil
144 # perl-indent-level: 4
145 # End: