OverViewController.php 12 KB

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