From 2e1d2fc428e7c9427c2d221738ecf7a60f5f921b Mon Sep 17 00:00:00 2001 From: Doar Mihai Date: Sat, 21 Mar 2026 12:08:01 +0200 Subject: [PATCH] fixed queries to use mysql functions for mysql and sqlite functions for sqlite, also commented some duplicate routes --- app/Services/AnalyticsQueryService.php | 40 ++++++++++++++++++++++---- package-lock.json | 2 +- routes/auth.php | 4 +-- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app/Services/AnalyticsQueryService.php b/app/Services/AnalyticsQueryService.php index 3ba575d..76be380 100644 --- a/app/Services/AnalyticsQueryService.php +++ b/app/Services/AnalyticsQueryService.php @@ -292,7 +292,7 @@ class AnalyticsQueryService $pvRaw = PageView::where('website_id', $this->website->id) ->whereBetween('viewed_at', [$rawFrom, $rawTo]) ->select( - DB::raw("strftime('{$groupFormat}', viewed_at) as period"), + DB::raw($this->dateFormat($groupFormat, 'viewed_at').' as period'), DB::raw('COUNT(*) as page_views'), DB::raw('COUNT(DISTINCT visitor_id) as visitors'), ) @@ -303,7 +303,7 @@ class AnalyticsQueryService $sessRaw = AnalyticsSession::where('website_id', $this->website->id) ->whereBetween('started_at', [$rawFrom, $rawTo]) ->select( - DB::raw("strftime('{$groupFormat}', started_at) as period"), + DB::raw($this->dateFormat($groupFormat, 'started_at').' as period'), DB::raw('COUNT(*) as sessions'), ) ->groupBy('period') @@ -602,6 +602,9 @@ class AnalyticsQueryService public function eventsOverview(int $limit = 50): Collection { + $driver = DB::connection()->getDriverName(); + $castType = $driver === 'sqlite' ? 'REAL' : 'DECIMAL(10,2)'; + return \App\Models\Event::where('website_id', $this->website->id) ->whereBetween('occurred_at', [$this->from, $this->to]) ->select( @@ -609,7 +612,7 @@ class AnalyticsQueryService 'category', DB::raw('COUNT(*) as total'), DB::raw('COUNT(DISTINCT visitor_id) as unique_visitors'), - DB::raw('SUM(CAST(value AS REAL)) as total_value'), + DB::raw("SUM(CAST(value AS {$castType})) as total_value"), ) ->groupBy('name', 'category') ->orderByDesc('total') @@ -621,13 +624,16 @@ class AnalyticsQueryService public function eventDetail(string $eventName): Collection { + $driver = DB::connection()->getDriverName(); + $castType = $driver === 'sqlite' ? 'REAL' : 'DECIMAL(10,2)'; + return \App\Models\Event::where('website_id', $this->website->id) ->whereBetween('occurred_at', [$this->from, $this->to]) ->where('name', $eventName) ->select( 'action', 'label', DB::raw('COUNT(*) as total'), - DB::raw('SUM(CAST(value AS REAL)) as total_value'), + DB::raw("SUM(CAST(value AS {$castType})) as total_value"), ) ->groupBy('action', 'label') ->orderByDesc('total') @@ -646,7 +652,7 @@ class AnalyticsQueryService ->whereBetween('occurred_at', [$this->from, $this->to]) ->where('name', $eventName) ->select( - DB::raw("strftime('{$groupFormat}', occurred_at) as period"), + DB::raw($this->dateFormat($groupFormat, 'occurred_at').' as period'), DB::raw('COUNT(*) as total'), ) ->groupBy('period') @@ -685,6 +691,28 @@ class AnalyticsQueryService ->get(); } + /** + * Get the appropriate date formatting function for the current database driver. + */ + private function dateFormat(string $format, string $column): string + { + $driver = DB::connection()->getDriverName(); + + if ($driver === 'sqlite') { + return "strftime('{$format}', {$column})"; + } + + // MySQL/MariaDB + // Convert SQLite strftime format to MySQL DATE_FORMAT + $mysqlFormat = str_replace( + ['%Y', '%m', '%d', '%H', '%M', '%S'], + ['%Y', '%m', '%d', '%H', '%i', '%s'], + $format + ); + + return "DATE_FORMAT({$column}, '{$mysqlFormat}')"; + } + // ---- Conversions ---- public function conversionsReport(): Collection @@ -722,7 +750,7 @@ class AnalyticsQueryService $data = \App\Models\GoalConversion::where('website_id', $this->website->id) ->whereBetween('converted_at', [$this->from, $this->to]) ->select( - DB::raw("strftime('{$groupFormat}', converted_at) as period"), + DB::raw($this->dateFormat($groupFormat, 'converted_at').' as period'), DB::raw('COUNT(*) as completions'), ) ->groupBy('period') diff --git a/package-lock.json b/package-lock.json index 0ab22e0..2e50c8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "test_app_one", + "name": "html", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/routes/auth.php b/routes/auth.php index 3926ecf..d4e900c 100644 --- a/routes/auth.php +++ b/routes/auth.php @@ -47,10 +47,10 @@ Route::middleware('auth')->group(function () { ->middleware('throttle:6,1') ->name('verification.send'); - Route::get('confirm-password', [ConfirmablePasswordController::class, 'show']) + /*Route::get('confirm-password', [ConfirmablePasswordController::class, 'show']) ->name('password.confirm'); - Route::post('confirm-password', [ConfirmablePasswordController::class, 'store']); + Route::post('confirm-password', [ConfirmablePasswordController::class, 'store']);*/ Route::put('password', [PasswordController::class, 'update'])->name('password.update');