OverViewController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 Carbon\Carbon;
  15. class OverViewController extends Controller
  16. {
  17. public const TTL = 86400;
  18. public function index()
  19. {
  20. $counters = Cache::remember('counters', self::TTL, function () {
  21. $output = collect();
  22. //Set basic variables in the collection
  23. $output->put('users', User::query()->count());
  24. $output->put('credits', number_format(User::query()->where("role","!=","admin")->sum('credits'), 2, '.', ''));
  25. $output->put('payments', Payment::query()->count());
  26. $output->put('eggs', Egg::query()->count());
  27. $output->put('nests', Nest::query()->count());
  28. $output->put('locations', Location::query()->count());
  29. //Prepare for counting
  30. $output->put('servers', collect());
  31. $output['servers']->active = 0;
  32. $output['servers']->total = 0;
  33. $output->put('earnings', collect());
  34. $output['earnings']->active = 0;
  35. $output['earnings']->total = 0;
  36. $output->put('totalUsagePercent', 0);
  37. //Prepare subCollection 'payments'
  38. $output->put('payments', collect());
  39. //Get and save payments from last 2 months for later filtering and looping
  40. $payments = Payment::query()->where('created_at', '>=', Carbon::today()->startOfMonth()->subMonth())->where('status', 'paid')->get();
  41. //Prepare collections and set a few variables
  42. $output['payments']->put('thisMonth', collect());
  43. $output['payments']->put('lastMonth', collect());
  44. $output['payments']['thisMonth']->timeStart = Carbon::today()->startOfMonth()->toDateString();
  45. $output['payments']['thisMonth']->timeEnd = Carbon::today()->toDateString();
  46. $output['payments']['lastMonth']->timeStart = Carbon::today()->startOfMonth()->subMonth()->toDateString();
  47. $output['payments']['lastMonth']->timeEnd = Carbon::today()->endOfMonth()->subMonth()->toDateString();
  48. //Fill out variables for each currency separately
  49. foreach($payments->where('created_at', '>=', Carbon::today()->startOfMonth()) as $payment){
  50. $paymentCurrency = $payment->currency_code;
  51. if(!isset($output['payments']['thisMonth'][$paymentCurrency])){
  52. $output['payments']['thisMonth']->put($paymentCurrency, collect());
  53. $output['payments']['thisMonth'][$paymentCurrency]->total = 0;
  54. $output['payments']['thisMonth'][$paymentCurrency]->count = 0;
  55. }
  56. $output['payments']['thisMonth'][$paymentCurrency]->total += $payment->total_price;
  57. $output['payments']['thisMonth'][$paymentCurrency]->count ++;
  58. }
  59. foreach($payments->where('created_at', '<', Carbon::today()->startOfMonth()) as $payment){
  60. $paymentCurrency = $payment->currency_code;
  61. if(!isset($output['payments']['lastMonth'][$paymentCurrency])){
  62. $output['payments']['lastMonth']->put($paymentCurrency, collect());
  63. $output['payments']['lastMonth'][$paymentCurrency]->total = 0;
  64. $output['payments']['lastMonth'][$paymentCurrency]->count = 0;
  65. }
  66. $output['payments']['lastMonth'][$paymentCurrency]->total += $payment->total_price;
  67. $output['payments']['lastMonth'][$paymentCurrency]->count ++;
  68. }
  69. $output['payments']->total = Payment::query()->count();
  70. return $output;
  71. });
  72. $lastEgg = Egg::query()->latest('updated_at')->first();
  73. $syncLastUpdate = $lastEgg ? $lastEgg->updated_at->isoFormat('LLL') : __('unknown');
  74. $nodes = Cache::remember('nodes', self::TTL, function() use($counters){
  75. $output = collect();
  76. foreach($nodes = Node::query()->get() as $node){ //gets all node information and prepares the structure
  77. $nodeId = $node['id'];
  78. $output->put($nodeId, collect());
  79. $output[$nodeId]->name = $node['name'];
  80. $node = Pterodactyl::getNode($nodeId);
  81. $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);
  82. $counters['totalUsagePercent'] += $output[$nodeId]->usagePercent;
  83. $output[$nodeId]->totalServers = 0;
  84. $output[$nodeId]->activeServers = 0;
  85. $output[$nodeId]->totalEarnings = 0;
  86. $output[$nodeId]->activeEarnings = 0;
  87. }
  88. $counters['totalUsagePercent'] = round($counters['totalUsagePercent']/$nodes->count(), 2);
  89. foreach(Pterodactyl::getServers() as $server){ //gets all servers from Pterodactyl and calculates total of credit usage for each node separately + total
  90. $nodeId = $server['attributes']['node'];
  91. if($CPServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first()){
  92. $prize = Product::query()->where('id', $CPServer->product_id)->first()->price;
  93. if (!$CPServer->suspended){
  94. $counters['earnings']->active += $prize;
  95. $counters['servers']->active ++;
  96. $output[$nodeId]->activeEarnings += $prize;
  97. $output[$nodeId]->activeServers ++;
  98. }
  99. $counters['earnings']->total += $prize;
  100. $counters['servers']->total ++;
  101. $output[$nodeId]->totalEarnings += $prize;
  102. $output[$nodeId]->totalServers ++;
  103. }
  104. }
  105. return $output;
  106. });
  107. //dd($counters);
  108. return view('admin.overview.index', [
  109. 'counters' => $counters,
  110. 'nodes' => $nodes,
  111. 'syncLastUpdate' => $syncLastUpdate,
  112. 'perPageLimit' => ($counters['servers']->total != Server::query()->count())?true:false
  113. ]);
  114. }
  115. /**
  116. * @description Sync locations,nodes,nests,eggs with the linked pterodactyl panel
  117. */
  118. public function syncPterodactyl()
  119. {
  120. Node::syncNodes();
  121. Egg::syncEggs();
  122. return redirect()->back()->with('success', __('Pterodactyl synced'));
  123. }
  124. }