OverViewController.php 12 KB

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