OverViewController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. $counters = Cache::remember('counters', self::TTL, function () {
  22. $output = collect();
  23. //Set basic variables in the collection
  24. $output->put('users', User::query()->count());
  25. $output->put('credits', number_format(User::query()->where("role","!=","admin")->sum('credits'), 2, '.', ''));
  26. $output->put('payments', Payment::query()->count());
  27. $output->put('eggs', Egg::query()->count());
  28. $output->put('nests', Nest::query()->count());
  29. $output->put('locations', Location::query()->count());
  30. //Prepare for counting
  31. $output->put('servers', collect());
  32. $output['servers']->active = 0;
  33. $output['servers']->total = 0;
  34. $output->put('earnings', collect());
  35. $output['earnings']->active = 0;
  36. $output['earnings']->total = 0;
  37. $output->put('totalUsagePercent', 0);
  38. //Prepare subCollection 'payments'
  39. $output->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. $output['payments']->put('thisMonth', collect());
  44. $output['payments']->put('lastMonth', collect());
  45. $output['payments']['thisMonth']->timeStart = Carbon::today()->startOfMonth()->toDateString();
  46. $output['payments']['thisMonth']->timeEnd = Carbon::today()->toDateString();
  47. $output['payments']['lastMonth']->timeStart = Carbon::today()->startOfMonth()->subMonth()->toDateString();
  48. $output['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($output['payments']['thisMonth'][$paymentCurrency])){
  53. $output['payments']['thisMonth']->put($paymentCurrency, collect());
  54. $output['payments']['thisMonth'][$paymentCurrency]->total = 0;
  55. $output['payments']['thisMonth'][$paymentCurrency]->count = 0;
  56. }
  57. $output['payments']['thisMonth'][$paymentCurrency]->total += $payment->total_price;
  58. $output['payments']['thisMonth'][$paymentCurrency]->count ++;
  59. }
  60. foreach($payments->where('created_at', '<', Carbon::today()->startOfMonth()) as $payment){
  61. $paymentCurrency = $payment->currency_code;
  62. if(!isset($output['payments']['lastMonth'][$paymentCurrency])){
  63. $output['payments']['lastMonth']->put($paymentCurrency, collect());
  64. $output['payments']['lastMonth'][$paymentCurrency]->total = 0;
  65. $output['payments']['lastMonth'][$paymentCurrency]->count = 0;
  66. }
  67. $output['payments']['lastMonth'][$paymentCurrency]->total += $payment->total_price;
  68. $output['payments']['lastMonth'][$paymentCurrency]->count ++;
  69. }
  70. $output['payments']->total = Payment::query()->count();
  71. return $output;
  72. });
  73. $lastEgg = Egg::query()->latest('updated_at')->first();
  74. $syncLastUpdate = $lastEgg ? $lastEgg->updated_at->isoFormat('LLL') : __('unknown');
  75. $nodes = Cache::remember('nodes', self::TTL, function() use($counters){
  76. $output = collect();
  77. foreach($nodes = Node::query()->get() as $node){ //gets all node information and prepares the structure
  78. $nodeId = $node['id'];
  79. $output->put($nodeId, collect());
  80. $output[$nodeId]->name = $node['name'];
  81. $node = Pterodactyl::getNode($nodeId);
  82. $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);
  83. $counters['totalUsagePercent'] += $output[$nodeId]->usagePercent;
  84. $output[$nodeId]->totalServers = 0;
  85. $output[$nodeId]->activeServers = 0;
  86. $output[$nodeId]->totalEarnings = 0;
  87. $output[$nodeId]->activeEarnings = 0;
  88. }
  89. $counters['totalUsagePercent'] = ($nodes->count())?round($counters['totalUsagePercent']/$nodes->count(), 2):0;
  90. foreach(Pterodactyl::getServers() as $server){ //gets all servers from Pterodactyl and calculates total of credit usage for each node separately + total
  91. $nodeId = $server['attributes']['node'];
  92. if($CPServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first()){
  93. $prize = Product::query()->where('id', $CPServer->product_id)->first()->price;
  94. if (!$CPServer->suspended){
  95. $counters['earnings']->active += $prize;
  96. $counters['servers']->active ++;
  97. $output[$nodeId]->activeEarnings += $prize;
  98. $output[$nodeId]->activeServers ++;
  99. }
  100. $counters['earnings']->total += $prize;
  101. $counters['servers']->total ++;
  102. $output[$nodeId]->totalEarnings += $prize;
  103. $output[$nodeId]->totalServers ++;
  104. }
  105. }
  106. return $output;
  107. });
  108. $tickets = Cache::remember('tickets', self::TTL, function(){
  109. $output = collect();
  110. foreach(Ticket::query()->latest()->take(3)->get() as $ticket){
  111. $output->put($ticket->ticket_id, collect());
  112. $output[$ticket->ticket_id]->title = $ticket->title;
  113. $user = User::query()->where('id', $ticket->user_id)->first();
  114. $output[$ticket->ticket_id]->user_id = $user->id;
  115. $output[$ticket->ticket_id]->user = $user->name;
  116. $output[$ticket->ticket_id]->status = $ticket->status;
  117. $output[$ticket->ticket_id]->last_updated = $ticket->updated_at->diffForHumans();
  118. switch ($ticket->status) {
  119. case 'Open':
  120. $output[$ticket->ticket_id]->statusBadgeColor = 'badge-success';
  121. break;
  122. case 'Closed':
  123. $output[$ticket->ticket_id]->statusBadgeColor = 'badge-danger';
  124. break;
  125. case 'Answered':
  126. $output[$ticket->ticket_id]->statusBadgeColor = 'badge-info';
  127. break;
  128. default:
  129. $output[$ticket->ticket_id]->statusBadgeColor = 'badge-warning';
  130. break;
  131. }
  132. }
  133. return $output;
  134. });
  135. //dd($counters);
  136. return view('admin.overview.index', [
  137. 'counters' => $counters,
  138. 'nodes' => $nodes,
  139. 'syncLastUpdate' => $syncLastUpdate,
  140. 'perPageLimit' => ($counters['servers']->total != Server::query()->count())?true:false,
  141. 'tickets' => $tickets
  142. ]);
  143. }
  144. /**
  145. * @description Sync locations,nodes,nests,eggs with the linked pterodactyl panel
  146. */
  147. public function syncPterodactyl()
  148. {
  149. Node::syncNodes();
  150. Egg::syncEggs();
  151. return redirect()->back()->with('success', __('Pterodactyl synced'));
  152. }
  153. }