OverViewController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. //Prepare subCollection 'taxPayments'
  50. $counters->put('taxPayments', collect());
  51. //Get and save taxPayments from last 2 years for later filtering and looping
  52. $taxPayments = Payment::query()->where('created_at', '>=', Carbon::today()->startOfYear()->subYear())->where('status', 'paid')->get();
  53. //Prepare collections and set a few variables
  54. $counters['taxPayments']->put('thisYear', collect());
  55. $counters['taxPayments']->put('lastYear', collect());
  56. $counters['taxPayments']['thisYear']->timeStart = Carbon::today()->startOfYear()->toDateString();
  57. $counters['taxPayments']['thisYear']->timeEnd = Carbon::today()->toDateString();
  58. $counters['taxPayments']['lastYear']->timeStart = Carbon::today()->startOfYear()->subYear()->toDateString();
  59. $counters['taxPayments']['lastYear']->timeEnd = Carbon::today()->endOfYear()->subYear()->toDateString();
  60. //Fill out variables for each currency separately
  61. foreach($payments->where('created_at', '>=', Carbon::today()->startOfMonth()) as $payment){
  62. $paymentCurrency = $payment->currency_code;
  63. if(!isset($counters['payments']['thisMonth'][$paymentCurrency])){
  64. $counters['payments']['thisMonth']->put($paymentCurrency, collect());
  65. $counters['payments']['thisMonth'][$paymentCurrency]->total = 0;
  66. $counters['payments']['thisMonth'][$paymentCurrency]->count = 0;
  67. }
  68. $counters['payments']['thisMonth'][$paymentCurrency]->total += $payment->total_price;
  69. $counters['payments']['thisMonth'][$paymentCurrency]->count ++;
  70. }
  71. foreach($payments->where('created_at', '<', Carbon::today()->startOfMonth()) as $payment){
  72. $paymentCurrency = $payment->currency_code;
  73. if(!isset($counters['payments']['lastMonth'][$paymentCurrency])){
  74. $counters['payments']['lastMonth']->put($paymentCurrency, collect());
  75. $counters['payments']['lastMonth'][$paymentCurrency]->total = 0;
  76. $counters['payments']['lastMonth'][$paymentCurrency]->count = 0;
  77. }
  78. $counters['payments']['lastMonth'][$paymentCurrency]->total += $payment->total_price;
  79. $counters['payments']['lastMonth'][$paymentCurrency]->count ++;
  80. }
  81. $counters['payments']->total = Payment::query()->count();
  82. foreach($taxPayments->where('created_at', '>=', Carbon::today()->startOfYear()->subYear()) as $taxPayment){
  83. $paymentCurrency = $payment->currency_code;
  84. if(!isset($counters['taxPayments']['thisYear'][$paymentCurrency])){
  85. $counters['taxPayments']['thisYear']->put($paymentCurrency, collect());
  86. $counters['taxPayments']['thisYear'][$paymentCurrency]->total = 0;
  87. $counters['taxPayments']['thisYear'][$paymentCurrency]->count = 0;
  88. $counters['taxPayments']['thisYear'][$paymentCurrency]->price = 0;
  89. $counters['taxPayments']['thisYear'][$paymentCurrency]->taxes = 0;
  90. }
  91. $counters['taxPayments']['thisYear'][$paymentCurrency]->total += $taxPayment->total_price;
  92. $counters['taxPayments']['thisYear'][$paymentCurrency]->count ++;
  93. $counters['taxPayments']['thisYear'][$paymentCurrency]->price += $taxPayment->price;
  94. $counters['taxPayments']['thisYear'][$paymentCurrency]->taxes += $taxPayment->tax_value;
  95. }
  96. foreach($taxPayments->where('created_at', '<', Carbon::today()->startOfYear()) as $taxPayment){
  97. $paymentCurrency = $payment->currency_code;
  98. if(!isset($counters['taxPayments']['lastYear'][$paymentCurrency])){
  99. $counters['taxPayments']['lastYear']->put($paymentCurrency, collect());
  100. $counters['taxPayments']['lastYear'][$paymentCurrency]->total = 0;
  101. $counters['taxPayments']['lastYear'][$paymentCurrency]->count = 0;
  102. $counters['taxPayments']['lastYear'][$paymentCurrency]->price = 0;
  103. $counters['taxPayments']['lastYear'][$paymentCurrency]->taxes = 0;
  104. }
  105. $counters['taxPayments']['lastYear'][$paymentCurrency]->total += $taxPayment->total_price;
  106. $counters['taxPayments']['lastYear'][$paymentCurrency]->count ++;
  107. $counters['taxPayments']['lastYear'][$paymentCurrency]->price += $taxPayment->price;
  108. $counters['taxPayments']['lastYear'][$paymentCurrency]->taxes += $taxPayment->tax_value;
  109. }
  110. $lastEgg = Egg::query()->latest('updated_at')->first();
  111. $syncLastUpdate = $lastEgg ? $lastEgg->updated_at->isoFormat('LLL') : __('unknown');
  112. //Get node information
  113. $nodes = collect();
  114. foreach($DBnodes = Node::query()->get() as $DBnode){ //gets all node information and prepares the structure
  115. $nodeId = $DBnode['id'];
  116. $nodes->put($nodeId, collect());
  117. $nodes[$nodeId]->name = $DBnode['name'];
  118. $pteroNode = Pterodactyl::getNode($nodeId);
  119. $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);
  120. $counters['totalUsagePercent'] += $nodes[$nodeId]->usagePercent;
  121. $nodes[$nodeId]->totalServers = 0;
  122. $nodes[$nodeId]->activeServers = 0;
  123. $nodes[$nodeId]->totalEarnings = 0;
  124. $nodes[$nodeId]->activeEarnings = 0;
  125. }
  126. $counters['totalUsagePercent'] = ($DBnodes->count())?round($counters['totalUsagePercent']/$DBnodes->count(), 2):0;
  127. foreach(Pterodactyl::getServers() as $server){ //gets all servers from Pterodactyl and calculates total of credit usage for each node separately + total
  128. $nodeId = $server['attributes']['node'];
  129. if($CPServer = Server::query()->where('pterodactyl_id', $server['attributes']['id'])->first()){
  130. $prize = Product::query()->where('id', $CPServer->product_id)->first()->price;
  131. if (!$CPServer->suspended){
  132. $counters['earnings']->active += $prize;
  133. $counters['servers']->active ++;
  134. $nodes[$nodeId]->activeEarnings += $prize;
  135. $nodes[$nodeId]->activeServers ++;
  136. }
  137. $counters['earnings']->total += $prize;
  138. $counters['servers']->total ++;
  139. $nodes[$nodeId]->totalEarnings += $prize;
  140. $nodes[$nodeId]->totalServers ++;
  141. }
  142. }
  143. //Get latest tickets
  144. $tickets = collect();
  145. foreach(Ticket::query()->latest()->take(5)->get() as $ticket){
  146. $tickets->put($ticket->ticket_id, collect());
  147. $tickets[$ticket->ticket_id]->title = $ticket->title;
  148. $user = User::query()->where('id', $ticket->user_id)->first();
  149. $tickets[$ticket->ticket_id]->user_id = $user->id;
  150. $tickets[$ticket->ticket_id]->user = $user->name;
  151. $tickets[$ticket->ticket_id]->status = $ticket->status;
  152. $tickets[$ticket->ticket_id]->last_updated = $ticket->updated_at->diffForHumans();
  153. switch ($ticket->status) {
  154. case 'Open':
  155. $tickets[$ticket->ticket_id]->statusBadgeColor = 'badge-success';
  156. break;
  157. case 'Closed':
  158. $tickets[$ticket->ticket_id]->statusBadgeColor = 'badge-danger';
  159. break;
  160. case 'Answered':
  161. $tickets[$ticket->ticket_id]->statusBadgeColor = 'badge-info';
  162. break;
  163. default:
  164. $tickets[$ticket->ticket_id]->statusBadgeColor = 'badge-warning';
  165. break;
  166. }
  167. }
  168. return view('admin.overview.index', [
  169. 'counters' => $counters,
  170. 'nodes' => $nodes,
  171. 'syncLastUpdate' => $syncLastUpdate,
  172. 'perPageLimit' => ($counters['servers']->total != Server::query()->count())?true:false,
  173. 'tickets' => $tickets
  174. ]);
  175. }
  176. /**
  177. * @description Sync locations,nodes,nests,eggs with the linked pterodactyl panel
  178. */
  179. public function syncPterodactyl()
  180. {
  181. Node::syncNodes();
  182. Egg::syncEggs();
  183. return redirect()->back()->with('success', __('Pterodactyl synced'));
  184. }
  185. }