添加 Speed/ServerUsagesSyncService.php
This commit is contained in:
parent
8aab3ff282
commit
4bcb891f24
49
Speed/ServerUsagesSyncService.php
Normal file
49
Speed/ServerUsagesSyncService.php
Normal 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
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user