OverViewController.php 12 KB

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