File size: 1,720 Bytes
9304daa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\Bandwidth\Columns\Metrics;

use Piwik\DataTable\Row;
use Piwik\Piwik;
use Piwik\Columns\Dimension;
use Piwik\Plugins\Bandwidth\Metrics;

/**
 * The average amount bandwidth per page.
 */
class AvgBandwidth extends Base
{
    public function getName()
    {
        return 'avg_bandwidth';
    }

    public function getTranslatedName()
    {
        return Piwik::translate('Bandwidth_ColumnAvgBandwidth');
    }

    public function compute(Row $row)
    {
        // NOTE: Fetching columns by name or id is required in some cases (like flattened reports)
        //       This should be changed in the future: see https://github.com/piwik/piwik/issues/10916
        if ($row->hasColumn('nb_hits_with_bandwidth')) {
            $hits = $this->getMetricAsIntSafe($row, 'nb_hits_with_bandwidth');
        } else {
            $hits = $this->getMetricAsIntSafe($row, Metrics::METRICS_NB_HITS_WITH_BANDWIDTH);
        }

        if ($row->hasColumn('sum_bandwidth')) {
            $sum = $this->getMetricAsIntSafe($row, 'sum_bandwidth');
        } else {
            $sum = $this->getMetricAsIntSafe($row, Metrics::METRICS_PAGE_SUM_BANDWIDTH);
        }

        if (!empty($hits) && !empty($sum)) {
            $avg = floor($sum / $hits);
        } else {
            $avg = 0;
        }

        return (int)$avg;
    }

    public function getDependentMetrics()
    {
        return ['nb_hits_with_bandwidth', 'sum_bandwidth'];
    }

    public function getSemanticType(): ?string
    {
        return Dimension::TYPE_NUMBER;
    }
}