Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ExcludeLowPopulation.php « Filter « DataTable « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9f4d5c723cf07cf2f7968382c46c3157bf4a22c1 (plain)
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
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */

/**
 * Delete all rows that have a $columnToFilter value less than the $minimumValue
 *
 * For example we delete from the countries report table all countries that have less than 3 visits.
 * It is very useful to exclude noise from the reports.
 * You can obviously apply this filter on a percentaged column, eg. remove all countries with the column 'percent_visits' less than 0.05
 *
 * @package Piwik
 * @subpackage Piwik_DataTable
 */
class Piwik_DataTable_Filter_ExcludeLowPopulation extends Piwik_DataTable_Filter
{
    const MINIMUM_SIGNIFICANT_PERCENTAGE_THRESHOLD = 0.02;
    
    /**
     * The minimum value to enforce in a datatable for a specified column. Rows found with
     * a value less than this are removed.
     * 
     * @var number
     */
    private $minimumValue;

    /**
     * Constructor
     *
     * @param Piwik_DataTable $table
     * @param string $columnToFilter              column to filter
     * @param number|Closure $minimumValue                minimum value
     * @param bool $minimumPercentageThreshold
     */
    public function __construct($table, $columnToFilter, $minimumValue, $minimumPercentageThreshold = false)
    {
        parent::__construct($table);
        $this->columnToFilter = $columnToFilter;
        
        if ($minimumValue == 0) {
            if ($minimumPercentageThreshold === false) {
                $minimumPercentageThreshold = self::MINIMUM_SIGNIFICANT_PERCENTAGE_THRESHOLD;
            }
            $allValues = $table->getColumn($this->columnToFilter);
            $sumValues = array_sum($allValues);
            $minimumValue = $sumValues * $minimumPercentageThreshold;
        }
        
        $this->minimumValue = $minimumValue;
    }

    /**
     * Executes filter and removes all rows below the defined minimum
     *
     * @param Piwik_DataTable $table
     */
    public function filter($table)
    {
        $minimumValue = $this->minimumValue;
        $isValueHighPopulation = function ($value) use ($minimumValue) {
            return $value >= $minimumValue;
        };
        
        $table->filter('ColumnCallbackDeleteRow', array($this->columnToFilter, $isValueHighPopulation));
    }
}