From 6868144e2566db209407ec9d43174b434f3fe33c Mon Sep 17 00:00:00 2001 From: James Collins Date: Wed, 3 May 2023 07:35:55 +1000 Subject: [PATCH] fix identifying sessions --- ...23_05_01_045630_update_analytics_table.php | 97 +++++++++++++------ 1 file changed, 65 insertions(+), 32 deletions(-) diff --git a/database/migrations/2023_05_01_045630_update_analytics_table.php b/database/migrations/2023_05_01_045630_update_analytics_table.php index bf80fb9..5ad2eee 100644 --- a/database/migrations/2023_05_01_045630_update_analytics_table.php +++ b/database/migrations/2023_05_01_045630_update_analytics_table.php @@ -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]); + // } + // } + // } } /**