From 0491f84ed45d4e7ab0069f9f808f47f256063bf7 Mon Sep 17 00:00:00 2001 From: cp6 Date: Tue, 19 Jul 2022 13:12:51 +1000 Subject: [PATCH] Updated Server model & Added LabelsAssigned model Huge update for Server to use relationships (no more joins) Created LabelsAssigned class for the labels relationship Removed pricingForService() function Removed now unused server cache forgets --- app/Http/Controllers/ServerController.php | 38 +-- app/Models/LabelsAssigned.php | 23 ++ app/Models/Pricing.php | 9 - app/Models/Server.php | 160 +++++----- resources/views/servers/compare.blade.php | 280 ++++++++++++------ resources/views/servers/edit.blade.php | 88 +++--- resources/views/servers/index.blade.php | 16 +- .../views/servers/public-index.blade.php | 34 +-- resources/views/servers/show.blade.php | 102 +++---- 9 files changed, 428 insertions(+), 322 deletions(-) create mode 100644 app/Models/LabelsAssigned.php diff --git a/app/Http/Controllers/ServerController.php b/app/Http/Controllers/ServerController.php index b86a182..5185081 100644 --- a/app/Http/Controllers/ServerController.php +++ b/app/Http/Controllers/ServerController.php @@ -19,9 +19,9 @@ class ServerController extends Controller public function index() { - $servers = Server::activeServersDataIndexPage(); + $servers = Server::allActiveServers(); - $non_active_servers = Server::nonActiveServersDataIndexPage(); + $non_active_servers = Server::allNonActiveServers(); return view('servers.index', compact(['servers', 'non_active_servers'])); } @@ -41,7 +41,7 @@ class ServerController extends Controller Session::save(); if ((Session::get('show_servers_public') === 1)) { - $servers = Server::publicServerData(); + $servers = Server::allPublicServers(); return view('servers.public-index', compact('servers')); } return response()->view('errors.404', array("status" => 404, "title" => "Page not found", "message" => ""), 404); @@ -121,24 +121,16 @@ class ServerController extends Controller public function show(Server $server) { - $server_extras = Server::serverDataShowPage($server->id); + $server_data = Server::server($server->id)[0]; - $network_speeds = Yabs::networkSpeedsForServer($server->id); - - $ip_addresses = IPs::ipsForServer($server->id); - - $labels = Labels::labelsForService($server->id); - - return view('servers.show', compact(['server', 'server_extras', 'network_speeds', 'labels', 'ip_addresses'])); + return view('servers.show', compact(['server_data'])); } public function edit(Server $server) { - $ip_addresses = IPs::ipsForServer($server->id); + $server_data = Server::server($server->id)[0]; - $server = Pricing::pricingForService($server->id); - - return view('servers.edit', compact(['server', 'ip_addresses'])); + return view('servers.edit', compact(['server_data'])); } public function update(Request $request, Server $server) @@ -229,28 +221,24 @@ class ServerController extends Controller } public function chooseCompare() - { + {//NOTICE: Selecting servers is not cached yet $all_servers = Server::where('has_yabs', 1)->get(); return view('servers.choose-compare', compact('all_servers')); } public function compareServers($server1, $server2) { - $server1_data = Server::serverCompareData($server1); + $server1_data = Server::server($server1); - if (count($server1_data) === 0) { + if (!isset($server1_data[0]->yabs[0])) { return response()->view('errors.404', array("status" => 404, "title" => "Page not found", "message" => "No server with YABs data was found for id '$server1'"), 404); } - $server1_network = Yabs::serverCompareNetwork($server1_data[0]->yabs_id); + $server2_data = Server::server($server2); - $server2_data = Server::serverCompareData($server2); - - if (count($server2_data) === 0) { + if (!isset($server2_data[0]->yabs[0])) { return response()->view('errors.404', array("status" => 404, "title" => "Page not found", "message" => "No server with YABs data was found for id '$server2'"), 404); } - - $server2_network = Yabs::serverCompareNetwork($server2_data[0]->yabs_id); - return view('servers.compare', compact('server1_data', 'server2_data', 'server1_network', 'server2_network')); + return view('servers.compare', compact('server1_data', 'server2_data')); } } diff --git a/app/Models/LabelsAssigned.php b/app/Models/LabelsAssigned.php new file mode 100644 index 0000000..d1e4b1d --- /dev/null +++ b/app/Models/LabelsAssigned.php @@ -0,0 +1,23 @@ +hasOne(Labels::class, 'id', 'label_id'); + } + +} diff --git a/app/Models/Pricing.php b/app/Models/Pricing.php index b5af33d..31f578c 100644 --- a/app/Models/Pricing.php +++ b/app/Models/Pricing.php @@ -141,13 +141,4 @@ class Pricing extends Model }); } - public static function pricingForService(string $service_id) - { - return Cache::remember("service_pricing.$service_id", now()->addWeek(1), function () use ($service_id) { - return DB::table('servers as s') - ->join('pricings as p', 's.id', '=', 'p.service_id') - ->where('s.id', '=', $service_id) - ->get(['s.*', 'p.*']); - }); - } } diff --git a/app/Models/Server.php b/app/Models/Server.php index 06a8f82..4bfd330 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Process; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; @@ -21,6 +22,48 @@ class Server extends Model */ private $id; + public static function allServers() + {//All servers and relationships (no using joins) + return Cache::remember("all_servers", now()->addMonth(1), function () { + return Server::with(['location', 'provider', 'os', 'price', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels', 'labels.label'])->get(); + }); + } + + public static function server(string $server_id) + {//Single server and relationships (no using joins) + return Cache::remember("server.$server_id", now()->addMonth(1), function () use ($server_id) { + return Server::where('id', $server_id) + ->with(['location', 'provider', 'os', 'price', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels', 'labels.label'])->get(); + }); + } + + public static function allActiveServers() + {//All ACTIVE servers and relationships replaces activeServersDataIndexPage() + return Cache::remember("all_active_servers", now()->addMonth(1), function () { + return Server::where('active', '=', 1) + ->with(['location', 'provider', 'os', 'price', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels', 'labels.label']) + ->get(); + }); + } + + public static function allNonActiveServers() + {//All NON ACTIVE servers and relationships replaces nonActiveServersDataIndexPage() + return Cache::remember("non_active_servers", now()->addMonth(1), function () { + return Server::where('active', '=', 0) + ->with(['location', 'provider', 'os', 'price', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels', 'labels.label']) + ->get(); + }); + } + + public static function allPublicServers() + {//server data that will be publicly viewable (values in settings) + return Cache::remember("public_server_data", now()->addMonth(1), function () { + return Server::where('show_public', '=', 1) + ->with(['location', 'provider', 'os', 'price', 'ips', 'yabs', 'yabs.disk_speed', 'yabs.network_speed', 'labels', 'labels.label']) + ->get(); + }); + } + public static function serviceServerType($type) { if ($type === 1) { @@ -158,11 +201,12 @@ class Server extends Model public static function serverRelatedCacheForget(): void { + Cache::forget('all_servers');//All servers Cache::forget('services_count');//Main page services_count cache Cache::forget('due_soon');//Main page due_soon cache Cache::forget('recently_added');//Main page recently_added cache - Cache::forget('all_active_servers');//all servers cache - Cache::forget('non_active_servers');//all servers cache + Cache::forget('all_active_servers');//all active servers cache + Cache::forget('non_active_servers');//all non active servers cache Cache::forget('servers_summary');//servers summary cache Cache::forget('public_server_data');//public servers Cache::forget('all_pricing');//All pricing @@ -172,88 +216,50 @@ class Server extends Model public static function serverSpecificCacheForget(string $server_id): void { - Cache::forget("server_show_data.$server_id");//data for show - Cache::forget("ip_addresses.$server_id");//ips for server - Cache::forget("labels_for_service.$server_id");//labels for server + Cache::forget("server.$server_id");//Will replace one below Cache::forget("service_pricing.$server_id");//Pricing } - public static function activeServersDataIndexPage() - { - return Cache::remember('all_active_servers', now()->addMonth(1), function () { - return DB::table('servers as s') - ->join('pricings as pr', 's.id', '=', 'pr.service_id') - ->join('providers as p', 's.provider_id', '=', 'p.id') - ->join('locations as l', 's.location_id', '=', 'l.id') - ->join('os as o', 's.os_id', '=', 'o.id') - ->where('s.active', '=', 1) - ->get(['s.*', 'pr.currency', 'pr.price', 'pr.term', 'pr.as_usd', 'pr.next_due_date', 'p.name as provider_name', 'l.name as location', 'o.name as os_name']); - }); - } - - public static function nonActiveServersDataIndexPage() - { - return Cache::remember('non_active_servers', now()->addMonth(1), function () { - return DB::table('servers as s') - ->join('pricings as pr', 's.id', '=', 'pr.service_id') - ->join('providers as p', 's.provider_id', '=', 'p.id') - ->join('locations as l', 's.location_id', '=', 'l.id') - ->join('os as o', 's.os_id', '=', 'o.id') - ->where('s.active', '=', 0) - ->get(['s.*', 'pr.currency', 'pr.price', 'pr.term', 'pr.as_usd', 'p.name as provider_name', 'l.name as location', 'o.name as os_name']); - }); - } - - public static function serverDataShowPage(string $server_id) - { - return Cache::remember("server_show_data.$server_id", now()->addDay(1), function () use ($server_id) { - return DB::table('servers as s') - ->join('pricings as pr', 's.id', '=', 'pr.service_id') - ->join('providers as p', 's.provider_id', '=', 'p.id') - ->join('locations as l', 's.location_id', '=', 'l.id') - ->join('os as o', 's.os_id', '=', 'o.id') - ->Leftjoin('yabs as y', 's.id', '=', 'y.server_id') - ->Leftjoin('disk_speed as ds', 'y.id', '=', 'ds.id') - ->where('s.id', '=', $server_id) - ->get(['s.*', 'p.name as provider', 'l.name as location', 'o.name as os_name', 'pr.*', 'y.*', 'ds.*']); - }); - } - - public static function publicServerData() - { - return Cache::remember('public_server_data', now()->addMonth(1), function () { - return DB::table('servers as s') - ->Join('pricings as pr', 's.id', '=', 'pr.service_id') - ->Join('providers as p', 's.provider_id', '=', 'p.id') - ->Join('locations as l', 's.location_id', '=', 'l.id') - ->Join('os as o', 's.os_id', '=', 'o.id') - ->LeftJoin('ips as i', 's.id', '=', 'i.service_id') - ->LeftJoin('yabs as y', 's.id', '=', 'y.server_id') - ->LeftJoin('disk_speed as ds', 'y.id', '=', 'ds.id') - ->where('s.show_public', '=', 1) - ->get(['pr.currency', 'pr.price', 'pr.term', 'pr.as_usd', 'pr.next_due_date', 'pr.service_id', 'p.name as provider_name', 'l.name as location', 'o.name as os_name', 'y.*', 'y.id as yabs_id', 'ds.*', 's.*', 'i.address as ip', 'i.is_ipv4']); - }); - } - - public static function serverCompareData(string $server_id) - { - return Cache::remember("server_compare.$server_id", now()->addMonth(1), function () use ($server_id) { - return DB::table('servers as s') - ->join('pricings as pr', 's.id', '=', 'pr.service_id') - ->join('providers as p', 's.provider_id', '=', 'p.id') - ->join('locations as l', 's.location_id', '=', 'l.id') - ->Join('yabs as y', 's.id', '=', 'y.server_id') - ->Join('disk_speed as ds', 'y.id', '=', 'ds.id') - ->where('s.id', '=', $server_id) - ->get(['s.*', 'p.name as provider_name', 'l.name as location', 'pr.*', 'y.*', 'y.id as yabs_id', 'ds.*']); - }); - } - public static function serverYabsAmount(string $server_id): int - { + {//Returns amount of YABs a server has return DB::table('yabs') ->where('server_id', '=', $server_id) ->get()->count(); } + public function yabs() + { + return $this->hasMany(Yabs::class, 'server_id', 'id'); + } + + public function ips() + { + return $this->hasMany(IPs::class, 'service_id', 'id'); + } + + public function location() + { + return $this->hasOne(Locations::class, 'id', 'location_id'); + } + + public function provider() + { + return $this->hasOne(Providers::class, 'id', 'provider_id'); + } + + public function os() + { + return $this->hasOne(OS::class, 'id', 'os_id'); + } + + public function price() + { + return $this->hasOne(Pricing::class, 'service_id', 'id'); + } + + public function labels() + { + return $this->hasMany(LabelsAssigned::class, 'service_id', 'id'); + } + } diff --git a/resources/views/servers/compare.blade.php b/resources/views/servers/compare.blade.php index eaba792..cd11811 100644 --- a/resources/views/servers/compare.blade.php +++ b/resources/views/servers/compare.blade.php @@ -1,4 +1,6 @@ -@section('title') {{'Compare servers'}} @endsection +@section('title') + {{'Compare servers'}} +@endsection @section('style')