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