beginning of OO reporting interface, create acadia-requested crosstab reports
[freeside.git] / htetc / global.asa
1 BEGIN { eval "use Devel::AutoProfiler;"; } #only if installed...
2 #BEGIN { package Devel::AutoProfiler; use vars qw(%caller_info); }
3 #use Devel::AutoProfiler;
4
5 use strict;
6 use vars qw( $cgi $p );
7 use Apache::ASP 2.55;
8 use CGI 2.47;
9 #use CGI::Carp qw(fatalsToBrowser);
10 use Date::Format;
11 use Date::Parse;
12 use Time::Local;
13 use Tie::IxHash;
14 use HTML::Entities;
15 use IO::Handle;
16 use IO::File;
17 use String::Approx qw(amatch);
18 use Chart::LinesPoints;
19 use HTML::Widgets::SelectLayers 0.03;
20 use FS::UID qw(cgisuidsetup dbh getotaker datasrc driver_name);
21 use FS::Record qw(qsearch qsearchs fields dbdef);
22 use FS::Conf;
23 use FS::CGI qw(header menubar popurl table itable ntable idiot eidiot
24                small_custview myexit http_header);
25 use FS::Msgcat qw(gettext geterror);
26 use FS::Misc qw( send_email );
27 use FS::Report::Table::Monthly;
28
29 use FS::agent;
30 use FS::agent_type;
31 use FS::domain_record;
32 use FS::cust_bill;
33 use FS::cust_bill_pay;
34 use FS::cust_credit;
35 use FS::cust_credit_bill;
36 use FS::cust_main;
37 use FS::cust_main_county;
38 use FS::cust_pay;
39 use FS::cust_pkg;
40 use FS::cust_refund;
41 use FS::cust_svc;
42 use FS::nas;
43 use FS::part_bill_event;
44 use FS::part_pkg;
45 use FS::part_referral;
46 use FS::part_svc;
47 use FS::part_svc_router;
48 use FS::part_virtual_field;
49 use FS::pkg_svc;
50 use FS::port;
51 use FS::queue qw(joblisting);
52 use FS::raddb;
53 use FS::session;
54 use FS::svc_acct;
55 use FS::svc_acct_pop qw(popselector);
56 use FS::svc_domain;
57 use FS::svc_forward;
58 use FS::svc_www;
59 use FS::router;
60 use FS::addr_block;
61 use FS::svc_broadband;
62 use FS::svc_external;
63 use FS::type_pkgs;
64 use FS::part_export;
65 use FS::part_export_option;
66 use FS::export_svc;
67 use FS::msgcat;
68
69 sub Script_OnStart {
70   $Response->AddHeader('Pragma' => 'no-cache');
71   $Response->AddHeader('Cache-control' => 'no-cache');
72 #  $Response->AddHeader('Expires' => 0);
73   $Response->{Expires} = -36288000;
74
75   $cgi = new CGI;
76   &cgisuidsetup($cgi);
77   $p = popurl(2);
78   #print $cgi->header( '-expires' => 'now' );
79   #dbh->{'private_profile'} = {} if dbh->can('sprintProfile');
80   dbh->{'private_profile'} = {} if UNIVERSAL::can(dbh, 'sprintProfile');
81
82   #really should check for FS::Profiler or something
83     # Devel::AutoProfiler _our_ VERSION?  thanks a fucking lot
84   if ( Devel::AutoProfiler->can('__recursively_fetch_subs_in_package') ) {
85     #should check to see it's my special version.  well, switch to FS::Profiler
86
87     #nicked from Devel::AutoProfiler::INIT
88     my %subs = Devel::AutoProfiler::__recursively_fetch_subs_in_package('main');
89
90
91     SUB : while( my ($name, $ref) = each(%subs) )
92       {
93         #next if $name =~ /^(main::)?Apache::/;
94         next unless $name =~ /FS/;
95         foreach my $sub (@Devel::AutoProfiler::do_not_instrument_this_sub)
96           {
97             if ($name =~ /$sub/)
98               {
99                 next SUB;
100               }
101           }
102         next if ($Devel::AutoProfiler::do_not_instrument_this_sub{$name});
103         #warn "INIT name is $name \n";
104         Devel::AutoProfiler::__instrument_sub($name, $ref);
105       }
106
107   }
108
109 }
110
111 sub Script_OnFlush {
112   my $ref = $Response->{BinaryRef};
113   #$$ref = $cgi->header( @FS::CGI::header ) . $$ref;
114   #$$ref = $cgi->header() . $$ref;
115   #warn "Script_OnFlush called with dbh ". dbh. "\n";
116   #if ( dbh->can('sprintProfile') ) {
117   if ( UNIVERSAL::can(dbh, 'sprintProfile') ) {
118     #warn "dbh can sprintProfile\n";
119     if ( lc($Response->{ContentType}) eq 'text/html' ) { #con
120       #warn "contenttype is sprintProfile\n";
121       $$ref =~ s/<\/BODY>[\s\n]*<\/HTML>[\s\n]*$//i
122         or warn "can't remove";
123   
124       #$$ref .= '<PRE>'. ("\n"x96). encode_entities(dbh->sprintProfile()). '</PRE>';
125       #  wtf?  konqueror...
126       $$ref .= '<PRE>'. ("\n"x4096). encode_entities(dbh->sprintProfile()).
127                "\n\n". &sprintAutoProfile(). '</PRE>';
128
129       $$ref .= '</BODY></HTML>';
130     }
131     dbh->{'private_profile'} = {};
132   }
133 }
134
135 #if ( defined(@DBIx::Profile::ISA) && DBIx::Profile::db->can('sprintProfile') ) {
136 #if ( defined(@DBIx::Profile::ISA) && UNIVERSAL::can('DBIx::Profile::db', 'sprintProfile') ) {
137 if ( defined(@DBIx::Profile::ISA) ) {
138
139   #warn "enabling profiling redirects";
140   *CGI::redirect = sub {
141     my( $self, $location) = @_;
142     my $page =
143       $cgi->header.
144       qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
145       '<BR><BR><PRE>'.
146         ( UNIVERSAL::can(dbh, 'sprintProfile')
147             ? encode_entities(dbh->sprintProfile())
148             : 'DBIx::Profile missing sprintProfile method;'.
149               'unpatched or too old?'                        ).
150       "\n\n". &sprintAutoProfile().  '</PRE>'.
151       '</BODY></HTML>';
152     dbh->{'private_profile'} = {};
153     return $page;
154   };
155
156 }
157
158 sub by_total_time 
159
160   return $a->{total_time_in_sub} <=> $b->{total_time_in_sub}; 
161 }
162
163 sub sprintAutoProfile {
164   my %caller_info = %Devel::AutoProfiler::caller_info;
165   return unless keys %caller_info;
166
167   %Devel::AutoProfiler::caller_info = ();
168
169   my @keys = keys(%caller_info);
170
171   foreach my $key (@keys)
172     {
173       my $href = $caller_info{$key};
174
175       $href->{who_am_i} = $key;
176     }
177
178   my @subs = values(%caller_info);
179
180   #my @sorted = sort by_total_time ( @subs );
181   my @sorted = reverse sort by_total_time ( @subs );
182
183   # print Dumper \@sorted;
184
185   my @readable_info;
186
187   foreach my $sort (@sorted)
188     {
189       push(@readable_info, delete($sort->{who_am_i}));
190       push(@readable_info, $sort);
191     }
192
193   use Data::Dumper;
194   return encode_entities(Dumper(\@readable_info));
195
196 }
197
198 sub include {
199   $Response->Include(@_);
200 }
201
202 if ( defined(@DBIx::Profile::ISA) ) {
203
204   #false laziness w/above
205   *redirect = sub {
206     my($location) = @_;
207
208     ${$Response->{BinaryRef}} = 
209       $cgi->header.
210       qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A>!.
211       '<BR><BR><PRE>'.
212         ( UNIVERSAL::can(dbh, 'sprintProfile')
213             ? encode_entities(dbh->sprintProfile())
214             : 'DBIx::Profile missing sprintProfile method;'.
215               'unpatched or too old?'                        ).
216       "\n\n". &sprintAutoProfile().  '</PRE>'.
217       '</BODY></HTML>';
218
219     dbh->{'private_profile'} = {};
220
221     $Response->End;
222
223   };
224
225 } else {
226
227   *redirect = sub {
228     $Response->Redirect(@_);
229   }
230
231 }
232
233 1;
234