/[pxelator]/lib/PXElator/store.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /lib/PXElator/store.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 570 - (hide annotations)
Sun May 25 08:49:40 2014 UTC (6 years, 5 months ago) by dpavlin
File size: 1503 byte(s)
remove dependency on mongodb

1 dpavlin 481 package store;
2    
3     use warnings;
4     use strict;
5    
6 dpavlin 483 use POSIX;
7 dpavlin 485 use Time::HiRes qw/time/;
8 dpavlin 496 use Data::Dump qw(dump);
9 dpavlin 483
10 dpavlin 505 our $audit;
11 dpavlin 483
12 dpavlin 570 #eval {
13     # require MongoDB;
14     #
15     # my $conn = MongoDB::Connection->new;
16     # my $db = $conn->get_database( 'pxelator' );
17     # $audit = $db->get_collection( 'audit' );
18     #};
19     #warn "ERROR: no store for audit: $@" if $@;
20 dpavlin 564
21 dpavlin 483
22     sub audit {
23     my $data = pop @_;
24    
25     my $url = join(' ', @_);
26     $url =~ s/\s+-\S+//g; # remove command line options
27     $url =~ s/\W+/-/g;
28    
29     my $time = time();
30    
31     my @caller = caller(0);
32     $caller[3] = (caller(1))[3];
33     $caller[3] =~ s{^.+::}{}; # stip package name from sub
34     $data->{package} = {
35     time => $time,
36     name => $caller[0],
37     line => $caller[2],
38     caller => $caller[3],
39     };
40    
41     if ( $ENV{DEBUG} ) {
42    
43     my $caller;
44     my $depth = 0;
45     while ( my @c = caller($depth) ) {
46     push @$caller, [ @c ];
47     $depth++;
48     }
49    
50     $data->{caller} = $caller;
51    
52     }
53    
54     # carp 'audit ', dump($data);
55    
56     # $time = int($time); # reduce granularity for url
57     $time = strftime("%Y-%m-%d.%H:%M:%S", localtime $time);
58     my $package = $caller[0];
59    
60 dpavlin 485 # CouchDB::_store_audit( "$time.$package.$url" => $data );
61 dpavlin 483
62 dpavlin 485 # $data->{_id} = "$time.$package.$url";
63 dpavlin 505 #
64     if ( $audit ) {
65     $audit->insert( $data );
66     } else {
67     warn "AUDIT ",dump($data);
68     }
69 dpavlin 483 }
70    
71    
72 dpavlin 487 sub query {
73 dpavlin 522 my ( $q, $callback ) = @_;
74 dpavlin 496 warn "# query ", dump($q);
75 dpavlin 522 $audit->ensure_index({ $_ => 1 }) foreach keys %$q;
76 dpavlin 496 my $cursor = $audit->query($q)->sort({ 'package.time' => -1 })->limit( 100 );
77 dpavlin 487 while( my $o = $cursor->next ) {
78     $callback->( $o );
79     }
80     }
81    
82 dpavlin 481 1;

  ViewVC Help
Powered by ViewVC 1.1.26