作者 Your Name
... ... @@ -38,7 +38,6 @@ class SyncFile extends Command
echo date('Y-m-d H:i:s') . '编辑的path为:'. $v['path'] .',主键id:'. $v['id'] . PHP_EOL;
$errorFileModel->edit(['status'=>1],['id'=>$v['id']]);
}
gc_collect_cycles();
}
echo date('Y-m-d H:i:s') . '编辑的end为:' . PHP_EOL;
return true;
... ...
<?php
namespace App\Console\Commands\Sync;
use App\Exceptions\InquiryFilterException;
use App\Models\Project\Project;
use App\Models\SyncSubmitTask\SyncSubmitTaskZbj as SyncSubmitTaskModel;
use App\Services\SyncSubmitTaskService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
/**
*
* Class SyncSubmitTask
* @package App\Console\Commands
* @author zbj
* @date 2023/11/28
*/
class SyncSubmitTaskZbj extends Command
{
protected $signature = 'sync_submit_task_zbj';
protected $description = '询盘、访问异步任务';
public function handle()
{
$backup = false;
while (true) {
$task_id = $this->getTaskId();
if ($task_id > 2000000) {
$backup = true;
}
if (empty($task_id)) {
if ($backup) {
$this->backup();
$backup = false;
}
sleep(5);
continue;
}
$this->output('任务' . $task_id . '开始');
$task_info = SyncSubmitTaskModel::find($task_id);
if (empty($task_info) || $task_info->status != 3) {
$this->output('任务不存在或者已执行');
continue;
}
try {
$project = Project::getProjectByDomain($task_info['data']['domain'] ?? '');
$task_info->project_id = $project->id;
SyncSubmitTaskService::handler($task_info, $task_info->created_at);
$task_info->status = 1;
$task_info->save();
$this->output('任务完成');
} catch (InquiryFilterException $e) {
$task_info->status = 1;
$task_info->is_filtered = 1;
$task_info->remark = $e->getMessage();
$task_info->save();
$this->output('任务完成');
} catch (\Exception $e) {
$task_info->retry = $task_info->retry + 1;
if ($task_info->retry >= 3) {
$task_info->status = 2;
$task_info->remark = Str::substr($e->getMessage(), 0, 200);
} else {
Redis::lpush('sync_submit_task_zbj', $task_id);
}
$task_info->save();
$this->output('任务失败:' . $e->getMessage());
}
}
}
public function getTaskId()
{
$task_id = Redis::rpop('sync_submit_task_zbj');
if (empty($task_id)) {
$ids = SyncSubmitTaskModel::where('status', 3)->limit(100)->pluck('id');
foreach ($ids as $id) {
Redis::lpush('sync_submit_task_zbj', $id);
}
$task_id = Redis::rpop('sync_submit_task_zbj');
}
return $task_id;
}
/**
* 输出处理日志
*/
public function output($message): bool
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
return true;
}
/**
* 备份数据
* @author zbj
* @date 2024/1/23
*/
public function backup()
{
DB::beginTransaction();
try {
$table = (new SyncSubmitTaskModel())->getTable();
$new_table = $table . '_backup_' . date('Ymd');
//重命名当前表
Schema::rename($table, $new_table);
//克隆表数据
DB::statement('CREATE TABLE ' . $table . ' LIKE ' . $new_table);
DB::commit();
$this->output('数据备份成功');
} catch (\Exception $e) {
$this->output('数据备份失败' . $e->getMessage());
DB::rollBack();
}
return true;
}
}
... ...
... ... @@ -10,11 +10,16 @@ use App\Models\HomeCount\Count;
use App\Models\Inquiry\InquiryFormData;
use App\Models\Nav\BNav;
use App\Models\Nav\BNavGroup;
use App\Models\Project\OnlineCheck;
use App\Models\Project\Project;
use App\Models\SyncSubmitTask\SyncSubmitTask as SyncSubmitTaskModel;
use App\Models\WebSetting\Translate as TranslateModel;
use App\Models\WebSetting\WebLanguage;
use App\Services\ProjectServer;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
/**
* Class Test
... ... @@ -53,6 +58,19 @@ class Test extends Command
*/
public function handle()
{
$date = '2024-05-16';
$list = $this->getProjectList();
foreach ($list as $item){
ProjectServer::useProject($item['project_id']);
//pv统计
$pv_num = $this->pv_num($date);
//ip统计
$ip_num = $this->ip_num($date);
echo $item['project_id'] . ',pv:' . $pv_num . ',ip:' . $ip_num . PHP_EOL;
}
exit;
$i=0;
while (true){
... ... @@ -167,4 +185,78 @@ class Test extends Command
$arr['country'] = json_encode($top20);
return $arr;
}
protected function getProjectList($type = 1){
$ru_lang_id = WebLanguage::getIdByLang('ru');
//推广项目
$list = Project::with('domainInfo')
->leftJoin('gl_project_deploy_optimize as pdo', 'pdo.project_id', '=', 'gl_project.id')
->leftJoin('gl_project_online_check as poc', 'poc.project_id', '=', 'gl_project.id')
->where('pdo.domain', '>', 0)
->where('poc.qa_status', OnlineCheck::STATUS_ONLINE_TRUE)
->whereIn('gl_project.type', [Project::TYPE_TWO, Project::TYPE_FOUR])
->where('gl_project.is_upgrade', 0) //非升级项目
->where('gl_project.main_lang_id', '<>', $ru_lang_id) //非俄语站
->where(function ($query) use ($type) {
if($type == 1){
//1-3个月项目
$startTime = Carbon::now()->addMonths(-4)->toDateString();
$endTime = Carbon::now()->addMonths(-1)->toDateString();
$query->whereBetween('pdo.start_date', [$startTime,$endTime]);
}elseif($type == 2){
//4-8个月项目
$startTime = Carbon::now()->addMonths(-9)->startOfDay()->toDateTimeString();
$endTime = Carbon::now()->addMonths(-4)->endOfDay()->toDateTimeString();
$query->whereBetween('pdo.start_date', [$startTime,$endTime]);
}else{
//大于9个月项目
$startTime = Carbon::now()->addMonths(-9)->startOfDay()->toDateTimeString();
$query->where('pdo.start_date', '<', $startTime);
}
})->select(['pdo.project_id','gl_project.main_lang_id','gl_project.id'])
->orderBy('project_id')
->get();
//其他地方在引流的域名
$other = DB::connection('projects_mysql')->table('projects')->where('switch', 1)->pluck('domain')->toArray();
$data = [];
foreach ($list as $project) {
$lang = WebLanguage::getLangById($project['main_lang_id']??1)['short'];
if(empty($project->domainInfo['domain'])){
continue;
}
//其他地方在引流就不再引流了
if(in_array($project->domainInfo['domain'], $other)){
continue;
}
$data[] = [
'project_id' => $project['project_id'],
'domain' => 'https://' . $project->domainInfo['domain'] . '/',
'lang' => $lang
];
}
return $data;
}
/**
* @name :(统计pv)pv_num
* @author :lyh
* @method :post
* @time :2023/6/14 15:40
*/
public function pv_num($yesterday){
$pv = DB::connection('custom_mysql')->table('gl_customer_visit_item')->where('updated_date', $yesterday)->count();
return $pv;
}
/**
* @name :(统计ip)ip_num
* @author :lyh
* @method :post
* @time :2023/6/14 15:40
*/
public function ip_num($yesterday)
{
$ip = DB::connection('custom_mysql')->table('gl_customer_visit')->where('updated_date', $yesterday)->count();
return $ip;
}
}
... ...
... ... @@ -21,6 +21,7 @@ use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;
/**
... ... @@ -172,22 +173,32 @@ class WebTrafficFix extends Command
$date = $this->argument('date');
$need_num = $this->argument('need_num');
$project_list = $this->getProjectList($type);
// $project_list = $this->getProjectList($type);
// exit;
while (true) {
$project = Redis::rpop('web_traffic_fix');
if(empty($project)){
break;
}
$project_list = [json_decode($project, true)];
foreach ($project_list as $project) {
ProjectServer::useProject($project['project_id']);
if($project['project_id'] <= 135){
continue;
}
// if($project['project_id'] <= 135){
// continue;
// }
echo 'project_id:' . $project['project_id'] . PHP_EOL;
Log::info('web_traffic_fix project_id:' . $project['project_id']);
$ip_num = DB::connection('custom_mysql')->table('gl_customer_visit')->whereDate('updated_date', $date)->count();
if($ip_num >= 30){
$ip_num = DB::connection('custom_mysql')->table('gl_customer_visit')->where('updated_date', $date)->count();
if ($ip_num >= 30) {
echo $ip_num . PHP_EOL;
continue;
}
$randomTime = [];
for ($i=0;$i<$need_num-$ip_num;$i++){
for ($i = 0; $i < $need_num - $ip_num; $i++) {
$randomTime[] = Carbon::make($date)->addSeconds(rand(0, 86400))->toDateTimeString();
}
sort($randomTime);
... ... @@ -195,7 +206,8 @@ class WebTrafficFix extends Command
$project_urls = $this->getProductUrls($project['project_id']);
$project_urls['home'] = $project['domain'];
foreach ($randomTime as $time){
$data = [];
foreach ($randomTime as $time) {
//随机引流间隔
$res_sjjg = $this->get_rand($this->sjjg);
if ($res_sjjg == 1) {
... ... @@ -207,10 +219,9 @@ class WebTrafficFix extends Command
//随机客户端
$project['device_port'] = $this->get_rand($this->yddzb);
$project['user_agent'] = $project['device_port'] == 1 ? Arr::random($this->pc_ua) : Arr::random($this->mobile_ua);
$a = time();
$project['ip'] = $this->getIpAreas([$project['project_id']], $time)[0] ?? '';
foreach ($project['visit_urls'] as $url){
foreach ($project['visit_urls'] as $url) {
$time = Carbon::make($time)->addSeconds(rand(2, 15))->toDateTimeString();
$url_array = parse_url($project['domain']);
$referrer_url = $this->getReferer($project['ip']['ip_area'], $project['lang']);
... ... @@ -226,14 +237,19 @@ class WebTrafficFix extends Command
'referrer_url' => $referrer_url
],
];
$task = new SyncSubmitTask();
$task->data = json_encode($array);
$task->type = SyncSubmitTask::TYPE_VISIT;
$task->created_at = $time;
$task->status = 3;
$task->traffic = 1;
$task->save();
$data[] = [
'data' => json_encode($array),
'type' => SyncSubmitTask::TYPE_VISIT,
'created_at' => $time,
'updated_at' => $time,
'status' => 3,
'traffic' => 1,
'project_id' => $project['project_id'],
];
}
}
DB::table('gl_sync_submit_task_20240516')->insert($data);
}
}
Log::info('web_traffic_fix finish');
... ... @@ -279,7 +295,7 @@ class WebTrafficFix extends Command
->get();
//其他地方在引流的域名
$other = DB::connection('projects_mysql')->table('projects')->where('switch', 1)->pluck('domain')->toArray();
$data = [];
foreach ($list as $project) {
$lang = WebLanguage::getLangById($project['main_lang_id']??1)['short'];
if(empty($project->domainInfo['domain'])){
... ... @@ -289,13 +305,15 @@ class WebTrafficFix extends Command
if(in_array($project->domainInfo['domain'], $other)){
continue;
}
$data[] = [
$data = [
'project_id' => $project['project_id'],
'domain' => 'https://' . $project->domainInfo['domain'] . '/',
'lang' => $lang
];
Redis::lpush('web_traffic_fix', json_encode($data));
}
return $data;
return true;
}
/**
... ... @@ -355,19 +373,26 @@ class WebTrafficFix extends Command
$main_countries = !empty($config->main_countries) ? explode(',',$config->main_countries) : [];
$filter_countries = !empty($config->filter_countries) ? explode(',',$config->filter_countries) : [];
//根据地区随机取该地区的IP
$ipdata = DB::table('gl_xunpan_ipdata')->whereIn('ip_area', $areas)
->where(function ($query) use ($main_countries, $filter_countries){
if($main_countries){
$query->whereIn('ip_area', $main_countries);
$areas = $main_countries;
}
if($filter_countries){
$query->whereNotIn('ip_area', $main_countries);
$areas2 = [];
foreach ($areas as $v){
if(!in_array($v, $filter_countries)){
$areas2[] = $v;
}
}
$areas = $areas2;
}
})->inRandomOrder()->first();
//根据地区随机取该地区的IP
$ipdata = DB::table('gl_xunpan_ipdata')->whereIn('ip_area', $areas)->inRandomOrder()->first();
if(!$ipdata){
continue;
}
$ipdata = (array)$ipdata ?: [];
$ipdata['diff'] = $time_zones[$ipdata['ip_area']];
$data[] = $ipdata;
... ...
<?php
namespace App\Models\SyncSubmitTask;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
/**
* @method static where(string $string, mixed $ip)
* @method static create(array $data)
*/
class SyncSubmitTaskZbj extends Model
{
const TYPE_INQUIRY = 'inquiry';
const TYPE_VISIT = 'visit';
const TRAFFIC_DEFAULT = 0;
const TRAFFIC_TRUE = 1;
//设置关联表名
/**
* @var mixed
*/
protected $table = 'gl_sync_submit_task_20240516';
protected $casts = [
'data' => 'array',
];
/**
* @param $type
* @param $data
* @return bool
* @author zbj
* @date 2023/11/28
*/
public static function addTask($type, $data): bool
{
if (empty($data)) {
return false;
}
try {
$data = [
'data' => $data['data'],
'domain' => !empty($data['domain']) ? $data['domain'] : request()->getHost(),
'ip' => !empty($data['ip']) ? $data['ip'] : request()->getClientIp(),
'referer' => !empty($data['referer']) ? $data['referer'] : request()->header('Referer'),
'user_agent' => !empty($data['user_agent']) ? $data['user_agent'] : request()->header('user_agent'),
];
if(empty($data['referer']) || empty($data['user_agent']) || empty($data['data'])){
return false;
}
$model = new self();
$model->type = $type;
$model->data = $data;
!empty($data['submit_time']) && $model->created_at = $data['submit_time'];
$model->save();
} catch (\Exception $e) {
Log::error('SyncSubmitTask addTask error', ['msg' => $e->getMessage(), 'data' => $data]);
return false;
}
return true;
}
}
... ...
... ... @@ -53,11 +53,14 @@ class Visit extends Base
/**
* 访问写入
*/
public static function saveData($data)
public static function saveData($data, $date)
{
if(!$date){
$date = date('Y-m-d');
}
//判断IP当天是否有一条数据
$visit = Visit::where("ip",$data['ip'])->where("created_at",">=",Carbon::now()->today()->startOfDay())
->where("created_at","<=",Carbon::now()->today()->endOfDay())
$visit = Visit::where("ip",$data['ip'])->where("created_at",">=",Carbon::make($date)->startOfDay())
->where("created_at","<=",Carbon::make($date)->endOfDay())
->first();
DB::connection('custom_mysql')->beginTransaction();
if (!empty($visit) && $visit->count() >= 1){
... ...
... ... @@ -31,7 +31,7 @@ class SyncSubmitTaskService
* @author zbj
* @date 2023/11/28
*/
public static function handler($task)
public static function handler($task, $date = '')
{
$data = $task['data'];
$checkIpCountry = self::checkIpCountry($data['domain'], $data['ip'], $task['type']);
... ... @@ -53,7 +53,7 @@ class SyncSubmitTaskService
$action = $task['type'];
$handler = new self();
return $handler->$action($data);
return $handler->$action($data, $date);
}
... ... @@ -85,7 +85,7 @@ class SyncSubmitTaskService
* @author zbj
* @date 2023/12/4
*/
public function inquiry($data)
public function inquiry($data, $date)
{
$this->inquiryFilter($data['project_id'], $data);
... ... @@ -113,7 +113,7 @@ class SyncSubmitTaskService
* @author zbj
* @date 2023/12/4
*/
public function visit($data)
public function visit($data, $date)
{
$visit_data = $data['data'];
... ... @@ -132,7 +132,7 @@ class SyncSubmitTaskService
$visit_data['country'] = $data['country'];
$visit_data['updated_date'] = $data['submit_at']->toDateString();
$visit_data['created_at'] = $data['submit_at'];
Visit::saveData($visit_data);
Visit::saveData($visit_data, $date);
return true;
}
... ...