UserHourlyDataFlow.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. /**
  6. * 用户流量每小时统计
  7. *
  8. * @property int $id
  9. * @property int $user_id 用户ID
  10. * @property int|null $node_id 节点ID,null表示统计全部节点
  11. * @property int $u 上传流量
  12. * @property int $d 下载流量
  13. * @property int $total 总流量
  14. * @property string|null $traffic 总流量(带单位)
  15. * @property \Illuminate\Support\Carbon $created_at 创建时间
  16. * @property-read \App\Models\Node|null $node
  17. * @property-read \App\Models\User $user
  18. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow newModelQuery()
  19. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow newQuery()
  20. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow query()
  21. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow userHourly($uid)
  22. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow userRecentUsed($uid)
  23. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereCreatedAt($value)
  24. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereD($value)
  25. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereId($value)
  26. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereNodeId($value)
  27. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereTotal($value)
  28. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereTraffic($value)
  29. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereU($value)
  30. * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereUserId($value)
  31. * @mixin \Eloquent
  32. */
  33. class UserHourlyDataFlow extends Model
  34. {
  35. public const UPDATED_AT = null;
  36. protected $table = 'user_hourly_data_flow';
  37. protected $guarded = [];
  38. public function user(): BelongsTo
  39. {
  40. return $this->belongsTo(User::class);
  41. }
  42. public function node(): BelongsTo
  43. {
  44. return $this->belongsTo(Node::class);
  45. }
  46. // 用户每时使用总流量
  47. public function scopeUserHourly($query, $uid)
  48. {
  49. return $query->whereUserId($uid)->whereNodeId(null);
  50. }
  51. public function scopeUserRecentUsed($query, $uid)
  52. {
  53. return $query->userHourly($uid)->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900));
  54. }
  55. // 1小时内流量异常用户
  56. public function trafficAbnormal(): array
  57. {
  58. $userTotalTrafficList = self::whereNodeId(null)
  59. ->where('total', '>', MB * 50)
  60. ->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900))
  61. ->groupBy('user_id')
  62. ->selectRaw('user_id, sum(total) as totalTraffic')->pluck('totalTraffic', 'user_id')
  63. ->toArray(); // 只统计50M以上的记录,加快速度
  64. foreach ($userTotalTrafficList as $user => $traffic) {
  65. if ($traffic > sysConfig('traffic_ban_value') * GB) {
  66. $result[] = $user;
  67. }
  68. }
  69. return $result ?? [];
  70. }
  71. }