Updated Home controller Added Home model
Moved most DB calls and logic into the model
This commit is contained in:
parent
07c0b8a655
commit
8e968a60c9
|
@ -3,24 +3,18 @@
|
|||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\DNS;
|
||||
use App\Models\Home;
|
||||
use App\Models\Labels;
|
||||
use App\Models\Pricing;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Process;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
|
||||
//Custom code example
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
|
@ -31,77 +25,18 @@ class HomeController extends Controller
|
|||
$p = new Process();
|
||||
$p->startTimer();
|
||||
|
||||
$services_count = Cache::remember('services_count', 1440, function () {
|
||||
return DB::table('pricings')
|
||||
->select('service_type', DB::raw('COUNT(*) as amount'))
|
||||
->groupBy('service_type')
|
||||
->where('active', '=', 1)
|
||||
->get();
|
||||
});
|
||||
$services_count = Home::servicesCount();
|
||||
|
||||
$due_soon = Cache::remember('due_soon', 1440, function () {
|
||||
return DB::table('pricings as p')
|
||||
->leftJoin('servers as s', 'p.service_id', '=', 's.id')
|
||||
->leftJoin('shared_hosting as sh', 'p.service_id', '=', 'sh.id')
|
||||
->leftJoin('reseller_hosting as r', 'p.service_id', '=', 'r.id')
|
||||
->leftJoin('domains as d', 'p.service_id', '=', 'd.id')
|
||||
->leftJoin('misc_services as ms', 'p.service_id', '=', 'ms.id')
|
||||
->where('p.active', '=', 1)
|
||||
->orderBy('next_due_date', 'ASC')
|
||||
->limit(Session::get('due_soon_amount'))
|
||||
->get(['p.*', 's.hostname', 'd.domain', 'd.extension', 'r.main_domain as reseller', 'sh.main_domain', 'ms.name']);
|
||||
});
|
||||
$due_soon = Home::dueSoonData();
|
||||
|
||||
$server_summary = Cache::remember('servers_summary', 1440, function () {
|
||||
$cpu_sum = DB::table('servers')->get()->where('active', '=', 1)->sum('cpu');
|
||||
$ram_mb = DB::table('servers')->get()->where('active', '=', 1)->sum('ram_as_mb');
|
||||
$disk_gb = DB::table('servers')->get()->where('active', '=', 1)->sum('disk_as_gb');
|
||||
$bandwidth = DB::table('servers')->get()->where('active', '=', 1)->sum('bandwidth');
|
||||
$locations_sum = DB::table('servers')->get()->where('active', '=', 1)->groupBy('location_id')->count();
|
||||
$providers_sum = DB::table('servers')->get()->where('active', '=', 1)->groupBy('provider_id')->count();
|
||||
return array(
|
||||
'cpu_sum' => $cpu_sum,
|
||||
'ram_mb_sum' => $ram_mb,
|
||||
'disk_gb_sum' => $disk_gb,
|
||||
'bandwidth_sum' => $bandwidth,
|
||||
'locations_sum' => $locations_sum,
|
||||
'providers_sum' => $providers_sum,
|
||||
);
|
||||
});
|
||||
$server_summary = Home::serverSummary();
|
||||
|
||||
//Check for past due date and refresh the due date if so:
|
||||
$pricing = new Pricing();
|
||||
$count = 0;
|
||||
foreach ($due_soon as $service) {
|
||||
if (Carbon::createFromFormat('Y-m-d', $service->next_due_date)->isPast()) {
|
||||
$months = $pricing->termAsMonths($service->term);//Get months for term to update the next due date to
|
||||
$new_due_date = Carbon::createFromFormat('Y-m-d', $service->next_due_date)->addMonths($months)->format('Y-m-d');
|
||||
DB::table('pricings')//Update the DB
|
||||
->where('service_id', $service->service_id)
|
||||
->update(['next_due_date' => $new_due_date]);
|
||||
$due_soon[$count]->next_due_date = $new_due_date;//Update array being sent to view
|
||||
} else {
|
||||
break;//Break because if this date isnt past than the ones after it in the loop wont be either
|
||||
}
|
||||
$count++;
|
||||
}
|
||||
$due_soon = Home::doDueSoon($due_soon);
|
||||
|
||||
Cache::put('due_soon', $due_soon);
|
||||
$recently_added = Home::recentlyAdded();
|
||||
|
||||
$recently_added = Cache::remember('recently_added', 1440, function () {
|
||||
return DB::table('pricings as p')
|
||||
->leftJoin('servers as s', 'p.service_id', '=', 's.id')
|
||||
->leftJoin('shared_hosting as sh', 'p.service_id', '=', 'sh.id')
|
||||
->leftJoin('reseller_hosting as r', 'p.service_id', '=', 'r.id')
|
||||
->leftJoin('domains as d', 'p.service_id', '=', 'd.id')
|
||||
->leftJoin('misc_services as ms', 'p.service_id', '=', 'ms.id')
|
||||
->where('p.active', '=', 1)
|
||||
->orderBy('created_at', 'DESC')
|
||||
->limit(Session::get('recently_added_amount'))
|
||||
->get(['p.*', 's.hostname', 'd.domain', 'd.extension', 'r.main_domain as reseller', 'sh.main_domain', 'ms.name']);
|
||||
});
|
||||
|
||||
$settings = Cache::remember('settings', 15, function () {
|
||||
$settings = Cache::remember('settings', now()->addMinute(1), function () {
|
||||
return DB::table('settings')
|
||||
->where('id', '=', 1)
|
||||
->get();
|
||||
|
@ -118,45 +53,15 @@ class HomeController extends Controller
|
|||
Session::put('show_server_value_location', $settings[0]->show_server_value_location ?? 0);
|
||||
Session::put('default_currency', $settings[0]->default_currency ?? 'USD');
|
||||
Session::put('default_server_os', $settings[0]->default_server_os ?? 1);
|
||||
Session::put('due_soon_amount', $settings[0]->due_soon_amount ?? 6);
|
||||
Session::put('recently_added_amount', $settings[0]->recently_added_amount ?? 6);
|
||||
Session::put('due_soon_amount', $settings[0]->due_soon_amount ?? 6);
|
||||
Session::put('recently_added_amount', $settings[0]->recently_added_amount ?? 6);
|
||||
Session::save();
|
||||
|
||||
$all_pricing = Pricing::allPricing();
|
||||
|
||||
$pricing = json_decode($all_pricing, true);
|
||||
|
||||
$total_cost_weekly = $total_cost_pm = $inactive_count = 0;
|
||||
foreach ($pricing as $price) {
|
||||
if ($price['active'] === 1) {
|
||||
if ($price['term'] === 1) {//1 month
|
||||
$total_cost_weekly += ($price['as_usd'] / 4);
|
||||
$total_cost_pm += $price['as_usd'];
|
||||
} elseif ($price['term'] === 2) {//3 months
|
||||
$total_cost_weekly += ($price['as_usd'] / 12);
|
||||
$total_cost_pm += ($price['as_usd'] / 3);
|
||||
} elseif ($price['term'] === 3) {// 6 month
|
||||
$total_cost_weekly += ($price['as_usd'] / 24);
|
||||
$total_cost_pm += ($price['as_usd'] / 6);
|
||||
} elseif ($price['term'] === 4) {// 1 year
|
||||
$total_cost_weekly += ($price['as_usd'] / 48);
|
||||
$total_cost_pm += ($price['as_usd'] / 12);
|
||||
} elseif ($price['term'] === 5) {//2 years
|
||||
$total_cost_weekly += ($price['as_usd'] / 96);
|
||||
$total_cost_pm += ($price['as_usd'] / 24);
|
||||
} elseif ($price['term'] === 6) {//3 years
|
||||
$total_cost_weekly += ($price['as_usd'] / 144);
|
||||
$total_cost_pm += ($price['as_usd'] / 36);
|
||||
}
|
||||
} else {
|
||||
$inactive_count++;
|
||||
}
|
||||
}
|
||||
$total_cost_yearly = ($total_cost_pm * 12);
|
||||
|
||||
|
||||
$services_count = json_decode($services_count, true);
|
||||
|
||||
$servers_count = $domains_count = $shared_count = $reseller_count = $other_count = $total_services = 0;
|
||||
$servers_count = $domains_count = $shared_count = $reseller_count = $other_count = $seedbox_count = $total_services = 0;
|
||||
|
||||
foreach ($services_count as $sc) {
|
||||
$total_services += $sc['amount'];
|
||||
|
@ -170,9 +75,13 @@ class HomeController extends Controller
|
|||
$domains_count = $sc['amount'];
|
||||
} else if ($sc['service_type'] === 5) {
|
||||
$other_count = $sc['amount'];
|
||||
} else if ($sc['service_type'] === 6) {
|
||||
$seedbox_count = $sc['amount'];
|
||||
}
|
||||
}
|
||||
|
||||
$pricing_breakdown = Home::breakdownPricing($all_pricing);
|
||||
|
||||
$p->stopTimer();
|
||||
|
||||
$information = array(
|
||||
|
@ -181,14 +90,15 @@ class HomeController extends Controller
|
|||
'shared' => $shared_count,
|
||||
'reseller' => $reseller_count,
|
||||
'misc' => $other_count,
|
||||
'seedbox' => $seedbox_count,
|
||||
'labels' => Labels::labelsCount(),
|
||||
'dns' => DNS::dnsCount(),
|
||||
'total_services' => $total_services,
|
||||
'total_inactive' => $inactive_count,
|
||||
'total_cost_weekly' => number_format($total_cost_weekly, 2),
|
||||
'total_cost_monthly' => number_format($total_cost_pm, 2),
|
||||
'total_cost_yearly' => number_format($total_cost_yearly, 2),
|
||||
'total_cost_2_yearly' => number_format(($total_cost_yearly * 2), 2),
|
||||
'total_inactive' => $pricing_breakdown['inactive_count'],
|
||||
'total_cost_weekly' => number_format($pricing_breakdown['total_cost_weekly'], 2),
|
||||
'total_cost_monthly' => number_format($pricing_breakdown['total_cost_montly'], 2),
|
||||
'total_cost_yearly' => number_format($pricing_breakdown['total_cost_yearly'], 2),
|
||||
'total_cost_2_yearly' => number_format(($pricing_breakdown['total_cost_yearly'] * 2), 2),
|
||||
'due_soon' => $due_soon,
|
||||
'newest' => $recently_added,
|
||||
'execution_time' => number_format($p->getTimeTaken(), 2),
|
||||
|
|
154
app/Models/Home.php
Normal file
154
app/Models/Home.php
Normal file
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
|
||||
class Home extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public static function homePageCacheForget()
|
||||
{
|
||||
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_pricing');//All the pricing
|
||||
}
|
||||
|
||||
public static function servicesCount()
|
||||
{
|
||||
return Cache::remember('services_count', now()->addHour(6), function () {
|
||||
return DB::table('pricings')
|
||||
->select('service_type', DB::raw('COUNT(*) as amount'))
|
||||
->groupBy('service_type')
|
||||
->where('active', '=', 1)
|
||||
->get();
|
||||
});
|
||||
}
|
||||
|
||||
public static function dueSoonData()
|
||||
{
|
||||
return Cache::remember('due_soon', now()->addHour(6), function () {
|
||||
return DB::table('pricings as p')
|
||||
->leftJoin('servers as s', 'p.service_id', '=', 's.id')
|
||||
->leftJoin('shared_hosting as sh', 'p.service_id', '=', 'sh.id')
|
||||
->leftJoin('reseller_hosting as r', 'p.service_id', '=', 'r.id')
|
||||
->leftJoin('domains as d', 'p.service_id', '=', 'd.id')
|
||||
->leftJoin('misc_services as ms', 'p.service_id', '=', 'ms.id')
|
||||
->leftJoin('seedboxes as sb', 'p.service_id', '=', 'sb.id')
|
||||
->where('p.active', '=', 1)
|
||||
->orderBy('next_due_date', 'ASC')
|
||||
->limit(Session::get('due_soon_amount'))
|
||||
->get(['p.*', 's.hostname', 'd.domain', 'd.extension', 'r.main_domain as reseller', 'sh.main_domain', 'ms.name', 'sb.title']);
|
||||
});
|
||||
}
|
||||
|
||||
public static function serverSummary()
|
||||
{
|
||||
return Cache::remember('servers_summary', now()->addHour(6), function () {
|
||||
$cpu_sum = DB::table('servers')->get()->where('active', '=', 1)->sum('cpu');
|
||||
$ram_mb = DB::table('servers')->get()->where('active', '=', 1)->sum('ram_as_mb');
|
||||
$disk_gb = DB::table('servers')->get()->where('active', '=', 1)->sum('disk_as_gb');
|
||||
$bandwidth = DB::table('servers')->get()->where('active', '=', 1)->sum('bandwidth');
|
||||
$locations_sum = DB::table('servers')->get()->where('active', '=', 1)->groupBy('location_id')->count();
|
||||
$providers_sum = DB::table('servers')->get()->where('active', '=', 1)->groupBy('provider_id')->count();
|
||||
return array(
|
||||
'cpu_sum' => $cpu_sum,
|
||||
'ram_mb_sum' => $ram_mb,
|
||||
'disk_gb_sum' => $disk_gb,
|
||||
'bandwidth_sum' => $bandwidth,
|
||||
'locations_sum' => $locations_sum,
|
||||
'providers_sum' => $providers_sum,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public static function recentlyAdded()
|
||||
{
|
||||
return Cache::remember('recently_added', now()->addHour(6), function () {
|
||||
return DB::table('pricings as p')
|
||||
->leftJoin('servers as s', 'p.service_id', '=', 's.id')
|
||||
->leftJoin('shared_hosting as sh', 'p.service_id', '=', 'sh.id')
|
||||
->leftJoin('reseller_hosting as r', 'p.service_id', '=', 'r.id')
|
||||
->leftJoin('domains as d', 'p.service_id', '=', 'd.id')
|
||||
->leftJoin('misc_services as ms', 'p.service_id', '=', 'ms.id')
|
||||
->leftJoin('seedboxes as sb', 'p.service_id', '=', 'sb.id')
|
||||
->where('p.active', '=', 1)
|
||||
->orderBy('created_at', 'DESC')
|
||||
->limit(Session::get('recently_added_amount'))
|
||||
->get(['p.*', 's.hostname', 'd.domain', 'd.extension', 'r.main_domain as reseller', 'sh.main_domain', 'ms.name', 'sb.title']);
|
||||
});
|
||||
}
|
||||
|
||||
public static function doDueSoon($due_soon)
|
||||
{
|
||||
$pricing = new Pricing();
|
||||
$count = $altered_due_soon = 0;
|
||||
foreach ($due_soon as $service) {
|
||||
if (Carbon::createFromFormat('Y-m-d', $service->next_due_date)->isPast()) {
|
||||
$months = $pricing->termAsMonths($service->term);//Get months for term to update the next due date to
|
||||
$new_due_date = Carbon::createFromFormat('Y-m-d', $service->next_due_date)->addMonths($months)->format('Y-m-d');
|
||||
DB::table('pricings')//Update the DB
|
||||
->where('service_id', $service->service_id)
|
||||
->update(['next_due_date' => $new_due_date]);
|
||||
$due_soon[$count]->next_due_date = $new_due_date;//Update array being sent to view
|
||||
$altered_due_soon = 1;
|
||||
} else {
|
||||
break;//Break because if this date isnt past than the ones after it in the loop wont be either
|
||||
}
|
||||
$count++;
|
||||
}
|
||||
|
||||
if ($altered_due_soon === 1) {//Made changes to due soon so re-write it
|
||||
Cache::put('due_soon', $due_soon);
|
||||
}
|
||||
|
||||
return $due_soon;
|
||||
}
|
||||
|
||||
public static function breakdownPricing($all_pricing): array
|
||||
{
|
||||
$pricing = json_decode($all_pricing, true);
|
||||
|
||||
$total_cost_weekly = $total_cost_pm = $inactive_count = 0;
|
||||
foreach ($pricing as $price) {
|
||||
if ($price['active'] === 1) {
|
||||
if ($price['term'] === 1) {//1 month
|
||||
$total_cost_weekly += ($price['as_usd'] / 4);
|
||||
$total_cost_pm += $price['as_usd'];
|
||||
} elseif ($price['term'] === 2) {//3 months
|
||||
$total_cost_weekly += ($price['as_usd'] / 12);
|
||||
$total_cost_pm += ($price['as_usd'] / 3);
|
||||
} elseif ($price['term'] === 3) {// 6 month
|
||||
$total_cost_weekly += ($price['as_usd'] / 24);
|
||||
$total_cost_pm += ($price['as_usd'] / 6);
|
||||
} elseif ($price['term'] === 4) {// 1 year
|
||||
$total_cost_weekly += ($price['as_usd'] / 48);
|
||||
$total_cost_pm += ($price['as_usd'] / 12);
|
||||
} elseif ($price['term'] === 5) {//2 years
|
||||
$total_cost_weekly += ($price['as_usd'] / 96);
|
||||
$total_cost_pm += ($price['as_usd'] / 24);
|
||||
} elseif ($price['term'] === 6) {//3 years
|
||||
$total_cost_weekly += ($price['as_usd'] / 144);
|
||||
$total_cost_pm += ($price['as_usd'] / 36);
|
||||
}
|
||||
} else {
|
||||
$inactive_count++;
|
||||
}
|
||||
}
|
||||
$total_cost_yearly = ($total_cost_pm * 12);
|
||||
|
||||
return array(
|
||||
'total_cost_weekly' => $total_cost_weekly,
|
||||
'total_cost_montly' => $total_cost_pm,
|
||||
'total_cost_yearly' => $total_cost_yearly,
|
||||
'inactive_count' => $inactive_count,
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user