option('extra'); $param = $this->argument('type'); if(empty($param)){ echo 'wvs {type:1,2,3,4,5...} {--extra}'.PHP_EOL; return false; } if(!is_numeric($param)){ return false; } if($options && !is_numeric($options)){ return false; } switch ($param){ case 1: $this->sendorderstats(); break; case 2: $this->sendOrderStatsEveryDay(); break; case 3: $this->allstats(); break; case 4: $this->allStatsEveryDay(); break; case 5: $this->activitystats(); break; case 6: $this->activityStatsEveryDay($options); break; case 7: $this->beforeForceSubAndBeforeVipStats(); break; case 8: $this->beforeForceSubAndBeforeVipStatsEveryDay(); break; case 9: $this->start(); break; case 10: $this->subscribeKeyDel(); break; case 11: $this->substituteStats(); break; } return false; } public function start() { $this->allStatsEveryDay(); $this->sendOrderStatsEveryDay(); $this->nowActivitys(); $this->beforeForceSubAndBeforeVipStatsEveryDay(); $this->browserSendOrderStatsEveryDay(); $this->substituteStats(); } private function sendorderstats() { $day = date('Y-m-d',time()-86400); // Redis::hincrby('send_order_uv_'.$param,$key,1); //Redis::hincrby('send_order_pv_'.$param,$key,1);//每天 DB::table('send_orders')->select('id')->orderBy('id')->where('created_at', '<', date('Y-m-d H:i:s'))->chunk(1000, function ($res) use ($day){ $now = date('Y-m-d H:i:s'); foreach ($res as $v) { $temp = Redis::hgetall('send_order_pv_' . $v->id); if ($temp) { $data = []; foreach ($temp as $d => $u) { if ($d == 'total') { continue; } if($d == date('Y-m-d')){ continue; } //$temp_uv = (int)Redis::hget('send_order_uv_' . $v->id, $d); //$data[] = ['day' => $d, 'key' => $v->id, 'from_type' => 'send_orders', 'pv' => $u, 'uv' => $temp_uv, 'type' => 3, 'created_at' => $now, 'updated_at' => $now]; Redis::hdel('send_order_pv_' . $v->id,$d); Redis::hdel('send_order_uv_' . $v->id,$d); } //DB::table('wap_visit_stats')->insert($data); if(!Redis::hget('send_order_pv_' . $v->id,date('Y-m-d'))){ Redis::del('send_order_pv_' . $v->id); } if(!Redis::hget('send_order_uv_' . $v->id,date('Y-m-d'))){ Redis::del('send_order_uv_' . $v->id); } } } }); } private function sendOrderStatsEveryDay() { $day = date('Y-m-d', time() - 86400); $now = date('Y-m-d H:i:s'); $key_like = 'send_order_pv_'; //$keys = Redis::keys($key_like . '*'); $data = []; $i = 0; $all_send_order_id = Redis::SMEMBERS('send_order'.$day); foreach ($all_send_order_id as $send_order_id) { $key = $key_like.$send_order_id; $temp = Redis::hget($key, $day); $temp_uv = (int)Redis::hget('send_order_uv_' . $send_order_id, $day); $data[] = ['day' => $day, 'key' => $send_order_id, 'from_type' => 'send_orders', 'pv' => $temp, 'uv' => $temp_uv, 'type' => 3, 'created_at' => $now, 'updated_at' => $now]; if ($i && $i % 100 == 0) { DB::table('wap_visit_stats')->insert($data); $data = []; } $i++; Redis::hdel($key,$day); if(!Redis::hget($key,date('Y-m-d'))){ Redis::del($key); } Redis::hdel('send_order_uv_' . $send_order_id,$day); if(!Redis::hget('send_order_uv_' . $send_order_id,date('Y-m-d'))){ Redis::del('send_order_uv_' . $send_order_id); } } if($data){ DB::table('wap_visit_stats')->insert($data); } Redis::del('send_order'.$day); } private function browserSendOrderStatsEveryDay() { $day = date('Y-m-d', time() - 86400); $now = date('Y-m-d H:i:s'); $key_like = 'browser_send_order_pv_'; //$keys = Redis::keys($key_like . '*'); $data = []; $i = 0; $all_send_order_id = Redis::SMEMBERS('browser_send_order'.$day); foreach ($all_send_order_id as $send_order_id) { $key = $key_like.$send_order_id; $temp = Redis::hget($key, $day); $temp_uv = (int)Redis::hget('browser_send_order_uv_' . $send_order_id, $day); $data[] = ['day' => $day, 'key' => $send_order_id, 'from_type' => 'browser_send_orders', 'pv' => $temp, 'uv' => $temp_uv, 'type' => 3, 'created_at' => $now, 'updated_at' => $now]; if ($i && $i % 100 == 0) { DB::table('wap_visit_stats')->insert($data); $data = []; } $i++; Redis::hdel($key,$day); if(!Redis::hget($key,date('Y-m-d'))){ Redis::del($key); } Redis::hdel('browser_send_order_uv_' . $send_order_id,$day); if(!Redis::hget('browser_send_order_uv_' . $send_order_id,date('Y-m-d'))){ Redis::del('browser_send_order_uv_' . $send_order_id); } } if($data){ DB::table('wap_visit_stats')->insert($data); } Redis::del('browser_send_order'.$day); } private function allstats() { //Redis::sadd('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . date('Y-m-d'), $uid_cookie); //Redis::hincrby('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, date('Y-m-d'), 1); $day = date('Y-m-d'); $now = date('Y-m-d H:i:s'); $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get(); //$all_channel_id = [14]; foreach ($all_channel_id as $v) { if($v->id == 2){ continue; } $like_pv_key = 'customer:push:click:distribution_channel_id:' . $v->id . 'from:'; $froms = $this->getFrom($v->id); foreach ($froms as $from) { $true_key = $like_pv_key.$from; //echo '$true_key: '.$true_key.PHP_EOL; $temp = Redis::hgetall($true_key); $data = []; $i = 0; foreach ($temp as $d => $u) { if($d == $day){ continue; } $uv_key = 'push:distribution_channel_id:' . $v->id . 'from:' . $from . ':date:' . $d; $temp_uv = (int)Redis::scard($uv_key); $data[] = ['day' => $d, 'key' => $v->id, 'from_type' => $from, 'pv' => $u, 'uv' => $temp_uv, 'type' => 1, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')]; Redis::del($uv_key); Redis::hdel($true_key,$d); if($i && $i%100 == 0){ DB::table('wap_visit_stats')->insert($data); $data = []; } $i++; } $temp = Redis::hget($true_key,date('Y-m-d')); if(!$temp){ Redis::del($true_key); } DB::table('wap_visit_stats')->insert($data); } } } private function allStatsEveryDay() { //Redis::sadd('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . date('Y-m-d'), $uid_cookie); //Redis::hincrby('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, date('Y-m-d'), 1); $day = date('Y-m-d', time() - 86400); $now = date('Y-m-d H:i:s'); //$all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get(); $alls = Redis::SMEMBERS('wap_from_stats'.$day); $i = 0; $data = []; foreach ($alls as $v){ $arr = explode('-',$v); $distribution_channel_id = $arr[0]; $from = $arr[1]; if(count($arr) == 3){ $from = $arr[1].$arr[2]; } $pv_key = 'customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from; $pv = (int)Redis::hget($pv_key, $day); $uv_key = 'push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . $day; //echo '$pv_key: '.$pv_key.',$uv_key: $uv_key'.$uv_key.PHP_EOL; $temp_uv = (int)Redis::scard($uv_key); //echo $pv.'--'.$temp_uv.PHP_EOL; $data[] = ['day' => $day, 'key' => $distribution_channel_id, 'from_type' => $from, 'pv' => $pv, 'uv' => $temp_uv, 'type' => 1, 'created_at' => $now, 'updated_at' => $now]; if($i && $i%100 == 0){ DB::table('wap_visit_stats')->insert($data); $data = []; } $i++; Redis::del($uv_key); Redis::hdel($pv_key, $day); //$temp = Redis::hget($pv_key,date('Y-m-d')); $temp = Redis::hget($pv_key,date('Y-m-d')); if(!$temp){ Redis::del($pv_key); } } Redis::del('wap_from_stats'.$day); if($data){ DB::table('wap_visit_stats')->insert($data); } } private function activitystats() { $day = date('Y-m-d'); $now = date('Y-m-d H:i:s'); $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv'; $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv'; $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get(); foreach ($all_channel_id as $v) { for ($i = 1; $i <= 140; $i++) { $pv_key = sprintf($pv_key_format, $i, $v->id); $pv_temp = Redis::hgetall($pv_key); if ($pv_temp) { $data = []; foreach ($pv_temp as $d => $u) { if($d == $day){ continue; } $uv_key = sprintf($uv_key_format, $i, $v->id, $d); //$temp_uv = (int)Redis::scard($uv_key); //$data[] = ['day' => $d, 'key' => $v->id, 'from_type' => $i, 'pv' => $u, 'uv' => $temp_uv, 'type' => 2, 'created_at' => $now, 'updated_at' => $now]; Redis::del($uv_key); } //DB::table('wap_visit_stats')->insert($data); } Redis::del($pv_key); } } } private function activityStatsEveryDay($activity_id) { $day = date('Y-m-d',time()-86400); $now = date('Y-m-d H:i:s'); $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv'; $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv'; $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get(); foreach ($all_channel_id as $v) { $i = $activity_id; $pv_key = sprintf($pv_key_format, $i, $v->id); $pv_temp = Redis::hgetall($pv_key); if ($pv_temp) { $data = []; foreach ($pv_temp as $d => $u) { if($d != $day){ continue; } $uv_key = sprintf($uv_key_format, $i, $v->id, $d); $temp_uv = (int)Redis::scard($uv_key); $data[] = ['day' => $d, 'key' => $v->id, 'from_type' => $i, 'pv' => $u, 'uv' => $temp_uv, 'type' => 2, 'created_at' => $now, 'updated_at' => $now]; Redis::del($uv_key); Redis::hdel($pv_key,$d); } DB::table('wap_visit_stats')->insert($data); if( !Redis::hget($pv_key,date('Y-m-d'))){ Redis::del($pv_key); } } } } private function nowActivitys(){ $day = date('Y-m-d',time()-86400); $activty_ids = DB::table('activity')->where('start_time','<=',$day.' 23:00:00')->where('end_time','>=',$day)->select('id')->get(); foreach ($activty_ids as $v){ //echo $v->id.PHP_EOL; $this->activityStatsEveryDay($v->id); } } private function beforeForceSubAndBeforeVipStats() { //$uv_key = 'book:'.$book->bid.':beforevip:uv:date:'.$date.':send_order_id:'.$send_order_id; //$pv_key = 'book:'.$book->bid.':beforevip:pv:send_order_id:'.$send_order_id; $send_order_ids = DB::table('send_orders')->where('send_time','>=','2017-12-10')->select('id')->get(); $now = date('Y-m-d H:i:s'); for ($i = 1; $i < 100; $i++) { $like_key = 'book:' . $i . ':beforevip:pv:send_order_id:'; //$all_key = Redis::keys($like_key . '*'); if ($send_order_ids) { foreach ($send_order_ids as $ids) { $data = []; $send_order_id = $ids->id; $kkk = $like_key.$send_order_id; $temp_pv = Redis::hgetall($kkk); foreach ($temp_pv as $d => $u) { if($d == 'total'){ continue; } if($d == date('Y-m-d')){ continue; } $uv_key = 'book:' . $i . ':beforevip:uv:date:' . $d . ':send_order_id:' . $send_order_id; //$temp_uv = (int)Redis::scard($uv_key); //$data[] = ['day' => $d, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $u, 'uv' => $temp_uv, 'type' => 4, 'created_at' => $now, 'updated_at' => $now]; Redis::del($uv_key); Redis::hdel($kkk,$d); } if(!Redis::hget($kkk,date('Y-m-d'))){ Redis::del($kkk); } //DB::table('wap_visit_stats')->insert($data); } } } //$uv_key = 'book:' . $book->bid . ':beforeforcesub:uv:date:' . $date . ':send_order_id:' . $send_order_id; //$pv_key = 'book:' . $book->bid . ':beforeforcesub::pvsend_order_id:' . $send_order_id; for ($i = 1; $i < 100; $i++){ $like_key = 'book:' . $i . ':beforeforcesub::pvsend_order_id:'; //$send_order_ids = DB::table('send_orders')->where('send_time','>=','2017-12-10')->select('id')->get(); //$all_key = Redis::keys($like_key . '*'); if($send_order_ids){ foreach ($send_order_ids as $vvvv){ $send_order_id = $vvvv->id; $ks = $like_key.$send_order_id; $data = []; $t_pv = Redis::hgetall($ks); foreach ($t_pv as $day => $v){ if($day == 'total'){ continue; } if($day == date('Y-m-d')){ continue; } $uv_key = 'book:' . $i . ':beforeforcesub:uv:date:' . $day . ':send_order_id:' . $send_order_id; //$t_uv = (int)Redis::scard($uv_key); //$data[] = ['day' => $day, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $v, 'uv' => $t_uv, 'type' => 5, 'created_at' => $now, 'updated_at' => $now]; Redis::del($uv_key); Redis::hdel($ks,$day); } if(!Redis::hget($ks,date('Y-m-d'))){ Redis::del($ks); } //DB::table('wap_visit_stats')->insert($data); } } } } private function beforeForceSubAndBeforeVipStatsEveryDay() { //$uv_key = 'book:'.$book->bid.':beforevip:uv:date:'.$date.':send_order_id:'.$send_order_id; //$pv_key = 'book:'.$book->bid.':beforevip:pv:send_order_id:'.$send_order_id; $now = date('Y-m-d H:i:s'); $day = date('Y-m-d',time()-86400); //Redis::SISMEMBER('beforevip'.$date,$book->bid.'-'.$send_order_id); $alls = Redis::SMEMBERS('beforevip'.$day); $j = 0; $data = []; foreach ($alls as $v){ $temp = explode('-',$v); $i = $temp[0]; $send_order_id = $temp[1]; $like_key = 'book:' . $i . ':beforevip:pv:send_order_id:'; $kkk = $like_key.$send_order_id; $temp_pv = Redis::hget($kkk,$day); $uv_key = 'book:' . $i . ':beforevip:uv:date:' . $day . ':send_order_id:' . $send_order_id; $temp_uv = (int)Redis::scard($uv_key); $data[] = ['day' => $day, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $temp_pv, 'uv' => $temp_uv, 'type' => 4, 'created_at' => $now, 'updated_at' => $now]; Redis::del($uv_key); Redis::hdel($kkk,$day); if($j && $j%100 == 0){ DB::table('wap_visit_stats')->insert($data); $data = []; } if(!Redis::hget($kkk,date('Y-m-d'))){ Redis::del($kkk); } $j++; } if($data){ DB::table('wap_visit_stats')->insert($data); } Redis::del('beforevip'.$day); $data = []; // Redis::sadd('beforeforcesub'.$date,$book->bid.'-'.$send_order_id); $alls = Redis::SMEMBERS('beforeforcesub'.$day); if($alls){ $i = 0; foreach ($alls as $vs){ $temp = explode('-',$vs); $bid = $temp[0]; $send_order_id = $temp[1]; $like_key = 'book:' . $bid . ':beforeforcesub::pvsend_order_id:'; $kkk = $like_key.$send_order_id; $temp_pv = Redis::hget($kkk,$day); $uv_key = 'book:' . $bid . ':beforeforcesub:uv:date:' . $day . ':send_order_id:' . $send_order_id; $temp_uv = (int)Redis::scard($uv_key); $data[] = ['day' => $day, 'key' => $bid, 'from_type' => $send_order_id, 'pv' => $temp_pv, 'uv' => $temp_uv, 'type' => 5, 'created_at' => $now, 'updated_at' => $now]; Redis::del($uv_key); Redis::hdel($kkk,$day); if($i && $i%100 == 0){ DB::table('wap_visit_stats')->insert($data); $data = []; } if(!Redis::hget($kkk,date('Y-m-d'))){ Redis::del($kkk); } $i++; } if($data){ DB::table('wap_visit_stats')->insert($data); } } Redis::del('beforeforcesub'.$day); } private function getFrom(int $channel_id):array { $from = ['point_push','not_pay','hot_push','unpaid_send_activity','pay_daily_push', 'auto_custom_trusteeship','subscribe_push','sign_push','reader','signcallback','reader-year','recovery_push']; $custom_ids = DB::table('custom_send_msgs')->select('id','trusteeship')->where('distribution_channel_id',$channel_id)->get(); if($custom_ids){ foreach ($custom_ids as $item){ if($item->trusteeship){ for ($i=1;$i<=4;$i++){ $from[] = 'custom_'.$item->id.'_'.$i; } $from[] = 'custom_'.$item->id; }else{ $from[] = 'custom_'.$item->id; } } } $template = DB::table('wechat_template_msgs')->select('id')->where('distribution_channel_id',$channel_id)->get(); if($template) { foreach ($template as $val){ $from[] = 'template_'.$val->id; } } $keyword = DB::table('wechat_keyword_msgs')->select('id')->where('distribution_channel_id',$channel_id)->get(); if($keyword){ foreach ($keyword as $value){ $from[] = 'keyword_'.$value->id; } } return $from; } private function subscribeKeyDel(){ $account = $this->officialAccount(); $key_format = 'www.zhuishuyun.comforce_subscribe:scene:%s:%s'; //print_r($account);return; /*for ($i=1;$i<10000;$i++){ foreach ($account as $appid){ Redis::del(sprintf($key_format,$i,$appid)); } }*/ for ($i = 10;$i<100;$i++){ $skip = $i*10000; $res = DB::table('users')->select('id','distribution_channel_id')->orderBy('id')->where('id','>=',$skip)->limit(10000)->get(); foreach ($res as $v){ if(isset($account[$v->distribution_channel_id]) && $account[$v->distribution_channel_id]){ foreach ($account[$v->distribution_channel_id] as $appid){ Redis::del(sprintf($key_format,$i,$appid)); } } } $res = null; } } private function officialAccount():array{ $res = DB::table('official_accounts')->select('distribution_channel_id','appid')->get(); $data = []; foreach ($res as $v){ $data[$v->distribution_channel_id][] = $v->appid; //$data[] = $v->appid; } return $data; } private function substituteStats(){ $day = date('Y-m-d',time()-86400); //代充值按钮 $substituteButtonUvPv = Redis::SMEMBERS('substitutebuttonpvuv'.$day); if($substituteButtonUvPv){ $i = 0; $data = []; foreach ($substituteButtonUvPv as $channel_id){ $temp_pv = Redis::hget('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,$day); $temp_uv_key = sprintf('substitutebuttonpvuv:distribution_channel_id:%s:date:%s',$channel_id,$day); $temp_uv = Redis::scard($temp_uv_key); $data[] = [ 'day' => $day, 'key' => $channel_id, 'from_type' => 'substitutebuttonpvuv', 'pv' => $temp_pv, 'uv' => $temp_uv, 'type' => 6, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; if($i && $i%100 == 0){ DB::table('wap_visit_stats')->insert($data); $data = []; } $i++; Redis::del($temp_uv_key); Redis::hdel('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,$day); $temp = Redis::hget('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,date('Y-m-d')); if(!$temp){ Redis::del('substitutebuttonpvuv:distribution_channel_id:' . $channel_id); } } if($data){ DB::table('wap_visit_stats')->insert($data); } } Redis::del('substitutebuttonpvuv'.$day); //代充值页面 $substituteButtonUvPv = Redis::SMEMBERS('substitutepagepvuv'.$day); if($substituteButtonUvPv){ $i = 0; $data = []; foreach ($substituteButtonUvPv as $channel_id){ $temp_pv = Redis::hget('substitutepagepvuv:distribution_channel_id:' . $channel_id,$day); $temp_uv_key = sprintf('substitutepagepvuv:distribution_channel_id:%s:date:%s',$channel_id,$day); $temp_uv = Redis::scard($temp_uv_key); $data[] = [ 'day' => $day, 'key' => $channel_id, 'from_type' => 'substitutepagepvuv', 'pv' => $temp_pv, 'uv' => $temp_uv, 'type' => 7, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; if($i && $i%100 == 0){ DB::table('wap_visit_stats')->insert($data); $data = []; } $i++; Redis::del($temp_uv_key); Redis::hdel('substitutepagepvuv:distribution_channel_id:' . $channel_id,$day); $temp = Redis::hget('substitutepagepvuv:distribution_channel_id:' . $channel_id,date('Y-m-d')); if(!$temp){ Redis::del('substitutepagepvuv:distribution_channel_id:' . $channel_id); } } if($data){ DB::table('wap_visit_stats')->insert($data); } } Redis::del('substitutepagepvuv'.$day); } }