| version 1.6, 2002/03/19 23:20:40 |
version 1.7, 2002/04/26 23:02:57 |
|
|
| # |
# |
| # utils/sessions_split.pl - Perl script used for spliting |
# utils/sessions_split.pl - Perl script used for spliting |
| # accesses in database into sessions according to timeout |
# accesses in database into sessions according to timeout |
| # __________________________________________________________ |
# ____________________________________________________________ |
| # |
# |
| # Developed by Ondrej Jombik <nepto@pobox.sk> |
# Developed by Ondrej Jombik <nepto@pobox.sk> |
| # Copyright (c) 2001-2002 Platon software development group |
# Copyright (c) 2001-2002 Platon SDG, http://www.platon.sk/ |
| # All rights reserved. |
# All rights reserved. |
| # |
# |
| # See README file for more informations about this software. |
# See README file for more information about this software. |
| # See COPYING file for license informations. |
# See COPYING file for license information. |
| # |
# |
| # Download the latest version from |
# Download the latest version from |
| # http://www.platon.sk/projects/phpWebLogAnalyzer/ |
# http://www.platon.sk/projects/phpWebLogAnalyzer/ |
|
|
| # |
# |
| |
|
| $dbh = DBI->connect( |
$dbh = DBI->connect( |
| "dbi:" . $sessions_split_config::db{driver} |
'dbi:' . $config::db{driver} |
| . ":database=" . $sessions_split_config::db{name} |
. ':database=' . $config::db{name} |
| . ";host=" . $sessions_split_config::db{host} |
. ';host=' . $config::db{host} |
| . ";port=" . $sessions_split_config::db{port}, |
. ';port=' . $config::db{port}, |
| $sessions_split_config::db{username}, |
$config::db{username}, |
| $sessions_split_config::db{password} ) |
$config::db{password} ) |
| || die "can't connect to database: $DBI::errstr"; |
|| die "can't connect to database: $DBI::errstr"; |
| |
|
| # |
# |
| Line 62 $dbh = DBI->connect( |
|
| Line 62 $dbh = DBI->connect( |
|
| # |
# |
| |
|
| $query = q{ |
$query = q{ |
| select remote_addr_id as ra_id, http_user_agent_id as ua_id |
SELECT remote_addr_id AS ra_id, http_user_agent_id AS ua_id |
| from weblog_accesses |
FROM weblog_accesses |
| group by remote_addr_id, http_user_agent_id }; |
GROUP BY remote_addr_id, http_user_agent_id }; |
| |
|
| $sth = $dbh->prepare($query) |
$sth = $dbh->prepare($query) |
| || die "can't prepare statement: $DBI::errstr"; |
|| die "can't prepare statement: $DBI::errstr"; |
|
|
| |
|
| if ( 0 && scalar(@ids) > 0 ) { |
if ( 0 && scalar(@ids) > 0 ) { |
| $query = q{ |
$query = q{ |
| select max(session_id) as max_session_id |
SELECT MAX(session_id) AS max_session_id |
| from weblog_accesses }; |
FROM weblog_accesses }; |
| |
|
| $sth = $dbh->prepare($query) |
$sth = $dbh->prepare($query) |
| || die "can't prepare statement: $DBI::errstr"; |
|| die "can't prepare statement: $DBI::errstr"; |
|
|
| $total_records = $total_sessions = 0; |
$total_records = $total_sessions = 0; |
| |
|
| foreach (@ids) { |
foreach (@ids) { |
| print "Processing (" |
print 'Processing (' |
| . ( $_->{ra_id} ? $_->{ra_id} : 'NULL' ) . ", " |
. ( $_->{ra_id} ? $_->{ra_id} : 'NULL' ) . ', ' |
| . ( $_->{ua_id} ? $_->{ua_id} : 'NULL' ) . "): "; |
. ( $_->{ua_id} ? $_->{ua_id} : 'NULL' ) . '): '; |
| |
|
| # |
# |
| # Fetching all click for current remote_addr and http_user_agent. |
# Fetching all click for current remote_addr and http_user_agent. |
| # |
# |
| |
|
| $query = |
$query = sprintf( |
| "select id, unix_timestamp(datetime) as time" |
'SELECT id, UNIX_TIMESTAMP(datetime) AS time' |
| . " from weblog_accesses" |
. ' FROM weblog_accesses' |
| . " where remote_addr_id " . ( $_->{ra_id} ? "= " . $_->{ra_id} : 'is null' ) |
. ' WHERE remote_addr_id %s' |
| . " and http_user_agent_id " . ( $_->{ua_id} ? "= " . $_->{ua_id} : 'is null' ) |
. ' AND http_user_agent_id %s' |
| . " order by time"; |
. ' ORDER BY time', |
| |
$_->{ra_id} ? '= ' . $_->{ra_id} : 'IS NULL', |
| |
$_->{ua_id} ? '= ' . $_->{ua_id} : 'IS NULL' ); |
| |
|
| $sth = $dbh->prepare($query) |
$sth = $dbh->prepare($query) |
| || die "can't prepare statement: $DBI::errstr"; |
|| die "can't prepare statement: $DBI::errstr"; |
| Line 131 foreach (@ids) { |
|
| Line 133 foreach (@ids) { |
|
| my @data = (); |
my @data = (); |
| my $start_session_id = $max_session_id; |
my $start_session_id = $max_session_id; |
| |
|
| for ( $i = 0 ; $data[$i] = $sth->fetchrow_hashref() ; $i++ ) { } |
for ( $i = 0 ; $data[$i] = $sth->fetchrow_hashref() ; $i++ ) { |
| |
$data[$i]->{time} = 0 if not defined $data[$i]->{time}; |
| |
} |
| |
|
| die $sth->errstr if $sth->err; |
die $sth->errstr if $sth->err; |
| pop (@data); |
pop(@data); |
| |
|
| $total_records += scalar(@data); |
$total_records += scalar(@data); |
| print scalar(@data) . " records, "; |
print scalar(@data) . ' records, '; |
| |
|
| # |
# |
| # Split fetched data into sessions according to $session_timeout. |
# Split fetched data into sessions according to $session_timeout. |
| # |
# |
| |
|
| for ( $i = 0 ; $i < scalar(@data) ; $i++ ) { |
for ( $i = 0 ; $i < scalar(@data) ; $i++ ) { |
| |
|
| |
#print "$i | " .$data[$i]->{time}." | ".$data[ $i - 1 ]->{time}."\n"; |
| if ($i) { |
if ($i) { |
| if ( $data[$i]->{time} - $data[ $i - 1 ]->{time} |
if ( $data[$i]->{time} - $data[ $i - 1 ]->{time} |
| > $sessions_split_config::session_timeout ) { |
> $config::session_timeout ) { |
| $max_session_id++; |
$max_session_id++; |
| } |
} |
| } |
} |
| |
|
| $query = "update weblog_accesses" |
$query = sprintf( 'UPDATE weblog_accesses' |
| . " set session_id = '$max_session_id'" |
. ' SET session_id = %d WHERE id = %d', |
| . " where id = " . $data[$i]->{id}; |
$max_session_id, $data[$i]->{id} ); |
| |
|
| $sth = $dbh->prepare($query) |
$sth = $dbh->prepare($query) |
| || die "can't prepare statement: $DBI::errstr"; |
|| die "can't prepare statement: $DBI::errstr"; |
| |
|
| $sth->execute |
$sth->execute() |
| || die "can't execute statement: $DBI::errstr"; |
|| die "can't execute statement: $DBI::errstr"; |
| } |
} |
| |
|
| Line 178 $dbh->disconnect(); |
|
| Line 184 $dbh->disconnect(); |
|
| # Total statistics print. |
# Total statistics print. |
| # |
# |
| |
|
| print "-" x 60 . "\n"; |
print '-' x 60 . "\n"; |
| print "Total records = $total_records\n"; |
print "Total records = $total_records\n"; |
| print "Total sessions = $total_sessions\n"; |
print "Total sessions = $total_sessions\n"; |
| |
|