838d2739237e14b82f54e4041cd3b370e268bb5b
[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 => "/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 <%ARGS>
110 $max => $Statistics::TimeToResolveMaxRows
111 $Queue => undef
112 $weekends =>$Statistics::TimeToResolveWeekends
113 $sMonth=>undef
114 $sDay=>undef
115 $sYear=>undef
116 $eMonth=>undef
117 $eDay=>undef
118 $eYear=>undef
119 $days=>undef
120 $currentMonth=>undef
121
122 $AllowSorting => undef
123 $Order => undef
124 $OrderBy => undef
125 $ShowNavigation => 1
126 $ShowHeader => 1
127 $Rows => 50
128 $Page => 1
129 $BaseURL => undef
130 </%ARGS>
131
132 <%INIT>
133 use RTx::Statistics;
134 use Time::Local;
135 my $n = 0;
136 my @data = ([]);
137 my @dates;
138 my @msgs;
139 my $size;
140 my $selected;
141 my $grandtotal = 0; 
142 my $diff;
143 my $sEpoch=0;
144 my $eEpoch=0;
145 my $QueryString;
146
147 my $maxitems = 4;
148 my %record;
149 my %values;
150 my $record = \%record;
151
152 $record{values} = \%values;
153
154
155 # If debugging, set things up and display all the args
156 Statistics::DebugClear();
157 Statistics::DebugLog("CallsQueueDay/index.html ARGS:\n");
158 for my $key (keys %ARGS) {
159   Statistics::DebugLog("ARG{ $key }=" . $ARGS{$key} . "\n");
160 }
161
162 my $Format = qq{ Statistics_Date, 
163                  '__Statistics_Created_Count__/STYLE:text-align:right;', 
164                  '__Statistics_Resolved_Count__/STYLE:text-align:right;', 
165                  '__Statistics_Dynamic__/KEY:Duration/TITLE:Time To Resolve/STYLE:text-align:right;' };
166 my $BoldFormat = qq{ '<B>__Statistics_Date__</B>', 
167                      '<B>__Statistics_Created_Count__</B>/STYLE:text-align:right;',
168                      '<B>__Statistics_Resolved_Count__</B>/STYLE:text-align:right;',
169                      '<B>__Statistics_Dynamic__</B>/KEY:Duration/TITLE:Time To Resolve/STYLE:text-align:right;' };
170
171 # TODO need way to make this cell do colspan
172 my $OneCellFormat = qq{ '<B>__Statistics_Dynamic__</B>/KEY:text/STYLE:text-align:left;','','','' };
173
174 my (@Format) = $m->comp('/RTx/Statistics/Elements/CollectionAsTable/ParseFormat', Format => $Format);
175 my (@BoldFormat) = $m->comp('/RTx/Statistics/Elements/CollectionAsTable/ParseFormat', Format => $BoldFormat);
176 my (@OneCellFormat) = $m->comp('/RTx/Statistics/Elements/CollectionAsTable/ParseFormat', Format => $OneCellFormat);
177
178 Statistics::DebugLog("CallsQueueDay/index.html Format array=" . join(',', @Format) . "\n");
179
180 if ($sDay > $Statistics::monthsMaxDay{$sMonth}) {
181   $sDay = $Statistics::monthsMaxDay{$sMonth};
182 }
183
184 if ($eDay > $Statistics::monthsMaxDay{$eMonth}) {
185   $eDay = $Statistics::monthsMaxDay{$eMonth};
186 }
187
188 if ($sYear){
189         $sEpoch = timelocal(0, 0, 0, $sDay, $sMonth, $sYear-1900);
190 }
191 if ($eYear){
192 Statistics::DebugLog("eMonth = " . $eMonth . "\n");
193         $eEpoch = timelocal(0, 0, 0, $eDay, $eMonth, $eYear-1900);
194 } else {
195         # This case happens when the page is first loaded
196         my @local = localtime(time);
197         ($eDay, $eMonth, $eYear) = ($local[3], $local[4], $local[5]);
198         $eYear += 1900; 
199         $eEpoch = timelocal(0, 0, 0, $local[3], $local[4], $local[5], $local[6], $local[7], $local[8]);
200 Statistics::DebugLog("Setting eEpoch=$eEpoch from current time.\n");
201 }
202
203 if (($eEpoch < $sEpoch) || ($sEpoch == 0)) {
204     # We have an end, but not a start, or, overlapping.
205     
206     # if $currentMonth is set, just set the day to 1
207     if($currentMonth) {
208       # set start vars from end, but with day set to 1
209       (undef, undef, undef, $sDay, $sMonth, $sYear) = localtime($eEpoch);
210       $sDay=1;
211       $sEpoch = timelocal(0, 0, 0, $sDay, $sMonth, $sYear);
212     } else {
213       # If the user has specified how many days back to go, use that,
214       # If not, set start to configured default period before end
215       if(defined $days) {
216         $sEpoch = $eEpoch - ($days * $Statistics::secsPerDay);
217       } else {
218         $sEpoch = $eEpoch - ($Statistics::PerDayPeriod * $Statistics::secsPerDay);
219       }
220       (undef, undef, undef, $sDay, $sMonth, $sYear) = localtime($sEpoch);
221     }
222     $sYear += 1900;
223 }
224
225 # Compute days to chart.
226 # The +1 is because we need to generate one more date. If the user
227 # selected a 10 day range, we need to generate 11 days.
228 $diff = int(($eEpoch - $sEpoch + $Statistics::secsPerDay - 1) / $Statistics::secsPerDay)+1;
229 Statistics::DebugLog("Setting diff=$diff\n");
230
231 Statistics::DebugLog("sEpoch=$sEpoch, components=" . join(',', localtime($sEpoch)) . "\n");
232 Statistics::DebugLog("eEpoch=$eEpoch, components=" . join(',', localtime($eEpoch)) . "\n");
233
234 my $QueueObj = new RT::Queue($session{'CurrentUser'});
235 if (!defined $Queue) {
236   $QueueObj->Load($Statistics::TimeToResolveQueue);
237   $Queue = $QueueObj->Id();
238 }
239
240 # Set up the string for the current query for bookmarkable link
241 $QueryString = "sDay=$sDay&sMonth=$sMonth&sYear=$sYear&eDay=$eDay&eMonth=$eMonth&eYear=$eYear&weekends=$weekends&Queue=$Queue";
242
243 # Set up the end date to be midnight(morning) of the date after the one the user wanted.
244 my $endRange = $eEpoch + $Statistics::secsPerDay;
245 $QueueObj->Load($Queue);
246 # NOTE: list loop starts at the end of the date range, unshifting dates onto 
247 # the arrays, so that they end up in start to finish order.
248 $eEpoch += $Statistics::secsPerDay;
249 $n = 0;
250 until ($#dates == $diff ) {     
251     my $date = new RT::Date($session{CurrentUser});
252     $date->Set(Value=>$endRange - $n, Format => 'unix');
253     # Note: we used to adjust the time to local midnight, but
254     # none of the other date entry fields in RT seem to adjust, so we've stopped.
255     #Statistics::DebugLog("Before adjust to midnight date " . Statistics::FormatDate("%c", $date) . "\n");
256     $n+= $Statistics::secsPerDay;
257     # If we aren't showing weekends and this is one, decrement the number
258     # of days to show and skip to the next date.
259     if(!$weekends and Statistics::RTDateIsWeekend($date)) {$diff--; next;}
260     unshift @dates, $date;
261 Statistics::DebugLog("pushing date " . Statistics::FormatDate("%c", $date) . "\n");
262     unshift @{ $data[0] }, Statistics::FormatDate($Statistics::PerDayLabelDateFormat, $date);
263 }
264 </%INIT>