UserHyperLogLogJob.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * 用户日活[渠道+设备].
  4. * 场景:新用户,登录,计算
  5. */
  6. namespace App\Jobs;
  7. use Carbon\Carbon;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Queue\SerializesModels;
  13. use Illuminate\Support\Facades\Redis;
  14. class UserHyperLogLogJob implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. protected $data = null;
  18. protected $params = [];
  19. /**
  20. * UserHyperLogLogJob constructor.
  21. * @param mixed $data
  22. * @param array $params
  23. */
  24. public function __construct($data, array $params = [])
  25. {
  26. $this->data = $data;
  27. $this->params = $params;
  28. }
  29. /**
  30. * Execute the job.
  31. *
  32. * @return void
  33. */
  34. public function handle()
  35. {
  36. if (!empty($this->data) && is_array($this->data)) {
  37. if (isset($this->data[0])) {
  38. // 二维数组.
  39. foreach ($this->data as $item) {
  40. self::threadOne($item);
  41. }
  42. } else {
  43. self::threadOne($this->data);
  44. }
  45. }
  46. }
  47. /**
  48. * 处理单个.
  49. * @param array $item
  50. */
  51. protected function threadOne(array $item)
  52. {
  53. $item['device_type'] = strtoupper($item['device_type']);
  54. $keyFlag = 'day_active:';
  55. if ($item['channel_uid'] == '') {
  56. // 空字符串,代表官方.
  57. $fixChannelUid = 'office__';
  58. } else {
  59. $fixChannelUid = $item['channel_uid'];
  60. }
  61. // 时间格式Y-m-d
  62. // 日期+渠道维度.
  63. $keyPrefix = $keyFlag . 'ch:' . $fixChannelUid . ':' . Carbon::parse($item['created_at'])->toDateString();
  64. $key = $keyPrefix . ':'. $item['device_type'];
  65. // 单种设备日活
  66. Redis::pfadd($key, [$item['user_uid']]);
  67. // 所有设备日活
  68. Redis::pfadd($keyPrefix, [$item['user_uid']]);
  69. // 日期+母包应用维度
  70. $keyPrefix = $keyFlag . 'mom_app:' . $item['mom_app_uid'] . ':' . Carbon::parse($item['created_at'])->toDateString();
  71. $key = $keyPrefix . ':'. $item['device_type'];
  72. // 单种设备日活
  73. Redis::pfadd($key, [$item['user_uid']]);
  74. // 所有设备日活
  75. Redis::pfadd($keyPrefix, [$item['user_uid']]);
  76. // 仅日期维度,所有渠道.
  77. $keyPrefix = $keyFlag . Carbon::parse($item['created_at'])->toDateString();
  78. $key = $keyPrefix . ':'. $item['device_type'];
  79. // 单种设备日活
  80. Redis::pfadd($key, [$item['user_uid']]);
  81. // 所有设备日活
  82. Redis::pfadd($keyPrefix, [$item['user_uid']]);
  83. }
  84. }