|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\UserActivityStat; |
|
6
|
|
|
use App\Models\UserDownload; |
|
7
|
|
|
use App\Models\UserRequest; |
|
8
|
|
|
use Carbon\Carbon; |
|
9
|
|
|
use Illuminate\Console\Command; |
|
10
|
|
|
|
|
11
|
|
|
class BackfillUserActivityStats extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The name and signature of the console command. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $signature = 'nntmux:backfill-user-activity-stats |
|
19
|
|
|
{--days=30 : Number of days to backfill} |
|
20
|
|
|
{--force : Force backfill even if data already exists}'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The console command description. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $description = 'Backfill user activity stats from existing user_downloads and user_requests data'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Execute the console command. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function handle(): int |
|
33
|
|
|
{ |
|
34
|
|
|
$days = (int) $this->option('days'); |
|
35
|
|
|
$force = $this->option('force'); |
|
36
|
|
|
|
|
37
|
|
|
$this->info("Backfilling user activity stats for the last {$days} days..."); |
|
38
|
|
|
|
|
39
|
|
|
$startDate = Carbon::now()->subDays($days - 1)->startOfDay(); |
|
|
|
|
|
|
40
|
|
|
$progressBar = $this->output->createProgressBar($days); |
|
41
|
|
|
|
|
42
|
|
|
$statsCollected = 0; |
|
43
|
|
|
$statsSkipped = 0; |
|
44
|
|
|
|
|
45
|
|
|
for ($i = $days - 1; $i >= 0; $i--) { |
|
46
|
|
|
$date = Carbon::now()->subDays($i)->format('Y-m-d'); |
|
47
|
|
|
|
|
48
|
|
|
// Check if stats already exist for this date |
|
49
|
|
|
if (! $force && UserActivityStat::where('stat_date', $date)->exists()) { |
|
50
|
|
|
$statsSkipped++; |
|
51
|
|
|
$progressBar->advance(); |
|
52
|
|
|
|
|
53
|
|
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Count downloads for the date |
|
57
|
|
|
$downloadsCount = UserDownload::query() |
|
58
|
|
|
->whereRaw('DATE(timestamp) = ?', [$date]) |
|
59
|
|
|
->count(); |
|
60
|
|
|
|
|
61
|
|
|
// Count API hits for the date |
|
62
|
|
|
$apiHitsCount = UserRequest::query() |
|
63
|
|
|
->whereRaw('DATE(timestamp) = ?', [$date]) |
|
64
|
|
|
->count(); |
|
65
|
|
|
|
|
66
|
|
|
// Store or update the stats |
|
67
|
|
|
UserActivityStat::updateOrCreate( |
|
68
|
|
|
['stat_date' => $date], |
|
69
|
|
|
[ |
|
70
|
|
|
'downloads_count' => $downloadsCount, |
|
71
|
|
|
'api_hits_count' => $apiHitsCount, |
|
72
|
|
|
] |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$statsCollected++; |
|
76
|
|
|
$progressBar->advance(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$progressBar->finish(); |
|
80
|
|
|
$this->newLine(2); |
|
81
|
|
|
|
|
82
|
|
|
$this->info('Backfill complete!'); |
|
83
|
|
|
$this->info("Stats collected: {$statsCollected}"); |
|
84
|
|
|
if ($statsSkipped > 0) { |
|
85
|
|
|
$this->info("Stats skipped (already existed): {$statsSkipped}"); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return Command::SUCCESS; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|