fix identifying sessions
This commit is contained in:
@@ -23,43 +23,76 @@ return new class extends Migration
|
||||
->where('type', 'pageview')
|
||||
->update(['type' => 'apirequest']);
|
||||
|
||||
$rows = DB::table('analytics')
|
||||
// Set first session
|
||||
$session = 0;
|
||||
|
||||
do {
|
||||
$rows = DB::table('analytics')
|
||||
->whereNull('session')
|
||||
->orWhere('session', 0)
|
||||
->orderBy('created_at', 'asc')
|
||||
->limit(1)
|
||||
->get();
|
||||
|
||||
// Loop through the rows and update `session` based on the logic you described
|
||||
$session = 1;
|
||||
foreach ($rows as $row) {
|
||||
// Check if this is the first row
|
||||
if ($row->created_at === $rows->first()->created_at) {
|
||||
DB::table('analytics')
|
||||
->where('id', $row->id)
|
||||
->update(['session' => $session]);
|
||||
} else {
|
||||
// Look for a previous row with the same useragent and ip within the last 30 minutes
|
||||
$previousRow = DB::table('analytics')
|
||||
->where('useragent', $row->useragent)
|
||||
->where('ip', $row->ip)
|
||||
->where('created_at', '>=', date('Y-m-d H:i:s', strtotime('-30 minutes', strtotime($row->created_at))))
|
||||
->whereNotNull('session')
|
||||
->orderBy('created_at', 'desc')
|
||||
->first();
|
||||
|
||||
if ($previousRow) {
|
||||
// If a previous row is found, set the session to the same value
|
||||
DB::table('analytics')
|
||||
->where('id', $row->id)
|
||||
->update(['session' => $previousRow->session]);
|
||||
} else {
|
||||
// If no previous row is found, increment the session value
|
||||
$session++;
|
||||
DB::table('analytics')
|
||||
->where('id', $row->id)
|
||||
->update(['session' => $session]);
|
||||
}
|
||||
if($rows->isEmpty()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$sessionRow = $rows->first();
|
||||
DB::table('analytics')->where('id', $sessionRow->id)->update(['session' => ++$session]);
|
||||
$lastSessionUpdate = $sessionRow->created_at;
|
||||
|
||||
do {
|
||||
$sameSessionRows = DB::table('analytics')
|
||||
->whereNull('session')
|
||||
->orWhere('session', 0)
|
||||
->where('useragent', $sessionRow->useragent)
|
||||
->where('created_at', '<=', date('Y-m-d H:i:s', strtotime('30 minutes', strtotime($lastSessionUpdate))))
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
|
||||
if($sameSessionRows->isEmpty()) {
|
||||
break;
|
||||
}
|
||||
|
||||
$ids = $sameSessionRows->pluck('id')->toArray();
|
||||
DB::table('analytics')->whereIn('id', $ids)->update(['session' => $session]);
|
||||
$lastSessionUpdate = $sameSessionRows->first()->created_at;
|
||||
} while(true);
|
||||
} while(true);
|
||||
|
||||
|
||||
// Loop through the rows and update `session` based on the logic you described
|
||||
// foreach ($rows as $row) {
|
||||
// // Check if this is the first row
|
||||
// if ($row->created_at === $rows->first()->created_at) {
|
||||
// DB::table('analytics')
|
||||
// ->where('id', $row->id)
|
||||
// ->update(['session' => $session]);
|
||||
// } else {
|
||||
// // Look for a previous row with the same useragent and ip within the last 30 minutes
|
||||
// $previousRow = DB::table('analytics')
|
||||
// ->where('useragent', $row->useragent)
|
||||
// ->where('ip', $row->ip)
|
||||
// ->where('created_at', '>=', date('Y-m-d H:i:s', strtotime('-30 minutes', strtotime($row->created_at))))
|
||||
// ->whereNotNull('session')
|
||||
// ->orderBy('created_at', 'desc')
|
||||
// ->first();
|
||||
|
||||
// if ($previousRow) {
|
||||
// // If a previous row is found, set the session to the same value
|
||||
// DB::table('analytics')
|
||||
// ->where('id', $row->id)
|
||||
// ->update(['session' => $previousRow->session]);
|
||||
// } else {
|
||||
// // If no previous row is found, increment the session value
|
||||
// $session++;
|
||||
// DB::table('analytics')
|
||||
// ->where('id', $row->id)
|
||||
// ->update(['session' => $session]);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user