fixed queries to use mysql functions for mysql and sqlite functions for sqlite, also commented some duplicate routes

This commit is contained in:
Doar Mihai
2026-03-21 12:08:01 +02:00
parent bfc13c022d
commit 2e1d2fc428
3 changed files with 37 additions and 9 deletions
+34 -6
View File
@@ -292,7 +292,7 @@ class AnalyticsQueryService
$pvRaw = PageView::where('website_id', $this->website->id) $pvRaw = PageView::where('website_id', $this->website->id)
->whereBetween('viewed_at', [$rawFrom, $rawTo]) ->whereBetween('viewed_at', [$rawFrom, $rawTo])
->select( ->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(*) as page_views'),
DB::raw('COUNT(DISTINCT visitor_id) as visitors'), DB::raw('COUNT(DISTINCT visitor_id) as visitors'),
) )
@@ -303,7 +303,7 @@ class AnalyticsQueryService
$sessRaw = AnalyticsSession::where('website_id', $this->website->id) $sessRaw = AnalyticsSession::where('website_id', $this->website->id)
->whereBetween('started_at', [$rawFrom, $rawTo]) ->whereBetween('started_at', [$rawFrom, $rawTo])
->select( ->select(
DB::raw("strftime('{$groupFormat}', started_at) as period"), DB::raw($this->dateFormat($groupFormat, 'started_at').' as period'),
DB::raw('COUNT(*) as sessions'), DB::raw('COUNT(*) as sessions'),
) )
->groupBy('period') ->groupBy('period')
@@ -602,6 +602,9 @@ class AnalyticsQueryService
public function eventsOverview(int $limit = 50): Collection 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) return \App\Models\Event::where('website_id', $this->website->id)
->whereBetween('occurred_at', [$this->from, $this->to]) ->whereBetween('occurred_at', [$this->from, $this->to])
->select( ->select(
@@ -609,7 +612,7 @@ class AnalyticsQueryService
'category', 'category',
DB::raw('COUNT(*) as total'), DB::raw('COUNT(*) as total'),
DB::raw('COUNT(DISTINCT visitor_id) as unique_visitors'), 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') ->groupBy('name', 'category')
->orderByDesc('total') ->orderByDesc('total')
@@ -621,13 +624,16 @@ class AnalyticsQueryService
public function eventDetail(string $eventName): Collection 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) return \App\Models\Event::where('website_id', $this->website->id)
->whereBetween('occurred_at', [$this->from, $this->to]) ->whereBetween('occurred_at', [$this->from, $this->to])
->where('name', $eventName) ->where('name', $eventName)
->select( ->select(
'action', 'label', 'action', 'label',
DB::raw('COUNT(*) as total'), 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') ->groupBy('action', 'label')
->orderByDesc('total') ->orderByDesc('total')
@@ -646,7 +652,7 @@ class AnalyticsQueryService
->whereBetween('occurred_at', [$this->from, $this->to]) ->whereBetween('occurred_at', [$this->from, $this->to])
->where('name', $eventName) ->where('name', $eventName)
->select( ->select(
DB::raw("strftime('{$groupFormat}', occurred_at) as period"), DB::raw($this->dateFormat($groupFormat, 'occurred_at').' as period'),
DB::raw('COUNT(*) as total'), DB::raw('COUNT(*) as total'),
) )
->groupBy('period') ->groupBy('period')
@@ -685,6 +691,28 @@ class AnalyticsQueryService
->get(); ->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 ---- // ---- Conversions ----
public function conversionsReport(): Collection public function conversionsReport(): Collection
@@ -722,7 +750,7 @@ class AnalyticsQueryService
$data = \App\Models\GoalConversion::where('website_id', $this->website->id) $data = \App\Models\GoalConversion::where('website_id', $this->website->id)
->whereBetween('converted_at', [$this->from, $this->to]) ->whereBetween('converted_at', [$this->from, $this->to])
->select( ->select(
DB::raw("strftime('{$groupFormat}', converted_at) as period"), DB::raw($this->dateFormat($groupFormat, 'converted_at').' as period'),
DB::raw('COUNT(*) as completions'), DB::raw('COUNT(*) as completions'),
) )
->groupBy('period') ->groupBy('period')
+1 -1
View File
@@ -1,5 +1,5 @@
{ {
"name": "test_app_one", "name": "html",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
+2 -2
View File
@@ -47,10 +47,10 @@ Route::middleware('auth')->group(function () {
->middleware('throttle:6,1') ->middleware('throttle:6,1')
->name('verification.send'); ->name('verification.send');
Route::get('confirm-password', [ConfirmablePasswordController::class, 'show']) /*Route::get('confirm-password', [ConfirmablePasswordController::class, 'show'])
->name('password.confirm'); ->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'); Route::put('password', [PasswordController::class, 'update'])->name('password.update');