PApp::Log - analyze and expire the state/user databases


 use PApp::Log;


PApp creates a new database entry for its %state hash for every new pageview. This state entry is usually between 100-4000 bytes in size, for an average of about 700-800 bytes, heavily depending on your application.

Clearly, the state database must be wiped clean from old states regularly. Similarly the user database must be cleaned up from anonymous users that didn't visit your site for a long time.

This module helps doing this, while at the same time giving you the ability to analyze the access patterns on your site/application, for example anonymizing and summarizing user behaviour to get the highly needed statistics for your customers ;)

There are two different tasks: logging of state/user entries (as done by log_state) und cleaning the state/user database of expired entries (done by expire_db).

expire_db also calls log_state and is usually the function you need to call.


During logging, the following callbacks will be called for the applications that define them:

(ALL of this is not yet implemented, sicne logging should be seperated from the applications, actually).

expire_user $username, $comment
expire_state $ctime
for_user <BLOCK>;
Add a callback that is called for each user once before she is removed. The callback is called with (userid, username, comment, prefs), where prefs is a hash representing the user's preferences in PApp's internal format which will change anytime ;->.

for_state <BLOCK>, [option => value...]
Add a callback that is called for each state (each unique page-view generates one entry in the state database). The callback is called with two hashes, the first a hash containing meta information (see below), the second is just your standard %state hash.

Contents of the meta hash:

 ctime        time this page was last viewed (unix timestamp)
 previd       parent state id
 userid       userid in effect when that state was created
 pmod         the (non-compiled) application

Additional options:

 app          call only for this application
 location     call only for this location
 module       call only for this module

You can get a reference to the location-specific %S by using:

 $S = $state->{$meta->{location}};


Define a callback that is called for every state:

   for_state {
      my ($meta, $state) = @_;
      print "STATE ",
            "APP=$meta->{pmod}{name}, ",
            "LOC=$meta->{location}, ",

Define a callback that's only called for applications with the name ``dbedit'':

   for_state {
   } app => "dbedit";


expire_db keepuser => <seconds>, keepstate => <seconds>, keepreguser => <seconds>.
Clean the user and state databases from old states, generating log events for state and user-entries that expire but haven't been logged. This is not (yet) atomic, so do not call this function concurrently.
 keepuser => <seconds> (default 60 days)
   the time after which unused anonymous users expire
 keepreguser => <seconds> (default 1 year)
   the time after which even registered users expire
 keepstate => <seconds> (default 14 days)
   the time after which unused state-entries expire

sub expire_db { my %arg = @_; my $now = time - 1; my $keepuser = $now - ($arg{keepuser} || 86400* 60); my $keepreguser = $now - ($arg{keepreguser} || 86400*365); my $keepstate = $now - ($arg{keepstate} || 86400* 14);

   local $DBH = PApp::Config::DBH;
   sql_exec "delete from state where ctime < from_unixtime(?)", $keepstate;
   my $st = sql_exec \my($id, $ctime, $prefs, $user, $comment),
                     "select id, ctime, prefs, user, comment
                      from user
                      where (user  = '' and ctime < from_unixtime(?))
                         or (user != '' and ctime < from_unixtime(?))",
                     $keepuser, $keepreguser;
   while ($st->fetch) {
      #undef $user if $user eq "";
      sql_exec "delete from usergrp where userid = ?", $id;
      sql_exec "delete from user where id = ?", $id;

Run through the whole state database (not the user database) and log all state entries that have not been logged before. This is not (yet) atomic, so do not call this function concurrently.


 - this module has not been written yet ;->


the PApp manpage.


 Marc Lehmann <>