@@ -59,7 +59,7 @@ Return a statement handle at the desired offset.
59
59
60
60
sub get_statement {
61
61
my ($session , $counter ) = @_ ;
62
- my $passive = q{ select SQL_CALC_FOUND_ROWS * from passiveLog where userId <> '1' order by userId, sessionId, timeStamp limit ?, 500000} ;
62
+ my $passive = q{ select SQL_CALC_FOUND_ROWS * from passiveLog where userId <> '1' limit ?, 500000} ;
63
63
my $sth = $session -> db-> read ($passive , [$counter +0]);
64
64
return $sth ;
65
65
}
@@ -93,68 +93,51 @@ sub execute {
93
93
my $lastUrl ;
94
94
my $counter = $instance -> getScratch(' counter' );
95
95
my $sth = get_statement($session , $counter );
96
- if ($counter ) {
97
- $lastUserId = $instance -> getScratch(' lastUserId' );
98
- $lastSessionId = $instance -> getScratch(' lastSessionId' );
99
- $lastTimeStamp = $instance -> getScratch(' lastTimeStamp' );
100
- $lastAssetId = $instance -> getScratch(' lastAssetId' );
101
- $lastUrl = $instance -> getScratch(' lastUrl' );
102
- }
103
- else {
104
- my $logLine = $sth -> hashRef();
105
- $lastUserId = $logLine -> {userId };
106
- $lastSessionId = $logLine -> {sessionId };
107
- $lastTimeStamp = $logLine -> {timeStamp };
108
- $lastAssetId = $logLine -> {assetId };
109
- $lastUrl = $logLine -> {url };
110
- $session -> db-> write (' delete from deltaLog' ); # #Only if we're starting out
96
+ if (! $counter ) { # Clean up from last time, just in case
97
+ $session -> db-> write (' delete from deltaLog' );
98
+ $session -> db-> write (' delete from PA_lastLog' );
111
99
}
112
100
113
101
my $total_rows = $session -> db-> quickScalar(' select found_rows()' );
114
102
115
- my $deltaLog = $session -> db-> prepare(' insert into deltaLog (userId, assetId, delta, timeStamp, url) VALUES (?,?,?,?,?)' );
103
+ my $deltaLog = $session -> db-> prepare(' insert into deltaLog (userId, assetId, timeStamp, url, delta) VALUES (?,?,?,?,?)' );
104
+ my $recordLast = $session -> db-> prepare(' REPLACE INTO PA_lastLog (userId, sessionId, timeStamp, url) VALUES (?,?,?,?)' );
105
+ my $fetchLast = $session -> db-> prepare(' select * from PA_lastLog where sessionId=? and userId=?' );
116
106
117
107
my $expired = 0;
118
108
LOG_CHUNK: while (1) {
119
109
LOG_ENTRY: while (my $logLine = $sth -> hashRef()) {
120
110
$counter ++;
121
- my $delta = $logLine -> {timeStamp } - $lastTimeStamp ;
122
- if ( $logLine -> {userId } eq $lastUserId
123
- && $logLine -> {sessionId } eq $lastSessionId
124
- && $delta < $deltaInterval ) {
125
- $deltaLog -> execute([$lastUserId , $lastAssetId , $delta , $lastTimeStamp , $lastUrl ]);
111
+ $fetchLast -> execute([@{$logLine }{qw/ sessionId userId/ }]);
112
+ my $lastLine = $fetchLast -> hashRef();
113
+ $recordLast -> execute([ (@{ $logLine }{qw/ userId sessionId timeStamp url/ }) ]);
114
+ if ($lastLine -> {timeStamp }) {
115
+ my $delta = $logLine -> {timeStamp } - $lastLine -> {timeStamp };
116
+ $deltaLog -> execute([ (@{ $lastLine }{qw/ userId assetId timeStamp url/ }), $delta ]);
126
117
}
127
- $lastUserId = $logLine -> {userId };
128
- $lastSessionId = $logLine -> {sessionId };
129
- $lastTimeStamp = $logLine -> {timeStamp };
130
- $lastAssetId = $logLine -> {assetId };
131
- $lastUrl = $logLine -> {url };
132
118
if (time () > $endTime ) {
133
- $instance -> setScratch(' lastUserId' , $lastUserId );
134
- $instance -> setScratch(' lastSessionId' , $lastSessionId );
135
- $instance -> setScratch(' lastTimeStamp' , $lastTimeStamp );
136
- $instance -> setScratch(' lastAssetId' , $lastAssetId );
137
- $instance -> setScratch(' lastUrl' , $lastUrl );
138
- $instance -> setScratch(' counter' , $counter );
139
119
$expired = 1;
140
120
last LOG_ENTRY;
141
121
}
142
122
}
143
123
144
- $sth -> finish;
145
124
if ($expired ) {
125
+ $deltaLog -> finish;
126
+ $recordLast -> finish;
127
+ $fetchLast -> finish;
128
+ $sth -> finish;
146
129
return $self -> WAITING(1);
147
130
}
148
131
last LOG_CHUNK if $counter >= $total_rows ;
149
132
$sth = get_statement($session , $counter );
150
133
}
151
134
152
- $instance -> deleteScratch(' lastUserId' );
153
- $instance -> deleteScratch(' lastSessionId' );
154
- $instance -> deleteScratch(' lastTimeStamp' );
155
- $instance -> deleteScratch(' lastAssetId' );
156
- $instance -> deleteScratch(' lastUrl' );
157
135
$instance -> deleteScratch(' counter' );
136
+ $deltaLog -> finish;
137
+ $recordLast -> finish;
138
+ $fetchLast -> finish;
139
+ $sth -> finish;
140
+ $session -> db-> write (' delete from PA_lastLog' );
158
141
return $self -> COMPLETE;
159
142
}
160
143
0 commit comments