From ed3756a9f10833f22e6070d1bcdf84a8a95519e9 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 23 Jan 2010 18:58:18 +0000 Subject: [PATCH] move common audit parts back into store --- lib/PXElator/CouchDB.pm | 43 +++---------------------------- lib/PXElator/store.pm | 55 +++++++++++++++++++++++++++++++++++++++- lib/PXElator/t/CouchDB.t | 2 +- 3 files changed, 58 insertions(+), 42 deletions(-) diff --git a/lib/PXElator/CouchDB.pm b/lib/PXElator/CouchDB.pm index 91e90a8..1d1401b 100644 --- a/lib/PXElator/CouchDB.pm +++ b/lib/PXElator/CouchDB.pm @@ -13,7 +13,6 @@ use Data::Structure::Util qw(unbless); use Scalar::Util qw/blessed/; use Storable qw/dclone/; use Carp qw/carp/; -use POSIX; sub new { my ($class, $host, $port, $options) = @_; @@ -115,45 +114,9 @@ sub post { our $audit = __PACKAGE__->new; -sub audit { - my $data = pop @_; - - my $url = join(' ', @_); - $url =~ s/\s+-\S+//g; # remove command line options - $url =~ s/\W+/-/g; - - my $time = time(); - - my @caller = caller(1); # skip store wrapper - $caller[3] = (caller(1))[3]; - $caller[3] =~ s{^.+::}{}; # stip package name from sub - $data->{package} = { - time => $time, - name => $caller[0], - line => $caller[2], - caller => $caller[3], - }; - - if ( $ENV{DEBUG} ) { - - my $caller; - my $depth = 0; - while ( my @c = caller($depth) ) { - push @$caller, [ @c ]; - $depth++; - } - - $data->{caller} = $caller; - - } - -# carp 'audit ', dump($data); - -# $time = int($time); # reduce granularity for url - $time = strftime("%Y-%m-%d.%H:%M:%S", localtime $time); - my $package = $caller[0]; - $audit->put( "pxelator/$time.$package.$url", $data ); - +sub _store_audit { + my ( $id, $data ) = @_; + $audit->put( "pxelator/$id", $data ); } 1; diff --git a/lib/PXElator/store.pm b/lib/PXElator/store.pm index e246781..0da25d4 100644 --- a/lib/PXElator/store.pm +++ b/lib/PXElator/store.pm @@ -3,8 +3,61 @@ package store; use warnings; use strict; +use POSIX; + use CouchDB; -sub audit { CouchDB::audit(@_) } +=for MongoDB + +use MongoDB; +my $conn = MongoDB::Connection->new; +my $db = $conn->get_database( 'pxelator' ); +our $audit = $db->get_collection( 'audit' ); + +=cut + +sub audit { + my $data = pop @_; + + my $url = join(' ', @_); + $url =~ s/\s+-\S+//g; # remove command line options + $url =~ s/\W+/-/g; + + my $time = time(); + + my @caller = caller(0); + $caller[3] = (caller(1))[3]; + $caller[3] =~ s{^.+::}{}; # stip package name from sub + $data->{package} = { + time => $time, + name => $caller[0], + line => $caller[2], + caller => $caller[3], + }; + + if ( $ENV{DEBUG} ) { + + my $caller; + my $depth = 0; + while ( my @c = caller($depth) ) { + push @$caller, [ @c ]; + $depth++; + } + + $data->{caller} = $caller; + + } + +# carp 'audit ', dump($data); + +# $time = int($time); # reduce granularity for url + $time = strftime("%Y-%m-%d.%H:%M:%S", localtime $time); + my $package = $caller[0]; + + CouchDB::_store_audit( "$time.$package.$url" => $data ); + +# $data->{_id} = "$time.$package.$url"; $audit->insert( $data ); +} + 1; diff --git a/lib/PXElator/t/CouchDB.t b/lib/PXElator/t/CouchDB.t index 49073f7..db397ee 100755 --- a/lib/PXElator/t/CouchDB.t +++ b/lib/PXElator/t/CouchDB.t @@ -24,5 +24,5 @@ ok( $db->put( $url, { foo => 99 }), "put $url" ); ok( $db->delete( $url ), 'delete' ); -ok( CouchDB::audit( 'test', { foo => 42, bar => 'baz' }), 'audit' ); +ok( CouchDB::_store_audit( 'test', { foo => 42, bar => 'baz' }), 'audit' ); -- 2.20.1