用户每日活跃检测

This commit is contained in:
cano
2024-03-02 16:53:43 +08:00
parent 1a9008b318
commit f5f7168009
9 changed files with 244 additions and 1 deletions

View File

@ -0,0 +1,35 @@
<?php
namespace App\Console\Commands;
use App\Models\Customer\CustomerUserExtendModel;
use Illuminate\Console\Command;
class DailyCheckUserActiveStatus extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:daily-check-user-active-status';
/**
* The console command description.
*
* @var string
*/
protected $description = '每日更新用户活跃状态';
/**
* Execute the console command.
*/
public function handle()
{
$this->info('每日更新用户活跃状态');
$this->info('开始...');
$oCustomerUserExtendModelExtend = new CustomerUserExtendModel();
$oCustomerUserExtendModelExtend->updateUserActiveStatus();
$this->info('结束...');
}
}

View File

@ -13,6 +13,7 @@ class Kernel extends ConsoleKernel
protected function schedule(Schedule $schedule): void protected function schedule(Schedule $schedule): void
{ {
// $schedule->command('inspire')->hourly(); // $schedule->command('inspire')->hourly();
$schedule->command('app:daily-check-user-active-status')->dailyAt('00:30')->onOneServer();
} }
/** /**

View File

@ -0,0 +1,50 @@
<?php
namespace App\Models\Customer;
use App\Cache\Table\TableCustomerUserCache;
use App\Models\Base\CustomerBaseModel;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
class CustomerChangeInfoLogModel extends CustomerBaseModel
{
protected $table = 'customer_change_info_log';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'type',
'uid',
'column',
'value',
'before_value',
'after_value',
'pid',
'remark_key',
'remark_desc',
'created_at',
];
const PID_SYSTEM = 0; //系统默认pid
const TYPE_CHANG_USER_ACTIVE_STATUS = 1;
const TYPE = [
self::TYPE_CHANG_USER_ACTIVE_STATUS => '修改用户活跃状态',
];
const REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES = 'dailyCheckUserActiveStatusYes';
const REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_NO = 'dailyCheckUserActiveStatusNo';
const REMARK = [
self::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES => '每日检查用户活跃状态-活跃',
self::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_NO => '每日检查用户活跃状态-不活跃',
];
function addLog($aItem): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
$sDateTime = Carbon::now()->toDateTimeString();
$aItem['created_at'] = $sDateTime;
return $this->addItem($aItem);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Models\Customer;
use App\Cache\Table\TableCustomerUserCache;
use App\Models\Base\CustomerBaseModel;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
class CustomerLoginHistoryModel extends CustomerBaseModel
{
protected $table = 'customer_login_history';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'status',
'uid',
'device',
'created_at',
];
}

View File

@ -4,6 +4,10 @@ namespace App\Models\Customer;
use App\Exceptions\ModelException; use App\Exceptions\ModelException;
use App\Models\Base\CustomerBaseModel; use App\Models\Base\CustomerBaseModel;
use App\Models\WebSocket\CustomerWsHistoryModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class CustomerUserExtendModel extends CustomerBaseModel class CustomerUserExtendModel extends CustomerBaseModel
{ {
@ -19,6 +23,8 @@ class CustomerUserExtendModel extends CustomerBaseModel
'updated_at', 'updated_at',
]; ];
const COL_IS_ACTIVE = 'is_active';
//是否活跃用户 //是否活跃用户
const IS_ACTIVE_YES = 1; const IS_ACTIVE_YES = 1;
const IS_ACTIVE_NO = 2; const IS_ACTIVE_NO = 2;
@ -69,4 +75,80 @@ class CustomerUserExtendModel extends CustomerBaseModel
return $this->newQuery()->where('uid',$uid)->decrement('follow_num'); return $this->newQuery()->where('uid',$uid)->decrement('follow_num');
} }
//获取用户活跃信息
function getUserActiveListLimit($nowUid, $limit = 500): \Illuminate\Database\Eloquent\Collection|array
{
return $this->newQuery()
->where('id', '>=', $nowUid)
->limit($limit)
->orderBy('id')
->get(['uid', 'is_active']);
}
//检测所有用户活跃状态
function updateUserActiveStatus($date = null): void
{
if(empty($date)) $date = Carbon::yesterday()->toDateString();
$oCustomerWsHistoryModel = new CustomerWsHistoryModel();
$aActiveUserIdList = $oCustomerWsHistoryModel->getActiveUserIdList($date); //三日内活跃用户
if(empty($aActiveUserIdList)) return;
$oCustomerChangeInfoLogModel = new CustomerChangeInfoLogModel();
$nowUid = 0;
while (true){
try{
Db::beginTransaction();
$aUserList = $this->getUserActiveListLimit($nowUid, 500);
if(empty($aUserList)) break;
$nowUid = max($aUserList->pluck('uid')->toArray());
foreach ($aUserList as $oUser){
$isChanged = false;
$aLogInsert = [
'type' => CustomerChangeInfoLogModel::TYPE_CHANG_USER_ACTIVE_STATUS,
'uid' => $oUser->uid,
'column' => self::COL_IS_ACTIVE,
'before_value' => $oUser->is_active,
'pid' => CustomerChangeInfoLogModel::PID_SYSTEM,
];
if(in_array($oUser->uid, $aActiveUserIdList)){ //在活跃列表中
if($oUser->is_active == self::IS_ACTIVE_YES) continue; //已经是活跃用户
//变更用户状态
$res = $this->newQuery()->where('uid',$oUser->uid)->update(['is_active'=>self::IS_ACTIVE_YES]);
if($res) {
$isChanged = true;
$aLogInsert['value'] = self::IS_ACTIVE_YES;
$aLogInsert['after_value'] = self::IS_ACTIVE_YES;
$aLogInsert['remark_key'] = CustomerChangeInfoLogModel::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES;
$aLogInsert['remark_desc'] = CustomerChangeInfoLogModel::REMARK[CustomerChangeInfoLogModel::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES];
}
}else{ //三日内不活跃
if($oUser->is_active == self::IS_ACTIVE_NO) continue; //已经是不活跃用户
//变更用户状态
$res = $this->newQuery()->where('uid',$oUser->uid)->update(['is_active'=>self::IS_ACTIVE_NO]);
if($res) {
$isChanged = true;
$aLogInsert['value'] = self::IS_ACTIVE_NO;
$aLogInsert['after_value'] = self::IS_ACTIVE_NO;
$aLogInsert['remark_key'] = CustomerChangeInfoLogModel::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_NO;
$aLogInsert['remark_desc'] = CustomerChangeInfoLogModel::REMARK[CustomerChangeInfoLogModel::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_NO];
}
}
if($isChanged){ //有变化插入日志
$oCustomerChangeInfoLogModel->addLog($aLogInsert);
}
}
Db::commit();
}catch (\Exception $e){
Log::error('updateUserActiveStatus error:'.$e->getMessage());
Db::rollBack();
}
}
}
} }

View File

@ -138,4 +138,6 @@ class CustomerUserModel extends CustomerBaseModel
// } // }
} }

View File

@ -96,7 +96,7 @@ class FollowModel extends BaseModel
$oModel = DB::table($oFollowModel->getTable() . ' as a'); $oModel = DB::table($oFollowModel->getTable() . ' as a');
return $oModel->where('a.uid', $uid) return $oModel->where('a.uid', $uid)
->leftjoin($oCustomerUserExtendModel->getTable() . ' as b', 'a.follow_uid', '=', 'b.uid') ->leftjoin($oCustomerUserExtendModel->getTable() . ' as b', 'a.follow_uid', '=', 'b.uid')
->where('b.fans_num', $iFansLimit) ->where('b.fans_num','>=', $iFansLimit)
->get($col); ->get($col);
} }

View File

@ -149,6 +149,7 @@ class PostPushBoxModel extends BaseModel
//放在用户状态更新时调用 //放在用户状态更新时调用
function pullBigFanMasterPost($uid) function pullBigFanMasterPost($uid)
{ {
//获取大v定义粉丝数,获取大于该粉丝数的大v文章
$iConfigFansPushLimit = intval(env('CONFIG_FANS_PUSH_LIMIT', 2000)); $iConfigFansPushLimit = intval(env('CONFIG_FANS_PUSH_LIMIT', 2000));
//@此处需针对大量数据进行分批获取 //@此处需针对大量数据进行分批获取
$oFollowModel = new FollowModel(); $oFollowModel = new FollowModel();

View File

@ -0,0 +1,49 @@
<?php
namespace App\Models\WebSocket;
use App\Cache\Table\TableCustomerUserCache;
use App\Models\Base\CustomerBaseModel;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class CustomerWsHistoryModel extends CustomerBaseModel
{
protected $table = 'customer_ws_history';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'event',
'status',
'uid',
'device',
'created_at',
];
const EVENT_ON_CONNECT = 1;
const EVENT = [
self::EVENT_ON_CONNECT => 'onConnect',
];
const STATUS_SUCCESS = 1;
const STATUS_FAIL = 2;
const STATUS = [
self::STATUS_SUCCESS => '成功',
self::STATUS_FAIL => '失败',
];
function getActiveUserIdList($date, $days = 3): array
{
return $this->newQuery()
->where('event', self::EVENT_ON_CONNECT)
->where('status', self::STATUS_SUCCESS)
->whereBetween(DB::raw("DATE_FORMAT(created_at,'%Y-%m-%d')"), [Carbon::parse($date)->subDays($days - 1)->toDateString(), Carbon::parse($date)->toDateString()])
->distinct(['uid'])
->get()
->pluck('uid')
->toArray();
}
}