12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * 用户流量每小时统计
- *
- * @property int $id
- * @property int $user_id 用户ID
- * @property int|null $node_id 节点ID,null表示统计全部节点
- * @property int $u 上传流量
- * @property int $d 下载流量
- * @property int $total 总流量
- * @property string|null $traffic 总流量(带单位)
- * @property \Illuminate\Support\Carbon $created_at 创建时间
- * @property-read \App\Models\Node|null $node
- * @property-read \App\Models\User $user
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow newModelQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow newQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow query()
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow userHourly($uid)
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow userRecentUsed($uid)
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereCreatedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereD($value)
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereNodeId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereTotal($value)
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereTraffic($value)
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereU($value)
- * @method static \Illuminate\Database\Eloquent\Builder|UserHourlyDataFlow whereUserId($value)
- * @mixin \Eloquent
- */
- class UserHourlyDataFlow extends Model
- {
- public const UPDATED_AT = null;
- protected $table = 'user_hourly_data_flow';
- protected $guarded = [];
- public function user(): BelongsTo
- {
- return $this->belongsTo(User::class);
- }
- public function node(): BelongsTo
- {
- return $this->belongsTo(Node::class);
- }
- // 用户每时使用总流量
- public function scopeUserHourly($query, $uid)
- {
- return $query->whereUserId($uid)->whereNodeId(null);
- }
- public function scopeUserRecentUsed($query, $uid)
- {
- return $query->userHourly($uid)->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900));
- }
- // 1小时内流量异常用户
- public function trafficAbnormal(): array
- {
- $userTotalTrafficList = self::whereNodeId(null)
- ->where('total', '>', MB * 50)
- ->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900))
- ->groupBy('user_id')
- ->selectRaw('user_id, sum(total) as totalTraffic')->pluck('totalTraffic', 'user_id')
- ->toArray(); // 只统计50M以上的记录,加快速度
- foreach ($userTotalTrafficList as $user => $traffic) {
- if ($traffic > sysConfig('traffic_ban_value') * GB) {
- $result[] = $user;
- }
- }
- return $result ?? [];
- }
- }
|