OverViewController.php 12 KB

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