per-agent configuration of batch processors, #71837
[freeside.git] / torrus / perllib / Torrus / ReportGenerator.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: ReportGenerator.pm,v 1.1 2010-12-27 00:03:37 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
19
20 # Package for reports generation
21 # Classes should inherit Torrus::ReportGenerator
22
23 package Torrus::ReportGenerator;
24
25 use strict;
26 use Date::Parse;
27
28 use Torrus::Log;
29 use Torrus::SQL::Reports;
30 use Torrus::SQL::SrvExport;
31
32 sub new
33 {
34     my $class = shift;
35     my $options = shift;
36
37     if( not ref( $options ) or
38         not defined( $options->{'Date'} ) or
39         not defined( $options->{'Time'} ) or
40         not defined( $options->{'Name'} ) )
41     {
42         Error('Missing options in Torrus::Report constructor');
43         return undef;
44     }
45     
46     my $self = {};
47     bless ($self, $class);
48
49     # For monthly reports, adjust date and time for the first day of the month
50     if( $self->isMonthly() )
51     {
52         $options->{'Time'} = '00:00';
53         my ($ss,$mm,$hh,$day,$month,$year,$zone) =
54             strptime( $options->{'Date'} );
55         $year += 1900;
56         $month++;
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;
62
63         # Count the number of seconds in the month and define the end date
64         my $endyear = $year;
65         my $endmonth = $month + 1;
66
67         if( $endmonth > 12 )
68         {
69             $endmonth = 1;
70             $endyear++;
71         }
72
73         my $enddate = sprintf('%.4d-%.2d-01', $endyear, $endmonth);
74         $self->{'EndDate'} = $enddate;
75         $self->{'EndUnixTime'} = str2time( $self->{'EndDate'} );
76         
77         $self->{'RangeSeconds'} =
78             $self->{'EndUnixTime'} - $self->{'StartUnixTime'};
79     }
80
81     if( $self->usesSrvExport() )
82     {
83         my $srvExp =
84             Torrus::SQL::SrvExport->new( $options->{'SrvExportSqlSubtype'} );
85         if( not defined( $srvExp ) )
86         {
87             Error('Cannot connect to the database');
88             return undef;
89         }
90         $self->{'srvexport'} = $srvExp;
91     }
92     
93     $self->{'options'} = $options;
94
95     my $sqlRep = Torrus::SQL::Reports->new( $options->{'ReportsSqlSubtype'} );
96     if( not defined( $sqlRep ) )
97     {
98         Error('Cannot connect to the database');
99         return undef;
100     }
101     $self->{'backend'} = $sqlRep;
102     
103     my $reportId = $sqlRep->reportId( $options->{'Date'},
104                                       $options->{'Time'},
105                                       $options->{'Name'} );
106     $self->{'reportId'} = $reportId;
107     
108     if( $sqlRep->isComplete( $reportId ) )
109     {
110         Error('Report already exists');
111         return undef;
112     }
113     
114     return $self;    
115 }
116
117
118 sub generate
119 {
120     die('Virtual method called');    
121 }
122
123
124 sub isMonthly
125 {
126     return 0;
127 }
128
129 sub usesSrvExport
130 {
131     return 0;
132 }
133
134 1;
135
136
137 # Local Variables:
138 # mode: perl
139 # indent-tabs-mode: nil
140 # perl-indent-level: 4
141 # End: