Busiest_Day_Of_Week_Module.php
5 months ago
Busiest_Time_Of_Day_Module.php
1 year ago
Line_Chart_Module.php
9 months ago
Map_Module.php
9 months ago
Module.php
3 months ago
New_Sessions_Module.php
1 year ago
Pie_Chart_Module.php
9 months ago
Quick_Stats_Module.php
9 months ago
Recent_Conversions_Module.php
5 months ago
Recent_Views_Module.php
5 months ago
Top_Ten_Module.php
9 months ago
Busiest_Time_Of_Day_Module.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Overview\Modules; |
| 4 | |
| 5 | use IAWPSCOPED\Carbon\CarbonImmutable; |
| 6 | use IAWP\Illuminate_Builder; |
| 7 | use IAWP\Tables; |
| 8 | use IAWP\Utils\Timezone; |
| 9 | use IAWPSCOPED\Illuminate\Support\Collection; |
| 10 | /** @internal */ |
| 11 | class Busiest_Time_Of_Day_Module extends \IAWP\Overview\Modules\Module |
| 12 | { |
| 13 | public function module_type() : string |
| 14 | { |
| 15 | return 'busiest-time-of-day'; |
| 16 | } |
| 17 | public function module_name() : string |
| 18 | { |
| 19 | return \__('Busiest Time of Day', 'independent-analytics'); |
| 20 | } |
| 21 | public function calculate_dataset() |
| 22 | { |
| 23 | $tables = Tables::class; |
| 24 | $offset = Timezone::site_offset(); |
| 25 | $sessions_by_hour_of_day = Illuminate_Builder::new()->selectRaw("HOUR(CONVERT_TZ(sessions.created_at, '+00:00', '{$offset}')) AS hour")->selectRaw("DAYOFYEAR(CONVERT_TZ(sessions.created_at, '+00:00', '{$offset}')) - 1 AS day")->selectRaw('COUNT(*) AS sessions')->from($tables::sessions(), 'sessions')->whereRaw("CONVERT_TZ(sessions.created_at, '+00:00', '{$offset}') > CONVERT_TZ(CURDATE(), '+00:00', '{$offset}') - INTERVAL 90 DAY")->groupBy(['day', 'hour']); |
| 26 | $query = Illuminate_Builder::new()->select(['hour'])->selectRaw('AVG(sessions) as sessions')->fromSub($sessions_by_hour_of_day, 'sessions')->groupBy('hour'); |
| 27 | return $query->get()->map(function ($row) { |
| 28 | return ['hour' => \intval($row->hour), 'sessions' => \intval($row->sessions)]; |
| 29 | })->all(); |
| 30 | } |
| 31 | public function get_labels(array $dataset) : array |
| 32 | { |
| 33 | $hours = \range(0, 23); |
| 34 | return \array_map(function ($hour) { |
| 35 | $time = CarbonImmutable::createFromTime($hour, 0, 0, Timezone::site_timezone()); |
| 36 | $format = 'g a'; |
| 37 | if (\IAWPSCOPED\iawp()->prefers_24_hour_clock()) { |
| 38 | $format = 'G'; |
| 39 | } |
| 40 | return \json_encode(['tick' => $time->format($format), 'tooltipLabel' => \__('Hour', 'independent-analytics') . ': ' . $time->format($format)]); |
| 41 | }, $hours); |
| 42 | } |
| 43 | public function get_sessions(array $dataset) : array |
| 44 | { |
| 45 | $hours = \range(0, 23); |
| 46 | $dataset_collection = Collection::make($dataset); |
| 47 | return \array_map(function ($hour) use($dataset_collection) { |
| 48 | $matching_row = $dataset_collection->first(function ($row) use($hour) { |
| 49 | $time = CarbonImmutable::createFromTime($row['hour'], 0, 0, Timezone::site_timezone())->setTimezone(Timezone::site_timezone()); |
| 50 | return $time->hour === $hour; |
| 51 | }); |
| 52 | if ($matching_row === null) { |
| 53 | return 0; |
| 54 | } |
| 55 | return $matching_row['sessions']; |
| 56 | }, $hours); |
| 57 | } |
| 58 | protected function module_fields() : array |
| 59 | { |
| 60 | return ['busiest_date_range']; |
| 61 | } |
| 62 | } |
| 63 |