integrate RTx::Statistics package, part of merging spiritone RT changes (#1661)
[freeside.git] / rt / html / RTx / Statistics / CallsQueueDay / index.html
1 <& /Elements/Header, Title => loc("Tickets per day in Queue:" . $QueueObj->Name()) &>
2 <& /RTx/Statistics/Elements/Tabs,  Title => loc("Tickets by status per day in Queue:" . $QueueObj->Name()) &>
3
4 <h3>Description</h3>
5 <p>This page displays details about tickets in the selected queue over the date range chosen. It shows how many tickets were created on
6 each day in the chosen range, and how many of those were either Resolved or Deleted.</p>
7 <p>To always show the current month to date, bookmark this <a href="<%$RT::WebPath%>/RTx/Statistics/CallsQueueDay/index.html?currentMonth=1">link</a>, or 
8 for a spreadsheet, use this <a href="<%$RT::WebPath%>/RTx/Statistics/CallsQueueDay/Results.tsv?currentMonth=1">link</a>.</p>
9
10 <form method="POST" action="index.html">
11
12 % Statistics::DebugLog("queue name=" . $QueueObj->Name() . "\n");
13
14 %my $title = "Ticket counts in " . $QueueObj->Name() . " by status per day from " . 
15 %         Statistics::FormatDate($Statistics::PerDayDateFormat, $dates[0]) . " through " .
16 %         Statistics::FormatDate($Statistics::PerDayDateFormat, $dates[$#dates-1]);
17 <&|/Elements/TitleBox, 
18         title => $title,
19         title_href => "/RTx/Statistics/CallsQueueDay/index.html?$QueryString" &>
20 <TABLE BORDER=0 cellspacing=0 cellpadding=1 WIDTH=100%>
21 % if ($ShowHeader) {
22 <& /RTx/Statistics/Elements/CollectionAsTable/Header, 
23     Format => \@Format, 
24     FormatString => $Format,
25     AllowSorting => $AllowSorting, 
26     Order => $Order, 
27     Query => undef,
28     Rows => $Rows,
29     Page => $Page,
30     OrderBy => $OrderBy , 
31     BaseURL => $BaseURL,
32     maxitems => $maxitems &> 
33 % }
34 % my $line = 1;
35 % LINE: for my $d (0..$#dates) {
36 % if ($d == $#dates){
37 %        next LINE;
38 % }
39 %     my $x = 1;
40 %     $values{Statistics_Date} = Statistics::FormatDate($Statistics::PerDayDateFormat, $dates[$d]);
41 %# NOTE need to handle all status values here....
42 %     for my $status (qw(created resolved deleted)) {
43 %         my $tix = new RT::Tickets($session{'CurrentUser'});
44 %         $tix->LimitQueue (VALUE => $Queue);
45 %         if ($status eq "created") {
46 %             $tix->LimitCreated(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
47 %             if ($dates[$d+1]) {
48 %                 $tix->LimitCreated(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
49 %            }
50 %            $values{Statistics_Created_Count} = $tix->Count;
51 %            $Totals{Statistics_Created_Count} += $tix->Count;
52 %         }
53 %         elsif ($status eq "resolved") {
54 %             $tix->LimitStatus(VALUE => $status);
55 %             $tix->LimitResolved(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
56 %             if ($dates[$d+1]) {
57 %                 $tix->LimitResolved(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
58 %             }
59 %            $values{Statistics_Resolved_Count} = $tix->Count;
60 %            $Totals{Statistics_Resolved_Count} += $tix->Count;
61 %         } 
62 %         elsif ($status eq "deleted") {
63 %             $tix->LimitStatus(VALUE => $status);
64 %             $tix->LimitLastUpdated(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
65 %             if ($dates[$d+1]) {
66 %                 $tix->LimitLastUpdated(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
67 %             }
68 %            $values{Statistics_Deleted_Count} = $tix->Count;
69 %            $Totals{Statistics_Deleted_Count} += $tix->Count;
70 %         }
71 %         $data[$x++][$d] = $tix->Count;
72 %     }
73 <&   /RTx/Statistics/Elements/CollectionAsTable/Row, Format => \@Format, i => $line, record => $record, maxitems => $maxitems &>
74 %    $line++;
75 % }
76 % $values {Statistics_Date} = "Totals";
77 % $values {Statistics_Created_Count} = $Totals{Statistics_Created_Count};
78 % $values {Statistics_Resolved_Count} = $Totals{Statistics_Resolved_Count};
79 % $values {Statistics_Deleted_Count} = $Totals{Statistics_Deleted_Count};
80 <&   /RTx/Statistics/Elements/CollectionAsTable/Row, Format => \@BoldFormat, i => $line, record => $record, maxitems => $maxitems &>
81 </table>
82 </&>
83
84 <hr>
85
86 <BR />
87 <BR />
88
89 <%perl>
90 # Create the graph URL
91 my $url= 'Elements/Chart?x_labels=';
92 for (1..$diff) {
93     $url .= $data[0][$_] . ",";
94 }
95 chop $url;
96 $url .= "&";
97 shift @data;
98 for (0..$#data) {
99     $url .= "data".(1+$_)."=".(join ",", @{$data[$_]})."&";
100 }
101 chop $url;
102 $url .= "&set_legend=Created,Resolved,Deleted";
103 </%perl>
104
105 <& /RTx/Statistics/Elements/GraphBox, GraphURL => $url &>
106
107 <& /RTx/Statistics/Elements/ControlsAsTable/ControlBox, 
108          Title => "Change Queue or Dates", 
109          ShowDates => 1, sMonth => \$sMonth, sDay => \$sDay, sYear => \$sYear,
110                          eMonth => \$eMonth, eDay => \$eDay, eYear => \$eYear,
111                          weekends => $weekends,
112          ShowSingleQueue => 1, Queue => $Queue
113  &>
114
115 </form>
116
117 <a href="<%$RT::WebPath%>/RTx/Statistics/CallsQueueDay/index.html?<% $QueryString %>"><&|/l&>Bookmarkable link</&></a> |
118 <a href="<%$RT::WebPath%>/RTx/Statistics/CallsQueueDay/Results.tsv?<%$QueryString%>"><&|/l&>spreadsheet</&></a>
119 <BR>
120 <BR>
121
122
123 % Statistics::DebugLog("ref of eMonth is " . ref($eMonth) . "\n");
124 % Statistics::DebugInit( $m );
125
126 <%ARGS>
127 $Queue => undef
128 $weekends => $Statistics::PerDayWeekends;
129 $sMonth=>undef
130 $sDay=>undef
131 $sYear=>undef
132 $eMonth=>undef
133 $eDay=>undef
134 $eYear=>undef
135 $days=>undef
136 $currentMonth=>undef
137
138 $AllowSorting => undef
139 $Order => undef
140 $OrderBy => undef
141 $ShowNavigation => 1
142 $ShowHeader => 1
143 $Rows => 50
144 $Page => 1
145 $BaseURL => undef
146 </%ARGS>
147
148 <%INIT>
149 use RTx::Statistics;
150 use Time::Local;
151 my $selected;
152 my $n = 0;
153 my @data = ([]);
154 my @dates;
155 my @msgs;
156 my $diff;
157 my $sEpoch=0;
158 my $eEpoch=0;
159 my %Totals;
160 my $QueryString;
161 my $maxitems = 4;
162 my %record;
163 my %values;
164 my $record = \%record;
165
166 $record{values} = \%values;
167
168
169 # If debugging, set things up and display all the args
170 Statistics::DebugClear();
171 Statistics::DebugLog("CallsQueueDay/index.html ARGS:\n");
172 for my $key (keys %ARGS) {
173   Statistics::DebugLog("ARG{ $key }=" . $ARGS{$key} . "\n");
174 }
175
176 my $Format = qq{ Statistics_Date, 
177                  '__Statistics_Created_Count__/STYLE:text-align:right;', 
178                  '__Statistics_Resolved_Count__/STYLE:text-align:right;', 
179                  '__Statistics_Deleted_Count__/STYLE:text-align:right;' };
180 my $BoldFormat = qq{ '<B>__Statistics_Date__</B>', 
181                      '<B>__Statistics_Created_Count__</B>/STYLE:text-align:right;',
182                      '<B>__Statistics_Resolved_Count__</B>/STYLE:text-align:right;',
183                      '<B>__Statistics_Deleted_Count__</B>/STYLE:text-align:right;' };
184 my (@Format) = $m->comp('/RTx/Statistics/Elements/CollectionAsTable/ParseFormat', Format => $Format);
185 my (@BoldFormat) = $m->comp('/RTx/Statistics/Elements/CollectionAsTable/ParseFormat', Format => $BoldFormat);
186 Statistics::DebugLog("CallsQueueDay/index.html Format array=" . join(',', @Format) . "\n");
187
188 if (!defined $Queue) {
189   my $QueueObj = new RT::Queue($session{'CurrentUser'});
190   $QueueObj->Load($Statistics::PerDayQueue);
191   $Queue = $QueueObj->Id();
192 }
193
194 if ($sDay > $Statistics::monthsMaxDay{$sMonth}) {
195   $sDay = $Statistics::monthsMaxDay{$sMonth};
196 }
197
198 if ($eDay > $Statistics::monthsMaxDay{$eMonth}) {
199   $eDay = $Statistics::monthsMaxDay{$eMonth};
200 }
201
202 if ($sYear){
203         $sEpoch = timelocal(0, 0, 0, $sDay, $sMonth, $sYear-1900);
204 }
205 if ($eYear){
206 Statistics::DebugLog("eMonth = " . $eMonth . "\n");
207         $eEpoch = timelocal(0, 0, 0, $eDay, $eMonth, $eYear-1900);
208 } else {
209         # This case happens when the page is first loaded
210         my @local = localtime(time);
211         ($eDay, $eMonth, $eYear) = ($local[3], $local[4], $local[5]);
212         $eYear += 1900; 
213         $eEpoch = timelocal(0, 0, 0, $local[3], $local[4], $local[5], $local[6], $local[7], $local[8]);
214 Statistics::DebugLog("Setting eEpoch=$eEpoch from current time.\n");
215 }
216
217 if (($eEpoch < $sEpoch) || ($sEpoch == 0)) {
218     # We have an end, but not a start, or, overlapping.
219     
220     # if $currentMonth is set, just set the day to 1
221     if($currentMonth) {
222       # set start vars from end, but with day set to 1
223       (undef, undef, undef, $sDay, $sMonth, $sYear) = localtime($eEpoch);
224       $sDay=1;
225       $sEpoch = timelocal(0, 0, 0, $sDay, $sMonth, $sYear);
226     } else {
227       # If the user has specified how many days back to go, use that,
228       # If not, set start to configured default period before end
229       if(defined $days) {
230         $sEpoch = $eEpoch - ($days * $Statistics::secsPerDay);
231       } else {
232         $sEpoch = $eEpoch - ($Statistics::PerDayPeriod * $Statistics::secsPerDay);
233       }
234       (undef, undef, undef, $sDay, $sMonth, $sYear) = localtime($sEpoch);
235     }
236     $sYear += 1900;
237 }
238
239 # Compute days to chart.
240 # The +1 is because we need to generate one more date. If the user
241 # selected a 10 day range, we need to generate 11 days.
242 $diff = int(($eEpoch - $sEpoch + $Statistics::secsPerDay - 1) / $Statistics::secsPerDay)+1;
243 Statistics::DebugLog("Setting diff=$diff\n");
244
245 Statistics::DebugLog("sEpoch=$sEpoch, components=" . join(',', localtime($sEpoch)) . "\n");
246 Statistics::DebugLog("eEpoch=$eEpoch, components=" . join(',', localtime($eEpoch)) . "\n");
247
248 # Set up the string for the current query for bookmarkable link
249 $QueryString = "sDay=$sDay&sMonth=$sMonth&sYear=$sYear&eDay=$eDay&eMonth=$eMonth&eYear=$eYear&weekends=$weekends&Queue=$Queue";
250
251 # Set up the end date to be midnight(morning) of the date after the one the user wanted.
252 my $endRange = $eEpoch + $Statistics::secsPerDay;
253 my $QueueObj = new RT::Queue($session{'CurrentUser'});
254 $QueueObj->Load($Queue);
255 $n = 0;
256 until ($#dates == $diff) {
257     my $date = new RT::Date($session{CurrentUser});
258     $date->Set(Value=>$endRange - $n, Format => 'unix');
259     # Note: we used to adjust the time to local midnight, but
260     # none of the other date entry fields in RT seem to adjust, so we've stopped.
261     #Statistics::DebugLog("Before adjust to midnight date " . Statistics::FormatDate("%c", $date) . "\n");
262     $n+= $Statistics::secsPerDay;
263     # If we aren't showing weekends and this is one, decrement the number
264     # of days to show and skip to the next date.
265     if(!$weekends and Statistics::RTDateIsWeekend($date)) {$diff--; next;}
266     unshift @dates, $date;
267 Statistics::DebugLog("pushing date " . Statistics::FormatDate("%c", $date) . "\n");
268     unshift @{ $data[0] }, Statistics::FormatDate($Statistics::PerDayLabelDateFormat, $date);
269 }
270
271 # We put an extra day into the lists to cover up till midnight of the next day,
272 # But we don't want that to appear in the labels, so pop it off.
273 pop( @{ $data[0] } );
274
275 </%INIT>