OverViewController.php 12 KB

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