2021-11-07 00:47:16 +00:00
< ? php
namespace App\Http\Controllers\Admin ;
2023-02-03 16:38:03 +00:00
use App\Classes\PterodactylClient ;
use App\Settings\PterodactylSettings ;
2023-02-06 20:34:58 +00:00
use App\Settings\GeneralSettings ;
2021-11-07 00:47:16 +00:00
use App\Http\Controllers\Controller ;
2023-02-03 16:38:03 +00:00
use App\Models\Pterodactyl\Egg ;
use App\Models\Pterodactyl\Location ;
use App\Models\Pterodactyl\Nest ;
use App\Models\Pterodactyl\Node ;
2021-11-07 00:47:16 +00:00
use App\Models\Payment ;
2022-08-19 17:18:44 +00:00
use App\Models\Product ;
2023-01-05 17:01:42 +00:00
use App\Models\Server ;
2022-08-20 19:19:27 +00:00
use App\Models\Ticket ;
2023-01-05 17:01:42 +00:00
use App\Models\User ;
2022-08-20 15:07:54 +00:00
use Carbon\Carbon ;
2021-11-07 00:47:16 +00:00
class OverViewController extends Controller
{
2023-05-04 23:11:55 +00:00
const READ_PERMISSION = " admin.overview.read " ;
const SYNC_PERMISSION = " admin.overview.sync " ;
2021-11-07 00:47:16 +00:00
public const TTL = 86400 ;
2023-02-06 20:16:54 +00:00
private $pterodactyl ;
public function __construct ( PterodactylSettings $ptero_settings )
{
$this -> pterodactyl = new PterodactylClient ( $ptero_settings );
}
2023-05-04 23:11:55 +00:00
2023-02-06 20:34:58 +00:00
public function index ( GeneralSettings $general_settings )
2021-11-07 00:47:16 +00:00
{
2023-05-04 23:11:55 +00:00
$this -> checkPermission ( self :: READ_PERMISSION );
2022-08-24 20:18:29 +00:00
//Get counters
$counters = collect ();
//Set basic variables in the collection
$counters -> put ( 'users' , User :: query () -> count ());
2023-01-05 17:01:42 +00:00
$counters -> put ( 'credits' , number_format ( User :: query () -> where ( 'role' , '!=' , 'admin' ) -> sum ( 'credits' ), 2 , '.' , '' ));
2022-08-24 20:18:29 +00:00
$counters -> put ( 'payments' , Payment :: query () -> count ());
$counters -> put ( 'eggs' , Egg :: query () -> count ());
$counters -> put ( 'nests' , Nest :: query () -> count ());
$counters -> put ( 'locations' , Location :: query () -> count ());
//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 ();
2023-01-04 17:57:57 +00:00
//Prepare collections
2022-08-24 20:18:29 +00:00
$counters [ 'payments' ] -> put ( 'thisMonth' , collect ());
$counters [ 'payments' ] -> put ( 'lastMonth' , collect ());
2022-10-14 20:59:02 +00:00
//Prepare subCollection 'taxPayments'
$counters -> put ( 'taxPayments' , collect ());
//Get and save taxPayments from last 2 years for later filtering and looping
$taxPayments = Payment :: query () -> where ( 'created_at' , '>=' , Carbon :: today () -> startOfYear () -> subYear ()) -> where ( 'status' , 'paid' ) -> get ();
2023-01-04 17:57:57 +00:00
//Prepare collections
2022-10-14 20:59:02 +00:00
$counters [ 'taxPayments' ] -> put ( 'thisYear' , collect ());
$counters [ 'taxPayments' ] -> put ( 'lastYear' , collect ());
2023-01-05 17:01:42 +00:00
2022-08-24 20:18:29 +00:00
//Fill out variables for each currency separately
2023-01-05 17:01:42 +00:00
foreach ( $payments -> where ( 'created_at' , '>=' , Carbon :: today () -> startOfMonth ()) as $payment ) {
2022-08-24 20:18:29 +00:00
$paymentCurrency = $payment -> currency_code ;
2023-01-05 17:01:42 +00:00
if ( ! isset ( $counters [ 'payments' ][ 'thisMonth' ][ $paymentCurrency ])) {
2022-08-24 20:18:29 +00:00
$counters [ 'payments' ][ 'thisMonth' ] -> put ( $paymentCurrency , collect ());
$counters [ 'payments' ][ 'thisMonth' ][ $paymentCurrency ] -> total = 0 ;
$counters [ 'payments' ][ 'thisMonth' ][ $paymentCurrency ] -> count = 0 ;
2022-08-20 15:07:54 +00:00
}
2022-08-24 20:18:29 +00:00
$counters [ 'payments' ][ 'thisMonth' ][ $paymentCurrency ] -> total += $payment -> total_price ;
2023-01-05 17:01:42 +00:00
$counters [ 'payments' ][ 'thisMonth' ][ $paymentCurrency ] -> count ++ ;
2022-08-24 20:18:29 +00:00
}
2023-01-05 17:01:42 +00:00
foreach ( $payments -> where ( 'created_at' , '<' , Carbon :: today () -> startOfMonth ()) as $payment ) {
2022-08-24 20:18:29 +00:00
$paymentCurrency = $payment -> currency_code ;
2023-01-05 17:01:42 +00:00
if ( ! isset ( $counters [ 'payments' ][ 'lastMonth' ][ $paymentCurrency ])) {
2022-08-24 20:18:29 +00:00
$counters [ 'payments' ][ 'lastMonth' ] -> put ( $paymentCurrency , collect ());
$counters [ 'payments' ][ 'lastMonth' ][ $paymentCurrency ] -> total = 0 ;
$counters [ 'payments' ][ 'lastMonth' ][ $paymentCurrency ] -> count = 0 ;
2022-08-20 15:07:54 +00:00
}
2022-08-24 20:18:29 +00:00
$counters [ 'payments' ][ 'lastMonth' ][ $paymentCurrency ] -> total += $payment -> total_price ;
2023-01-05 17:01:42 +00:00
$counters [ 'payments' ][ 'lastMonth' ][ $paymentCurrency ] -> count ++ ;
2022-08-24 20:18:29 +00:00
}
2023-01-04 17:57:57 +00:00
//sort currencies alphabetically and set some additional variables
$counters [ 'payments' ][ 'thisMonth' ] = $counters [ 'payments' ][ 'thisMonth' ] -> sortKeys ();
$counters [ 'payments' ][ 'thisMonth' ] -> timeStart = Carbon :: today () -> startOfMonth () -> toDateString ();
$counters [ 'payments' ][ 'thisMonth' ] -> timeEnd = Carbon :: today () -> toDateString ();
$counters [ 'payments' ][ 'lastMonth' ] = $counters [ 'payments' ][ 'lastMonth' ] -> sortKeys ();
$counters [ 'payments' ][ 'lastMonth' ] -> timeStart = Carbon :: today () -> startOfMonth () -> subMonth () -> toDateString ();
$counters [ 'payments' ][ 'lastMonth' ] -> timeEnd = Carbon :: today () -> endOfMonth () -> subMonth () -> toDateString ();
2022-08-24 20:18:29 +00:00
$counters [ 'payments' ] -> total = Payment :: query () -> count ();
2021-11-07 00:47:16 +00:00
2023-01-05 23:13:50 +00:00
2023-01-04 17:57:57 +00:00
foreach ( $taxPayments -> where ( 'created_at' , '>=' , Carbon :: today () -> startOfYear ()) as $taxPayment ){
$paymentCurrency = $taxPayment -> currency_code ;
2022-10-14 20:59:02 +00:00
if ( ! isset ( $counters [ 'taxPayments' ][ 'thisYear' ][ $paymentCurrency ])){
2023-01-05 23:13:50 +00:00
2022-10-14 20:59:02 +00:00
$counters [ 'taxPayments' ][ 'thisYear' ] -> put ( $paymentCurrency , collect ());
$counters [ 'taxPayments' ][ 'thisYear' ][ $paymentCurrency ] -> total = 0 ;
$counters [ 'taxPayments' ][ 'thisYear' ][ $paymentCurrency ] -> count = 0 ;
$counters [ 'taxPayments' ][ 'thisYear' ][ $paymentCurrency ] -> price = 0 ;
$counters [ 'taxPayments' ][ 'thisYear' ][ $paymentCurrency ] -> taxes = 0 ;
}
$counters [ 'taxPayments' ][ 'thisYear' ][ $paymentCurrency ] -> total += $taxPayment -> total_price ;
2023-01-05 17:01:42 +00:00
$counters [ 'taxPayments' ][ 'thisYear' ][ $paymentCurrency ] -> count ++ ;
2022-10-14 20:59:02 +00:00
$counters [ 'taxPayments' ][ 'thisYear' ][ $paymentCurrency ] -> price += $taxPayment -> price ;
$counters [ 'taxPayments' ][ 'thisYear' ][ $paymentCurrency ] -> taxes += $taxPayment -> tax_value ;
}
2023-01-05 23:13:50 +00:00
2023-01-04 17:57:57 +00:00
foreach ( $taxPayments -> where ( 'created_at' , '>=' , Carbon :: today () -> startOfYear () -> subYear ()) -> where ( 'created_at' , '<' , Carbon :: today () -> startOfYear ()) as $taxPayment ){
$paymentCurrency = $taxPayment -> currency_code ;
2022-10-14 20:59:02 +00:00
if ( ! isset ( $counters [ 'taxPayments' ][ 'lastYear' ][ $paymentCurrency ])){
2023-01-05 23:13:50 +00:00
2022-10-14 20:59:02 +00:00
$counters [ 'taxPayments' ][ 'lastYear' ] -> put ( $paymentCurrency , collect ());
$counters [ 'taxPayments' ][ 'lastYear' ][ $paymentCurrency ] -> total = 0 ;
$counters [ 'taxPayments' ][ 'lastYear' ][ $paymentCurrency ] -> count = 0 ;
$counters [ 'taxPayments' ][ 'lastYear' ][ $paymentCurrency ] -> price = 0 ;
$counters [ 'taxPayments' ][ 'lastYear' ][ $paymentCurrency ] -> taxes = 0 ;
}
$counters [ 'taxPayments' ][ 'lastYear' ][ $paymentCurrency ] -> total += $taxPayment -> total_price ;
2023-01-05 17:01:42 +00:00
$counters [ 'taxPayments' ][ 'lastYear' ][ $paymentCurrency ] -> count ++ ;
2022-10-14 20:59:02 +00:00
$counters [ 'taxPayments' ][ 'lastYear' ][ $paymentCurrency ] -> price += $taxPayment -> price ;
$counters [ 'taxPayments' ][ 'lastYear' ][ $paymentCurrency ] -> taxes += $taxPayment -> tax_value ;
}
2023-01-04 17:57:57 +00:00
//sort currencies alphabetically and set some additional variables
$counters [ 'taxPayments' ][ 'thisYear' ] = $counters [ 'taxPayments' ][ 'thisYear' ] -> sortKeys ();
$counters [ 'taxPayments' ][ 'thisYear' ] -> timeStart = Carbon :: today () -> startOfYear () -> toDateString ();
$counters [ 'taxPayments' ][ 'thisYear' ] -> timeEnd = Carbon :: today () -> toDateString ();
$counters [ 'taxPayments' ][ 'lastYear' ] = $counters [ 'taxPayments' ][ 'lastYear' ] -> sortKeys ();
$counters [ 'taxPayments' ][ 'lastYear' ] -> timeStart = Carbon :: today () -> startOfYear () -> subYear () -> toDateString ();
$counters [ 'taxPayments' ][ 'lastYear' ] -> timeEnd = Carbon :: today () -> endOfYear () -> subYear () -> toDateString ();
2021-11-07 11:07:30 +00:00
$lastEgg = Egg :: query () -> latest ( 'updated_at' ) -> first ();
$syncLastUpdate = $lastEgg ? $lastEgg -> updated_at -> isoFormat ( 'LLL' ) : __ ( 'unknown' );
2022-08-24 20:18:29 +00:00
2022-10-26 17:13:56 +00:00
//Get node information and prepare collection
$pteroNodeIds = [];
2023-02-04 16:40:42 +00:00
foreach ( $this -> pterodactyl -> getNodes () as $pteroNode ) {
2022-10-26 17:13:56 +00:00
array_push ( $pteroNodeIds , $pteroNode [ 'attributes' ][ 'id' ]);
}
2022-08-24 20:18:29 +00:00
$nodes = collect ();
2023-01-05 17:01:42 +00:00
foreach ( $DBnodes = Node :: query () -> get () as $DBnode ) { //gets all node information and prepares the structure
2022-08-24 20:18:29 +00:00
$nodeId = $DBnode [ 'id' ];
2023-01-05 17:01:42 +00:00
if ( ! in_array ( $nodeId , $pteroNodeIds )) {
continue ;
} //Check if node exists on pterodactyl too, if not, skip
2022-08-24 20:18:29 +00:00
$nodes -> put ( $nodeId , collect ());
$nodes [ $nodeId ] -> name = $DBnode [ 'name' ];
2023-02-04 16:40:42 +00:00
$pteroNode = $this -> pterodactyl -> getNode ( $nodeId );
2023-01-05 17:01:42 +00:00
$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 );
2022-08-24 20:18:29 +00:00
$counters [ 'totalUsagePercent' ] += $nodes [ $nodeId ] -> usagePercent ;
$nodes [ $nodeId ] -> totalServers = 0 ;
$nodes [ $nodeId ] -> activeServers = 0 ;
$nodes [ $nodeId ] -> totalEarnings = 0 ;
$nodes [ $nodeId ] -> activeEarnings = 0 ;
}
2023-01-05 17:01:42 +00:00
$counters [ 'totalUsagePercent' ] = ( $DBnodes -> count ()) ? round ( $counters [ 'totalUsagePercent' ] / $DBnodes -> count (), 2 ) : 0 ;
2022-08-24 20:18:29 +00:00
2023-02-04 16:40:42 +00:00
foreach ( $this -> pterodactyl -> getServers () as $server ) { //gets all servers from Pterodactyl and calculates total of credit usage for each node separately + total
2022-08-24 20:18:29 +00:00
$nodeId = $server [ 'attributes' ][ 'node' ];
2023-01-05 17:01:42 +00:00
if ( $CPServer = Server :: query () -> where ( 'pterodactyl_id' , $server [ 'attributes' ][ 'id' ]) -> first ()) {
2022-10-23 08:46:02 +00:00
$price = Product :: query () -> where ( 'id' , $CPServer -> product_id ) -> first () -> price ;
2023-01-05 17:01:42 +00:00
if ( ! $CPServer -> suspended ) {
2022-10-23 08:46:02 +00:00
$counters [ 'earnings' ] -> active += $price ;
2023-01-05 17:01:42 +00:00
$counters [ 'servers' ] -> active ++ ;
2022-10-23 08:46:02 +00:00
$nodes [ $nodeId ] -> activeEarnings += $price ;
2023-01-05 17:01:42 +00:00
$nodes [ $nodeId ] -> activeServers ++ ;
2022-08-19 17:18:44 +00:00
}
2022-10-23 08:46:02 +00:00
$counters [ 'earnings' ] -> total += $price ;
2023-01-05 17:01:42 +00:00
$counters [ 'servers' ] -> total ++ ;
2022-10-23 08:46:02 +00:00
$nodes [ $nodeId ] -> totalEarnings += $price ;
2023-01-05 17:01:42 +00:00
$nodes [ $nodeId ] -> totalServers ++ ;
2022-08-19 17:18:44 +00:00
}
2022-08-24 20:18:29 +00:00
}
//Get latest tickets
2022-10-14 20:59:02 +00:00
$tickets = collect ();
2023-01-05 17:01:42 +00:00
foreach ( Ticket :: query () -> latest () -> take ( 5 ) -> get () as $ticket ) {
2022-10-14 20:59:02 +00:00
$tickets -> put ( $ticket -> ticket_id , collect ());
$tickets [ $ticket -> ticket_id ] -> title = $ticket -> title ;
$user = User :: query () -> where ( 'id' , $ticket -> user_id ) -> first ();
$tickets [ $ticket -> ticket_id ] -> user_id = $user -> id ;
$tickets [ $ticket -> ticket_id ] -> user = $user -> name ;
$tickets [ $ticket -> ticket_id ] -> status = $ticket -> status ;
$tickets [ $ticket -> ticket_id ] -> last_updated = $ticket -> updated_at -> diffForHumans ();
switch ( $ticket -> status ) {
case 'Open' :
$tickets [ $ticket -> ticket_id ] -> statusBadgeColor = 'badge-success' ;
break ;
case 'Closed' :
$tickets [ $ticket -> ticket_id ] -> statusBadgeColor = 'badge-danger' ;
break ;
case 'Answered' :
$tickets [ $ticket -> ticket_id ] -> statusBadgeColor = 'badge-info' ;
break ;
default :
$tickets [ $ticket -> ticket_id ] -> statusBadgeColor = 'badge-warning' ;
break ;
2022-08-20 19:19:27 +00:00
}
2022-10-14 20:59:02 +00:00
}
2022-08-24 20:18:29 +00:00
2022-08-19 17:18:44 +00:00
return view ( 'admin.overview.index' , [
2023-01-05 17:01:42 +00:00
'counters' => $counters ,
'nodes' => $nodes ,
2022-08-19 17:18:44 +00:00
'syncLastUpdate' => $syncLastUpdate ,
2023-01-05 17:01:42 +00:00
'deletedNodesPresent' => ( $DBnodes -> count () != count ( $pteroNodeIds )) ? true : false ,
'perPageLimit' => ( $counters [ 'servers' ] -> total != Server :: query () -> count ()) ? true : false ,
'tickets' => $tickets ,
2023-02-06 20:34:58 +00:00
'credits_display_name' => $general_settings -> credits_display_name
2021-11-07 00:47:16 +00:00
]);
2023-01-05 17:01:42 +00:00
}
2021-11-07 11:16:46 +00:00
2021-11-07 11:07:30 +00:00
/**
* @ description Sync locations , nodes , nests , eggs with the linked pterodactyl panel
*/
public function syncPterodactyl ()
{
2023-05-04 23:11:55 +00:00
$this -> checkPermission ( self :: SYNC_PERMISSION );
2021-11-07 11:07:30 +00:00
Node :: syncNodes ();
Egg :: syncEggs ();
return redirect () -> back () -> with ( 'success' , __ ( 'Pterodactyl synced' ));
}
2021-11-07 00:47:16 +00:00
}