StatisticsActive.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * 统计日活用户相关.
  4. */
  5. namespace App\Console\Commands\Statistics;
  6. use App\Jobs\Statistics\ActiveChannelJob;
  7. use App\Repositories\Statistics\StatisticsActiveRepository;
  8. use Carbon\Carbon;
  9. use Illuminate\Console\Command;
  10. use App\Models\Mysql\ChannelModel;
  11. class StatisticsActive extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'statistics:active {--date=}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = '统计日活用户相关, 可选参数--date=2021-03-04.';
  25. /**
  26. * 定义仓库.
  27. * @var null|StatisticsActiveRepository
  28. */
  29. protected $repo = null;
  30. /**
  31. * Create a new command instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct()
  36. {
  37. parent::__construct();
  38. }
  39. /**
  40. * Execute the console command.
  41. *
  42. * @return mixed
  43. */
  44. public function handle()
  45. {
  46. $date = $this->getArgsDate();
  47. // 指派调度统计某1天的所有日活.
  48. dispatch(new ActiveChannelJob($date));
  49. $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:active]['. $date .']所有每日日活统计,丢入队列');
  50. // 指派调度统计某1天的官方日活.
  51. dispatch(new ActiveChannelJob($date, ''));
  52. $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:active]['. $date .']官方每日日活统计,丢入队列');
  53. // 分片处理.
  54. $chunkPer = 1000;
  55. ChannelModel::orderBy('id', 'ASC')->chunk($chunkPer, function ($res) use ($date) {
  56. foreach ($res as $item) {
  57. if (!empty($item->uid)) {
  58. dispatch(new ActiveChannelJob($date, $item->uid));
  59. }
  60. }
  61. });
  62. $this->comment('['. Carbon::now()->toDateTimeString() .'][statistics:active]['. $date .']单独渠道代理每日日活统计,全部丢入队列');
  63. }
  64. /**
  65. * 获取日期参数.
  66. * @return string
  67. */
  68. private function getArgsDate()
  69. {
  70. $date = $this->option('date');
  71. if (empty($date)) {
  72. // 没有传参数,则默认使用执行脚本服务器环境昨天日期为准.
  73. $date = Carbon::now()->subDays(1)->toDateString();
  74. }
  75. return $date;
  76. }
  77. /**
  78. * 获取所有渠道.
  79. * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|ChannelModel[]
  80. */
  81. private function getAllChannel()
  82. {
  83. $columns = ['id', 'uid'];
  84. $items = ChannelModel::orderBy('id', 'DESC')->all($columns);
  85. return $items;
  86. }
  87. /**
  88. * 获取仓库.
  89. * @return StatisticsActiveRepository
  90. */
  91. private function getRepo()
  92. {
  93. if (!$this->repo instanceof StatisticsActiveRepository) {
  94. $this->repo = new StatisticsActiveRepository();
  95. }
  96. return $this->repo;
  97. }
  98. }