From 007890855b52deb34e8286112056ff993d3e5b9b Mon Sep 17 00:00:00 2001 From: cp6 Date: Wed, 10 Aug 2022 00:34:03 +1000 Subject: [PATCH] Added YABS to API Get YABS from API Add/create/post/store YABS --- app/Http/Controllers/ApiController.php | 124 +++++++++++++++++++++++++ routes/api.php | 4 + 2 files changed, 128 insertions(+) diff --git a/app/Http/Controllers/ApiController.php b/app/Http/Controllers/ApiController.php index c67eb7b..adddea3 100644 --- a/app/Http/Controllers/ApiController.php +++ b/app/Http/Controllers/ApiController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\DiskSpeed; use App\Models\Domains; use App\Models\IPs; use App\Models\Labels; @@ -14,6 +15,8 @@ use App\Models\Reseller; use App\Models\SeedBoxes; use App\Models\Server; use App\Models\Shared; +use App\Models\Yabs; +use App\Process; use DataTables; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; @@ -470,4 +473,125 @@ class ApiController extends Controller return response()->json(array('result' => 'fail', 'request' => $request->post()), 500); } + public function storeYabs(Request $request) + { + $rules = array( + 'server_id' => 'required|string|size:8', + 'yabs_output' => 'required|string', + ); + + $messages = array( + 'required' => ':attribute is required', + 'string' => ':attribute must be a string', + 'size' => ':attribute must be exactly :size characters' + ); + + $validator = Validator::make($request->all(), $rules, $messages); + + if ($validator->fails()) { + return response()->json(['result' => 'fail', 'messages' => $validator->messages()], 400); + } + + //dd($request->all()); + $process = new Process(); + $yabs = $process->yabsOutputAsJson($request->server_id, $request->yabs_output); + //dd($yabs); + + $yabs_id = Str::random(8); + + $yabs_insert = Yabs::create([ + 'id' => $yabs_id, + 'server_id' => $request->server_id, + 'has_ipv6' => $yabs['has_ipv6'], + 'aes' => $yabs['aes'], + 'vm' => $yabs['vm'], + 'output_date' => $yabs['output_date'], + 'cpu_cores' => $yabs['cpu_cores'], + 'cpu_freq' => $yabs['cpu_freq'], + 'cpu_model' => $yabs['cpu'], + 'ram' => $yabs['ram'], + 'ram_type' => $yabs['ram_type'], + 'ram_mb' => $yabs['ram_mb'], + 'swap' => $yabs['swap'], + 'swap_type' => $yabs['swap_type'], + 'swap_mb' => $yabs['swap_mb'], + 'disk' => $yabs['disk'], + 'disk_type' => $yabs['disk_type'], + 'disk_gb' => $yabs['disk_gb'], + 'gb5_single' => $yabs['GB5_single'], + 'gb5_multi' => $yabs['GB5_mult'], + 'gb5_id' => $yabs['GB5_id'], + 'uptime' => $yabs['uptime'], + 'distro' => $yabs['distro'], + 'kernel' => $yabs['kernel'] + ]); + + DiskSpeed::create([ + 'id' => $yabs_id, + 'server_id' => $request->server_id, + 'd_4k' => $yabs['disk_speed']['4k_total'], + 'd_4k_type' => $yabs['disk_speed']['4k_total_type'], + 'd_4k_as_mbps' => $yabs['disk_speed']['4k_total_mbps'], + 'd_64k' => $yabs['disk_speed']['64k_total'], + 'd_64k_type' => $yabs['disk_speed']['64k_total_type'], + 'd_64k_as_mbps' => $yabs['disk_speed']['64k_total_mbps'], + 'd_512k' => $yabs['disk_speed']['512k_total'], + 'd_512k_type' => $yabs['disk_speed']['512k_total_type'], + 'd_512k_as_mbps' => $yabs['disk_speed']['512k_total_mbps'], + 'd_1m' => $yabs['disk_speed']['1m_total'], + 'd_1m_type' => $yabs['disk_speed']['1m_total_type'], + 'd_1m_as_mbps' => $yabs['disk_speed']['1m_total_mbps'] + ]); + + foreach ($yabs['network_speed'] as $y) { + NetworkSpeed::create([ + 'id' => $yabs_id, + 'server_id' => $request->server_id, + 'location' => $y['location'], + 'send' => $y['send'], + 'send_type' => $y['send_type'], + 'send_as_mbps' => $y['send_type_mbps'], + 'receive' => $y['receive'], + 'receive_type' => $y['receive_type'], + 'receive_as_mbps' => $y['receive_type_mbps'] + ]); + } + + $update_server = DB::table('servers') + ->where('id', $request->server_id) + ->update([ + 'ram' => $yabs['ram'], + 'ram_type' => $yabs['ram_type'], + 'ram_as_mb' => ($yabs['ram_type'] === 'GB') ? ($yabs['ram'] * 1024) : $yabs['ram'], + 'disk' => $yabs['disk'], + 'disk_type' => $yabs['disk_type'], + 'disk_as_gb' => ($yabs['disk_type'] === 'TB') ? ($yabs['disk'] * 1024) : $yabs['disk'], + 'cpu' => $yabs['cpu_cores'], + 'has_yabs' => 1 + ]); + + Cache::forget('all_active_servers');//all servers cache + Cache::forget('non_active_servers');//all servers cache + Cache::forget('all_yabs');//Forget the all YABs cache + + if ($yabs_insert) { + return response()->json(array('result' => 'success', 'yabs_id' => $yabs_id), 200); + } + + return response()->json(array('result' => 'fail', 'request' => $request->post()), 500); + + } + + public function getAllYabs() + { + $yabs = Yabs::allYabs()->toJson(JSON_PRETTY_PRINT); + return response($yabs, 200); + } + + protected function getYabs($id) + { + $yabs = Yabs::yabs($id)->toJson(JSON_PRETTY_PRINT); + return response($yabs, 200); + } + } diff --git a/routes/api.php b/routes/api.php index 78df219..d5307fe 100644 --- a/routes/api.php +++ b/routes/api.php @@ -73,3 +73,7 @@ Route::middleware('auth:api')->get('shared/{id}', 'App\Http\Controllers\ApiContr Route::middleware('auth:api')->get('online/{hostname}', 'App\Http\Controllers\ApiController@checkHostIsUp'); Route::middleware('auth:api')->get('dns/{domainName}/{type}', 'App\Http\Controllers\ApiController@getIpForDomain'); + +Route::middleware('auth:api')->post('yabs/', 'App\Http\Controllers\ApiController@storeYabs'); +Route::middleware('auth:api')->get('yabs/', 'App\Http\Controllers\ApiController@getAllYabs'); +Route::middleware('auth:api')->get('yabs/{id}', 'App\Http\Controllers\ApiController@getYabs');