integrate RTx::Statistics package, part of merging spiritone RT changes (#1661)
[freeside.git] / rt / html / RTx / Statistics / CallsQueueDay / Results.tsv
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
6 %#                                          <jesse@bestpractical.com>
7 %# 
8 %# (Except where explicitly superseded by other copyright notices)
9 %# 
10 %# 
11 %# LICENSE:
12 %# 
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %# 
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %# 
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 %# 
27 %# 
28 %# CONTRIBUTION SUBMISSION POLICY:
29 %# 
30 %# (The following paragraph is not intended to limit the rights granted
31 %# to you to modify and distribute this software under the terms of
32 %# the GNU General Public License and is only of importance to you if
33 %# you choose to contribute your changes and enhancements to the
34 %# community by submitting them to Best Practical Solutions, LLC.)
35 %# 
36 %# By intentionally submitting any modifications, corrections or
37 %# derivatives to this work, or any other work intended for use with
38 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 %# you are the copyright holder for those contributions and you grant
40 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
41 %# royalty-free, perpetual, license to use, copy, create derivative
42 %# works based on those contributions, and sublicense and distribute
43 %# those contributions and any derivatives thereof.
44 %# 
45 %# END BPS TAGGED BLOCK }}}
46 <%ARGS>
47 $Queue => undef
48 $weekends => $Statistics::PerDayWeekends;
49 $sMonth=>undef
50 $sDay=>undef
51 $sYear=>undef
52 $eMonth=>undef
53 $eDay=>undef
54 $eYear=>undef
55 $days=>undef
56 $currentMonth=>undef
57 </%ARGS>
58
59 <%INIT>
60 use RTx::Statistics;
61 use Time::Local;
62 my @dates;
63 my $n = 0;
64 my %Totals;
65 my $now = new RT::Date($session{CurrentUser});
66 my $sEpoch;
67 my $eEpoch;
68
69 if (!defined $Queue) {
70   $Queue = $Statistics::PerDayQueue;
71 }
72
73 if ($sDay > $Statistics::monthsMaxDay{$sMonth}) {
74   $sDay = $Statistics::monthsMaxDay{$sMonth};
75 }
76
77 if ($eDay > $Statistics::monthsMaxDay{$eMonth}) {
78   $eDay = $Statistics::monthsMaxDay{$eMonth};
79 }
80
81 if ($sYear){
82         $sEpoch = timelocal(0, 0, 0, $sDay, $sMonth, $sYear-1900);
83 }
84 if ($eYear){
85 Statistics::DebugLog("eMonth = " . $eMonth . "\n");
86         $eEpoch = timelocal(0, 0, 0, $eDay, $eMonth, $eYear-1900);
87 } else {
88         # This case happens when the page is first loaded
89         my @local = localtime(time);
90         ($eDay, $eMonth, $eYear) = ($local[3], $local[4], $local[5]);
91         $eYear += 1900; 
92         $eEpoch = timelocal(0, 0, 0, $local[3], $local[4], $local[5], $local[6], $local[7], $local[8]);
93 Statistics::DebugLog("Setting eEpoch=$eEpoch from current time.\n");
94 }
95
96 if (($eEpoch < $sEpoch) || ($sEpoch == 0)) {
97     # We have an end, but not a start, or, overlapping.
98     
99     # if $currentMonth is set, just set the day to 1
100     if($currentMonth) {
101       # set start vars from end, but with day set to 1
102       (undef, undef, undef, $sDay, $sMonth, $sYear) = localtime($eEpoch);
103       $sDay=1;
104       $sEpoch = timelocal(0, 0, 0, $sDay, $sMonth, $sYear);
105     } else {
106       # If the user has specified how many days back to go, use that,
107       # If not, set start to configured default period before end
108       if(defined $days) {
109         $sEpoch = $eEpoch - ($days * $Statistics::secsPerDay);
110       } else {
111         $sEpoch = $eEpoch - ($Statistics::PerDayPeriod * $Statistics::secsPerDay);
112       }
113       (undef, undef, undef, $sDay, $sMonth, $sYear) = localtime($sEpoch);
114     }
115     $sYear += 1900;
116 }
117
118 # set content type
119 $r->content_type('application/vnd.ms-excel');
120
121 # Put out some data about the generation of this file
122 $m->out("Tickets per day for Queue:\t" . $Queue . "\tGenerated at:\t" . Statistics::FormatDate("%x %X", $now). "\n\n");
123
124
125 # Compute days to chart.
126 # The +1 is because we need to generate one more date. If the user
127 # selected a 10 day range, we need to generate 11 days.
128 my $diff = int(($eEpoch - $sEpoch + $Statistics::secsPerDay - 1) / $Statistics::secsPerDay)+1;
129
130 # Build array of dates
131 my $endRange = $eEpoch + $Statistics::secsPerDay;
132 my $QueueObj = new RT::Queue($session{'CurrentUser'});
133 $QueueObj->Load($Queue);
134 until ($#dates == $diff) {
135     my $date = new RT::Date($session{CurrentUser});
136     $date->Set(Value=>$endRange - $n, Format => 'unix');
137     # Note: we used to adjust the time to local midnight, but
138     # none of the other date entry fields in RT seem to adjust, so we've stopped.
139     #Statistics::DebugLog("Before adjust to midnight date " . Statistics::FormatDate("%c", $date) . "\n");
140     $n+= $Statistics::secsPerDay;
141     # If we aren't showing weekends and this is one, decrement the number
142     # of days to show and skip to the next date.
143     if(!$weekends and Statistics::RTDateIsWeekend($date)) {$diff--; next;}
144     unshift @dates, $date;
145 }
146
147 # Output header row
148 $m->out("Date\tcreate\tresolved\tdeleted\n");
149
150
151 LINE: for my $d (0..$#dates) {
152   if ($d == $#dates){
153     next LINE;
154   }
155   my $x = 1;
156   # Output the date for this row
157   $m->out(Statistics::FormatDate($Statistics::PerDayDateFormat, $dates[$d]));
158   
159   # output the 3 columns for this row
160   for my $status (qw(created resolved deleted)) {
161     my $tix = new RT::Tickets($session{'CurrentUser'});
162     if ($status eq "created") {
163       $tix->LimitCreated(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
164       if ($dates[$d+1]) {
165         $tix->LimitCreated(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
166       }
167     } elsif ($status eq "resolved") {
168       $tix->LimitStatus(VALUE => $status);
169       $tix->LimitResolved(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
170       if ($dates[$d+1]) {
171          $tix->LimitResolved(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
172       }
173     } elsif ($status eq "deleted") {
174       $tix->LimitStatus(VALUE => $status);
175       $tix->LimitLastUpdated(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
176       if ($dates[$d+1]) {
177         $tix->LimitLastUpdated(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
178       }
179     }
180     $tix->LimitQueue (VALUE => $Queue);
181     $m->out( "\t" . $tix->Count ); 
182     $Totals{$status} += $tix->Count;
183   }
184   $m->out("\n");
185 }
186
187 # Output the totals
188 $m->out("Totals\t$Totals{created}\t$Totals{resolved}\t$Totals{deleted}\n");
189
190 $m->abort();
191 </%INIT>