RecentTraffic.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\RealTime; |
| 4 | |
| 5 | use IAWP\Date_Range\Exact_Date_Range; |
| 6 | use IAWP\Examiner_Config; |
| 7 | use IAWP\Illuminate_Builder; |
| 8 | use IAWP\Query_Taps; |
| 9 | use IAWP\Tables; |
| 10 | /** @internal */ |
| 11 | class RecentTraffic |
| 12 | { |
| 13 | private array $short_labels; |
| 14 | private array $long_labels; |
| 15 | private array $timestamps; |
| 16 | private int $visitors; |
| 17 | private array $views; |
| 18 | private array $orders; |
| 19 | private array $clicks; |
| 20 | private array $form_submissions; |
| 21 | public function __construct(Exact_Date_Range $range, ?Examiner_Config $examiner_config) |
| 22 | { |
| 23 | $this->populate_data($range, $examiner_config); |
| 24 | } |
| 25 | public function short_labels() : array |
| 26 | { |
| 27 | return $this->short_labels; |
| 28 | } |
| 29 | public function long_labels() : array |
| 30 | { |
| 31 | return $this->long_labels; |
| 32 | } |
| 33 | public function timestamps() : array |
| 34 | { |
| 35 | return $this->timestamps; |
| 36 | } |
| 37 | public function visitor_count() : int |
| 38 | { |
| 39 | return $this->visitors; |
| 40 | } |
| 41 | public function views() : array |
| 42 | { |
| 43 | return $this->views; |
| 44 | } |
| 45 | public function view_count() : int |
| 46 | { |
| 47 | return \array_sum($this->views); |
| 48 | } |
| 49 | public function orders() : array |
| 50 | { |
| 51 | return $this->orders; |
| 52 | } |
| 53 | public function order_count() : int |
| 54 | { |
| 55 | return \array_sum($this->orders); |
| 56 | } |
| 57 | public function clicks() : array |
| 58 | { |
| 59 | return $this->clicks; |
| 60 | } |
| 61 | public function click_count() : int |
| 62 | { |
| 63 | return \array_sum($this->clicks); |
| 64 | } |
| 65 | public function form_submissions() : array |
| 66 | { |
| 67 | return $this->form_submissions; |
| 68 | } |
| 69 | public function form_submission_count() : int |
| 70 | { |
| 71 | return \array_sum($this->form_submissions); |
| 72 | } |
| 73 | private function populate_data(Exact_Date_Range $range, ?Examiner_Config $examiner_config) : void |
| 74 | { |
| 75 | $visitors_query = Illuminate_Builder::new()->selectRaw('COUNT(DISTINCT sessions.visitor_id) AS visitors')->from(Tables::views(), 'views')->leftJoin(Tables::sessions() . ' AS sessions', 'sessions.session_id', '=', 'views.session_id')->tap(Query_Taps::tap_related_to_examined_record($examiner_config))->whereBetween('viewed_at', [$range->iso_start(), $range->iso_end()]); |
| 76 | $visitors = $visitors_query->value('visitors'); |
| 77 | $clicks_query = Illuminate_Builder::new()->selectRaw('0 AS views')->selectRaw('COUNT(*) AS clicks')->selectRaw('0 AS form_submissions')->selectRaw('0 AS orders')->selectRaw("ABS(CEILING(TIMESTAMPDIFF(SECOND, '{$range->iso_end()}', clicks.created_at) / 10)) AS interval_ago")->from(Tables::clicks(), 'clicks')->leftJoin(Tables::views() . ' AS views', 'views.id', '=', 'clicks.view_id')->leftJoin(Tables::sessions() . ' AS sessions', 'sessions.session_id', '=', 'views.session_id')->tap(Query_Taps::tap_related_to_examined_record($examiner_config))->whereBetween('clicks.created_at', [$range->iso_start(), $range->iso_end()])->groupBy('interval_ago'); |
| 78 | $orders_query = Illuminate_Builder::new()->selectRaw('0 AS views')->selectRaw('0 AS clicks')->selectRaw('0 AS form_submissions')->selectRaw('COUNT(*) AS orders')->selectRaw("ABS(CEILING(TIMESTAMPDIFF(SECOND, '{$range->iso_end()}', orders.created_at) / 10)) AS interval_ago")->from(Tables::orders(), 'orders')->leftJoin(Tables::views() . ' AS views', 'views.id', '=', 'orders.view_id')->leftJoin(Tables::sessions() . ' AS sessions', 'sessions.session_id', '=', 'views.session_id')->tap(Query_Taps::tap_related_to_examined_record($examiner_config))->whereBetween('orders.created_at', [$range->iso_start(), $range->iso_end()])->groupBy('interval_ago'); |
| 79 | $form_submissions_query = Illuminate_Builder::new()->selectRaw('0 AS views')->selectRaw('0 AS clicks')->selectRaw('COUNT(*) AS form_submissions')->selectRaw('0 AS orders')->selectRaw("ABS(CEILING(TIMESTAMPDIFF(SECOND, '{$range->iso_end()}', form_submissions.created_at) / 10)) AS interval_ago")->from(Tables::form_submissions(), 'form_submissions')->leftJoin(Tables::views() . ' AS views', 'views.id', '=', 'form_submissions.view_id')->leftJoin(Tables::sessions() . ' AS sessions', 'sessions.session_id', '=', 'views.session_id')->tap(Query_Taps::tap_related_to_examined_record($examiner_config))->whereBetween('form_submissions.created_at', [$range->iso_start(), $range->iso_end()])->groupBy('interval_ago'); |
| 80 | $views_query = Illuminate_Builder::new()->selectRaw('COUNT(*) AS views')->selectRaw('0 AS clicks')->selectRaw('0 AS form_submissions')->selectRaw('0 AS orders')->selectRaw("ABS(CEILING(TIMESTAMPDIFF(SECOND, '{$range->iso_end()}', views.viewed_at) / 10)) AS interval_ago")->from(Tables::views(), 'views')->leftJoin(Tables::sessions() . ' AS sessions', 'sessions.session_id', '=', 'views.session_id')->tap(Query_Taps::tap_related_to_examined_record($examiner_config))->whereBetween('viewed_at', [$range->iso_start(), $range->iso_end()])->unionAll($clicks_query)->unionAll($form_submissions_query)->unionAll($orders_query)->groupBy('interval_ago'); |
| 81 | $union_query = Illuminate_Builder::new()->select(['interval_ago'])->selectRaw('SUM(views) AS views')->selectRaw('SUM(clicks) AS clicks')->selectRaw('SUM(form_submissions) AS form_submissions')->selectRaw('SUM(orders) AS orders')->from($views_query)->groupBy('interval_ago')->orderBy('interval_ago'); |
| 82 | // Illuminate_Builder::ray($union_query); |
| 83 | $rows = $union_query->get()->all(); |
| 84 | $interval = new \DateInterval('PT10S'); |
| 85 | $period = new \DatePeriod($range->start(), $interval, $range->end()); |
| 86 | foreach ($period as $interval => $date) { |
| 87 | $views = 0; |
| 88 | $orders = 0; |
| 89 | $clicks = 0; |
| 90 | $form_submissions = 0; |
| 91 | // Search the rows for a matching interval to pull data from |
| 92 | foreach ($rows as $row) { |
| 93 | if (\intval($row->interval_ago) !== $interval) { |
| 94 | continue; |
| 95 | } |
| 96 | $views = \intval($row->views); |
| 97 | $orders = \intval($row->orders); |
| 98 | $clicks = \intval($row->clicks); |
| 99 | $form_submissions = \intval($row->form_submissions); |
| 100 | break; |
| 101 | } |
| 102 | $this->short_labels[] = $this->get_short_label($interval); |
| 103 | $this->long_labels[] = $this->get_long_label($interval); |
| 104 | $this->timestamps[] = $date->getTimestamp(); |
| 105 | $this->views[] = $views; |
| 106 | $this->orders[] = $orders; |
| 107 | $this->clicks[] = $clicks; |
| 108 | $this->form_submissions[] = $form_submissions; |
| 109 | } |
| 110 | $this->short_labels = \array_reverse($this->short_labels); |
| 111 | $this->long_labels = \array_reverse($this->long_labels); |
| 112 | $this->timestamps = \array_reverse($this->timestamps); |
| 113 | $this->visitors = $visitors; |
| 114 | $this->views = \array_reverse($this->views); |
| 115 | $this->orders = \array_reverse($this->orders); |
| 116 | $this->clicks = \array_reverse($this->clicks); |
| 117 | $this->form_submissions = \array_reverse($this->form_submissions); |
| 118 | } |
| 119 | private function get_short_label(int $interval) : string |
| 120 | { |
| 121 | if ($interval === 0) { |
| 122 | return \__('Now', 'independent-analytics'); |
| 123 | } |
| 124 | $seconds_passed = $interval * 10; |
| 125 | $is_whole_minute = $seconds_passed % 60 === 0; |
| 126 | if (!$is_whole_minute) { |
| 127 | // Don't show short labels for partial minutes |
| 128 | return ''; |
| 129 | } |
| 130 | $minutes_passed = $seconds_passed / 60; |
| 131 | if ($minutes_passed === 1) { |
| 132 | return '-' . $minutes_passed . ' ' . \__('min', 'independent-analytics'); |
| 133 | } |
| 134 | return '-' . $minutes_passed . ' ' . \__('mins', 'independent-analytics'); |
| 135 | } |
| 136 | private function get_long_label(int $interval) : string |
| 137 | { |
| 138 | if ($interval === 0) { |
| 139 | return \__('Now', 'independent-analytics'); |
| 140 | } |
| 141 | $seconds_passed = $interval * 10; |
| 142 | return $seconds_passed . ' ' . \__('seconds ago', 'independent-analytics'); |
| 143 | } |
| 144 | } |
| 145 |