Files
Xboard/app/Services/ServerAutoOnlineService.php
T
yinjianm e847252e12 fix(api): 修复节点流量限额共享统计与父子显隐联动
统一节点流量统计与限额展示口径,节点详情新增昨日流量,
并让今日、昨日和本月使用清晰的半开时间窗口聚合

同 machine_id 或同 host 的节点现在共享当前账期已用流量,
管理端优先使用后端 traffic_limit_snapshot 展示月额度状态,
mi-node 下发的 current_used 也改为共享账期统计

新增 parent_auto_hidden 标记与父节点显隐联动服务,父节点
因自动上线或流量限额变为不可展示时会隐藏当前显示的子节点,
恢复时只恢复这批自动隐藏的子节点,避免覆盖手动操作
2026-04-29 02:24:57 +08:00

106 lines
3.3 KiB
PHP

<?php
namespace App\Services;
use App\Models\Server;
use App\Models\ServerGfwCheck;
use Illuminate\Support\Collection;
class ServerAutoOnlineService
{
public function sync(): array
{
$servers = Server::query()
->where('auto_online', true)
->get();
return $this->syncServers($servers);
}
public function syncServer(Server $server): array
{
if (!(bool) $server->auto_online) {
return $this->emptyResult();
}
return $this->syncServers(collect([$server]));
}
private function syncServers(Collection $servers): array
{
$gfwStatuses = app(ServerGfwCheckService::class)->getLatestStatusesForServers($servers);
$result = $this->emptyResult($servers->count());
foreach ($servers as $server) {
$this->syncServerWithStatuses($server, $gfwStatuses, $result);
}
return $result;
}
private function syncServerWithStatuses(Server $server, array $gfwStatuses, array &$result): void
{
$sourceNodeId = (int) ($server->parent_id ?: $server->id);
$gfwStatus = $gfwStatuses[$sourceNodeId] ?? null;
$isGfwManaged = (bool) ($server->gfw_check_enabled ?? true) && $gfwStatus !== null;
$isGfwBlocked = $isGfwManaged && $gfwStatus === ServerGfwCheck::STATUS_BLOCKED;
$isGfwHeld = $isGfwManaged
&& (bool) $server->gfw_auto_hidden
&& $gfwStatus !== ServerGfwCheck::STATUS_NORMAL;
$shouldShow = !$isGfwBlocked && !$isGfwHeld && (int) $server->available_status !== Server::STATUS_OFFLINE;
$shouldClearGfwAutoHidden = $gfwStatus === ServerGfwCheck::STATUS_NORMAL
&& (bool) $server->gfw_auto_hidden;
$wasShown = (bool) $server->show;
if ($wasShown === $shouldShow && !$shouldClearGfwAutoHidden) {
$this->syncChildrenForFinalState($server, $shouldShow, $result);
$result['unchanged']++;
return;
}
$server->show = $shouldShow;
if ($isGfwBlocked) {
$server->gfw_auto_hidden = true;
$server->gfw_auto_action_at = time();
} elseif ($shouldClearGfwAutoHidden) {
$server->gfw_auto_hidden = false;
$server->gfw_auto_action_at = time();
}
$server->save();
$result['updated']++;
if ($wasShown !== $shouldShow) {
$shouldShow ? $result['shown']++ : $result['hidden']++;
}
$this->syncChildrenForFinalState($server, $shouldShow, $result);
}
private function syncChildrenForFinalState(Server $server, bool $shouldShow, array &$result): void
{
$childResult = app(ServerParentVisibilityService::class)
->syncChildrenForParent($server, $shouldShow);
$hidden = (int) ($childResult['hidden'] ?? 0);
$restored = (int) ($childResult['restored'] ?? 0);
$childUpdates = $hidden + $restored;
if ($childUpdates <= 0) {
return;
}
$result['updated'] += $childUpdates;
$result['hidden'] += $hidden;
$result['shown'] += $restored;
}
private function emptyResult(int $total = 0): array
{
return [
'total' => $total,
'updated' => 0,
'shown' => 0,
'hidden' => 0,
'unchanged' => 0,
];
}
}