Files
ProxyPanel/app/Models/UserHourlyDataFlow.php
兔姬桑 396fbbad56 优化 自动任务
1. 使用chunk分段处理大规模数据处理;
2. 优化部分任务的查询逻辑;
3. 提取各任务时间戳至,config/tasks.php,允许机场主自定义各任务;
2021-01-24 23:21:43 -05:00

56 lines
1.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* 用户流量每小时统计
*/
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 ?? [];
}
}