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