| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.0.12 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2023 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Admin\Analytics\Metrics; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FireBox\Core\Admin\Analytics\Metric; |
| 20 |
|
| 21 |
class AverageTimeOpen extends Metric |
| 22 |
{ |
| 23 |
/** |
| 24 |
* The suffix that is added to the results. |
| 25 |
* s => seconds |
| 26 |
* Todo: make it translatable |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $suffix = 's'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Metric specific data |
| 34 |
*/ |
| 35 |
protected function getMetricSpecificData() |
| 36 |
{ |
| 37 |
return [ |
| 38 |
'type' => 'averagetimeopen', |
| 39 |
'color' => '#5e35b1', |
| 40 |
'hover_color' => '#7e57c2', |
| 41 |
'clean_label' => fpframework()->_('FPF_AVG_TIME_OPEN'), |
| 42 |
'label' => fpframework()->_('FPF_AVG_TIME_OPEN'), |
| 43 |
'tooltip' => firebox()->_('FB_AVG_TIME_OPEN_TOOLTIP'), |
| 44 |
'read_more_link' => FPF_SITE_URL . 'docs/firebox/analytics/the-average-time-open-metric', |
| 45 |
'decimal_points' => 0, |
| 46 |
'hide_from_results_toolbar_items' => [ |
| 47 |
'event' |
| 48 |
] |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Parses the metric total amount |
| 54 |
* |
| 55 |
* @param int $total |
| 56 |
* @param array $data |
| 57 |
* @param bool $parseMetricTotal |
| 58 |
* |
| 59 |
* @return int |
| 60 |
*/ |
| 61 |
public function parseMetricTotal($total, $data, $parseMetricTotal = false) |
| 62 |
{ |
| 63 |
$total_data = count($data); |
| 64 |
|
| 65 |
if (!$total_data) |
| 66 |
{ |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
return $total / $total_data; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Runs when we are initializing the Metric. |
| 75 |
* Useful when we want to run something related to this metric only. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function onInit() |
| 80 |
{ |
| 81 |
add_filter('firebox/analytics/metric/averagetimeopen/filter_query_data', [$this, 'filterQueryData']); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Runs prior to fetching Results Table data |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function onResults() |
| 90 |
{ |
| 91 |
add_filter('firebox/analytics/metric/averagetimeopen/filter/all/results_query_data', [$this, 'filterQueryData']); |
| 92 |
add_filter('firebox/analytics/metric/averagetimeopen/filter/all/results_last_period_query_data', [$this, 'filterQueryData']); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Ensure we only get average time open data |
| 97 |
* |
| 98 |
* @param array $payload |
| 99 |
* |
| 100 |
* @return array |
| 101 |
*/ |
| 102 |
public function filterQueryData($payload = []) |
| 103 |
{ |
| 104 |
if (!$payload || !is_array($payload)) |
| 105 |
{ |
| 106 |
return []; |
| 107 |
} |
| 108 |
|
| 109 |
$configuration = $this->getConfiguration(); |
| 110 |
|
| 111 |
$left_join_key = 'left join ' . $configuration['logs_details_table'] . ' as bld'; |
| 112 |
|
| 113 |
// if JOIN is not set, set it |
| 114 |
if (!isset($payload['join'][$left_join_key])) |
| 115 |
{ |
| 116 |
$payload['join'][$left_join_key] = ' ON bl.id = bld.log_id'; |
| 117 |
} |
| 118 |
|
| 119 |
// remove current `as total` and set it to return the average time open |
| 120 |
foreach ($payload['select'] as $key => $value) |
| 121 |
{ |
| 122 |
if (strpos(strtolower($value), 'as total') !== false) |
| 123 |
{ |
| 124 |
unset($payload['select'][$key]); |
| 125 |
$payload['select'][] = 'IFNULL(TRUNCATE(avg(TIMESTAMPDIFF(SECOND, bl.date, bld.date)), 2), 0) AS total'; |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return $payload; |
| 131 |
} |
| 132 |
} |