2022-03-05 16:02:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2022-09-07 05:56:56 +02:00
|
|
|
use DateTime;
|
|
|
|
use Exception;
|
2022-03-05 16:02:12 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-09-07 05:56:56 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2022-05-14 17:21:51 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-09-07 05:56:56 +02:00
|
|
|
use Illuminate\Support\Str;
|
2022-03-05 16:02:12 +01:00
|
|
|
|
|
|
|
class Yabs extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
public $incrementing = false;
|
|
|
|
|
2022-07-22 15:49:18 +02:00
|
|
|
protected $keyType = 'string';
|
|
|
|
|
2022-03-05 16:02:12 +01:00
|
|
|
protected $table = 'yabs';
|
|
|
|
|
2022-07-20 04:51:45 +02:00
|
|
|
protected $fillable = ['id', 'server_id', 'has_ipv6', 'aes', 'vm', 'output_date', 'cpu_cores', 'cpu_freq', 'cpu_model', 'ram', 'ram_type', 'ram_mb', 'disk', 'disk_type', 'disk_gb', 'gb5_single', 'gb5_multi', 'gb5_id', '4k', '4k_type', '4k_as_mbps', '64k', '64k_type', '64k_as_mbps', '512k', '512k_type', '512k_as_mbps', '1m', '1m_type', '1m_as_mbps', 'location', 'send', 'send_type', 'send_as_mbps', 'receive', 'receive_type', 'receive_as_mbps', 'uptime', 'distro', 'kernel', 'swap', 'swap_type', 'swap_mb'];
|
2022-05-14 17:21:51 +02:00
|
|
|
|
2022-07-18 06:27:11 +02:00
|
|
|
public static function yabs(string $yabs_id)
|
|
|
|
{
|
|
|
|
return Cache::remember("yabs.$yabs_id", now()->addMonth(1), function () use ($yabs_id) {
|
2022-07-19 16:20:15 +02:00
|
|
|
return self::where('id', $yabs_id)->with(['server', 'disk_speed', 'network_speed', 'server.location', 'server.provider'])
|
2022-11-05 02:47:08 +01:00
|
|
|
->first();
|
2022-07-18 06:27:11 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function allYabs()
|
|
|
|
{
|
|
|
|
return Cache::remember("all_yabs", now()->addMonth(1), function () {
|
2022-07-19 16:20:15 +02:00
|
|
|
return self::with(['server', 'disk_speed', 'network_speed', 'server.location', 'server.provider'])
|
2022-07-18 06:27:11 +02:00
|
|
|
->get();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-19 15:46:18 +02:00
|
|
|
public function server(): \Illuminate\Database\Eloquent\Relations\HasOne
|
2022-07-18 06:27:11 +02:00
|
|
|
{
|
|
|
|
return $this->hasOne(Server::class, 'id', 'server_id');
|
|
|
|
}
|
|
|
|
|
2023-08-19 15:46:18 +02:00
|
|
|
public function disk_speed(): \Illuminate\Database\Eloquent\Relations\HasOne
|
2022-07-18 06:27:11 +02:00
|
|
|
{
|
|
|
|
return $this->hasOne(DiskSpeed::class, 'id', 'id');
|
|
|
|
}
|
|
|
|
|
2023-08-19 15:46:18 +02:00
|
|
|
public function network_speed(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2022-07-18 06:27:11 +02:00
|
|
|
{
|
|
|
|
return $this->hasMany(NetworkSpeed::class, 'id', 'id');
|
|
|
|
}
|
|
|
|
|
2022-07-19 16:20:15 +02:00
|
|
|
public static function buildYabsArray($data): array
|
|
|
|
{
|
|
|
|
$speed_tests = [];
|
|
|
|
foreach ($data->network_speed as $ns) {
|
2023-03-31 02:38:21 +02:00
|
|
|
$speed_tests[] = [
|
2022-07-19 16:20:15 +02:00
|
|
|
'location' => $ns->location,
|
2022-07-20 04:51:45 +02:00
|
|
|
'send' => $ns->send . ' ' . $ns->send_type,
|
|
|
|
'receive' => $ns->receive . ' ' . $ns->receive_type,
|
2023-03-31 02:38:21 +02:00
|
|
|
];
|
2022-07-19 16:20:15 +02:00
|
|
|
}
|
2023-03-31 02:38:21 +02:00
|
|
|
return [
|
2022-07-19 16:20:15 +02:00
|
|
|
'date_time' => $data->output_date,
|
|
|
|
'location' => $data->server->location->name,
|
|
|
|
'provider' => $data->server->provider->name,
|
2022-07-20 04:51:45 +02:00
|
|
|
'uptime' => $data->uptime,
|
|
|
|
'distro' => $data->distro,
|
|
|
|
'kernel' => $data->kernel,
|
2023-03-31 02:38:21 +02:00
|
|
|
'cpu' => [
|
2022-07-19 16:20:15 +02:00
|
|
|
'cores' => $data->cpu_cores,
|
|
|
|
'speed_mhz' => $data->cpu_freq,
|
|
|
|
'model' => $data->cpu_model,
|
|
|
|
'aes' => $data->aes === 1,
|
|
|
|
'vm' => $data->vm === 1,
|
|
|
|
'GB5_single' => $data->gb5_single,
|
|
|
|
'GB5_multi' => $data->gb5_multi,
|
2023-03-31 02:38:21 +02:00
|
|
|
],
|
|
|
|
'ram' => [
|
2022-07-20 04:51:45 +02:00
|
|
|
'amount' => $data->ram . ' ' . $data->ram_type,
|
2022-07-19 16:20:15 +02:00
|
|
|
'mb' => $data->ram_mb,
|
2023-03-31 02:38:21 +02:00
|
|
|
'swap' => [
|
2022-07-20 04:51:45 +02:00
|
|
|
'amount' => $data->swap ?? null . ' ' . $data->swap_type ?? null,
|
|
|
|
'mb' => $data->swap_mb ?? null,
|
2023-03-31 02:38:21 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'disk' => [
|
2022-07-20 04:51:45 +02:00
|
|
|
'amount' => $data->disk . ' ' . $data->disk_type,
|
2022-07-19 16:20:15 +02:00
|
|
|
'gb' => $data->disk_gb,
|
2023-03-31 02:38:21 +02:00
|
|
|
'speed_tests' => [
|
2022-07-20 04:51:45 +02:00
|
|
|
'4k' => $data->disk_speed->d_4k . ' ' . $data->disk_speed->d_4k_type,
|
|
|
|
'64k' => $data->disk_speed->d_64k . ' ' . $data->disk_speed->d_64k_type,
|
|
|
|
'512k' => $data->disk_speed->d_512k . ' ' . $data->disk_speed->d_512k_type,
|
|
|
|
'1m' => $data->disk_speed->d_1m . ' ' . $data->disk_speed->d_1m_type,
|
2023-03-31 02:38:21 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'network' => [
|
2022-07-19 16:20:15 +02:00
|
|
|
'has_ipv6' => $data->has_ipv6 === 1,
|
|
|
|
'speed_tests' => $speed_tests
|
2023-03-31 02:38:21 +02:00
|
|
|
],
|
|
|
|
];
|
2022-07-19 16:20:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 05:56:56 +02:00
|
|
|
public static function speedAsMbps(string $string): float
|
|
|
|
{
|
|
|
|
$data = explode(" ", $string);
|
|
|
|
if ($data[0] === 'busy') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if ($data[1] === "Gbits/sec") {
|
|
|
|
return $data[0] * 1000;
|
|
|
|
} else if ($data[1] === "Mbits/sec") {
|
|
|
|
return $data[0];
|
|
|
|
} else {//Kbps
|
|
|
|
return $data[0] / 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function speedType(string $string): string
|
|
|
|
{
|
|
|
|
$data = explode(" ", $string);
|
|
|
|
if ($data[0] === 'busy') {
|
|
|
|
return "MBps";
|
|
|
|
}
|
|
|
|
if ($data[1] === "Gbits/sec") {
|
|
|
|
return "GBps";
|
|
|
|
} else if ($data[1] === "Mbits/sec") {
|
|
|
|
return "MBps";
|
|
|
|
} else {//Kbps
|
|
|
|
return "KBps";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function speedAsFloat(string $string): float
|
|
|
|
{
|
|
|
|
$data = explode(" ", $string);
|
|
|
|
if ($data[0] === 'busy') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (float)$data[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function formatRunTime(string $date): string
|
|
|
|
{
|
|
|
|
return DateTime::createFromFormat('Ymd-His', $date)->format('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function gb5IdFromURL(string $url): int
|
|
|
|
{
|
|
|
|
return str_replace("https://browser.geekbench.com/v5/cpu/", "", $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function KBstoMBs(int $kbs): float
|
|
|
|
{
|
|
|
|
return $kbs / 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function insertFromJson($data, string $server_id)
|
|
|
|
{
|
|
|
|
$data = (object)$data;
|
|
|
|
try {
|
|
|
|
$date_ran = self::formatRunTime($data->time);
|
|
|
|
$version = $data['version'];
|
|
|
|
$has_ipv4 = $data['net']['ipv4'];
|
|
|
|
$has_ipv6 = $data['net']['ipv6'];
|
2022-11-02 12:02:12 +01:00
|
|
|
//OS
|
2022-09-07 05:56:56 +02:00
|
|
|
$arch = $data['os']['arch'];
|
|
|
|
$distro = $data['os']['distro'];
|
|
|
|
$kernel = $data['os']['kernel'];
|
|
|
|
$uptime = $data['os']['uptime'];
|
2022-11-02 12:02:12 +01:00
|
|
|
//CPU
|
2022-09-07 05:56:56 +02:00
|
|
|
$model = $data['cpu']['model'];
|
|
|
|
$cores = $data['cpu']['cores'];
|
|
|
|
$freq = $data['cpu']['freq'];
|
|
|
|
$aes = $data['cpu']['aes'];
|
|
|
|
$virt = $data['cpu']['virt'];
|
2022-11-02 12:02:12 +01:00
|
|
|
//RAM Disk
|
2022-09-07 05:56:56 +02:00
|
|
|
$ram = $data['mem']['ram'];
|
|
|
|
$swap = $data['mem']['swap'];
|
|
|
|
$disk = $data['mem']['disk'];
|
|
|
|
if (isset($data['geekbench'][0]) && $data['geekbench'][0]['version'] === 5) {
|
|
|
|
$gb5_single = $data['geekbench'][0]['single'];
|
|
|
|
$gb5_multi = $data['geekbench'][0]['multi'];
|
|
|
|
$gb5_id = self::gb5IdFromURL($data['geekbench'][0]['url']);
|
|
|
|
} elseif (isset($data['geekbench'][1]) && $data['geekbench'][1]['version'] === 5) {
|
|
|
|
$gb5_single = $data['geekbench'][1]['single'];
|
|
|
|
$gb5_multi = $data['geekbench'][1]['multi'];
|
|
|
|
$gb5_id = self::gb5IdFromURL($data['geekbench'][1]['url']);
|
|
|
|
} else {
|
|
|
|
$gb5_single = $gb5_multi = $gb5_id = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$yabs_id = Str::random(8);
|
|
|
|
|
2022-09-22 04:26:56 +02:00
|
|
|
if ($ram > 999999) {
|
2022-09-07 05:56:56 +02:00
|
|
|
$ram_f = ($ram / 1024 / 1024);
|
|
|
|
$ram_type = 'GB';
|
|
|
|
} else {
|
|
|
|
$ram_f = ($ram / 1024);
|
|
|
|
$ram_type = 'MB';
|
|
|
|
}
|
|
|
|
|
2022-09-22 04:26:56 +02:00
|
|
|
if ($disk > 100000000) {
|
|
|
|
$disk_f = ($disk / 1024 / 1024 / 1024);
|
2022-09-07 05:56:56 +02:00
|
|
|
$disk_type = 'TB';
|
|
|
|
} else {
|
2022-09-22 04:26:56 +02:00
|
|
|
$disk_f = ($disk / 1024 / 1024);
|
2022-09-07 05:56:56 +02:00
|
|
|
$disk_type = 'GB';
|
|
|
|
}
|
|
|
|
|
|
|
|
self::create([
|
|
|
|
'id' => $yabs_id,
|
|
|
|
'server_id' => $server_id,
|
|
|
|
'has_ipv6' => $has_ipv6,
|
|
|
|
'aes' => $aes,
|
|
|
|
'vm' => $virt,
|
|
|
|
'distro' => $distro,
|
|
|
|
'kernel' => $kernel,
|
|
|
|
'uptime' => $uptime,
|
|
|
|
'cpu_model' => $model,
|
|
|
|
'cpu_cores' => $cores,
|
|
|
|
'cpu_freq' => (float)$freq,
|
|
|
|
'ram' => $ram_f,
|
|
|
|
'ram_type' => $ram_type,
|
|
|
|
'ram_mb' => ($ram / 1024),
|
|
|
|
'swap' => $swap / 1024,
|
|
|
|
'swap_mb' => ($swap / 1024),
|
|
|
|
'swap_type' => 'MB',
|
|
|
|
'disk' => $disk_f,
|
|
|
|
'disk_gb' => ($disk / 1024 / 1024),
|
|
|
|
'disk_type' => $disk_type,
|
|
|
|
'output_date' => $date_ran,
|
|
|
|
'gb5_single' => $gb5_single,
|
|
|
|
'gb5_multi' => $gb5_multi,
|
|
|
|
'gb5_id' => $gb5_id
|
|
|
|
]);
|
|
|
|
|
2022-11-02 12:02:12 +01:00
|
|
|
//fio
|
2022-09-07 05:56:56 +02:00
|
|
|
foreach ($data['fio'] as $ds) {
|
|
|
|
if ($ds['bs'] === '4k') {
|
2022-09-22 04:26:56 +02:00
|
|
|
$d4k = ($ds['speed_rw'] > 999999) ? ($ds['speed_rw'] / 1000 / 1000) : $ds['speed_rw'] / 1000;
|
|
|
|
$d4k_type = ($ds['speed_rw'] > 999999) ? 'GB/s' : 'MB/s';
|
2022-09-07 05:56:56 +02:00
|
|
|
$d4k_mbps = self::KBstoMBs($ds['speed_rw']);
|
|
|
|
}
|
|
|
|
if ($ds['bs'] === '64k') {
|
2022-09-22 04:26:56 +02:00
|
|
|
$d64k = ($ds['speed_rw'] > 999999) ? ($ds['speed_rw'] / 1000 / 1000) : $ds['speed_rw'] / 1000;
|
|
|
|
$d64k_type = ($ds['speed_rw'] > 999999) ? 'GB/s' : 'MB/s';
|
2022-09-07 05:56:56 +02:00
|
|
|
$d64k_mbps = self::KBstoMBs($ds['speed_rw']);
|
|
|
|
}
|
|
|
|
if ($ds['bs'] === '512k') {
|
2022-09-22 04:26:56 +02:00
|
|
|
$d512k = ($ds['speed_rw'] > 999999) ? ($ds['speed_rw'] / 1000 / 1000) : $ds['speed_rw'] / 1000;
|
|
|
|
$d512k_type = ($ds['speed_rw'] > 999999) ? 'GB/s' : 'MB/s';
|
2022-09-07 05:56:56 +02:00
|
|
|
$d512k_mbps = self::KBstoMBs($ds['speed_rw']);
|
|
|
|
}
|
|
|
|
if ($ds['bs'] === '1m') {
|
2022-09-22 04:26:56 +02:00
|
|
|
$d1m = ($ds['speed_rw'] > 999999) ? ($ds['speed_rw'] / 1000 / 1000) : $ds['speed_rw'] / 1000;
|
|
|
|
$d1m_type = ($ds['speed_rw'] > 999999) ? 'GB/s' : 'MB/s';
|
2022-09-07 05:56:56 +02:00
|
|
|
$d1m_mbps = self::KBstoMBs($ds['speed_rw']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DiskSpeed::create([
|
|
|
|
'id' => $yabs_id,
|
|
|
|
'server_id' => $server_id,
|
|
|
|
'd_4k' => $d4k,
|
|
|
|
'd_4k_type' => $d4k_type,
|
|
|
|
'd_4k_as_mbps' => $d4k_mbps,
|
|
|
|
'd_64k' => $d64k,
|
|
|
|
'd_64k_type' => $d64k_type,
|
|
|
|
'd_64k_as_mbps' => $d64k_mbps,
|
|
|
|
'd_512k' => $d512k,
|
|
|
|
'd_512k_type' => $d512k_type,
|
|
|
|
'd_512k_as_mbps' => $d512k_mbps,
|
|
|
|
'd_1m' => $d1m,
|
|
|
|
'd_1m_type' => $d1m_type,
|
|
|
|
'd_1m_as_mbps' => $d1m_mbps
|
|
|
|
]);
|
|
|
|
|
2022-11-02 12:02:12 +01:00
|
|
|
//iperf
|
2022-09-07 05:56:56 +02:00
|
|
|
foreach ($data['iperf'] as $st) {
|
|
|
|
($has_ipv4) ? $match = 'IPv4' : $match = 'IPv6';
|
|
|
|
if ($st['mode'] === $match) {
|
|
|
|
if ($st['send'] !== "busy " || $st['recv'] !== "busy ") {
|
|
|
|
NetworkSpeed::create([
|
|
|
|
'id' => $yabs_id,
|
|
|
|
'server_id' => $server_id,
|
|
|
|
'location' => $st['loc'],
|
|
|
|
'send' => self::speedAsFloat($st['send']),
|
|
|
|
'send_type' => self::speedType($st['send']),
|
|
|
|
'send_as_mbps' => self::speedAsMbps($st['send']),
|
|
|
|
'receive' => self::speedAsFloat($st['recv']),
|
|
|
|
'receive_type' => self::speedType($st['recv']),
|
|
|
|
'receive_as_mbps' => self::speedAsMbps($st['recv'])
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Update server
|
|
|
|
$update_server = DB::table('servers')
|
|
|
|
->where('id', $server_id)
|
|
|
|
->update([
|
|
|
|
'ram' => $ram_f,
|
|
|
|
'ram_type' => $ram_type,
|
|
|
|
'ram_as_mb' => ($ram / 1024),
|
|
|
|
'disk' => $disk_f,
|
|
|
|
'disk_as_gb' => ($disk / 1024 / 1024),
|
|
|
|
'disk_type' => $disk_type,
|
|
|
|
'cpu' => $cores,
|
|
|
|
'has_yabs' => 1
|
|
|
|
]);
|
|
|
|
|
2022-09-22 03:12:29 +02:00
|
|
|
Cache::forget("yabs.$yabs_id");
|
|
|
|
Cache::forget("all_yabs");
|
|
|
|
Cache::forget("server.$server_id");
|
|
|
|
Cache::forget("all_servers");
|
|
|
|
|
2022-09-07 05:56:56 +02:00
|
|
|
} catch (Exception $e) {//Not valid JSON
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-05 16:02:12 +01:00
|
|
|
}
|