User Statistik
This commit is contained in:
parent
70240d2b6a
commit
53bdba33cd
24 changed files with 2633 additions and 9 deletions
127
app/Console/Commands/BackofficeStoreStatisticsSnapshots.php
Normal file
127
app/Console/Commands/BackofficeStoreStatisticsSnapshots.php
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\BackofficeStatisticsSnapshot;
|
||||
use App\Services\Backoffice\BackofficeDashboardService;
|
||||
use App\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class BackofficeStoreStatisticsSnapshots extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'backoffice:store-statistics-snapshots
|
||||
{--user= : Nur einen bestimmten User berechnen (user_id)}
|
||||
{--month= : Nur einen bestimmten Monat berechnen}
|
||||
{--year= : Nur ein bestimmtes Jahr berechnen}
|
||||
{--force : Bereits vorhandene Snapshots überschreiben}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Speichert Backoffice-Statistik-Snapshots fuer abgeschlossene Monate';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(BackofficeDashboardService $dashboardService): int
|
||||
{
|
||||
$months = $this->monthsToStore();
|
||||
$force = (bool) $this->option('force');
|
||||
|
||||
if ($months === []) {
|
||||
$this->info('Keine abgeschlossenen Monate zum Speichern.');
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$userQuery = User::query()
|
||||
->where('admin', '>=', 1)
|
||||
->where('admin', '<', 4)
|
||||
->whereNull('deleted_at');
|
||||
|
||||
if ($userId = $this->option('user')) {
|
||||
$userQuery->where('id', $userId);
|
||||
}
|
||||
|
||||
$users = $userQuery->get();
|
||||
$this->info('Berechne Backoffice-Snapshots fuer '.$users->count().' User und '.count($months).' Monate...');
|
||||
|
||||
$bar = $this->output->createProgressBar($users->count());
|
||||
$bar->start();
|
||||
|
||||
$stored = 0;
|
||||
$skipped = 0;
|
||||
|
||||
foreach ($users as $user) {
|
||||
foreach ($months as [$year, $month]) {
|
||||
$exists = BackofficeStatisticsSnapshot::query()
|
||||
->where('user_id', $user->id)
|
||||
->where('year', $year)
|
||||
->where('month', $month)
|
||||
->exists();
|
||||
|
||||
if ($exists && ! $force) {
|
||||
$skipped++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$dashboardService->storeSnapshot($user, $month, $year);
|
||||
$stored++;
|
||||
}
|
||||
|
||||
$bar->advance();
|
||||
gc_collect_cycles();
|
||||
}
|
||||
|
||||
$bar->finish();
|
||||
$this->newLine();
|
||||
$this->info("Fertig. Gespeichert: {$stored}, uebersprungen: {$skipped}");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<array{int, int}>
|
||||
*/
|
||||
private function monthsToStore(): array
|
||||
{
|
||||
$monthOption = $this->option('month');
|
||||
$yearOption = $this->option('year');
|
||||
|
||||
if ($monthOption && $yearOption) {
|
||||
$month = max(1, min(12, (int) $monthOption));
|
||||
$year = (int) $yearOption;
|
||||
|
||||
if (! $this->isClosedMonth($month, $year)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [[$year, $month]];
|
||||
}
|
||||
|
||||
$months = [];
|
||||
$cursor = Carbon::create(2026, 1, 1)->startOfMonth();
|
||||
$lastClosedMonth = now()->startOfMonth()->subMonth();
|
||||
|
||||
while ($cursor->lte($lastClosedMonth)) {
|
||||
$months[] = [(int) $cursor->year, (int) $cursor->month];
|
||||
$cursor->addMonth();
|
||||
}
|
||||
|
||||
return $months;
|
||||
}
|
||||
|
||||
private function isClosedMonth(int $month, int $year): bool
|
||||
{
|
||||
return Carbon::create($year, $month, 1)->endOfMonth()->lt(now()->startOfMonth());
|
||||
}
|
||||
}
|
||||
|
|
@ -55,6 +55,9 @@ class Kernel extends ConsoleKernel
|
|||
// Abo-Chart-Snapshots: vergangene Monate einfrieren (nach allen Abo-Jobs)
|
||||
$schedule->command('abo:store-chart-snapshots')->dailyAt('04:30');
|
||||
|
||||
// Backoffice-Statistik-Snapshots: abgeschlossene Monate fuer VIP-Statistiken einfrieren
|
||||
$schedule->command('backoffice:store-statistics-snapshots')->dailyAt('04:45');
|
||||
|
||||
// Incentive: Punkteberechnung täglich nach business:store-optimized
|
||||
$schedule->command('incentive:calculate')->dailyAt('05:00');
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue