OverViewController.php 12 KB

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