添加 Speed/ServerUsagesSyncService.php

This commit is contained in:
ste87 2026-03-14 23:15:14 +08:00
parent 8aab3ff282
commit 4bcb891f24

View File

@ -0,0 +1,49 @@
<?php
namespace Convoy\Services\Nodes;
use Carbon\Carbon;
use Convoy\Models\Node;
use Convoy\Models\Server;
use Convoy\Enums\Server\MetricTimeframe;
use Convoy\Repositories\Proxmox\Server\ProxmoxMetricsRepository;
use Convoy\Exceptions\Repository\Proxmox\ProxmoxConnectionException;
class ServerUsagesSyncService
{
public function __construct(private ProxmoxMetricsRepository $repository)
{
}
public function handle(Node $node)
{
$servers = $node->servers;
$servers->each(function (Server $server) {
try {
$metrics = $this->repository->setServer($server)->getMetrics(MetricTimeframe::HOUR);
$bandwidth = $server->bandwidth_usage;
$endingDate = $server->hydrated_at ? Carbon::parse($server->hydrated_at) : Carbon::now()->firstOfMonth();
foreach ($metrics as $metric) {
if (Carbon::createFromTimestamp($metric['time'])->gt($endingDate)) {
// we multiply it by 60 seconds because each metric is
// recorded every 1 minute but the values like netin and
// netout are in bytes/sec
$bandwidth += (int) $metric['netin'] * 60 + (int) $metric['netout'] * 60;
}
}
if ($bandwidth > 0) {
$server->update([
'bandwidth_usage' => $bandwidth,
'hydrated_at' => now(),
]);
}
} catch (ProxmoxConnectionException $e) {
// do nothing
}
});
}
}