Analytics.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services;
  4. use App\Models\Node;
  5. use App\Models\User;
  6. use App\Utils\Tools;
  7. class Analytics
  8. {
  9. public function getTotalUser()
  10. {
  11. return User::count();
  12. }
  13. public function getCheckinUser()
  14. {
  15. return User::where('last_check_in_time', '>', 0)->count();
  16. }
  17. public function getTodayCheckinUser()
  18. {
  19. return User::where('last_check_in_time', '>', strtotime('today'))->count();
  20. }
  21. public function getTrafficUsage()
  22. {
  23. $total = User::sum('u') + User::sum('d');
  24. return Tools::flowAutoShow($total);
  25. }
  26. public function getTodayTrafficUsage()
  27. {
  28. $total = User::sum('u') + User::sum('d') - User::sum('last_day_t');
  29. return Tools::flowAutoShow($total);
  30. }
  31. public function getRawTodayTrafficUsage()
  32. {
  33. return User::sum('u') + User::sum('d') - User::sum('last_day_t');
  34. }
  35. public function getLastTrafficUsage()
  36. {
  37. $total = User::sum('last_day_t');
  38. return Tools::flowAutoShow($total);
  39. }
  40. public function getRawLastTrafficUsage()
  41. {
  42. return User::sum('last_day_t');
  43. }
  44. public function getUnusedTrafficUsage()
  45. {
  46. $total = User::sum('transfer_enable') - User::sum('u') - User::sum('d');
  47. return Tools::flowAutoShow($total);
  48. }
  49. public function getRawUnusedTrafficUsage()
  50. {
  51. return User::sum('transfer_enable') - User::sum('u') - User::sum('d');
  52. }
  53. public function getTotalTraffic()
  54. {
  55. $total = User::sum('transfer_enable');
  56. return Tools::flowAutoShow($total);
  57. }
  58. public function getRawTotalTraffic()
  59. {
  60. return User::sum('transfer_enable');
  61. }
  62. public function getOnlineUser($time)
  63. {
  64. $time = time() - $time;
  65. return User::where('t', '>', $time)->count();
  66. }
  67. public function getUnusedUser()
  68. {
  69. return User::where('t', '=', 0)->count();
  70. }
  71. public function getTotalNode()
  72. {
  73. return Node::count();
  74. }
  75. public function getTotalNodes()
  76. {
  77. return Node::where('node_heartbeat', '>', 0)->where(
  78. static function ($query): void {
  79. $query->Where('sort', '=', 0)
  80. ->orWhere('sort', '=', 10)
  81. ->orWhere('sort', '=', 11)
  82. ->orWhere('sort', '=', 12)
  83. ->orWhere('sort', '=', 13)
  84. ->orWhere('sort', '=', 14);
  85. }
  86. )->count();
  87. }
  88. public function getAliveNodes()
  89. {
  90. return Node::where(
  91. static function ($query): void {
  92. $query->Where('sort', '=', 0)
  93. ->orWhere('sort', '=', 10)
  94. ->orWhere('sort', '=', 11)
  95. ->orWhere('sort', '=', 12)
  96. ->orWhere('sort', '=', 13)
  97. ->orWhere('sort', '=', 14);
  98. }
  99. )->where('node_heartbeat', '>', time() - 90)->count();
  100. }
  101. }