integrate RTx::Statistics package, part of merging spiritone RT changes (#1661)
[freeside.git] / rt / html / RTx / Statistics / CallsMultiQueue / index.html
1 <& /Elements/Header, Title => loc('Tickets per day in Multiple queues') &>
2 <& /RTx/Statistics/Elements/Tabs, Title => loc('Tickets per day in Multiple Queues by status') &>
3
4 <h3>Description</h3>
5 <p>This chart shows details of tickets per day by their status. You can select multiple queues to display at the same time, but only one status. You can chose any of the defined status values. 
6 There is also the option to display all available queues at the same time.
7 The default display shows tickets resolved in your default queue (General unless altered locally).
8 The line chart below shows the same information in a graphical form.
9
10 <br />
11
12 <form method="POST" action="index.html">
13
14 %# Build Legend
15 % my @legend;
16 % for (sort keys %queues_to_show) {
17 %   push @legend, $_;
18 % }
19
20 %my $title = "Tickets with Status $status in " . join(', ', @queues) . ", per day from " .
21 %         Statistics::FormatDate($Statistics::PerDayDateFormat, $dates[0]) . " through " .
22 %         Statistics::FormatDate($Statistics::PerDayDateFormat, $dates[$#dates-1]);
23
24 <& /Elements/TitleBoxStart, title => $title, title_href => "/RTx/Statistics/OpenStalled/index.html?$QueryString"&>
25 <TABLE BORDER=0 cellspacing=0 cellpadding=1 WIDTH="100%">
26 % if ($ShowHeader) {
27 <& /RTx/Statistics/Elements/CollectionAsTable/Header, 
28     Format => \@RowFormat, 
29     FormatString => $RowFormat,
30     AllowSorting => $AllowSorting, 
31     Order => $Order, 
32     Query => undef,
33     Rows => $Rows,
34     Page => $Page,
35     OrderBy => $OrderBy , 
36     BaseURL => $BaseURL,
37     maxitems => $maxitems &> 
38 % }
39 % my $line = 0;
40 % LINE: for my $d (0..$#dates) {
41 %   if ($d == $#dates ){
42 %     next LINE;
43 %   }
44 %   $line++;
45 %   my $x = 1;
46 %   $values{Statistics_Date} = Statistics::FormatDate($dateformat, $dates[$d]);
47 %   my $row_total=0;
48 %   foreach my $q (sort keys %queues_to_show) {
49 %     my $tix = new RT::Tickets($session{'CurrentUser'});
50 %     if ($status eq "resolved") {
51 %       $tix->LimitStatus(VALUE => $status);
52 %       $tix->LimitResolved(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
53 %       if ($dates[$d+1]) {
54 %         $tix->LimitResolved(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
55 %       }
56 %     } 
57 %     elsif ($status eq "new") {
58 %       $tix->LimitCreated(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
59 %       if ($dates[$d+1]) {
60 %         $tix->LimitCreated(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
61 %       }
62 %     } 
63 %     elsif ($status eq "deleted") {
64 %       $tix->LimitStatus(VALUE => $status);
65 %       $tix->LimitLastUpdated(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
66 %       if ($dates[$d+1]) {
67 %         $tix->LimitLastUpdated(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
68 %       }
69 %     }
70 %     elsif ($status eq "stalled") {
71 %       $tix->LimitStatus(VALUE => $status);
72 %       $tix->LimitLastUpdated(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
73 %       if ($dates[$d+1]) {
74 %         $tix->LimitLastUpdated(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
75 %       }
76 %     }
77 %     elsif ($status eq "open") {
78 %       $tix->LimitStatus(VALUE => $status);
79 %       $tix->LimitLastUpdated(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
80 %       if ($dates[$d+1]) {
81 %         $tix->LimitLastUpdated(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
82 %       }
83 %     }
84 %     elsif ($status eq "rejected") {
85 %       $tix->LimitStatus(VALUE => $status);
86 %       $tix->LimitLastUpdated(VALUE => $dates[$d]->ISO, OPERATOR => ">=");
87 %       if ($dates[$d+1]) {
88 %         $tix->LimitLastUpdated(VALUE => $dates[$d+1]->ISO, OPERATOR => "<=");
89 %       }
90 %     }
91 %     $tix->LimitQueue (VALUE => $q);
92 %     $values{$q} = $tix->Count;
93 %     $row_total += $tix->Count;
94 %     $data[$x++][$d] = $tix->Count;
95 %   }
96 %   $values{Statistics_Totals} = $row_total;
97 <&  /RTx/Statistics/Elements/CollectionAsTable/Row, Format => \@RowFormat, i => $line, record => $record, maxitems => $maxitems &>
98 % }
99 </table>
100 <& /Elements/TitleBoxEnd&>
101
102 <hr>
103
104 <BR />
105 <BR />
106
107 <!--    <td>Show:</td>
108     <td COLSPAN=2><SELECT NAME="status">
109 % for (qw(resolved new deleted stalled rejected open)) {
110     <OPTION VALUE="<% $_ %>" <% $_ eq $status && "SELECTED" %>>
111     <% loc($_) %></OPTION>
112 % }
113 --!>
114
115 <%perl>
116 # Create the graph URL
117 my $url = 'Elements/Chart?x_labels=';
118 #$url .= join ",", @{ shift @data } . "&";
119 for (0..$max) {
120      $url .=  $m->interp->apply_escapes($data[0][$_],'u') . ",";
121 }
122 chop $url;
123 $url .= "&";
124 shift @data;
125 $url .=  'set_legend='.(join ",", @legend)."&";
126 for (0..$#data) {
127   $url .= "data".(1+$_)."=". (join ",", @{$data[$_]})."&";
128 }
129 chop $url;
130 </%perl>
131
132 <& /RTx/Statistics/Elements/GraphBox, GraphURL => $url &>
133
134 <& /RTx/Statistics/Elements/ControlsAsTable/ControlBox, 
135          Title => "Change Status, Queues or Dates", 
136          ShowDates => 1, sMonth => \$sMonth, sDay => \$sDay, sYear => \$sYear,
137                          eMonth => \$eMonth, eDay => \$eDay, eYear => \$eYear,
138                          weekends => $weekends,
139          ShowMultiQueues => 1, queues_ref => \@queues,
140          ShowStatus => 1, Status => $status
141  &>
142
143 </form>
144
145 <a href="<%$RT::WebPath%>/RTx/Statistics/CallsMultiQueue/index.html?<% $QueryString %>"><&|/l&>Bookmarkable link</&></a>
146 %# | <a href="<%$RT::WebPath%>/RTx/Statistics/CallsMultiQueue/Results.tsv?<%$QueryString%>"><&|/l&>spreadsheet</&></a>
147 <BR>
148 <BR>
149
150 <%ARGS>
151 $status => $Statistics::MultiQueueStatus
152 $max => $Statistics::MultiQueueMaxRows
153 @queues => @Statistics::MultiQueueQueueList
154 $weekends => $Statistics::PerDayWeekends;
155 $sMonth=>undef
156 $sDay=>undef
157 $sYear=>undef
158 $eMonth=>undef
159 $eDay=>undef
160 $eYear=>undef
161 $days=>undef
162 $dateformat => $Statistics::MultiQueueDateFormat
163 $currentMonth=>undef
164
165 $AllowSorting => undef
166 $Order => undef
167 $OrderBy => undef
168 $ShowNavigation => 1
169 $ShowHeader => 1
170 $Rows => 50
171 $Page => 1
172 $BaseURL => undef
173 $AddAllCheck => undef
174 </%ARGS>
175
176 <%INIT>
177
178 use RTx::Statistics;
179 use Time::Local;
180 my $n = 0;
181 my @data = ([]);
182 my @dates;
183 my @msgs;
184 my $selected;
185 my $diff;
186 my %queues_to_show;
187 my $secsPerDay=86400;
188 my $sEpoch;
189 my $eEpoch;
190 my $QueryString;
191 my $maxitems;
192 my $RowFormat;
193 my $BoldRowFormat;
194 my %record;
195 my %values;
196 my $record = \%record;
197
198 $record{values} = \%values;
199
200 Statistics::DebugClear();
201 Statistics::DebugLog("CallsQueueDay/index.html ARGS:\n");
202 for my $key (keys %ARGS) {
203   Statistics::DebugLog("ARG{ $key }=" . $ARGS{$key} . "\n");
204 }
205
206
207   # Handle the Add All Checkbox
208   if($AddAllCheck eq "on") {
209     $AddAllCheck = undef;
210     undef (@queues);
211     my $q=new RT::Queues($session{'CurrentUser'});
212     $q->UnLimit;
213     while (my $queue=$q->Next) {
214       next if !$queue->CurrentUserHasRight('SeeQueue');
215       push @queues, $queue->Name;
216     }
217   }
218
219   # If the user has the right to see the queue, put it into the map
220   for my $q (@queues) {
221       my $Queueobj = new RT::Queue($session{'CurrentUser'});
222       $Queueobj->Load($q);
223       next if !$Queueobj->CurrentUserHasRight('SeeQueue');
224       $queues_to_show{$q} = 1;
225   }
226
227   $maxitems = (scalar @queues) + 2;
228
229   # Build the format strings
230   $RowFormat = "'__Statistics_Date__'";
231   $BoldRowFormat = "'<B>__Statistics_Date__</B>'";
232   for my $q (@queues) {
233       $RowFormat .= ",'__Statistics_Dynamic__/KEY:$q/TITLE:$q/STYLE:text-align:right;'";
234       $BoldRowFormat .= ",'<B>__Statistics_Dynamic__</B>/KEY:$q/TITLE:$q/STYLE:text-align:right;'";
235   }
236   $RowFormat .= ",'<B>__Statistics_Totals__</B>/STYLE:text-align:right;'";
237   $BoldRowFormat .= ",'<B>__Statistics_Totals__</B>/STYLE:text-align:right;'";
238   # Parse the formats into structures.
239   my (@RowFormat) = $m->comp('/RTx/Statistics/Elements/CollectionAsTable/ParseFormat', Format => $RowFormat);
240   my (@BoldRowFormat) = $m->comp('/RTx/Statistics/Elements/CollectionAsTable/ParseFormat', Format => $BoldRowFormat);
241
242 if ($sDay > $Statistics::monthsMaxDay{$sMonth}) {
243   $sDay = $Statistics::monthsMaxDay{$sMonth};
244 }
245
246 if ($eDay > $Statistics::monthsMaxDay{$eMonth}) {
247   $eDay = $Statistics::monthsMaxDay{$eMonth};
248 }
249
250 if ($sYear){
251         $sEpoch = timelocal(0, 0, 0, $sDay, $sMonth, $sYear-1900);
252 }
253 if ($eYear){
254 Statistics::DebugLog("eMonth = " . $eMonth . "\n");
255         $eEpoch = timelocal(0, 0, 0, $eDay, $eMonth, $eYear-1900);
256 } else {
257         # This case happens when the page is first loaded
258         my @local = localtime(time);
259         ($eDay, $eMonth, $eYear) = ($local[3], $local[4], $local[5]);
260         $eYear += 1900; 
261         $eEpoch = timelocal(0, 0, 0, $local[3], $local[4], $local[5], $local[6], $local[7], $local[8]);
262 Statistics::DebugLog("Setting eEpoch=$eEpoch from current time.\n");
263 }
264
265 if (($eEpoch < $sEpoch) || ($sEpoch == 0)) {
266     # We have an end, but not a start, or, overlapping.
267     
268     # if $currentMonth is set, just set the day to 1
269     if($currentMonth) {
270       # set start vars from end, but with day set to 1
271       (undef, undef, undef, $sDay, $sMonth, $sYear) = localtime($eEpoch);
272       $sDay=1;
273       $sEpoch = timelocal(0, 0, 0, $sDay, $sMonth, $sYear);
274     } else {
275       # If the user has specified how many days back to go, use that,
276       # If not, set start to configured default period before end
277       if(defined $days) {
278         $sEpoch = $eEpoch - ($days * $Statistics::secsPerDay);
279       } else {
280         $sEpoch = $eEpoch - ($Statistics::PerDayPeriod * $Statistics::secsPerDay);
281       }
282       (undef, undef, undef, $sDay, $sMonth, $sYear) = localtime($sEpoch);
283     }
284     $sYear += 1900;
285 }
286
287 # Compute days to chart.
288 # The +1 is because we need to generate one more date. If the user
289 # selected a 10 day range, we need to generate 11 days.
290 $diff = int(($eEpoch - $sEpoch + $Statistics::secsPerDay - 1) / $Statistics::secsPerDay)+1;
291 Statistics::DebugLog("Setting diff=$diff\n");
292
293 Statistics::DebugLog("sEpoch=$sEpoch, components=" . join(',', localtime($sEpoch)) . "\n");
294 Statistics::DebugLog("eEpoch=$eEpoch, components=" . join(',', localtime($eEpoch)) . "\n");
295
296 # Build the new query string
297 $QueryString = "queues=" . join("&queues=", @queues);
298 $QueryString .= "&sDay=$sDay&sMonth=$sMonth&sYear=$sYear&eDay=$eDay&eMonth=$eMonth&eYear=$eYear&weekends=$weekends";
299
300
301
302
303 # Set up the end date to be midnight(morning) of the date after the one the user wanted.
304 my $endRange = $eEpoch + $Statistics::secsPerDay;
305 $n = 0;
306 until ($#dates == $diff) {
307     my $date = new RT::Date($session{CurrentUser});
308     $date->Set(Value=>$endRange - $n, Format => 'unix');
309     # Note: we used to adjust the time to local midnight, but
310     # none of the other date entry fields in RT seem to adjust, so we've stopped.
311     #Statistics::DebugLog("Before adjust to midnight date " . Statistics::FormatDate("%c", $date) . "\n");
312     $n+= $Statistics::secsPerDay;
313     # If we aren't showing weekends and this is one, decrement the number
314     # of days to show and skip to the next date.
315     if(!$weekends and Statistics::RTDateIsWeekend($date)) {$diff--; next;}
316     unshift @dates, $date;
317 Statistics::DebugLog("pushing date " . Statistics::FormatDate("%c", $date) . "\n");
318     unshift @{ $data[0] }, Statistics::FormatDate($Statistics::PerDayLabelDateFormat, $date);
319 }
320
321 # We put an extra day into the lists to cover up till midnight of the next day,
322 # But we don't want that to appear in the labels, so pop it off.
323 pop( @{ $data[0] } );
324
325 my $queue = new RT::Queues($session{CurrentUser});
326 $queue->UnLimit;
327
328 my $QueueObj = new RT::Queue($session{'CurrentUser'});
329 $QueueObj->Load($queue);
330 </%INIT>