Cleaned up server controller

Moved DB calls into models & cached them
This commit is contained in:
cp6 2022-05-15 01:21:51 +10:00
parent ed0d76df25
commit 2f3eaadb25
7 changed files with 172 additions and 101 deletions

View File

@ -9,6 +9,8 @@ use App\Models\Pricing;
use App\Models\Server;
use App\Models\Providers;
use App\Models\Locations;
use App\Models\Settings;
use App\Models\Yabs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
@ -23,34 +25,16 @@ class ServerController extends Controller
public function index()
{
$servers = 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']);
});
$servers = Server::activeServersDataIndexPage();
$non_active_servers = 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']);
});
$non_active_servers = Server::nonActiveServersDataIndexPage();
return view('servers.index', compact(['servers', 'non_active_servers']));
}
public function showServersPublic()
{
$settings = DB::table('settings')
->where('id', '=', 1)
->get();
$settings = Settings::getSettings();
Session::put('timer_version_footer', $settings[0]->show_versions_footer ?? 1);
Session::put('show_servers_public', $settings[0]->show_servers_public ?? 0);
@ -63,17 +47,7 @@ class ServerController extends Controller
Session::save();
if ((Session::get('show_servers_public') === 1)) {
$servers = 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']);
$servers = Server::publicServerData();
return view('servers.public-index', compact('servers'));
}
return response()->view('errors.404', array("status" => 404, "title" => "Page not found", "message" => ""), 404);
@ -145,12 +119,7 @@ class ServerController extends Controller
Labels::insertLabelsAssigned([$request->label1, $request->label2, $request->label3, $request->label4], $server_id);
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('servers_summary');//servers summary cache
Server::serverRelatedCacheForget();
return redirect()->route('servers.index')
->with('success', 'Server Created Successfully.');
@ -158,42 +127,22 @@ class ServerController extends Controller
public function show(Server $server)
{
$server_extras = 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.*']);
$server_extras = Server::serverDataShowPage($server->id);
$network_speeds = json_decode(DB::table('network_speed')
->where('network_speed.server_id', '=', $server->id)
->get(), true);
$network_speeds = Yabs::networkSpeedsForServer($server->id);
$ip_addresses = json_decode(DB::table('ips as i')
->where('i.service_id', '=', $server->id)
->get(), true);
$ip_addresses = IPs::ipsForServer($server->id);
$labels = DB::table('labels_assigned as l')
->join('labels', 'l.label_id', '=', 'labels.id')
->where('l.service_id', '=', $server->id)
->get(['labels.label']);
$labels = Labels::labelsForService($server->id);
return view('servers.show', compact(['server', 'server_extras', 'network_speeds', 'labels', 'ip_addresses']));
}
public function edit(Server $server)
{
$ip_addresses = json_decode(DB::table('ips as i')
->where('i.service_id', '=', $server->id)
->get(), true);
$ip_addresses = IPs::ipsForServer($server->id);
$server = DB::table('servers as s')
->join('pricings as p', 's.id', '=', 'p.service_id')
->where('s.id', '=', $server->id)
->get(['s.*', 'p.*']);
$server = Pricing::pricingForService($server->id);
return view('servers.edit', compact(['server', 'ip_addresses']));
}
@ -259,12 +208,8 @@ class ServerController extends Controller
}
}
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('servers_summary');//servers summary cache
Server::serverRelatedCacheForget();
Server::serverSpecificCacheForget($server_id);
return redirect()->route('servers.index')
->with('success', 'Server Updated Successfully.');
@ -283,12 +228,7 @@ class ServerController extends Controller
IPs::deleteIPsAssignedTo($server->id);
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('servers_summary');//servers summary cache
Server::serverRelatedCacheForget();
return redirect()->route('servers.index')
->with('success', 'Server was deleted Successfully.');
@ -302,40 +242,20 @@ class ServerController extends Controller
public function compareServers($server1, $server2)
{
$server1_data = 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', '=', $server1)
->get(['s.*', 'p.name as provider_name', 'l.name as location', 'pr.*', 'y.*', 'y.id as yabs_id', 'ds.*']);
$server1_data = Server::serverCompareData($server1);
if (count($server1_data) === 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 = DB::table('network_speed')
->where('id', '=', $server1_data[0]->yabs_id)
->get();
$server2_data = 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', '=', $server2)
->get(['s.*', 'p.name as provider_name', 'l.name as location', 'pr.*', 'y.*', 'y.id as yabs_id', 'ds.*']);
$server1_network = Yabs::serverCompareNetwork($server1_data[0]->yabs_id);
$server2_data = Server::serverCompareData($server2);
if (count($server2_data) === 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 = DB::table('network_speed')
->where('id', '=', $server2_data[0]->yabs_id)
->get();
$server2_network = Yabs::serverCompareNetwork($server2_data[0]->yabs_id);
return view('servers.compare', compact('server1_data', 'server2_data', 'server1_network', 'server2_network'));
}
}

View File

@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
@ -35,4 +36,13 @@ class IPs extends Model
);
}
public static function ipsForServer(string $server_id)
{
return Cache::remember("ip_addresses.$server_id", now()->addHour(1), function () use ($server_id) {
return json_decode(DB::table('ips as i')
->where('i.service_id', '=', $server_id)
->get(), true);
});
}
}

View File

@ -41,4 +41,14 @@ class Labels extends Model
});
}
public static function labelsForService(string $service_id)
{
return Cache::remember("labels_for_service.$service_id", now()->addMinute(1), function () use ($service_id) {
return DB::table('labels_assigned as l')
->join('labels', 'l.label_id', '=', 'labels.id')
->where('l.service_id', '=', $service_id)
->get(['labels.label']);
});
}
}

View File

@ -115,4 +115,14 @@ class Pricing extends Model
->get();
});
}
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.*']);
});
}
}

View File

@ -4,6 +4,8 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class Server extends Model
{
@ -11,7 +13,7 @@ class Server extends Model
public $incrementing = false;
protected $fillable = ['id', 'hostname', 'ipv4', 'ipv6', 'server_type', 'os_id', 'location_id', 'provider_id', 'ram', 'disk', 'ram_type', 'disk_type', 'ns1', 'ns2', 'label', 'bandwidth', 'ram_as_mb', 'disk_as_gb', 'has_yabs', 'was_promo', 'owned_since', 'ssh', 'active','show_public'];
protected $fillable = ['id', 'hostname', 'ipv4', 'ipv6', 'server_type', 'os_id', 'location_id', 'provider_id', 'ram', 'disk', 'ram_type', 'disk_type', 'ns1', 'ns2', 'label', 'bandwidth', 'ram_as_mb', 'disk_as_gb', 'has_yabs', 'was_promo', 'owned_since', 'ssh', 'active', 'show_public'];
/**
* @var mixed
*/
@ -152,4 +154,94 @@ class Server extends Model
return $str;
}
public static function serverRelatedCacheForget(): void
{
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('servers_summary');//servers summary cache
Cache::forget('public_server_data');//public servers
}
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("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.*']);
});
}
}

View File

@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Settings extends Model
{
@ -12,4 +13,11 @@ class Settings extends Model
protected $table = 'settings';
protected $fillable = ['id', 'show_versions_footer', 'show_servers_public'];
public static function getSettings()
{
return DB::table('settings')
->where('id', '=', 1)
->get();
}
}

View File

@ -4,6 +4,8 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class Yabs extends Model
{
@ -14,4 +16,23 @@ class Yabs extends Model
protected $table = 'yabs';
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'];
public static function networkSpeedsForServer(string $server_id)
{
return Cache::remember("network_speeds.$server_id", now()->addMonth(1), function () use ($server_id) {
return json_decode(DB::table('network_speed')
->where('network_speed.server_id', '=', $server_id)
->get(), true);
});
}
public static function serverCompareNetwork(string $yabs_id)
{
return Cache::remember("compare_network_speeds.$yabs_id", now()->addMonth(1), function () use ($yabs_id) {
return DB::table('network_speed')
->where('id', '=', $yabs_id)
->get();
});
}
}