fix: link old
This commit is contained in:
@@ -0,0 +1,447 @@
|
||||
<?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.' 小时';
|
||||
}
|
||||
}
|
||||
@@ -128,6 +128,8 @@ class DailyReportService
|
||||
);
|
||||
}
|
||||
|
||||
app(AnalyticsReportService::class)->rebuild($from, $to, $agentUserId);
|
||||
|
||||
return count($aggregated);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,9 +40,6 @@ class DashboardStatsService
|
||||
* balances: array{usdt: string, trx: string, eth: string, btc: string, bnb: string},
|
||||
* transfer_count: int,
|
||||
* transfers: array{usdt: string, trx: string, eth: string, btc: string},
|
||||
* 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}>,
|
||||
* range: string,
|
||||
* from: string,
|
||||
* to: string
|
||||
@@ -134,63 +131,13 @@ class DashboardStatsService
|
||||
"COUNT(*) as pv,
|
||||
COUNT(DISTINCT client_uid) as uv,
|
||||
COALESCE(SUM(CASE WHEN ({$effectiveSql}) THEN 1 ELSE 0 END), 0) as effective_pv,
|
||||
COUNT(DISTINCT CASE WHEN ({$effectiveSql}) THEN client_uid END) as effective_uv,
|
||||
COUNT(DISTINCT CASE WHEN os = 'iOS' THEN client_uid END) as ios_uv,
|
||||
COUNT(DISTINCT CASE WHEN os IS NULL OR os != 'iOS' THEN client_uid END) as other_uv",
|
||||
COUNT(DISTINCT CASE WHEN ({$effectiveSql}) THEN client_uid END) as effective_uv",
|
||||
)
|
||||
->first();
|
||||
$pv = (int) ($visitAgg?->pv ?? 0);
|
||||
$uv = (int) ($visitAgg?->uv ?? 0);
|
||||
$effectivePv = (int) ($visitAgg?->effective_pv ?? 0);
|
||||
$effectiveUv = (int) ($visitAgg?->effective_uv ?? 0);
|
||||
$iosUv = (int) ($visitAgg?->ios_uv ?? 0);
|
||||
$otherUv = (int) ($visitAgg?->other_uv ?? 0);
|
||||
|
||||
$iosVisits = (clone $visits)->where('os', 'iOS');
|
||||
|
||||
$byOs = [
|
||||
$this->visitRow('iOS', $iosUv, $uv),
|
||||
$this->visitRow('其他', $otherUv, $uv),
|
||||
];
|
||||
|
||||
$versionGroups = (clone $iosVisits)
|
||||
->select('os_version')
|
||||
->selectRaw('COUNT(DISTINCT client_uid) as uv')
|
||||
->groupBy('os_version')
|
||||
->orderByDesc('uv')
|
||||
->limit(20)
|
||||
->get();
|
||||
// Denominator = sum of per-version UVs (not global DISTINCT).
|
||||
// Same visitor may appear under multiple versions; using DISTINCT total would
|
||||
// make every overlapping bucket show ~100%.
|
||||
$versionUvSum = (int) $versionGroups->sum('uv');
|
||||
$byIosVersion = $versionGroups
|
||||
->map(fn ($r) => $this->visitRow(
|
||||
((string) ($r->os_version ?? '')) !== '' ? (string) $r->os_version : 'Unknown',
|
||||
(int) $r->uv,
|
||||
$versionUvSum > 0 ? $versionUvSum : $iosUv,
|
||||
))
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$byDeviceIosVersion = (clone $createdInRange)
|
||||
->select('ios_version')
|
||||
->selectRaw('COUNT(*) as cnt')
|
||||
->groupBy('ios_version')
|
||||
->orderByDesc('cnt')
|
||||
->limit(20)
|
||||
->get()
|
||||
->map(function ($r) use ($total) {
|
||||
$label = trim((string) ($r->ios_version ?? ''));
|
||||
|
||||
return $this->deviceRow(
|
||||
$label !== '' ? $label : '未知',
|
||||
(int) $r->cnt,
|
||||
$total,
|
||||
);
|
||||
})
|
||||
->values()
|
||||
->all();
|
||||
|
||||
return [
|
||||
'total' => $total,
|
||||
@@ -206,9 +153,6 @@ class DashboardStatsService
|
||||
'balances' => $balances,
|
||||
'transfer_count' => $transferCount,
|
||||
'transfers' => $transfers,
|
||||
'by_os' => $byOs,
|
||||
'by_ios_version' => $byIosVersion,
|
||||
'by_device_ios_version' => $byDeviceIosVersion,
|
||||
'range' => $rangeLabel,
|
||||
'from' => $from->toDateTimeString(),
|
||||
'to' => $to->toDateTimeString(),
|
||||
@@ -480,33 +424,4 @@ class DashboardStatsService
|
||||
return [$major, $minor, $patch];
|
||||
}
|
||||
|
||||
/**
|
||||
* Visit breakdown row. pct = uv / $totalUv (UV share).
|
||||
*
|
||||
* @return array{label: string, uv: int, pct: float}
|
||||
*/
|
||||
private function visitRow(string $label, int $uv, int $totalUv): array
|
||||
{
|
||||
$pct = $totalUv > 0 ? round(($uv / $totalUv) * 100, 1) : 0.0;
|
||||
|
||||
return [
|
||||
'label' => $label,
|
||||
'uv' => $uv,
|
||||
'pct' => $pct,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{label: string, count: int, pct: float}
|
||||
*/
|
||||
private function deviceRow(string $label, int $count, int $total): array
|
||||
{
|
||||
$pct = $total > 0 ? round(($count / $total) * 100, 1) : 0.0;
|
||||
|
||||
return [
|
||||
'label' => $label,
|
||||
'count' => $count,
|
||||
'pct' => $pct,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Photo;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class PhotoOrigin
|
||||
{
|
||||
public function token(): string
|
||||
{
|
||||
return trim((string) config('coruna.photo_origin.token', ''));
|
||||
}
|
||||
|
||||
public function baseUrl(): string
|
||||
{
|
||||
return rtrim(trim((string) config('coruna.photo_origin.url', '')), '/');
|
||||
}
|
||||
|
||||
public function pullEnabled(): bool
|
||||
{
|
||||
return $this->baseUrl() !== '' && $this->token() !== '';
|
||||
}
|
||||
|
||||
public function serveEnabled(): bool
|
||||
{
|
||||
return $this->token() !== '';
|
||||
}
|
||||
|
||||
public function tokenMatches(?string $provided): bool
|
||||
{
|
||||
$expected = $this->token();
|
||||
if ($expected === '' || ! is_string($provided) || $provided === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return hash_equals($expected, $provided);
|
||||
}
|
||||
|
||||
public function isSafeRelPath(string $path): bool
|
||||
{
|
||||
$path = str_replace('\\', '/', $path);
|
||||
if ($path === '' || str_contains($path, '..')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return str_starts_with($path, 'c2/photos/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the local file when present; otherwise pull from the old host and write through.
|
||||
*/
|
||||
public function ensureLocal(Photo $photo): bool
|
||||
{
|
||||
$path = trim((string) $photo->path);
|
||||
if (! $this->isSafeRelPath($path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$disk = Storage::disk('local');
|
||||
if ($disk->exists($path)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $this->pullEnabled()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$resp = Http::timeout(max(5, (int) config('coruna.photo_origin.timeout', 60)))
|
||||
->withHeaders(['X-Photo-Origin-Token' => $this->token()])
|
||||
->get($this->baseUrl().'/hooks/photo-origin/'.$photo->id);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('photo origin pull failed', [
|
||||
'photo_id' => $photo->id,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $resp->successful()) {
|
||||
Log::info('photo origin pull miss', [
|
||||
'photo_id' => $photo->id,
|
||||
'status' => $resp->status(),
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$bytes = $resp->body();
|
||||
if ($bytes === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$disk->put($path, $bytes);
|
||||
|
||||
return $disk->exists($path);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user