default to a session cookie instead of setting an explicit timeout, weird timezone...
[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 my @lt = localtime;
63 @years = reverse( 2002 .. ($lt[5]+1900) );
64 @months=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;      
65 %monthsMaxDay = (
66                  0 => 31,  # January
67                  1 => 29,  # February, allow for leap year
68                  2 => 31,  # March
69                  3 => 30,  # April
70                  4 => 31,  # May
71                  5 => 30,  # June
72                  6 => 31,  # July
73                  7 => 31,  # August
74                  8 => 30,  # September
75                  9 => 31,  # October
76                  10=> 30,  # November
77                  11=> 31   # December
78                  );
79
80 # Set to one to prevent users without the ShowConfigTab right from seeing Statistics
81 $RestrictAccess = 0;
82
83 # Variables to control debugging
84 my $debugging=0;  # set to 1 to enable debugging
85 my $debugtext="";
86
87 =head2 FormatDate 
88
89 Returns a string representing the specified date formatted by the specified string
90
91 =cut
92 sub FormatDate {
93     my $fmt = shift;
94     my $self = shift;
95     return POSIX::strftime($fmt, localtime($self->Unix));
96 }
97
98
99 =head2 RTDateSetToLocalMidnight
100
101 Sets the date to midnight (at the beginning of the day) local time
102 Returns the unixtime at midnight.
103
104 =cut
105 sub RTDateSetToLocalMidnight {
106     my $self = shift;
107     
108     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime($self->Unix);
109     $self->Unix(timelocal (0,0,0,$mday,$mon,$year,$wday,$yday));
110     
111     return ($self->Unix);
112 }
113
114 =head2 RTDateIsWeekend
115
116 Returns 1 if the date is on saturday or sunday
117
118 =cut
119 sub RTDateIsWeekend {
120     my $self = shift;
121     
122     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime($self->Unix);
123     return 1 if (($wday==6) || ($wday==0));
124     0;
125 }
126
127 =head2 RTDateGetDateWeekday
128
129 Returns the localized name of the day specified by date
130
131 =cut
132 sub RTDateGetDateWeekday {
133     my $self = shift;
134     
135     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime($self->Unix);
136     return $self->GetWeekday($wday);
137 }
138
139 =head2 RTDateSubDay
140
141 Subtracts 24 hours from the current time
142
143 =cut
144
145 sub RTDateSubDay {
146     my $self = shift;
147     $self->AddSeconds(0 - $DAY);
148 }
149
150 =head2 RTDateSubDays $DAYS
151
152 Subtracts 24 hours * $DAYS from the current time
153
154 =cut
155
156 sub RTDateSubDays {
157     my $self = shift;
158     my $days = shift;
159     $self->AddSeconds(0 - ($days * $DAY));
160 }
161
162 =head2 DebugInit
163
164 Creates a text area on the page if debugging is on.
165
166 =cut
167
168 sub DebugInit {
169     if($debugging) {
170         my $m = shift;
171         $m->print("<TEXTAREA NAME=debugarea COLS=120 ROWS=50>$debugtext</TEXTAREA>\n");
172     }
173 }
174
175 =head2 DebugLog $logmsg
176
177 Adds a message to the debug area
178
179 =cut
180
181 sub DebugLog {
182     if($debugging) {
183         my $line = shift;
184         $debugtext .= $line;
185         $RT::Logger->debug($line);
186     }
187 }
188
189 =head2 DebugClear
190
191 Clears the current debug string, otherwise it builds from page to page
192
193 =cut
194
195 sub DebugClear {
196     if($debugging) {
197         $debugtext = undef;
198     }
199 }
200
201 =head2 DurationAsString 
202
203 Returns a string representing the specified duration
204
205 =cut
206
207 sub DurationAsString {
208   my $Duration = shift;
209   my $MINUTE = 60;
210   my $HOUR =  $MINUTE*60;
211   my $DAY = $HOUR * 24;
212   my $WEEK = $DAY * 7;
213   my $days = int($Duration / $DAY);
214   $Duration = $Duration % $DAY;
215   my $hours = int($Duration / $HOUR);
216   $hours = sprintf("%02d", $hours);
217   $Duration = $Duration % $HOUR;
218   my $minutes = int($Duration/$MINUTE);
219   $minutes = sprintf("%02d", $minutes);
220   $Duration = $Duration % $MINUTE;
221   my $secs = sprintf("%02d", $Duration);
222
223   if(!$days) {
224       $days = "00";
225   }
226   if(!$hours) {
227       $hours = "00";
228   }
229   if(!$minutes) {
230       $minutes = "00";
231   }
232   if(!$secs) {
233       $secs = "00";
234   }
235   return "$days days $hours:$minutes:$secs";
236 }
237
238 1;
239
240