OverViewController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Egg;
  5. use App\Models\Location;
  6. use App\Models\Nest;
  7. use App\Models\Node;
  8. use App\Models\Payment;
  9. use App\Models\Server;
  10. use App\Models\User;
  11. use Illuminate\Support\Facades\Cache;
  12. use App\Classes\Pterodactyl;
  13. use App\Models\Product;
  14. use App\Models\Ticket;
  15. use Carbon\Carbon;
  16. class OverViewController extends Controller
  17. {
  18. public const TTL = 86400;
  19. public function index()
  20. {
  21. //Get counters
  22. $counters = collect();
  23. //Set basic variables in the collection
  24. $counters->put('users', User::query()->count());
  25. $counters->put('credits', number_format(User::query()->where("role","!=","admin")->sum('credits'), 2, '.', ''));
  26. $counters->put('payments', Payment::query()->count());
  27. $counters->put('eggs', Egg::query()->count());
  28. $counters->put('nests', Nest::query()->count());
  29. $counters->put('locations', Location::query()->count());
  30. //Prepare for counting
  31. $counters->put('servers', collect());
  32. $counters['servers']->active = 0;
  33. $counters['servers']->total = 0;
  34. $counters->put('earnings', collect());
  35. $counters['earnings']->active = 0;
  36. $counters['earnings']->total = 0;
  37. $counters->put('totalUsagePercent', 0);
  38. //Prepare subCollection 'payments'
  39. $counters->put('payments', collect());
  40. //Get and save payments from last 2 months for later filtering and looping
  41. $payments = Payment::query()->where('created_at', '>=', Carbon::today()->startOfMonth()->subMonth())->where('status', 'paid')->get();
  42. //Prepare collections and set a few variables
  43. $counters['payments']->put('thisMonth', collect());
  44. $counters['payments']->put('lastMonth', collect());
  45. $counters['payments']['thisMonth']->timeStart = Carbon::today()->startOfMonth()->toDateString();
  46. $counters['payments']['thisMonth']->timeEnd = Carbon::today()->toDateString();
  47. $counters['payments']['lastMonth']->timeStart = Carbon::today()->startOfMonth()->subMonth()->toDateString();
  48. $counters['payments']['lastMonth']->timeEnd = Carbon::today()->endOfMonth()->subMonth()->toDateString();
  49. //Fill out variables for each currency separately
  50. foreach($payments->where('created_at', '>=', Carbon::today()->startOfMonth()) as $payment){
  51. $paymentCurrency = $payment->currency_code;
  52. if(!isset($counters['payments']['thisMonth'][$paymentCurrency])){
  53. $counters['payments']['thisMonth']->put($paymentCurrency, collect());
  54. $counters['payments']['thisMonth'][$paymentCurrency]->total = 0;
  55. $counters['payments']['thisMonth'][$paymentCurrency]->count = 0;
  56. }
  57. $counters['payments']['thisMonth'][$paymentCurrency]->total += $payment->total_price;
  58. $counters['payments']['thisMonth'][$paymentCurrency]->count ++;
  59. }
  60. foreach($payments->where('created_at', '<', Carbon::today()->startOfMonth()) as $payment){
  61. $paymentCurrency = $payment->currency_code;
  62. if(!isset($counters['payments']['lastMonth'][$paymentCurrency])){
  63. $counters['payments']['lastMonth']->put($paymentCurrency, collect());
  64. $counters['payments']['lastMonth'][$paymentCurrency]->total = 0;
  65. $counters['payments']['lastMonth'][$paymentCurrency]->count = 0;
  66. }
  67. $counters['payments']['lastMonth'][$paymentCurrency]->total += $payment->total_price;
  68. $counters['payments']['lastMonth'][$paymentCurrency]->count ++;
  69. }
  70. $counters['payments']->total = Payment::query()->count();
  71. $lastEgg = Egg::query()->latest('updated_at')->first();
  72. $syncLastUpdate = $lastEgg ? $lastEgg->updated_at->isoFormat('LLL') : __('unknown');
  73. //Get node information and prepare collection
  74. $pteroNodeIds = [];
  75. foreach(Pterodactyl::getNodes() as $pteroNode){
  76. array_push($pteroNodeIds, $pteroNode['attributes']['id']);
  77. }
  78. $nodes = collect();
  79. foreach($DBnodes = Node::query()->get() as $DBnode){ //gets all node information and prepares the structure
  80. $nodeId = $DBnode['id'];
  81. if(!in_array($nodeId, $pteroNodeIds)) continue; //Check if node exists on pterodactyl too, if not, skip
  82. $nodes->put($nodeId, collect());
  83. $nodes[$nodeId]->name = $DBnode['name'];
  84. $pteroNode = Pterodactyl::getNode($nodeId);
  85. $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);
  86. $counters['totalUsagePercent'] += $nodes[$nodeId]->usagePercent;
  87. $nodes[$nodeId]->totalServers = 0;
  88. $nodes[$nodeId]->activeServers = 0;
  89. $nodes[$nodeId]->totalEarnings = 0;
  90. $nodes[$nodeId]->activeEarnings = 0;
  91. }
  92. $counters['totalUsagePercent'] = ($DBnodes->count())?round($counters['totalUsagePercent']/$DBnodes->count(), 2):0;
  93. foreach(Pterodactyl::getServers() as $server){ //gets all servers from Pterodactyl and calculates total of credit usage for each node separately + total
  94. $nodeId = $server['attributes']['node'];
  95. if($CPServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first()){
  96. $price = Product::query()->where('id', $CPServer->product_id)->first()->price;
  97. if (!$CPServer->suspended){
  98. $counters['earnings']->active += $price;
  99. $counters['servers']->active ++;
  100. $nodes[$nodeId]->activeEarnings += $price;
  101. $nodes[$nodeId]->activeServers ++;
  102. }
  103. $counters['earnings']->total += $price;
  104. $counters['servers']->total ++;
  105. $nodes[$nodeId]->totalEarnings += $price;
  106. $nodes[$nodeId]->totalServers ++;
  107. }
  108. }
  109. //Get latest tickets
  110. $tickets = Cache::remember('tickets', self::TTL, function(){
  111. $output = collect();
  112. foreach(Ticket::query()->latest()->take(3)->get() as $ticket){
  113. $output->put($ticket->ticket_id, collect());
  114. $output[$ticket->ticket_id]->title = $ticket->title;
  115. $user = User::query()->where('id', $ticket->user_id)->first();
  116. $output[$ticket->ticket_id]->user_id = $user->id;
  117. $output[$ticket->ticket_id]->user = $user->name;
  118. $output[$ticket->ticket_id]->status = $ticket->status;
  119. $output[$ticket->ticket_id]->last_updated = $ticket->updated_at->diffForHumans();
  120. switch ($ticket->status) {
  121. case 'Open':
  122. $output[$ticket->ticket_id]->statusBadgeColor = 'badge-success';
  123. break;
  124. case 'Closed':
  125. $output[$ticket->ticket_id]->statusBadgeColor = 'badge-danger';
  126. break;
  127. case 'Answered':
  128. $output[$ticket->ticket_id]->statusBadgeColor = 'badge-info';
  129. break;
  130. default:
  131. $output[$ticket->ticket_id]->statusBadgeColor = 'badge-warning';
  132. break;
  133. }
  134. }
  135. return $output;
  136. });
  137. return view('admin.overview.index', [
  138. 'counters' => $counters,
  139. 'nodes' => $nodes,
  140. 'syncLastUpdate' => $syncLastUpdate,
  141. 'deletedNodesPresent'=> ($DBnodes->count() != count($pteroNodeIds))?true:false,
  142. 'perPageLimit' => ($counters['servers']->total != Server::query()->count())?true:false,
  143. 'tickets' => $tickets
  144. ]);
  145. }
  146. /**
  147. * @description Sync locations,nodes,nests,eggs with the linked pterodactyl panel
  148. */
  149. public function syncPterodactyl()
  150. {
  151. Node::syncNodes();
  152. Egg::syncEggs();
  153. return redirect()->back()->with('success', __('Pterodactyl synced'));
  154. }
  155. }