448 lines
16 KiB
PHP
448 lines
16 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\AnalyticsDailyDim;
|
|
use App\Models\Device;
|
|
use App\Models\PageVisit;
|
|
use App\Models\User;
|
|
use App\Support\AgentScope;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class AnalyticsReportService
|
|
{
|
|
public function __construct(
|
|
private readonly DailyReportService $reports,
|
|
private readonly DashboardStatsService $dashboard,
|
|
) {}
|
|
|
|
/**
|
|
* @param array{date_from?: string, date_to?: string, date_range?: string, range?: string, agent_user_id?: ?int} $filters
|
|
* @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<array{label: string, uv: int, pct: float}>,
|
|
* by_ios_version: list<array{label: string, uv: int, pct: float}>,
|
|
* by_device_ios_version: list<array{label: string, count: int, pct: float}>,
|
|
* control_by_version: list<array{label: string, effective_uv: int, devices: int, pct: float|null}>
|
|
* }
|
|
*/
|
|
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): int
|
|
{
|
|
[$from, $to] = $from !== null && $to !== null
|
|
? [$from->copy()->startOfDay(), $to->copy()->endOfDay()]
|
|
: $this->reports->window();
|
|
|
|
$scope = DailyReportService::scopeKey($agentUserId);
|
|
$now = Carbon::now();
|
|
$days = 0;
|
|
$cursor = $from->copy()->startOfDay();
|
|
$end = $to->copy()->startOfDay();
|
|
while ($cursor->lte($end)) {
|
|
$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;
|
|
$have = $this->cachedDays($scope, AnalyticsDailyDim::KIND_SURVIVAL, $from, $to);
|
|
$haveWallet = $this->cachedDays($scope, AnalyticsDailyDim::KIND_SURVIVAL_WALLET, $from, $to);
|
|
if ($have < $expected || $haveWallet < $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);
|
|
}
|
|
}
|
|
|
|
private 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<array{kind: string, dim: string, uv: int, effective_uv: int, devices: int, seconds_sum: int}>
|
|
*/
|
|
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,
|
|
);
|
|
}
|
|
|
|
$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<array{label: string, uv: int, pct: float}>,
|
|
* by_ios_version: list<array{label: string, uv: int, pct: float}>,
|
|
* by_device_ios_version: list<array{label: string, count: int, pct: float}>,
|
|
* control_by_version: list<array{label: string, effective_uv: int, devices: int, pct: float|null}>
|
|
* }
|
|
*/
|
|
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 = [];
|
|
$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_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),
|
|
'control_by_version' => $control,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string, int> $versions
|
|
* @return list<array{label: string, uv: int, pct: float}>
|
|
*/
|
|
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<string, int> $versions
|
|
* @return list<array{label: string, count: int, pct: float}>
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* @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 versionDimSql(string $column): string
|
|
{
|
|
return "CASE WHEN {$column} IS NULL OR TRIM({$column}) = '' THEN '未知' ELSE 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.' 小时';
|
|
}
|
|
}
|