fix: statisc
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\Http\Controllers\Concerns\PortalAware;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Device;
|
||||
use App\Models\PageVisit;
|
||||
use App\Models\User;
|
||||
use App\Support\AgentScope;
|
||||
use Carbon\Carbon;
|
||||
@@ -49,6 +50,49 @@ class DashboardController extends Controller
|
||||
$newCount = (clone $base)->whereBetween('created_at', [$from, $to])->count();
|
||||
$activeCount = (clone $base)->whereBetween('updated_at', [$from, $to])->count();
|
||||
|
||||
$visits = PageVisit::query();
|
||||
AgentScope::applyChannelIdScope($visits, $this->agent());
|
||||
if (! $this->isAgentPortal() && $agentUserId > 0) {
|
||||
AgentScope::applyChannelIdAgentUserFilter($visits, $agentUserId);
|
||||
}
|
||||
if ($channelId !== '') {
|
||||
$visits->where('channel_id', 'like', '%'.$channelId.'%');
|
||||
}
|
||||
$visits->whereBetween('created_at', [$from, $to]);
|
||||
|
||||
$pv = (clone $visits)->count();
|
||||
$uv = (int) (clone $visits)->selectRaw('COUNT(DISTINCT client_uid) as aggregate')->value('aggregate');
|
||||
|
||||
$byOs = (clone $visits)
|
||||
->select('os')
|
||||
->selectRaw('COUNT(*) as pv')
|
||||
->selectRaw('COUNT(DISTINCT client_uid) as uv')
|
||||
->groupBy('os')
|
||||
->orderByDesc('pv')
|
||||
->limit(10)
|
||||
->get()
|
||||
->map(static fn ($r) => [
|
||||
'label' => ((string) ($r->os ?? '')) !== '' ? (string) $r->os : 'Unknown',
|
||||
'pv' => (int) $r->pv,
|
||||
'uv' => (int) $r->uv,
|
||||
])
|
||||
->values();
|
||||
|
||||
$byBrowser = (clone $visits)
|
||||
->select('browser')
|
||||
->selectRaw('COUNT(*) as pv')
|
||||
->selectRaw('COUNT(DISTINCT client_uid) as uv')
|
||||
->groupBy('browser')
|
||||
->orderByDesc('pv')
|
||||
->limit(10)
|
||||
->get()
|
||||
->map(static fn ($r) => [
|
||||
'label' => ((string) ($r->browser ?? '')) !== '' ? (string) $r->browser : 'Unknown',
|
||||
'pv' => (int) $r->pv,
|
||||
'uv' => (int) $r->uv,
|
||||
])
|
||||
->values();
|
||||
|
||||
return response()->json([
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
@@ -56,6 +100,10 @@ class DashboardController extends Controller
|
||||
'total' => $total,
|
||||
'new_count' => $newCount,
|
||||
'active_count' => $activeCount,
|
||||
'pv' => $pv,
|
||||
'uv' => $uv,
|
||||
'by_os' => $byOs,
|
||||
'by_browser' => $byBrowser,
|
||||
'range' => $range,
|
||||
'from' => $from->toDateTimeString(),
|
||||
'to' => $to->toDateTimeString(),
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Concerns\PortalAware;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\PageVisit;
|
||||
use App\Models\User;
|
||||
use App\Support\AgentScope;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
class PageVisitController extends Controller
|
||||
{
|
||||
use PortalAware;
|
||||
|
||||
public function index()
|
||||
{
|
||||
$agents = $this->isAgentPortal()
|
||||
? collect()
|
||||
: User::query()->orderBy('username')->get(['id', 'username']);
|
||||
|
||||
return view('admin.visits.index', [
|
||||
'portal' => $this->portal(),
|
||||
'agents' => $agents,
|
||||
]);
|
||||
}
|
||||
|
||||
public function data(Request $request)
|
||||
{
|
||||
$q = $this->scopedQuery($request);
|
||||
$limit = min(100, max(1, (int) $request->query('limit', 20)));
|
||||
$page = max(1, (int) $request->query('page', 1));
|
||||
|
||||
$total = (clone $q)->count();
|
||||
$rows = (clone $q)
|
||||
->orderByDesc('id')
|
||||
->forPage($page, $limit)
|
||||
->get([
|
||||
'id', 'channel_id', 'client_uid', 'os', 'os_version',
|
||||
'browser', 'browser_version', 'user_agent', 'ip', 'path', 'created_at',
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'count' => $total,
|
||||
'data' => $rows->map(static fn (PageVisit $v) => [
|
||||
'id' => $v->id,
|
||||
'channel_id' => $v->channel_id,
|
||||
'client_uid' => $v->client_uid,
|
||||
'os' => $v->os ?: '',
|
||||
'os_version' => $v->os_version ?: '',
|
||||
'browser' => $v->browser ?: '',
|
||||
'browser_version' => $v->browser_version ?: '',
|
||||
'user_agent' => $v->user_agent ?: '',
|
||||
'ip' => $v->ip ?: '',
|
||||
'path' => $v->path ?: '',
|
||||
'created_at' => optional($v->created_at)?->toDateTimeString(),
|
||||
])->values(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function groups(Request $request)
|
||||
{
|
||||
$group = (string) $request->query('group', 'os');
|
||||
$base = $this->scopedQuery($request);
|
||||
$builder = match ($group) {
|
||||
'os_version' => $base
|
||||
->select('os', 'os_version')
|
||||
->selectRaw('COUNT(*) as pv')
|
||||
->selectRaw('COUNT(DISTINCT client_uid) as uv')
|
||||
->groupBy('os', 'os_version'),
|
||||
'browser' => $base
|
||||
->select('browser')
|
||||
->selectRaw('COUNT(*) as pv')
|
||||
->selectRaw('COUNT(DISTINCT client_uid) as uv')
|
||||
->groupBy('browser'),
|
||||
'browser_version' => $base
|
||||
->select('browser', 'browser_version')
|
||||
->selectRaw('COUNT(*) as pv')
|
||||
->selectRaw('COUNT(DISTINCT client_uid) as uv')
|
||||
->groupBy('browser', 'browser_version'),
|
||||
default => $base
|
||||
->select('os')
|
||||
->selectRaw('COUNT(*) as pv')
|
||||
->selectRaw('COUNT(DISTINCT client_uid) as uv')
|
||||
->groupBy('os'),
|
||||
};
|
||||
|
||||
$rows = $builder
|
||||
->orderByDesc('pv')
|
||||
->limit(50)
|
||||
->get()
|
||||
->map(static function ($row) use ($group) {
|
||||
$label = match ($group) {
|
||||
'os_version' => trim(((string) ($row->os ?? '')).' '.((string) ($row->os_version ?? ''))),
|
||||
'browser' => (string) ($row->browser ?? ''),
|
||||
'browser_version' => trim(((string) ($row->browser ?? '')).' '.((string) ($row->browser_version ?? ''))),
|
||||
default => (string) ($row->os ?? ''),
|
||||
};
|
||||
|
||||
return [
|
||||
'label' => $label !== '' ? $label : 'Unknown',
|
||||
'pv' => (int) $row->pv,
|
||||
'uv' => (int) $row->uv,
|
||||
];
|
||||
})
|
||||
->values();
|
||||
|
||||
return response()->json([
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'data' => [
|
||||
'group' => $group,
|
||||
'rows' => $rows,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private function scopedQuery(Request $request)
|
||||
{
|
||||
$q = PageVisit::query();
|
||||
AgentScope::applyChannelIdScope($q, $this->agent());
|
||||
|
||||
$agentUserId = $this->isAgentPortal()
|
||||
? (int) ($this->agent()?->id ?? 0)
|
||||
: (int) $request->query('agent_user_id', 0);
|
||||
if (! $this->isAgentPortal() && $agentUserId > 0) {
|
||||
AgentScope::applyChannelIdAgentUserFilter($q, $agentUserId);
|
||||
}
|
||||
|
||||
$channelId = trim((string) $request->query('channel_id', ''));
|
||||
if ($channelId !== '') {
|
||||
$q->where('channel_id', 'like', '%'.$channelId.'%');
|
||||
}
|
||||
|
||||
$os = trim((string) $request->query('os', ''));
|
||||
if ($os !== '') {
|
||||
$q->where('os', $os);
|
||||
}
|
||||
$osVersion = trim((string) $request->query('os_version', ''));
|
||||
if ($osVersion !== '') {
|
||||
$q->where('os_version', $osVersion);
|
||||
}
|
||||
$browser = trim((string) $request->query('browser', ''));
|
||||
if ($browser !== '') {
|
||||
$q->where('browser', $browser);
|
||||
}
|
||||
$browserVersion = trim((string) $request->query('browser_version', ''));
|
||||
if ($browserVersion !== '') {
|
||||
$q->where('browser_version', $browserVersion);
|
||||
}
|
||||
|
||||
[$from, $to] = $this->rangeBounds((string) $request->query('range', '30d'));
|
||||
$q->whereBetween('created_at', [$from, $to]);
|
||||
|
||||
return $q;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: Carbon, 1: Carbon}
|
||||
*/
|
||||
private function rangeBounds(string $range): array
|
||||
{
|
||||
$now = Carbon::now();
|
||||
|
||||
return match ($range) {
|
||||
'today' => [$now->copy()->startOfDay(), $now->copy()->endOfDay()],
|
||||
'yesterday' => [
|
||||
$now->copy()->subDay()->startOfDay(),
|
||||
$now->copy()->subDay()->endOfDay(),
|
||||
],
|
||||
'7d' => [$now->copy()->subDays(6)->startOfDay(), $now->copy()->endOfDay()],
|
||||
default => [$now->copy()->subDays(29)->startOfDay(), $now->copy()->endOfDay()],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -22,9 +22,13 @@ class SystemSettingsController extends Controller
|
||||
'telegram_owner_chat_id' => ['nullable', 'string', 'max:64'],
|
||||
'channels_max_per_agent' => ['nullable', 'integer', 'min:1', 'max:100'],
|
||||
'channels_domains' => ['nullable', 'string', 'max:4000'],
|
||||
'panel_admin_hosts' => ['nullable', 'string', 'max:4000'],
|
||||
'panel_agent_hosts' => ['nullable', 'string', 'max:4000'],
|
||||
]);
|
||||
|
||||
$domains = SettingsService::parseDomains($data['channels_domains'] ?? null);
|
||||
$adminHosts = SettingsService::parseDomains($data['panel_admin_hosts'] ?? null);
|
||||
$agentHosts = SettingsService::parseDomains($data['panel_agent_hosts'] ?? null);
|
||||
|
||||
$settings->putMany([
|
||||
'telegram.bot_token' => $data['telegram_bot_token'] ?? null,
|
||||
@@ -34,6 +38,9 @@ class SystemSettingsController extends Controller
|
||||
: null,
|
||||
// Persist normalized comma-separated list (empty clears override to []).
|
||||
'channels.domains' => $domains === [] ? '' : implode(',', $domains),
|
||||
// Empty = no Host restriction for that panel.
|
||||
'panel.admin_hosts' => $adminHosts === [] ? '' : implode(',', $adminHosts),
|
||||
'panel.agent_hosts' => $agentHosts === [] ? '' : implode(',', $agentHosts),
|
||||
]);
|
||||
|
||||
return response()->json(['code' => 0, 'msg' => '已保存']);
|
||||
|
||||
Reference in New Issue
Block a user