database profiling bs
[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::pkg_svc;
46 use FS::port;
47 use FS::queue qw(joblisting);
48 use FS::raddb;
49 use FS::session;
50 use FS::svc_acct;
51 use FS::svc_acct_pop qw(popselector);
52 use FS::svc_domain;
53 use FS::svc_forward;
54 use FS::svc_www;
55 use FS::router;
56 use FS::part_router_field;
57 use FS::router_field;
58 use FS::addr_block;
59 use FS::part_sb_field;
60 use FS::sb_field;
61 use FS::svc_broadband;
62 use FS::type_pkgs;
63 use FS::part_export;
64 use FS::part_export_option;
65 use FS::export_svc;
66 use FS::msgcat;
67
68 sub Script_OnStart {
69   $Response->AddHeader('Pragma' => 'no-cache');
70   $Response->AddHeader('Cache-control' => 'no-cache');
71 #  $Response->AddHeader('Expires' => 0);
72   $Response->{Expires} = -36288000;
73
74   $cgi = new CGI;
75   &cgisuidsetup($cgi);
76   $p = popurl(2);
77   #print $cgi->header( '-expires' => 'now' );
78   dbh->{'private_profile'} = {} if dbh->can('sprintProfile');
79
80   #really should check for FS::Profiler or something
81     # Devel::AutoProfiler _our_ VERSION?  thanks a fucking lot
82   if ( Devel::AutoProfiler->can('__recursively_fetch_subs_in_package') ) {
83     #should check to see it's my special version.  well, switch to FS::Profiler
84
85     #nicked from Devel::AutoProfiler::INIT
86     my %subs = Devel::AutoProfiler::__recursively_fetch_subs_in_package('main');
87
88
89     SUB : while( my ($name, $ref) = each(%subs) )
90       {
91         #next if $name =~ /^(main::)?Apache::/;
92         next unless $name =~ /FS/;
93         foreach my $sub (@Devel::AutoProfiler::do_not_instrument_this_sub)
94           {
95             if ($name =~ /$sub/)
96               {
97                 next SUB;
98               }
99           }
100         next if ($Devel::AutoProfiler::do_not_instrument_this_sub{$name});
101         #warn "INIT name is $name \n";
102         Devel::AutoProfiler::__instrument_sub($name, $ref);
103       }
104
105   }
106
107 }
108
109 sub Script_OnFlush {
110   my $ref = $Response->{BinaryRef};
111   #$$ref = $cgi->header( @FS::CGI::header ) . $$ref;
112   #$$ref = $cgi->header() . $$ref;
113   if ( dbh->can('sprintProfile') ) {
114     if ( lc($Response->{ContentType}) eq 'text/html' ) {
115       $$ref =~ s/<\/BODY>[\s\n]*<\/HTML>[\s\n]*$//i
116         or warn "can't remove";
117   
118       #$$ref .= '<PRE>'. ("\n"x96). encode_entities(dbh->sprintProfile()). '</PRE>';
119       #  wtf?  konqueror...
120       $$ref .= '<PRE>'. ("\n"x4096). encode_entities(dbh->sprintProfile()).
121                "\n\n". &sprintAutoProfile(). '</PRE>';
122
123       $$ref .= '</BODY></HTML>';
124     }
125     dbh->{'private_profile'} = {};
126   }
127 }
128
129 #if ( defined(@DBIx::Profile::ISA) && DBIx::Profile::db->can('sprintProfile') ) {
130 #if ( defined(@DBIx::Profile::ISA) && UNIVERSAL::can('DBIx::Profile::db', 'sprintProfile') ) {
131 if ( defined(@DBIx::Profile::ISA) ) {
132
133   #warn "enabling profiling redirects";
134   *CGI::redirect = sub {
135     my( $self, $location) = @_;
136     my $page =
137       $cgi->header.
138       qq!<HTML><BODY>Redirect to <A HREF="$location">$location</A><BR><BR>!.
139       '<PRE>'. encode_entities(dbh->sprintProfile()).
140       "\n\n". &sprintAutoProfile().  '</PRE>'.
141       '</BODY></HTML>';
142     dbh->{'private_profile'} = {};
143     return $page;
144   };
145
146 }
147
148 sub by_total_time 
149
150   return $a->{total_time_in_sub} <=> $b->{total_time_in_sub}; 
151 }
152
153 sub sprintAutoProfile {
154   my %caller_info = %Devel::AutoProfiler::caller_info;
155   return unless keys %caller_info;
156
157   %Devel::AutoProfiler::caller_info = ();
158
159   my @keys = keys(%caller_info);
160
161   foreach my $key (@keys)
162     {
163       my $href = $caller_info{$key};
164
165       $href->{who_am_i} = $key;
166     }
167
168   my @subs = values(%caller_info);
169
170   #my @sorted = sort by_total_time ( @subs );
171   my @sorted = reverse sort by_total_time ( @subs );
172
173   # print Dumper \@sorted;
174
175   my @readable_info;
176
177   foreach my $sort (@sorted)
178     {
179       push(@readable_info, delete($sort->{who_am_i}));
180       push(@readable_info, $sort);
181     }
182
183   use Data::Dumper;
184   return encode_entities(Dumper(\@readable_info));
185
186 }
187
188 1;
189