move common audit parts back into store
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 23 Jan 2010 18:58:18 +0000 (18:58 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 23 Jan 2010 18:58:18 +0000 (18:58 +0000)
lib/PXElator/CouchDB.pm
lib/PXElator/store.pm
lib/PXElator/t/CouchDB.t

index 91e90a8..1d1401b 100644 (file)
@@ -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;
index e246781..0da25d4 100644 (file)
@@ -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;
index 49073f7..db397ee 100755 (executable)
@@ -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' );