From a4b9df90dd795489daff110fb0e29203001bd866 Mon Sep 17 00:00:00 2001 From: Xavier Guimard Date: Mon, 12 May 2008 10:30:09 +0000 Subject: [PATCH] LEMONLDAP::NG : top 10 URL by code in Status.pm --- .../lib/Lemonldap/NG/Handler/Status.pm | 102 ++++++++++++++---- 1 file changed, 80 insertions(+), 22 deletions(-) diff --git a/modules/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Status.pm b/modules/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Status.pm index a2c5fe144..a76809943 100644 --- a/modules/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Status.pm +++ b/modules/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Status.pm @@ -2,6 +2,7 @@ package Lemonldap::NG::Handler::Status; use strict; use POSIX; +use Data::Dumper; our $status = {}; our $activity = []; @@ -46,8 +47,7 @@ eval { sub run { my ( $localStorage, $localStorageOptions ) = ( shift, shift ); my $refLocalStorage; - eval -"use $localStorage; \$refLocalStorage = new $localStorage(\$localStorageOptions);"; + eval "use $localStorage; \$refLocalStorage = new $localStorage(\$localStorageOptions);"; die($@) if ($@); $| = 1; my ( $lastMn, $mn ); @@ -83,12 +83,16 @@ sub run { # Status requests - # $args conatins parameters passed to url status page (a=1 for example + # $args contains parameters passed to url status page (a=1 for example # if request is http://test.example.com/status?a=1). To be used # later... elsif (/^STATUS(?:\s+(\S+))?$/) { - my $args = $1; - my ( $c, $a, $u ); + my $tmp = $1; + my $args = {}; + %$args = split (/[=&]/,$tmp) if($tmp); + &head; + #print Dumper($args),&end;next; + my ( $c, $m, $u ); while ( my ( $user, $v ) = each( %{ $status->{user} } ) ) { $u++ unless ( $user =~ /^\d+\.\d+\.\d+\.\d+$/ ); @@ -98,33 +102,78 @@ sub run { } } foreach my $mn (@$activity) { - $a->{$_} += $mn->{$_} foreach ( keys %$mn ); + $m->{$_} += $mn->{$_} foreach ( keys %$mn ); + } + foreach ( keys %$m ) { + $m->{$_} = sprintf( "%.2f", $m->{$_} / MN_COUNT ); + $m->{$_} = int( $m->{$_} ) if ( $m->{$_} > 99 ); } - foreach ( keys %$a ) { - $a->{$_} = sprintf( "%.2f", $a->{$_} / MN_COUNT ); - $a->{$_} = int( $a->{$_} ) if ( $a->{$_} > 99 ); + if ( $args->{'dump'} ) { + print "
\n";
+                print Dumper( $status, $activity );
+                print "
\n"; } - # DEVEL - #use Data::Dumper; - #print '
' . Dumper( $c, $a, $status ) . '
'; - my @t = - $refLocalStorage->get_keys( $localStorageOptions->{namespace} ); - &head; - print "

Total

\n";
+            # Total requests
+            print "

Total

\n
\n";
             print sprintf( "%-30s : %d\n", $_, $c->{$_} )
               foreach ( sort keys %$c );
-            print "\n

Average

\n";
-            print sprintf( "%-30s : %s\n", $_, $a->{$_} )
-              foreach ( sort keys %$a );
-            print "\n

\nTotal users : $u\n

\nLocal Cache : " . @t - . " objects\n

"; + print "\n
\n"; + # Average + print "

Average

\n
\n";
+            print sprintf( "%-30s : %s / mn\n", $_, $m->{$_} )
+              foreach ( sort keys %$m );
+            print "\n
\n"; + # Users connected + print "

\nTotal users : $u\n

\n"; + # Local cache + my @t = $refLocalStorage->get_keys( $localStorageOptions->{namespace} ); + print "

\nLocal Cache : " . @t + . " objects\n

\n"; + # Top uri + if ( $args->{top} ) { + my $count = {}; + $args->{categories} ||= 'REJECT,PORTAL_FIRSTACCESS,LOGOUT,OK'; + # General + print "

Top used URI

\n
\n";
+                foreach my $uri (keys %{$status->{uri}}) {
+                    $count->{$uri} += $_ foreach (values %{$status->{uri}->{$uri}});
+                }
+                my $i=0;
+                foreach (sort {$count->{$b} <=> $count->{$a}} keys %$count) {
+                    last if($i == $args->{top});
+                    last unless($count->{$_});
+                    $i++;
+                    print sprintf( "%-80s : %4d\n", $_, $count->{$_} );
+                }
+                print "\n
\n"; + # Top by category + print "\n"; + foreach my $cat (split /,/,$args->{categories}) { + print ""; + } + print '
CodeTop
$cat
\n
\n"; + topByCat($cat,$args->{top}); + print "
\n
'; + } &end; - print "\nEND\n"; } } } +sub topByCat { + my($cat,$max) = @_; + my $i=0; + print "
\n";
+    foreach (sort {$status->{uri}->{$b}->{$cat} <=> $status->{uri}->{$a}->{$cat}} keys %{$status->{uri}}) {
+        last if($i == $max);
+        last unless($status->{uri}->{$_}->{$cat});
+        $i++;
+        print sprintf( "%-80s : %4d\n", $_, $status->{uri}->{$_}->{$cat} );
+    }
+    print "
\n"; +} + sub head { print <<"EOF"; + +END EOF } 1;