integrate RTx::Statistics package, part of merging spiritone RT changes (#1661)
[freeside.git] / rt / lib / RTx / Statistics.pm
1 package Statistics;
2
3 use vars qw(
4 $MultiQueueStatus $MultiQueueDateFormat @MultiQueueQueueList $MultiQueueMaxRows $MultiQueueWeekends $MultiQueueLabelDateFormat
5 $PerDayStatus $PerDayDateFormat $PerDayQueue $PerDayMaxRows $PerDayWeekends $PerDayLabelDateFormat $PerDayPeriod
6 $DayOfWeekQueue
7 @OpenStalledQueueList $OpenStalledWeekends
8 $TimeToResolveDateFormat $TimeToResolveQueue $TimeToResolveMaxRows $TimeToResolveWeekends $TimeToResolveLabelDateFormat
9 $TimeToResolveGraphQueue
10 @years @months %monthsMaxDay
11 $secsPerDay
12 $RestrictAccess
13 $GraphWidth $GraphHeight
14 );
15
16 use Time::Local;
17
18 # I couldn't figure out a way to override these in RT_SiteConfig, which would be
19 # preferable.
20
21 # Width and Height of all graphics
22 $GraphWidth=500;
23 $GraphHeight=400;
24
25 # Initial settings for the CallsMultiQueue stat page
26 $MultiQueueStatus = "resolved";
27 $MultiQueueDateFormat = "%a %b %d %Y";  # format for dates on Multi Queue report, see "man strftime" for options
28 @MultiQueueQueueList = ("General"); # list of queues to start Multi Queue per day reports
29 $MultiQueueMaxRows = 10;
30 $MultiQueueWeekends = 1;
31 $MultiQueueLabelDateFormat = "%a";
32
33 # Initial settings for the CallsQueueDay stat page
34 $PerDayStatus = "resolved";
35 $PerDayDateFormat = "%a %b %d %Y";
36 $PerDayQueue = "General";
37 $PerDayMaxRows = 10;
38 $PerDayWeekends = 1;
39 $PerDayLabelDateFormat = "%a";
40 $PerDayPeriod = 10;
41
42 # Initial settings for the DayOfWeek stat page
43 $DayOfWeekQueue = "General";
44
45 # Initial settings for the OpenStalled stat page
46 @OpenStalledQueueList = ("General");
47 $OpenStalledWeekends = 1;
48
49 # Initial settings for the TimeToResolve stat page
50 $TimeToResolveDateFormat = "%a %b %d"; 
51 $TimeToResolveQueue = "General";
52 $TimeToResolveMaxRows = 10;
53 $TimeToResolveWeekends = 1;
54 $TimeToResolveLabelDateFormat = "%a";
55
56 # Initial settings for the TimeToResolve Graph page
57 $TimeToResolveGraphQueue = "General";
58
59 $secsPerDay = 86400;
60
61 # List of years and months to populate drop down lists
62 @years =('2010', '2009', '2008', '2007', '2006', '2005', '2004', '2003' ,'2003' ,'2002');
63 @months=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;      
64 %monthsMaxDay = (
65                  0 => 31,  # January
66                  1 => 29,  # February, allow for leap year
67                  2 => 31,  # March
68                  3 => 30,  # April
69                  4 => 31,  # May
70                  5 => 30,  # June
71                  6 => 31,  # July
72                  7 => 31,  # August
73                  8 => 30,  # September
74                  9 => 31,  # October
75                  10=> 30,  # November
76                  11=> 31   # December
77                  );
78
79 # Set to one to prevent users without the ShowConfigTab right from seeing Statistics
80 $RestrictAccess = 0;
81
82 # Variables to control debugging
83 my $debugging=0;  # set to 1 to enable debugging
84 my $debugtext="";
85
86 =head2 FormatDate 
87
88 Returns a string representing the specified date formatted by the specified string
89
90 =cut
91 sub FormatDate {
92     my $fmt = shift;
93     my $self = shift;
94     return POSIX::strftime($fmt, localtime($self->Unix));
95 }
96
97
98 =head2 RTDateSetToLocalMidnight
99
100 Sets the date to midnight (at the beginning of the day) local time
101 Returns the unixtime at midnight.
102
103 =cut
104 sub RTDateSetToLocalMidnight {
105     my $self = shift;
106     
107     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime($self->Unix);
108     $self->Unix(timelocal (0,0,0,$mday,$mon,$year,$wday,$yday));
109     
110     return ($self->Unix);
111 }
112
113 =head2 RTDateIsWeekend
114
115 Returns 1 if the date is on saturday or sunday
116
117 =cut
118 sub RTDateIsWeekend {
119     my $self = shift;
120     
121     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime($self->Unix);
122     return 1 if (($wday==6) || ($wday==0));
123     0;
124 }
125
126 =head2 RTDateGetDateWeekday
127
128 Returns the localized name of the day specified by date
129
130 =cut
131 sub RTDateGetDateWeekday {
132     my $self = shift;
133     
134     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime($self->Unix);
135     return $self->GetWeekday($wday);
136 }
137
138 =head2 RTDateSubDay
139
140 Subtracts 24 hours from the current time
141
142 =cut
143
144 sub RTDateSubDay {
145     my $self = shift;
146     $self->AddSeconds(0 - $DAY);
147 }
148
149 =head2 RTDateSubDays $DAYS
150
151 Subtracts 24 hours * $DAYS from the current time
152
153 =cut
154
155 sub RTDateSubDays {
156     my $self = shift;
157     my $days = shift;
158     $self->AddSeconds(0 - ($days * $DAY));
159 }
160
161 =head2 DebugInit
162
163 Creates a text area on the page if debugging is on.
164
165 =cut
166
167 sub DebugInit {
168     if($debugging) {
169         my $m = shift;
170         $m->print("<TEXTAREA NAME=debugarea COLS=120 ROWS=50>$debugtext</TEXTAREA>\n");
171     }
172 }
173
174 =head2 DebugLog $logmsg
175
176 Adds a message to the debug area
177
178 =cut
179
180 sub DebugLog {
181     if($debugging) {
182         my $line = shift;
183         $debugtext .= $line;
184         $RT::Logger->debug($line);
185     }
186 }
187
188 =head2 DebugClear
189
190 Clears the current debug string, otherwise it builds from page to page
191
192 =cut
193
194 sub DebugClear {
195     if($debugging) {
196         $debugtext = undef;
197     }
198 }
199
200 =head2 DurationAsString 
201
202 Returns a string representing the specified duration
203
204 =cut
205
206 sub DurationAsString {
207   my $Duration = shift;
208   my $MINUTE = 60;
209   my $HOUR =  $MINUTE*60;
210   my $DAY = $HOUR * 24;
211   my $WEEK = $DAY * 7;
212   my $days = int($Duration / $DAY);
213   $Duration = $Duration % $DAY;
214   my $hours = int($Duration / $HOUR);
215   $hours = sprintf("%02d", $hours);
216   $Duration = $Duration % $HOUR;
217   my $minutes = int($Duration/$MINUTE);
218   $minutes = sprintf("%02d", $minutes);
219   $Duration = $Duration % $MINUTE;
220   my $secs = sprintf("%02d", $Duration);
221
222   if(!$days) {
223       $days = "00";
224   }
225   if(!$hours) {
226       $hours = "00";
227   }
228   if(!$minutes) {
229       $minutes = "00";
230   }
231   if(!$secs) {
232       $secs = "00";
233   }
234   return "$days days $hours:$minutes:$secs";
235 }
236
237 1;
238
239