TaskHourly.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Node;
  4. use App\Models\User;
  5. use App\Notifications\DataAnomaly;
  6. use DB;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Database\Eloquent\Builder;
  9. use Log;
  10. use Notification;
  11. class TaskHourly extends Command
  12. {
  13. protected $signature = 'task:hourly';
  14. protected $description = '每小时任务';
  15. public function handle(): void
  16. {
  17. $jobTime = microtime(true);
  18. $this->userTrafficStatistics(); // 用户小时流量统计
  19. $this->nodeTrafficStatistics(); // 节点小时流量统计
  20. $jobTime = round(microtime(true) - $jobTime, 4);
  21. Log::info(__('----「:job」Completed, Used :time seconds ----', ['job' => $this->description, 'time' => $jobTime]));
  22. }
  23. private function userTrafficStatistics(): void
  24. {
  25. $created_at = date('Y-m-d H:59:59', strtotime('-1 hour'));
  26. $end = strtotime($created_at);
  27. $start = $end - 3599;
  28. $data_anomaly_notification = sysConfig('data_anomaly_notification');
  29. $traffic_abuse_threshold = (int) sysConfig('traffic_abuse_limit') * GiB;
  30. User::activeUser()->whereHas('dataFlowLogs', function (Builder $query) use ($start, $end) {
  31. $query->whereBetween('log_time', [$start, $end]);
  32. })->with([
  33. 'dataFlowLogs' => function ($query) use ($start, $end) {
  34. $query->whereBetween('log_time', [$start, $end]);
  35. },
  36. ])->chunk(sysConfig('tasks_chunk'), function ($users) use ($traffic_abuse_threshold, $created_at, $data_anomaly_notification) {
  37. foreach ($users as $user) {
  38. $dataFlowLogs = $user->dataFlowLogs->groupBy('node_id');
  39. $data = $dataFlowLogs->map(function ($logs, $nodeId) use ($created_at) {
  40. $totals = $logs->reduce(function ($carry, $log) {
  41. $carry['u'] += $log['u'];
  42. $carry['d'] += $log['d'];
  43. return $carry;
  44. }, ['u' => 0, 'd' => 0]);
  45. return [
  46. 'node_id' => $nodeId,
  47. 'u' => $totals['u'],
  48. 'd' => $totals['d'],
  49. 'created_at' => $created_at,
  50. ];
  51. })->values()->all();
  52. $sum_u = array_sum(array_column($data, 'u'));
  53. $sum_d = array_sum(array_column($data, 'd'));
  54. $data[] = [ // 每小时节点流量合计
  55. 'node_id' => null,
  56. 'u' => $sum_u,
  57. 'd' => $sum_d,
  58. 'created_at' => $created_at,
  59. ];
  60. $user->hourlyDataFlows()->createMany($data);
  61. $sum_all = $sum_u + $sum_d;
  62. // 用户流量异常警告
  63. if ($data_anomaly_notification && $sum_all >= $traffic_abuse_threshold) {
  64. Notification::send(User::find(1), new DataAnomaly($user->id, formatBytes($sum_u), formatBytes($sum_d), formatBytes($sum_all)));
  65. }
  66. }
  67. });
  68. }
  69. private function nodeTrafficStatistics(): void
  70. {
  71. $created_at = date('Y-m-d H:59:59', strtotime('-1 hour'));
  72. $end = strtotime($created_at);
  73. $start = $end - 3599;
  74. Node::whereHas('userDataFlowLogs', function (Builder $query) use ($start, $end) {
  75. $query->whereBetween('log_time', [$start, $end]);
  76. })->withCount([
  77. 'userDataFlowLogs as u_sum' => function ($query) use ($start, $end) {
  78. $query->select(DB::raw('SUM(u)'))->whereBetween('log_time', [$start, $end]);
  79. },
  80. ])->withCount([
  81. 'userDataFlowLogs as d_sum' => function ($query) use ($start, $end) {
  82. $query->select(DB::raw('SUM(d)'))->whereBetween('log_time', [$start, $end]);
  83. },
  84. ])->chunk(sysConfig('tasks_chunk'), function ($nodes) use ($created_at) {
  85. foreach ($nodes as $node) {
  86. $node->hourlyDataFlows()->create(['u' => $node->u_sum, 'd' => $node->d_sum, 'created_at' => $created_at]);
  87. }
  88. });
  89. }
  90. }