TaskHourly.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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->created_at = $created_at;
  40. })->toArray();
  41. $overall = [ // 每小时节点流量合计
  42. 'node_id' => null,
  43. 'u' => $logs->sum('u'),
  44. 'd' => $logs->sum('d'),
  45. 'created_at' => $created_at,
  46. ];
  47. $data[] = $overall;
  48. $user->hourlyDataFlows()->createMany($data);
  49. $overall['total'] = $overall['u'] + $overall['d'];
  50. // 用户流量异常警告
  51. if ($data_anomaly_notification && $overall['total'] >= $traffic_ban_value) {
  52. Notification::send(User::find(1), new DataAnomaly($user->username, formatBytes($overall['u']), formatBytes($overall['d']), formatBytes($overall['total'])));
  53. }
  54. }
  55. });
  56. }
  57. private function nodeTrafficStatistics(): void
  58. {
  59. $created_at = date('Y-m-d H:59:59', strtotime('-1 hour'));
  60. $end = strtotime($created_at);
  61. $start = $end - 3599;
  62. Node::orderBy('id')->with('userDataFlowLogs')->whereHas('userDataFlowLogs', function (Builder $query) use ($start, $end) {
  63. $query->whereBetween('log_time', [$start, $end]);
  64. })->chunk(config('tasks.chunk'), function ($nodes) use ($start, $end, $created_at) {
  65. foreach ($nodes as $node) {
  66. $traffic = $node->userDataFlowLogs()->whereBetween('log_time', [$start, $end])->selectRaw('sum(`u`) as u, sum(`d`) as d')->first();
  67. $node->hourlyDataFlows()->create(['u' => $traffic->u, 'd' => $traffic->d, 'created_at' => $created_at]);
  68. }
  69. });
  70. }
  71. }