| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- /**
- * 统计日活用户相关.
- */
- namespace App\Console\Commands\Statistics;
- use App\Jobs\Statistics\ActiveChannelJob;
- use App\Repositories\Statistics\StatisticsActiveRepository;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use App\Models\Mysql\ChannelModel;
- class StatisticsActive extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'statistics:active {--date=}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '统计日活用户相关, 可选参数--date=2021-03-04.';
- /**
- * 定义仓库.
- * @var null|StatisticsActiveRepository
- */
- protected $repo = null;
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $date = $this->getArgsDate();
- // 指派调度统计某1天的所有日活.
- dispatch(new ActiveChannelJob($date));
- $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:active]['. $date .']所有每日日活统计,丢入队列');
- // 指派调度统计某1天的官方日活.
- dispatch(new ActiveChannelJob($date, ''));
- $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:active]['. $date .']官方每日日活统计,丢入队列');
- // 分片处理.
- $chunkPer = 1000;
- ChannelModel::orderBy('id', 'ASC')->chunk($chunkPer, function ($res) use ($date) {
- foreach ($res as $item) {
- if (!empty($item->uid)) {
- dispatch(new ActiveChannelJob($date, $item->uid));
- }
- }
- });
- $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:active]['. $date .']单独渠道代理每日日活统计,全部丢入队列');
- }
- /**
- * 获取日期参数.
- * @return string
- */
- private function getArgsDate()
- {
- $date = $this->option('date');
- if (empty($date)) {
- // 没有传参数,则默认使用执行脚本服务器环境昨天日期为准.
- $date = Carbon::now()->subDays(1)->toDateString();
- }
- return $date;
- }
- /**
- * 获取所有渠道.
- * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|ChannelModel[]
- */
- private function getAllChannel()
- {
- $columns = ['id', 'uid'];
- $items = ChannelModel::orderBy('id', 'DESC')->all($columns);
- return $items;
- }
- /**
- * 获取仓库.
- * @return StatisticsActiveRepository
- */
- private function getRepo()
- {
- if (!$this->repo instanceof StatisticsActiveRepository) {
- $this->repo = new StatisticsActiveRepository();
- }
- return $this->repo;
- }
- }
|