ChannelReaderVisitStats.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Modules\Statistic\Services\WapVisitStatService;
  4. use DB;
  5. use Illuminate\Console\Command;
  6. use Redis;
  7. class ChannelReaderVisitStats extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'channel_reader_visit_stats';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command description';
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. }
  25. public function handle()
  26. {
  27. /*$start_date = '2016-08-29';
  28. $end_date = date("Y-m-d", strtotime("-1 day"));
  29. $end_timestamp = strtotime($end_date);
  30. $start_timestamp = strtotime($start_date);
  31. // 计算日期段内有多少天
  32. $days = ($end_timestamp - $start_timestamp) / 86400 + 1;
  33. for ($i = 0; $i < $days; $i++) {
  34. $date = date('Y-m-d', $start_timestamp + (86400 * $i));
  35. $this->getReadVisitUvInfo($date);
  36. }
  37. /*$yesterday = date("Y-m-d", strtotime("-1 day"));
  38. $this->getReadVisitUvInfo($yesterday);*/
  39. $yesterday = date("Y-m-d", strtotime("-1 day"));
  40. $this->getReadVisitUvInfo($yesterday);
  41. }
  42. function getReadVisitUvInfo($date)
  43. {
  44. $channels = Redis::smembers(sprintf('recordReaderUvAndPv:date:%s', $date));
  45. if ($channels) {
  46. foreach ($channels as $channel) {
  47. $data = WapVisitStatService::getReaderUvAndPv($channel, $date);
  48. if ($data) {
  49. $uv = $data['uv'];
  50. $pv = $data['pv'];
  51. $distribution_channel_id = $channel;
  52. $created_at = date("Y-m-d H:i:s");
  53. $updated_at = date("Y-m-d H:i:s");
  54. $data = compact('date', 'uv', 'pv', 'created_at', 'updated_at', 'distribution_channel_id');
  55. DB::table('channel_reader_visit_stats')->insert($data);
  56. }
  57. }
  58. }
  59. WapVisitStatService::deleteReaderUvAndPvRedisKey($date);
  60. }
  61. }