, * by_ios_version: list, * by_device_ios_version: list, * by_visit_country: list, * by_device_country: list, * control_by_version: list * } */ public function collect(?User $agent, array $filters = []): array { [$from, $to] = $this->reports->resolveBounds($filters); $agentUserId = $this->reports->resolveAgentUserId($agent, $filters); $this->ensureCached($from, $to, $agentUserId); return $this->present($from, $to, $agentUserId); } 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->reports->window(); $scope = DailyReportService::scopeKey($agentUserId); $now = Carbon::now(); $refreshFrom = Carbon::now()->copy()->subDay()->startOfDay(); $days = 0; $cursor = $from->copy()->startOfDay(); $end = $to->copy()->startOfDay(); while ($cursor->lte($end)) { if ($force || $cursor->gte($refreshFrom) || ! $this->dayHasSurvival($scope, $cursor)) { $this->rebuildDay($cursor, $scope, $agentUserId, $now); $days++; } $cursor->addDay(); } return $days; } private function ensureCached(Carbon $from, Carbon $to, ?int $agentUserId): void { $scope = DailyReportService::scopeKey($agentUserId); $expected = (int) $from->copy()->startOfDay()->diffInDays($to->copy()->startOfDay()) + 1; // 一次查询拿到范围内每个 kind 的去重天数,避免逐 kind COUNT。 $have = AnalyticsDailyDim::query() ->where('scope_key', $scope) ->whereBetween('stat_date', [$from->toDateString(), $to->toDateString()]) ->selectRaw('kind, COUNT(DISTINCT stat_date) AS days') ->groupBy('kind') ->pluck('days', 'kind') ->all(); // 检查所有按日聚合的 kind 是否都有完整缓存;任一缺失即重建。 $requiredKinds = [ AnalyticsDailyDim::KIND_SURVIVAL, AnalyticsDailyDim::KIND_SURVIVAL_WALLET, AnalyticsDailyDim::KIND_VISIT_OS, AnalyticsDailyDim::KIND_VISIT_VERSION, AnalyticsDailyDim::KIND_DEVICE_VERSION, AnalyticsDailyDim::KIND_VISIT_COUNTRY, AnalyticsDailyDim::KIND_DEVICE_COUNTRY, ]; foreach ($requiredKinds as $kind) { if ((int) ($have[$kind] ?? 0) < $expected) { $this->rebuild($from, $to, $agentUserId); return; } } $today = Carbon::now()->toDateString(); if ($from->toDateString() > $today || $to->toDateString() < $today) { return; } $row = AnalyticsDailyDim::query() ->where('scope_key', $scope) ->where('kind', AnalyticsDailyDim::KIND_SURVIVAL) ->where('stat_date', $today) ->first(); if ($row === null || $row->computed_at === null || $row->computed_at->lt(Carbon::now()->subMinutes(DailyReportService::TODAY_STALE_MINUTES))) { $this->rebuild(Carbon::now()->startOfDay(), Carbon::now()->endOfDay(), $agentUserId); } } public function rebuildDay(Carbon $day, string $scope, ?int $agentUserId, Carbon $now): void { $from = $day->copy()->startOfDay(); $to = $day->copy()->endOfDay(); $date = $day->toDateString(); $rows = $this->aggregateDay($from, $to, $agentUserId); AnalyticsDailyDim::query() ->where('scope_key', $scope) ->where('stat_date', $date) ->delete(); foreach ($rows as $row) { AnalyticsDailyDim::query()->create([ 'stat_date' => $date, 'scope_key' => $scope, 'kind' => $row['kind'], 'dim' => $row['dim'], 'uv' => $row['uv'], 'effective_uv' => $row['effective_uv'], 'devices' => $row['devices'], 'seconds_sum' => $row['seconds_sum'], 'computed_at' => $now, ]); } } /** * @return list */ private function aggregateDay(Carbon $from, Carbon $to, ?int $agentUserId): array { $out = []; $effectiveSql = $this->dashboard->effectiveVisitSql(); $versionExpr = $this->versionDimSql('os_version'); $visits = PageVisit::query(); AgentScope::applyChannelIdAgentUserFilter($visits, $agentUserId); $visits->whereBetween('created_at', [$from, $to]); $osRows = (clone $visits) ->selectRaw("CASE WHEN os = 'iOS' THEN 'iOS' ELSE '其他' END as dim, COUNT(DISTINCT client_uid) as uv") ->groupByRaw("CASE WHEN os = 'iOS' THEN 'iOS' ELSE '其他' END") ->get(); foreach ($osRows as $row) { $out[] = $this->dimRow( AnalyticsDailyDim::KIND_VISIT_OS, (string) $row->dim, (int) $row->uv, ); } $versionRows = (clone $visits) ->where('os', 'iOS') ->selectRaw( "{$versionExpr} as dim, COUNT(DISTINCT client_uid) as uv, COUNT(DISTINCT CASE WHEN ({$effectiveSql}) THEN client_uid END) as effective_uv" ) ->groupByRaw($versionExpr) ->get(); foreach ($versionRows as $row) { $out[] = $this->dimRow( AnalyticsDailyDim::KIND_VISIT_VERSION, $this->clipDim((string) $row->dim), (int) $row->uv, (int) $row->effective_uv, ); } $devices = Device::query(); AgentScope::applyAgentUserFilter($devices, $agentUserId); $devices->whereBetween('created_at', [$from, $to]); $deviceVersionExpr = $this->versionDimSql('ios_version'); $deviceRows = (clone $devices) ->selectRaw("{$deviceVersionExpr} as dim, COUNT(*) as cnt") ->groupByRaw($deviceVersionExpr) ->get(); foreach ($deviceRows as $row) { $out[] = $this->dimRow( AnalyticsDailyDim::KIND_DEVICE_VERSION, $this->clipDim((string) $row->dim), 0, 0, (int) $row->cnt, ); } $countryExpr = $this->countryDimSql('country'); $visitCountryRows = (clone $visits) ->selectRaw("{$countryExpr} as dim, COUNT(DISTINCT client_uid) as uv") ->groupByRaw($countryExpr) ->get(); foreach ($visitCountryRows as $row) { $out[] = $this->dimRow( AnalyticsDailyDim::KIND_VISIT_COUNTRY, $this->clipDim((string) $row->dim), (int) $row->uv, ); } $deviceCountryRows = (clone $devices) ->selectRaw("{$countryExpr} as dim, COUNT(*) as cnt") ->groupByRaw($countryExpr) ->get(); foreach ($deviceCountryRows as $row) { $out[] = $this->dimRow( AnalyticsDailyDim::KIND_DEVICE_COUNTRY, $this->clipDim((string) $row->dim), 0, 0, (int) $row->cnt, ); } $secondsExpr = $this->survivalSecondsSql(); $survival = (clone $devices) ->selectRaw("COUNT(*) as cnt, COALESCE(SUM({$secondsExpr}), 0) as seconds_sum") ->first(); $out[] = $this->dimRow( AnalyticsDailyDim::KIND_SURVIVAL, '', 0, 0, (int) ($survival?->cnt ?? 0), (int) ($survival?->seconds_sum ?? 0), ); $walletSurvival = (clone $devices) ->where('has_wallet', Device::WALLET_YES) ->selectRaw("COUNT(*) as cnt, COALESCE(SUM({$secondsExpr}), 0) as seconds_sum") ->first(); $out[] = $this->dimRow( AnalyticsDailyDim::KIND_SURVIVAL_WALLET, '', 0, 0, (int) ($walletSurvival?->cnt ?? 0), (int) ($walletSurvival?->seconds_sum ?? 0), ); return $out; } /** * @return array{ * from: string, * to: string, * computed_at: ?string, * survival: array{avg_seconds: int|null, avg_label: string, devices: int}, * survival_wallet: array{avg_seconds: int|null, avg_label: string, devices: int}, * by_os: list, * by_ios_version: list, * by_device_ios_version: list, * by_visit_country: list, * by_device_country: list, * control_by_version: list * } */ private function present(Carbon $from, Carbon $to, ?int $agentUserId): array { $scope = DailyReportService::scopeKey($agentUserId); $rows = AnalyticsDailyDim::query() ->where('scope_key', $scope) ->whereBetween('stat_date', [$from->toDateString(), $to->toDateString()]) ->get(); $osUv = [AnalyticsDailyDim::DIM_IOS => 0, AnalyticsDailyDim::DIM_OTHER => 0]; $visitVersions = []; $visitEffective = []; $deviceVersions = []; $visitCountries = []; $deviceCountries = []; $survivalDevices = 0; $survivalSeconds = 0; $walletDevices = 0; $walletSeconds = 0; $computedAt = null; foreach ($rows as $row) { if ($row->computed_at !== null && ($computedAt === null || $row->computed_at->gt($computedAt))) { $computedAt = $row->computed_at; } if ($row->kind === AnalyticsDailyDim::KIND_VISIT_OS) { $osUv[$row->dim] = ($osUv[$row->dim] ?? 0) + (int) $row->uv; } elseif ($row->kind === AnalyticsDailyDim::KIND_VISIT_VERSION) { $visitVersions[$row->dim] = ($visitVersions[$row->dim] ?? 0) + (int) $row->uv; $visitEffective[$row->dim] = ($visitEffective[$row->dim] ?? 0) + (int) $row->effective_uv; } elseif ($row->kind === AnalyticsDailyDim::KIND_DEVICE_VERSION) { $deviceVersions[$row->dim] = ($deviceVersions[$row->dim] ?? 0) + (int) $row->devices; } elseif ($row->kind === AnalyticsDailyDim::KIND_VISIT_COUNTRY) { $visitCountries[$row->dim] = ($visitCountries[$row->dim] ?? 0) + (int) $row->uv; } elseif ($row->kind === AnalyticsDailyDim::KIND_DEVICE_COUNTRY) { $deviceCountries[$row->dim] = ($deviceCountries[$row->dim] ?? 0) + (int) $row->devices; } elseif ($row->kind === AnalyticsDailyDim::KIND_SURVIVAL) { $survivalDevices += (int) $row->devices; $survivalSeconds += (int) $row->seconds_sum; } elseif ($row->kind === AnalyticsDailyDim::KIND_SURVIVAL_WALLET) { $walletDevices += (int) $row->devices; $walletSeconds += (int) $row->seconds_sum; } } $osTotal = $osUv[AnalyticsDailyDim::DIM_IOS] + $osUv[AnalyticsDailyDim::DIM_OTHER]; $visitUvSum = array_sum($visitVersions); $deviceTotal = array_sum($deviceVersions); $avgSeconds = $survivalDevices > 0 ? (int) round($survivalSeconds / $survivalDevices) : null; $walletAvgSeconds = $walletDevices > 0 ? (int) round($walletSeconds / $walletDevices) : null; $labels = array_values(array_unique([...array_keys($visitEffective), ...array_keys($deviceVersions)])); usort($labels, $this->versionSort(...)); $control = []; foreach ($labels as $label) { $effectiveUv = (int) ($visitEffective[$label] ?? 0); $devices = (int) ($deviceVersions[$label] ?? 0); $control[] = [ 'label' => $label, 'effective_uv' => $effectiveUv, 'devices' => $devices, 'pct' => $effectiveUv > 0 ? round($devices / $effectiveUv * 100, 1) : null, ]; } usort($control, function (array $a, array $b): int { return $b['effective_uv'] <=> $a['effective_uv'] ?: $b['devices'] <=> $a['devices'] ?: $this->versionSort($a['label'], $b['label']); }); return [ 'from' => $from->toDateString(), 'to' => $to->toDateString(), 'computed_at' => $computedAt?->toDateTimeString(), 'survival' => [ 'avg_seconds' => $avgSeconds, 'avg_label' => $this->formatDuration($avgSeconds), 'devices' => $survivalDevices, ], 'survival_wallet' => [ 'avg_seconds' => $walletAvgSeconds, 'avg_label' => $this->formatDuration($walletAvgSeconds), 'devices' => $walletDevices, ], 'by_os' => [ $this->visitRow(AnalyticsDailyDim::DIM_IOS, $osUv[AnalyticsDailyDim::DIM_IOS], $osTotal), $this->visitRow(AnalyticsDailyDim::DIM_OTHER, $osUv[AnalyticsDailyDim::DIM_OTHER], $osTotal), ], 'by_ios_version' => $this->visitVersionRows($visitVersions, $visitUvSum), 'by_device_ios_version' => $this->deviceVersionRows($deviceVersions, $deviceTotal), 'by_visit_country' => $this->countryRows($visitCountries, array_sum($visitCountries), 'uv'), 'by_device_country' => $this->countryRows($deviceCountries, array_sum($deviceCountries), 'count'), 'control_by_version' => $control, ]; } /** * @param array $versions * @return list */ private function visitVersionRows(array $versions, int $total): array { $labels = array_keys($versions); usort($labels, $this->versionSort(...)); $rows = []; foreach ($labels as $label) { $uv = (int) $versions[$label]; $rows[] = $this->visitRow($label, $uv, $total); } usort($rows, fn (array $a, array $b) => $b['uv'] <=> $a['uv'] ?: $this->versionSort($a['label'], $b['label'])); return $rows; } /** * @param array $versions * @return list */ private function deviceVersionRows(array $versions, int $total): array { $labels = array_keys($versions); usort($labels, $this->versionSort(...)); $rows = []; foreach ($labels as $label) { $count = (int) $versions[$label]; $pct = $total > 0 ? round(($count / $total) * 100, 1) : 0.0; $rows[] = [ 'label' => $label, 'count' => $count, 'pct' => $pct, ]; } usort($rows, fn (array $a, array $b) => $b['count'] <=> $a['count'] ?: $this->versionSort($a['label'], $b['label'])); return $rows; } /** * 按国家维度汇总,dim 存 ISO 代码,label 转中文名展示。 * * @param array $countries * @return list|list */ private function countryRows(array $countries, int $total, string $valueKey): array { $labels = array_keys($countries); usort($labels, function (string $a, string $b) use ($countries): int { return ($countries[$b] ?? 0) <=> ($countries[$a] ?? 0) ?: strcmp($a, $b); }); $rows = []; foreach ($labels as $code) { $value = (int) $countries[$code]; $label = $code === AnalyticsDailyDim::DIM_UNKNOWN ? AnalyticsDailyDim::DIM_UNKNOWN : CfIpCountry::label($code); $rows[] = [ 'label' => $label ?: $code, $valueKey => $value, 'pct' => $total > 0 ? round(($value / $total) * 100, 1) : 0.0, ]; } return $rows; } /** * @return array{label: string, uv: int, pct: float} */ private function visitRow(string $label, int $uv, int $total): array { return [ 'label' => $label, 'uv' => $uv, 'pct' => $total > 0 ? round(($uv / $total) * 100, 1) : 0.0, ]; } /** * @return array{kind: string, dim: string, uv: int, effective_uv: int, devices: int, seconds_sum: int} */ private function dimRow( string $kind, string $dim, int $uv = 0, int $effectiveUv = 0, int $devices = 0, int $secondsSum = 0, ): array { return [ 'kind' => $kind, 'dim' => $dim, 'uv' => $uv, 'effective_uv' => $effectiveUv, 'devices' => $devices, 'seconds_sum' => $secondsSum, ]; } private function cachedDays(string $scope, string $kind, Carbon $from, Carbon $to): int { return AnalyticsDailyDim::query() ->where('scope_key', $scope) ->where('kind', $kind) ->whereBetween('stat_date', [$from->toDateString(), $to->toDateString()]) ->count(); } private function dayHasSurvival(string $scope, Carbon $day): bool { $date = $day->toDateString(); 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(); } private function versionDimSql(string $column): string { return "CASE WHEN {$column} IS NULL OR TRIM({$column}) = '' THEN '未知' ELSE TRIM({$column}) END"; } private function countryDimSql(string $column): string { return "CASE WHEN {$column} IS NULL OR TRIM({$column}) = '' THEN '未知' ELSE UPPER(TRIM({$column})) END"; } private function survivalSecondsSql(): string { if (DB::connection()->getDriverName() === 'sqlite') { return "MAX(0, CAST(strftime('%s', updated_at) AS INTEGER) - CAST(strftime('%s', created_at) AS INTEGER))"; } return 'GREATEST(0, TIMESTAMPDIFF(SECOND, created_at, updated_at))'; } private function clipDim(string $dim): string { $dim = trim($dim); if ($dim === '') { return AnalyticsDailyDim::DIM_UNKNOWN; } return mb_substr($dim, 0, 64); } private function versionSort(string $a, string $b): int { if ($a === AnalyticsDailyDim::DIM_UNKNOWN) { return $b === AnalyticsDailyDim::DIM_UNKNOWN ? 0 : 1; } if ($b === AnalyticsDailyDim::DIM_UNKNOWN) { return -1; } $cmp = version_compare($a, $b); return $cmp === 0 ? strcmp($a, $b) : $cmp; } private function formatDuration(?int $seconds): string { if ($seconds === null) { return '—'; } $seconds = max(0, $seconds); if ($seconds < 60) { return $seconds.' 秒'; } if ($seconds < 3600) { return intdiv($seconds, 60).' 分钟'; } $days = intdiv($seconds, 86400); $hours = intdiv($seconds % 86400, 3600); $mins = intdiv($seconds % 3600, 60); if ($days > 0) { return $hours > 0 ? $days.' 天 '.$hours.' 小时' : $days.' 天'; } return $mins > 0 ? $hours.' 小时 '.$mins.' 分钟' : $hours.' 小时'; } }