TaskHourly.php 3.8 KB

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