[Jifty-commit] r4705 - in jifty/trunk: lib/Jifty/Plugin lib/Jifty/Plugin/Gladiator

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Sat Dec 15 07:30:39 EST 2007


Author: sartak
Date: Sat Dec 15 07:30:38 2007
New Revision: 4705

Added:
   jifty/trunk/lib/Jifty/Plugin/Gladiator/
   jifty/trunk/lib/Jifty/Plugin/Gladiator/Dispatcher.pm
   jifty/trunk/lib/Jifty/Plugin/Gladiator/View.pm
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Plugin/Gladiator.pm

Log:
 r49097 at onn:  sartak | 2007-12-15 07:30:02 -0500
 Rewrite the Gladiator plugin so that it now works, and do the log/view thing (which really wants to be generalized.. later)


Modified: jifty/trunk/lib/Jifty/Plugin/Gladiator.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Plugin/Gladiator.pm	(original)
+++ jifty/trunk/lib/Jifty/Plugin/Gladiator.pm	Sat Dec 15 07:30:38 2007
@@ -5,7 +5,9 @@
 __PACKAGE__->mk_accessors(qw/prev_data/);
 
 use Devel::Gladiator;
-use Jifty::Util;
+use List::Util 'reduce';
+
+our @requests;
 
 our $VERSION = 0.01;
 
@@ -24,71 +26,64 @@
 
     return if $self->_pre_init;
 
-
-    Jifty::Handler->add_trigger(
-        before_request => sub { $self->before_request(@_) }
-    );
-
     Jifty::Handler->add_trigger(
-        after_request => sub { $self->after_request }
+        after_request => sub { $self->after_request(@_) }
     );
 }
 
-=head2 before_request
-
-Log as much of the request state as we can.
+=head2 after_request
 
 =cut
 
-sub before_request
-{
+sub after_request {
     my $self    = shift;
     my $handler = shift;
     my $cgi     = shift;
 
+    # walk the arena, noting the type of each value
+    my %types;
+    for (@{ Devel::Gladiator::walk_arena() }) {
+        ++$types{ ref $_ };
+    }
 
-    Jifty->log->error("Unable to probe for gladiatorg: $@") if $@;
-}
-
-=head2 after_request
-
-Append the current user to the request log. This isn't done in one fell swoop
-because if the server explodes during a request, we would lose the request's
-data for logging.
-
-This, strictly speaking, isn't necessary. But we don't always want to lug the
-sessions table around, so this gets us most of the way there.
-
-C<logged_request> is checked to ensure that we don't append the current
-user if the current request couldn't be logged for whatever reason (perhaps
-a serialization error?).
-
-=cut
-
-sub after_request {
-    my $self = shift;
-
-        my $type_map = {};
-    eval {
-        my $array = Devel::Gladiator::walk_arena();
-    use Devel::Cycle;
-        for my $entry (@$array) {
-            find_cycle($entry);
-            $type_map->{ ref($entry) }++;
+    # basic stats
+    my $all_values = reduce { $a + $b } values %types;
+    my $all_types  = keys %types;
+    my $new_values = 0;
+    my $new_types  = 0;
+
+    my %prev = %{ $self->prev_data || {} };
+
+    # copy so when we modify %types it doesn't affect prev_data
+    my %new_prev = %types;
+    $self->prev_data(\%new_prev);
+
+    # find the difference
+    for my $type (keys %types) {
+        my $diff = $types{$type} - ($prev{$type} || 0);
+
+        if ($diff != 0) {
+            $new_values += $diff;
+            ++$new_types;
         }
-    };
 
-    my $prev = $self->prev_data || {};
-    for (keys %$type_map) {
-        $type_map->{$_} -= $prev->{$_};
-        delete $type_map->{$_} if $type_map->{$_} == 0;
+        $types{$type} = {
+            all => $types{$type},
+            new => $diff,
+        }
     }
 
-    warn "This request";
-    warn Jifty::YAML::Dump($type_map);
-
-    $self->prev_data($type_map);
-
+    push @requests, {
+        id         => 1 + @requests,
+        url        => $cgi->url(-absolute=>1,-path_info=>1),
+        time       => scalar gmtime,
+
+        all_values => $all_values,
+        all_types  => $all_types,
+        new_values => $new_values,
+        new_types  => $new_types,
+        diff       => \%types,
+    };
 }
 
 

Added: jifty/trunk/lib/Jifty/Plugin/Gladiator/Dispatcher.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/Gladiator/Dispatcher.pm	Sat Dec 15 07:30:38 2007
@@ -0,0 +1,49 @@
+package Jifty::Plugin::Gladiator::Dispatcher;
+use warnings;
+use strict;
+
+use Jifty::Dispatcher -base;
+
+# http://your.app/arena
+on '/__jifty/admin/arena' => run {
+    set 'skip_zero' => 1;
+    show "/__jifty/admin/arena/all";
+};
+
+# http://your.app/arena/all
+on '/__jifty/admin/arena/all' => run {
+    set 'skip_zero' => 0;
+    show "/__jifty/admin/arena/all";
+};
+
+# http://your.app/arena/clear
+on '/__jifty/admin/arena/clear' => run {
+    @Jifty::Plugin::Gladiator::requests = ();
+    set 'skip_zero' => 1;
+    redirect "/__jifty/admin/arena";
+};
+
+# http://your.app/arena/xxx
+on '/__jifty/admin/arena/#' => run {
+    abort(404) if $1 < 1;
+    my $arena = $Jifty::Plugin::Gladiator::requests[$1 - 1]
+        or abort(404);
+    set arena => $arena;
+    show "/__jifty/admin/arena/one";
+};
+
+=head1 SEE ALSO
+
+L<Jifty::Plugin::Gladiator>, L<Jifty::Plugin::Gladiator::View>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2007 Best Practical Solutions
+
+This is free software and may be modified and distributed under the same terms as Perl itself.
+
+=cut
+
+1;
+
+

Added: jifty/trunk/lib/Jifty/Plugin/Gladiator/View.pm
==============================================================================
--- (empty file)
+++ jifty/trunk/lib/Jifty/Plugin/Gladiator/View.pm	Sat Dec 15 07:30:38 2007
@@ -0,0 +1,114 @@
+use strict;
+use warnings;
+
+package Jifty::Plugin::Gladiator::View;
+use Jifty::View::Declare -base;
+use Scalar::Util 'blessed';
+
+=head1 NAME
+
+Jifty::Plugin::Gladiator::View - Views for database arena
+
+=head1 TEMPLATES
+
+=cut
+
+template '/__jifty/admin/arena/all' => page {
+    my $skip_zero = get 'skip_zero';
+
+    h1 { "Queries" }
+    p {
+        if ($skip_zero) {
+            a { attr { href => "/__jifty/admin/arena/all" }
+                "Show zero-arena requests" }
+        }
+        else {
+            a { attr { href => "/__jifty/admin/arena" }
+                "Hide zero-arena requests" }
+        }
+        a { attr { href => "/__jifty/admin/arena/clear" }
+            "Clear arena log" }
+    }
+    hr {}
+
+    h3 { "All arena" };
+    table {
+        row {
+            th { "ID"         }
+            th { "New values" }
+            th { "New types"  }
+            th { "All values" }
+            th { "All types"  }
+            th { "URL"        }
+        };
+
+        for (@Jifty::Plugin::Gladiator::requests)
+        {
+            next if $skip_zero && $_->{new_values} == 0;
+
+            row {
+                cell { a {
+                    attr { href => "/__jifty/admin/arena/$_->{id}" }
+                    $_->{id} } }
+
+                cell { $_->{new_values} }
+                cell { $_->{new_types}  }
+                cell { $_->{all_values} }
+                cell { $_->{all_types}  }
+                cell { $_->{url}        }
+            };
+        }
+    }
+};
+
+template '/__jifty/admin/arena/one' => page {
+    my $arena = get 'arena';
+
+    h1 { "Queries from Request $arena->{id}" }
+    ul {
+        li { "URL: $arena->{url}" }
+        li { "At: " . $arena->{time} }
+        li { "New values: $arena->{new_values}" }
+        li { "New types:  $arena->{new_types}"  }
+        li { "All values: $arena->{all_values}" }
+        li { "All types:  $arena->{all_types}"  }
+    }
+
+    table {
+        row {
+            th { "Type" }
+            th { "New" }
+            th { "All" }
+        };
+
+        my @sorted = sort {
+            $arena->{diff}->{$b}->{new} <=> $arena->{diff}->{$a}->{new}
+                                         ||
+            $arena->{diff}->{$b}->{all} <=> $arena->{diff}->{$a}->{all}
+        } keys %{ $arena->{diff} };
+
+        for my $type (@sorted) {
+            row {
+                cell { $type }
+                cell { $arena->{diff}->{$type}->{new} }
+                cell { $arena->{diff}->{$type}->{all} }
+            }
+        }
+    }
+};
+
+=head1 SEE ALSO
+
+L<Jifty::Plugin::Gladiator>, L<Jifty::Plugin::Gladiator::Dispatcher>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2007 Best Practical Solutions
+
+This is free software and may be modified and distributed under the same terms as Perl itself.
+
+=cut
+
+1;
+
+


More information about the Jifty-commit mailing list