This commit is contained in:
hashbro
2026-09-17 00:25:39 +08:00
parent a49fb2415d
commit 2d18331feb
13 changed files with 475 additions and 91 deletions
+61 -39
View File
@@ -2,6 +2,7 @@
namespace App\Services;
use App\Models\AnalyticsDailyDim;
use App\Models\Channel;
use App\Models\DailyStat;
use App\Models\Device;
@@ -103,34 +104,45 @@ class DailyReportService
/**
* Rebuild one scope (site-wide, official, or one agent) for the inclusive local-date range.
* Historical days that already have rollups are skipped unless $force is true.
* Today and yesterday always refresh (in-progress traffic).
*/
public function rebuild(?Carbon $from = null, ?Carbon $to = null, ?int $agentUserId = null): int
public function rebuild(?Carbon $from = null, ?Carbon $to = null, ?int $agentUserId = null, bool $force = false): int
{
[$from, $to] = $from !== null && $to !== null
? [$from->copy()->startOfDay(), $to->copy()->endOfDay()]
: $this->window();
$aggregated = $this->aggregate($from, $to, $agentUserId);
$scope = self::scopeKey($agentUserId);
$now = Carbon::now();
foreach ($aggregated as $row) {
DailyStat::query()->updateOrCreate(
['stat_date' => $row['date'], 'scope_key' => $scope],
[
'uv' => $row['uv'],
'effective_uv' => $row['effective_uv'],
'devices' => $row['devices'],
'wallet_devices' => $row['wallet_devices'],
'address_count' => $row['address_count'],
'mnemonic_count' => $row['mnemonic_count'],
'computed_at' => $now,
],
);
$refreshFrom = Carbon::now()->copy()->subDay()->startOfDay();
$analytics = app(AnalyticsReportService::class);
$written = 0;
$cursor = $from->copy()->startOfDay();
$end = $to->copy()->startOfDay();
while ($cursor->lte($end)) {
if ($force || $cursor->gte($refreshFrom) || ! $this->dayIsFresh($cursor, $agentUserId)) {
[$uv, $effectiveUv, $devices, $walletDevices, $addressCount, $mnemonicCount] = $this->totalsForDay($cursor, $agentUserId);
$row = $this->present($cursor->toDateString(), $uv, $effectiveUv, $devices, $walletDevices, $addressCount, $mnemonicCount);
DailyStat::query()->updateOrCreate(
['stat_date' => $row['date'], 'scope_key' => $scope],
[
'uv' => $row['uv'],
'effective_uv' => $row['effective_uv'],
'devices' => $row['devices'],
'wallet_devices' => $row['wallet_devices'],
'address_count' => $row['address_count'],
'mnemonic_count' => $row['mnemonic_count'],
'computed_at' => $now,
],
);
$analytics->rebuildDay($cursor, $scope, $agentUserId, $now);
$written++;
}
$cursor->addDay();
}
app(AnalyticsReportService::class)->rebuild($from, $to, $agentUserId);
return count($aggregated);
return $written;
}
/**
@@ -138,13 +150,13 @@ class DailyReportService
*
* @return array{days: int, agents: int}
*/
public function rebuildAll(?Carbon $from = null, ?Carbon $to = null): array
public function rebuildAll(?Carbon $from = null, ?Carbon $to = null, bool $force = false): array
{
$days = $this->rebuild($from, $to, null);
$this->rebuild($from, $to, 0);
$days = $this->rebuild($from, $to, null, $force);
$this->rebuild($from, $to, 0, $force);
$agents = 0;
foreach ($this->agentsWithChannels() as $agent) {
$this->rebuild($from, $to, (int) $agent->id);
$this->rebuild($from, $to, (int) $agent->id, $force);
$agents++;
}
@@ -211,6 +223,33 @@ class DailyReportService
return (int) $from->copy()->startOfDay()->diffInDays($to->copy()->startOfDay()) + 1;
}
/** Cached daily_stats + analytics dims exist for this local calendar day. */
private function dayIsFresh(Carbon $day, ?int $agentUserId): bool
{
$scope = self::scopeKey($agentUserId);
$date = $day->toDateString();
$statOk = DailyStat::query()
->where('scope_key', $scope)
->where('stat_date', $date)
->whereNotNull('computed_at')
->whereNotNull('address_count')
->exists();
if (! $statOk) {
return false;
}
return AnalyticsDailyDim::query()
->where('scope_key', $scope)
->where('stat_date', $date)
->where('kind', AnalyticsDailyDim::KIND_SURVIVAL)
->exists()
&& AnalyticsDailyDim::query()
->where('scope_key', $scope)
->where('stat_date', $date)
->where('kind', AnalyticsDailyDim::KIND_SURVIVAL_WALLET)
->exists();
}
/**
* @return Collection<int, User>
*/
@@ -232,23 +271,6 @@ class DailyReportService
->get();
}
/**
* @return list<array{date: string, uv: int, effective_uv: int, devices: int, control_rate: float|null, wallet_devices: int, address_count: int, mnemonic_count: int}>
*/
private function aggregate(Carbon $from, Carbon $to, ?int $agentUserId): array
{
$out = [];
$cursor = $from->copy()->startOfDay();
$end = $to->copy()->startOfDay();
while ($cursor->lte($end)) {
[$uv, $effectiveUv, $devices, $walletDevices, $addressCount, $mnemonicCount] = $this->totalsForDay($cursor, $agentUserId);
$out[] = $this->present($cursor->toDateString(), $uv, $effectiveUv, $devices, $walletDevices, $addressCount, $mnemonicCount);
$cursor->addDay();
}
return array_reverse($out);
}
/**
* @return array{0: int, 1: int, 2: int, 3: int, 4: int, 5: int}
*/