1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\PagePerformance\Columns\Metrics;
use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\Metrics\Formatter;
use Piwik\Piwik;
use Piwik\Plugin\ProcessedMetric;
use Piwik\Columns\Dimension;
/**
* The average amount for a certain performance metric. Calculated as
*
* sum_time / nb_hits_with_time
*
* The above metrics are calculated during archiving. This metric is calculated before
* serving a report.
*/
abstract class AveragePerformanceMetric extends ProcessedMetric
{
public const ID = '';
public function getName()
{
return 'avg_' . static::ID;
}
public function getDependentMetrics()
{
return array('sum_' . static::ID, 'nb_hits_with_' . static::ID);
}
public function getTemporaryMetrics()
{
return array('sum_' . static::ID);
}
public function compute(Row $row)
{
$sumGenerationTime = $this->getMetric($row, 'sum_' . static::ID);
$hitsWithTimeGeneration = $this->getMetric($row, 'nb_hits_with_' . static::ID);
return Piwik::getQuotientSafe($sumGenerationTime, $hitsWithTimeGeneration, $precision = 3);
}
public function format($value, Formatter $formatter)
{
if (
$formatter instanceof Formatter\Html
&& !$value
) {
return '-';
} else {
return $formatter->getPrettyTimeFromSeconds($value, $displayAsSentence = true);
}
}
public function beforeCompute($report, DataTable $table)
{
$hasTimeGeneration = array_sum($this->getMetricValues($table, 'sum_' . static::ID)) > 0;
if (
!$hasTimeGeneration
&& $table->getRowsCount() != 0
&& !$this->hasAverageMetric($table)
) {
// No generation time: remove it from the API output and add it to empty_columns metadata, so that
// the columns can also be removed from the view
$table->filter('ColumnDelete', array(array(
'sum_' . static::ID,
'nb_hits_with_' . static::ID,
'min_' . static::ID,
'max_' . static::ID,
)));
if ($table instanceof DataTable) {
$emptyColumns = $table->getMetadata(DataTable::EMPTY_COLUMNS_METADATA_NAME);
if (!is_array($emptyColumns)) {
$emptyColumns = array();
}
$emptyColumns[] = 'sum_' . static::ID;
$emptyColumns[] = 'nb_hits_with_' . static::ID;
$emptyColumns[] = 'min_' . static::ID;
$emptyColumns[] = 'max_' . static::ID;
$table->setMetadata(DataTable::EMPTY_COLUMNS_METADATA_NAME, $emptyColumns);
}
}
return $hasTimeGeneration;
}
private function hasAverageMetric(DataTable $table)
{
return $table->getFirstRow()->getColumn($this->getName()) !== false;
}
public function getSemanticType(): ?string
{
return Dimension::TYPE_DURATION_S;
}
}
|