Merge branch 'Admin-overview-fixed-counters'

This commit is contained in:
ok236449 2022-08-24 22:23:49 +02:00
commit 3da23fdc7e
7 changed files with 251 additions and 57 deletions

View file

@ -13,6 +13,8 @@ use App\Models\User;
use Illuminate\Support\Facades\Cache;
use App\Classes\Pterodactyl;
use App\Models\Product;
use App\Models\Ticket;
use Carbon\Carbon;
class OverViewController extends Controller
{
@ -20,61 +22,126 @@ class OverViewController extends Controller
public function index()
{
$counters = Cache::remember('counters', self::TTL, function () {
$output = collect();
$output->put('users', User::query()->count());
$output->put('credits', number_format(User::query()->where("role","!=","admin")->sum('credits'), 2, '.', ''));
$output->put('payments', Payment::query()->count());
$output->put('eggs', Egg::query()->count());
$output->put('nests', Nest::query()->count());
$output->put('locations', Location::query()->count());
//Get counters
$counters = collect();
//Set basic variables in the collection
$counters->put('users', User::query()->count());
$counters->put('credits', number_format(User::query()->where("role","!=","admin")->sum('credits'), 2, '.', ''));
$counters->put('payments', Payment::query()->count());
$counters->put('eggs', Egg::query()->count());
$counters->put('nests', Nest::query()->count());
$counters->put('locations', Location::query()->count());
$output->put('servers', collect());
$output['servers']->active = 0;
$output['servers']->total = 0;
$output->put('earnings', collect());
$output['earnings']->active = 0;
$output['earnings']->total = 0;
$output->put('totalUsagePercent', 0);
return $output;
});
//Prepare for counting
$counters->put('servers', collect());
$counters['servers']->active = 0;
$counters['servers']->total = 0;
$counters->put('earnings', collect());
$counters['earnings']->active = 0;
$counters['earnings']->total = 0;
$counters->put('totalUsagePercent', 0);
//Prepare subCollection 'payments'
$counters->put('payments', collect());
//Get and save payments from last 2 months for later filtering and looping
$payments = Payment::query()->where('created_at', '>=', Carbon::today()->startOfMonth()->subMonth())->where('status', 'paid')->get();
//Prepare collections and set a few variables
$counters['payments']->put('thisMonth', collect());
$counters['payments']->put('lastMonth', collect());
$counters['payments']['thisMonth']->timeStart = Carbon::today()->startOfMonth()->toDateString();
$counters['payments']['thisMonth']->timeEnd = Carbon::today()->toDateString();
$counters['payments']['lastMonth']->timeStart = Carbon::today()->startOfMonth()->subMonth()->toDateString();
$counters['payments']['lastMonth']->timeEnd = Carbon::today()->endOfMonth()->subMonth()->toDateString();
//Fill out variables for each currency separately
foreach($payments->where('created_at', '>=', Carbon::today()->startOfMonth()) as $payment){
$paymentCurrency = $payment->currency_code;
if(!isset($counters['payments']['thisMonth'][$paymentCurrency])){
$counters['payments']['thisMonth']->put($paymentCurrency, collect());
$counters['payments']['thisMonth'][$paymentCurrency]->total = 0;
$counters['payments']['thisMonth'][$paymentCurrency]->count = 0;
}
$counters['payments']['thisMonth'][$paymentCurrency]->total += $payment->total_price;
$counters['payments']['thisMonth'][$paymentCurrency]->count ++;
}
foreach($payments->where('created_at', '<', Carbon::today()->startOfMonth()) as $payment){
$paymentCurrency = $payment->currency_code;
if(!isset($counters['payments']['lastMonth'][$paymentCurrency])){
$counters['payments']['lastMonth']->put($paymentCurrency, collect());
$counters['payments']['lastMonth'][$paymentCurrency]->total = 0;
$counters['payments']['lastMonth'][$paymentCurrency]->count = 0;
}
$counters['payments']['lastMonth'][$paymentCurrency]->total += $payment->total_price;
$counters['payments']['lastMonth'][$paymentCurrency]->count ++;
}
$counters['payments']->total = Payment::query()->count();
$lastEgg = Egg::query()->latest('updated_at')->first();
$syncLastUpdate = $lastEgg ? $lastEgg->updated_at->isoFormat('LLL') : __('unknown');
$nodes = Cache::remember('nodes', self::TTL, function() use($counters){
$output = collect();
foreach($nodes = Node::query()->get() as $node){ //gets all node information and prepares the structure
$nodeId = $node['id'];
$output->put($nodeId, collect());
$output[$nodeId]->name = $node['name'];
$node = Pterodactyl::getNode($nodeId);
$output[$nodeId]->usagePercent = round(max($node['allocated_resources']['memory']/($node['memory']*($node['memory_overallocate']+100)/100), $node['allocated_resources']['disk']/($node['disk']*($node['disk_overallocate']+100)/100))*100, 2);
$counters['totalUsagePercent'] += $output[$nodeId]->usagePercent;
$output[$nodeId]->totalServers = 0;
$output[$nodeId]->activeServers = 0;
$output[$nodeId]->totalEarnings = 0;
$output[$nodeId]->activeEarnings = 0;
//Get node information
$nodes = collect();
foreach($DBnodes = Node::query()->get() as $DBnode){ //gets all node information and prepares the structure
$nodeId = $DBnode['id'];
$nodes->put($nodeId, collect());
$nodes[$nodeId]->name = $DBnode['name'];
$pteroNode = Pterodactyl::getNode($nodeId);
$nodes[$nodeId]->usagePercent = round(max($pteroNode['allocated_resources']['memory']/($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100), $pteroNode['allocated_resources']['disk']/($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100))*100, 2);
$counters['totalUsagePercent'] += $nodes[$nodeId]->usagePercent;
$nodes[$nodeId]->totalServers = 0;
$nodes[$nodeId]->activeServers = 0;
$nodes[$nodeId]->totalEarnings = 0;
$nodes[$nodeId]->activeEarnings = 0;
}
$counters['totalUsagePercent'] = ($DBnodes->count())?round($counters['totalUsagePercent']/$DBnodes->count(), 2):0;
foreach(Pterodactyl::getServers() as $server){ //gets all servers from Pterodactyl and calculates total of credit usage for each node separately + total
$nodeId = $server['attributes']['node'];
if($CPServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first()){
$prize = Product::query()->where('id', $CPServer->product_id)->first()->price;
if (!$CPServer->suspended){
$counters['earnings']->active += $prize;
$counters['servers']->active ++;
$nodes[$nodeId]->activeEarnings += $prize;
$nodes[$nodeId]->activeServers ++;
}
$counters['earnings']->total += $prize;
$counters['servers']->total ++;
$nodes[$nodeId]->totalEarnings += $prize;
$nodes[$nodeId]->totalServers ++;
}
$counters['totalUsagePercent'] = ($nodes->count())?round($counters['totalUsagePercent']/$nodes->count(), 2):0;
}
foreach(Pterodactyl::getServers() as $server){ //gets all servers from Pterodactyl and calculates total of credit usage for each node separately + total
$nodeId = $server['attributes']['node'];
if($CPServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first()){
$prize = Product::query()->where('id', $CPServer->product_id)->first()->price;
if (!$CPServer->suspended){
$counters['earnings']->active += $prize;
$counters['servers']->active ++;
$output[$nodeId]->activeEarnings += $prize;
$output[$nodeId]->activeServers ++;
}
$counters['earnings']->total += $prize;
$counters['servers']->total ++;
$output[$nodeId]->totalEarnings += $prize;
$output[$nodeId]->totalServers ++;
//Get latest tickets
$tickets = Cache::remember('tickets', self::TTL, function(){
$output = collect();
foreach(Ticket::query()->latest()->take(3)->get() as $ticket){
$output->put($ticket->ticket_id, collect());
$output[$ticket->ticket_id]->title = $ticket->title;
$user = User::query()->where('id', $ticket->user_id)->first();
$output[$ticket->ticket_id]->user_id = $user->id;
$output[$ticket->ticket_id]->user = $user->name;
$output[$ticket->ticket_id]->status = $ticket->status;
$output[$ticket->ticket_id]->last_updated = $ticket->updated_at->diffForHumans();
switch ($ticket->status) {
case 'Open':
$output[$ticket->ticket_id]->statusBadgeColor = 'badge-success';
break;
case 'Closed':
$output[$ticket->ticket_id]->statusBadgeColor = 'badge-danger';
break;
case 'Answered':
$output[$ticket->ticket_id]->statusBadgeColor = 'badge-info';
break;
default:
$output[$ticket->ticket_id]->statusBadgeColor = 'badge-warning';
break;
}
}
return $output;
@ -84,7 +151,8 @@ class OverViewController extends Controller
'counters' => $counters,
'nodes' => $nodes,
'syncLastUpdate' => $syncLastUpdate,
'perPageLimit' => ($counters['servers']->total != Server::query()->count())?true:false
'perPageLimit' => ($counters['servers']->total != Server::query()->count())?true:false,
'tickets' => $tickets
]);
}

View file

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Classes\Pterodactyl;
use App\Models\Egg;
use App\Models\Location;
use App\Models\Node;
@ -55,6 +56,10 @@ class ProductController extends Controller
public function getLocationsBasedOnEgg(Request $request, Egg $egg)
{
$nodes = $this->getNodesBasedOnEgg($request, $egg);
foreach($nodes as $key => $node){
$pteroNode = Pterodactyl::getNode($node->id);
if($pteroNode['allocated_resources']['memory']>=($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)||$pteroNode['allocated_resources']['disk']>=($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)) $nodes->forget($key);
}
$locations = collect();
//locations
@ -87,7 +92,7 @@ class ProductController extends Controller
{
if (is_null($egg->id) || is_null($node->id)) return response()->json('node and egg id is required', '400');
return Product::query()
$products = Product::query()
->where('disabled', '=', false)
->whereHas('nodes', function (Builder $builder) use ($node) {
$builder->where('id', '=', $node->id);
@ -96,5 +101,12 @@ class ProductController extends Controller
$builder->where('id', '=', $egg->id);
})
->get();
$pteroNode = Pterodactyl::getNode($node->id);
foreach($products as $key => $product){
if($product->memory>($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)-$pteroNode['allocated_resources']['memory']||$product->disk>($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)-$pteroNode['allocated_resources']['disk']) $product->doesNotFit = true;
}
return $products;
}
}

View file

@ -241,6 +241,9 @@ class ServerController extends Controller
$serverRelationships = $serverAttributes['relationships'];
$serverLocationAttributes = $serverRelationships['location']['attributes'];
//Get current product
$currentProduct = Product::where('id', $server->product_id)->first();
//Set server infos
$server->location = $serverLocationAttributes['long'] ?
$serverLocationAttributes['long'] :
@ -249,11 +252,19 @@ class ServerController extends Controller
$server->node = $serverRelationships['node']['attributes']['name'];
$server->name = $serverAttributes['name'];
$server->egg = $serverRelationships['egg']['attributes']['name'];
$products = Product::orderBy("created_at")->get();
$pteroNode = Pterodactyl::getNode($serverRelationships['node']['attributes']['id']);
$products = Product::orderBy("created_at")
->whereHas('nodes', function (Builder $builder) use ($serverRelationships) { //Only show products for that node
$builder->where('id', '=', $serverRelationships['node']['attributes']['id']);
})
->get();
// Set the each product eggs array to just contain the eggs name
foreach ($products as $product) {
$product->eggs = $product->eggs->pluck('name')->toArray();
if($product->memory-$currentProduct->memory>($pteroNode['memory']*($pteroNode['memory_overallocate']+100)/100)-$pteroNode['allocated_resources']['memory']||$product->disk-$currentProduct->disk>($pteroNode['disk']*($pteroNode['disk_overallocate']+100)/100)-$pteroNode['allocated_resources']['disk']) $product->doesNotFit = true;
}
return view('servers.settings')->with([

View file

@ -90,7 +90,7 @@
<div class="info-box-content">
<span class="info-box-text">{{__('Payments')}}</span>
<span class="info-box-number">{{$counters['payments']}}</span>
<span class="info-box-number">{{$counters['payments']->total}}</span>
</div>
<!-- /.info-box-content -->
</div>
@ -142,6 +142,42 @@
<span><i class="fas fa-sync mr-2"></i>{{__('Last updated :date', ['date' => $syncLastUpdate])}}</span>
</div>
</div>
<div class="card">
<div class="card-header">
<div class="d-flex justify-content-between">
<div class="card-title ">
<span><i class="fas fa-ticket-alt mr-2"></i>{{__('Latest tickets')}}</span>
</div>
</div>
</div>
<div class="card-body py-1">
@if(!$tickets->count())<span style="font-size: 16px; font-weight:700">{{__('There are no tickets')}}.</span>
@else
<table class="table">
<thead>
<tr>
<th>{{__('Title')}}</th>
<th>{{__('User')}}</th>
<th>{{__('Status')}}</th>
<th>{{__('Last updated')}}</th>
</tr>
</thead>
<tbody>
@foreach($tickets as $ticket_id => $ticket)
<tr>
<td><a class="text-info" href="{{route('moderator.ticket.show', ['ticket_id' => $ticket_id])}}">#{{$ticket_id}} - {{$ticket->title}}</td>
<td><a href="{{route('admin.users.show', $ticket->user_id)}}">{{$ticket->user}}</a></td>
<td><span class="badge {{$ticket->statusBadgeColor}}">{{$ticket->status}}</span></td>
<td>{{$ticket->last_updated}}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
</div>
</div>
<div class="card">
<div class="card-header">
<div class="d-flex justify-content-between">
@ -163,7 +199,7 @@
<div class="card-header">
<div class="d-flex justify-content-between">
<div class="card-title ">
<span><i class="fas fa-file-invoice-dollar mr-2"></i>{{__('Individual nodes')}}</span>
<span><i class="fas fa-server mr-2"></i>{{__('Individual nodes')}}</span>
</div>
</div>
</div>
@ -210,6 +246,71 @@
</table>
</div>
</div>
<div class="card">
<div class="card-header">
<div class="d-flex justify-content-between">
<div class="card-title ">
<span><i class="fas fa-file-invoice-dollar mr-2"></i>{{__('Latest payments')}}</span>
</div>
</div>
</div>
<div class="card-body py-1">
<div class="row">
<div class="col-md-6" style="border-right:1px solid #6c757d">
<span style="margin:auto; display:table; font-size: 18px; font-weight:700">{{__('Last month')}}:
<i data-toggle="popover" data-trigger="hover" data-html="true"
data-content="{{ __('Payments in this time window') }}:<br>{{$counters['payments']['lastMonth']->timeStart}} - {{$counters['payments']['lastMonth']->timeEnd}}"
class="fas fa-info-circle"></i>
</span>
<table class="table">
<thead>
<tr>
<th><b>{{__('Currency')}}</b></th>
<th>{{__('Number of payments')}}</th>
<th>{{__('Total income')}}</th>
</tr>
</thead>
<tbody>
@foreach($counters['payments']['lastMonth'] as $currency => $income)
<tr>
<td>{{$currency}}</td>
<td>{{$income->count}}</td>
<td>{{$income->total}}</td>
</tr>
@endforeach
</tbody>
</table>
<hr style="width: 100%; height:1px; border-width:0; background-color:#6c757d; margin-top: -16px">
</div><div class="col-md-6">
<span style="margin:auto; display:table; font-size: 18px; font-weight:700">{{__('This month')}}:
<i data-toggle="popover" data-trigger="hover" data-html="true"
data-content="{{ __('Payments in this time window') }}:<br>{{$counters['payments']['thisMonth']->timeStart}} - {{$counters['payments']['thisMonth']->timeEnd}}"
class="fas fa-info-circle"></i>
</span>
<table class="table">
<thead>
<tr>
<th><b>{{__('Currency')}}</b></th>
<th>{{__('Number of payments')}}</th>
<th>{{__('Total income')}}</th>
</tr>
</thead>
<tbody>
@foreach($counters['payments']['thisMonth'] as $currency => $income)
<tr>
<td>{{$currency}}</td>
<td>{{$income->count}}</td>
<td>{{$income->total}}</td>
</tr>
@endforeach
</tbody>
</table>
<hr style="width: 100%; height:1px; border-width:0; background-color:#6c757d; margin-top: -16px">
</div>
</div>
</div>
</div>
</div>
</div>

View file

@ -51,11 +51,13 @@
@endif
</p>
<p><b>Created on:</b> {{ $ticket->created_at->diffForHumans() }}</p>
@if($ticket->status!='Closed')
<form class="d-inline" method="post" action="{{route('moderator.ticket.close', ['ticket_id' => $ticket->ticket_id ])}}">
{{csrf_field()}}
{{method_field("POST") }}
<button data-content="{{__("Close")}}" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm text-white btn-warning mr-1"><i class="fas fa-times"></i>{{__("Close")}}</button>
</form>
@endif
</div>
</div>
</div>

View file

@ -218,10 +218,10 @@
</div>
</div>
<button type="submit" x-model="selectedProduct" name="product"
:disabled="product.minimum_credits > user.credits"
:disabled="product.minimum_credits > user.credits||product.doesNotFit == true"
:class="product.minimum_credits > user.credits ? 'disabled' : ''"
class="btn btn-primary btn-block mt-2" @click="setProduct(product.id)"
x-text=" product.minimum_credits > user.credits ? '{{ __('Not enough') }} {{ CREDITS_DISPLAY_NAME }}!' : '{{ __('Create server') }}'">
x-text=" product.doesNotFit == true? '{{ __('Server can´t fit on this node') }}' : (product.minimum_credits > user.credits ? '{{ __('Not enough') }} {{ CREDITS_DISPLAY_NAME }}!' : '{{ __('Create server') }}')">
</button>
</div>
</div>

View file

@ -255,13 +255,13 @@
<option value="">{{__("Select the product")}}</option>
@foreach($products as $product)
@if(in_array($server->egg, $product->eggs) && $product->id != $server->product->id && $product->disabled == false)
<option value="{{ $product->id }}">{{ $product->name }} [ {{ CREDITS_DISPLAY_NAME }} {{ $product->price }} @if($product->minimum_credits!=-1) /
{{__("Required")}}: {{$product->minimum_credits}} {{ CREDITS_DISPLAY_NAME }}@endif ]</option>
<option value="{{ $product->id }}" @if($product->doesNotFit)disabled @endif>{{ $product->name }} [ {{ CREDITS_DISPLAY_NAME }} {{ $product->price }} @if($product->doesNotFit)] {{__('Server can´t fit on this node')}} @else @if($product->minimum_credits!=-1) /
{{__("Required")}}: {{$product->minimum_credits}} {{ CREDITS_DISPLAY_NAME }}@endif ] @endif</option>
@endif
@endforeach
</select>
<br> {{__("Once the Upgrade button is pressed, we will automatically deduct the amount for the first hour according to the new product from your credits")}}. <br>
<br> {{_("Server will be automatically restarted once upgraded")}}
<br> {{__("Server will be automatically restarted once upgraded")}}
</div>
<div class="modal-footer card-body">
<button type="submit" class="btn btn-primary upgrade-once" style="width: 100%"><strong>{{__("Change Product")}}</strong></button>