Skip to content

Commit

Permalink
Fix wrong "this year" statistic
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasss93 committed Jan 28, 2025
1 parent 8f47507 commit 27b0a23
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions app/Enums/Stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function data(): array
'yesterday' => Statistic::optimizedStickers()->ofYesterday()->count(),
'last_7_days' => Statistic::optimizedStickers()->ofLast7Days()->count(),
'last_30_days' => Statistic::optimizedStickers()->ofLast30Days()->count(),
'year' => Statistic::optimizedStickers()->ofLastYear()->count(),
'year' => Statistic::optimizedStickers()->ofThisYear()->count(),
'total' => Statistic::optimizedStickers()->count(),
'last_update' => now()->format('Y-m-d H:i:s e'),
],
Expand All @@ -56,7 +56,7 @@ public function data(): array
'yesterday' => Statistic::optimizedVideos()->ofYesterday()->count(),
'last_7_days' => Statistic::optimizedVideos()->ofLast7Days()->count(),
'last_30_days' => Statistic::optimizedVideos()->ofLast30Days()->count(),
'year' => Statistic::optimizedVideos()->ofLastYear()->count(),
'year' => Statistic::optimizedVideos()->ofThisYear()->count(),
'total' => Statistic::optimizedVideos()->count(),
'last_update' => now()->format('Y-m-d H:i:s e'),
],
Expand All @@ -65,7 +65,7 @@ public function data(): array
'yesterday' => Statistic::userActions()->ofYesterday()->count('chat_id'),
'last_7_days' => Statistic::userActions()->ofLast7Days()->count('chat_id'),
'last_30_days' => Statistic::userActions()->ofLast30Days()->count('chat_id'),
'year' => Statistic::userActions()->ofLastYear()->count('chat_id'),
'year' => Statistic::userActions()->ofThisYear()->count('chat_id'),
'total' => null,
'last_update' => now()->format('Y-m-d H:i:s e'),
],
Expand All @@ -74,7 +74,7 @@ public function data(): array
'yesterday' => Chat::ofYesterday()->count(),
'last_7_days' => Chat::ofLast7Days()->count(),
'last_30_days' => Chat::ofLast30Days()->count(),
'year' => Chat::ofLastYear()->count(),
'year' => Chat::ofThisYear()->count(),
'total' => Chat::count(),
'last_update' => now()->format('Y-m-d H:i:s e'),
],
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Support\ExtraDateScopes;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand Down Expand Up @@ -43,6 +44,7 @@ class Chat extends Model
use HasFactory;
use HasSettingsTable;
use DateScopes;
use ExtraDateScopes;

protected $primaryKey = 'chat_id';
protected $keyType = 'string';
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Statistic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Support\ExtraDateScopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -10,6 +11,7 @@
class Statistic extends Model
{
use DateScopes;
use ExtraDateScopes;

public $timestamps = false;
protected static $unguarded = true;
Expand Down
22 changes: 22 additions & 0 deletions app/Support/ExtraDateScopes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Support;

use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
* @method static Builder ofThisYear()
* @mixin Model
*/
trait ExtraDateScopes
{
public function scopeOfThisYear(Builder $query): Builder
{
$createdColumnName = self::CREATED_AT !== 'created_at' ? self::CREATED_AT : config('date-scopes.created_column');
$now = CarbonImmutable::now();

return $query->whereBetween($createdColumnName, [$now->startOfYear(), $now->endOfYear()]);
}
}

0 comments on commit 27b0a23

Please sign in to comment.