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