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

Archiver.php « PagePerformance « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b12cac165c46ef9dac4ac21a94224e960464319 (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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?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\PagePerformance;

use Piwik\Plugins\PagePerformance\Columns\TimeDomCompletion;
use Piwik\Plugins\PagePerformance\Columns\TimeDomProcessing;
use Piwik\Plugins\PagePerformance\Columns\TimeNetwork;
use Piwik\Plugins\PagePerformance\Columns\TimeServer;
use Piwik\Plugins\PagePerformance\Columns\TimeOnLoad;
use Piwik\Plugins\PagePerformance\Columns\TimeTransfer;

/**
 * Class Archiver
 */
class Archiver extends \Piwik\Plugin\Archiver
{
    const PAGEPERFORMANCE_TOTAL_NETWORK_TIME = 'PagePerformance_network_time';
    const PAGEPERFORMANCE_TOTAL_NETWORK_HITS = 'PagePerformance_network_hits';
    const PAGEPERFORMANCE_TOTAL_SERVER_TIME = 'PagePerformance_servery_time';
    const PAGEPERFORMANCE_TOTAL_SERVER_HITS = 'PagePerformance_server_hits';
    const PAGEPERFORMANCE_TOTAL_TRANSFER_TIME = 'PagePerformance_transfer_time';
    const PAGEPERFORMANCE_TOTAL_TRANSFER_HITS = 'PagePerformance_transfer_hits';
    const PAGEPERFORMANCE_TOTAL_DOMPROCESSING_TIME = 'PagePerformance_domprocessing_time';
    const PAGEPERFORMANCE_TOTAL_DOMPROCESSING_HITS = 'PagePerformance_domprocessing_hits';
    const PAGEPERFORMANCE_TOTAL_DOMCOMPLETION_TIME = 'PagePerformance_domcompletion_time';
    const PAGEPERFORMANCE_TOTAL_DOMCOMPLETION_HITS = 'PagePerformance_domcompletion_hits';
    const PAGEPERFORMANCE_TOTAL_ONLOAD_TIME = 'PagePerformance_onload_time';
    const PAGEPERFORMANCE_TOTAL_ONLOAD_HITS = 'PagePerformance_onload_hits';
    const PAGEPERFORMANCE_TOTAL_PAGE_LOAD_TIME = 'PagePerformance_pageload_time';
    const PAGEPERFORMANCE_TOTAL_PAGE_LOAD_HITS = 'PagePerformance_pageload_hits';

    public function aggregateDayReport()
    {
        $selects = $totalColumns = $hitsColumns = [];
        $table  = 'log_link_visit_action';

        $performanceDimensions = [
            new TimeNetwork(),
            new TimeServer(),
            new TimeTransfer(),
            new TimeDomProcessing(),
            new TimeDomCompletion(),
            new TimeOnLoad()
        ];

        foreach($performanceDimensions as $dimension) {
            $column = $dimension->getColumnName();
            $selects[] = "sum($table.$column) as {$column}_total";
            $selects[] = "sum(if($table.$column is null, 0, 1)) as {$column}_hits";
            $totalColumns[] = "$table.$column";
            $hitsColumns[] = "if($table.$column is null, 0, 1)";
        }

        $selects[] = sprintf('SUM(%s) as page_load_total', implode(' + ', $totalColumns));
        $selects[] = sprintf('(SUM(%s)/%s) as page_load_hits', implode(' + ', $hitsColumns), count($hitsColumns));

        $joinLogActionOnColumn = array('idaction_url');

        $query = $this->getLogAggregator()->queryActionsByDimension([], '', $selects, false, null, $joinLogActionOnColumn);

        $result = $query->fetchAll();

        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_NETWORK_TIME, 'time_network_total');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_NETWORK_HITS, 'time_network_hits');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_SERVER_TIME, 'time_server_total');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_SERVER_HITS, 'time_server_hits');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_TRANSFER_TIME, 'time_transfer_total');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_TRANSFER_HITS, 'time_transfer_hits');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_DOMPROCESSING_TIME, 'time_dom_processing_total');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_DOMPROCESSING_HITS, 'time_dom_processing_hits');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_DOMCOMPLETION_TIME, 'time_dom_completion_total');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_DOMCOMPLETION_HITS, 'time_dom_completion_hits');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_ONLOAD_TIME, 'time_on_load_total');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_ONLOAD_HITS, 'time_on_load_hits');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_PAGE_LOAD_TIME, 'page_load_total');
        $this->sumAndInsertNumericRecord($result, self::PAGEPERFORMANCE_TOTAL_PAGE_LOAD_HITS, 'page_load_hits');
    }

    /**
     * @param $result
     * @param string $metric
     * @param string $field
     */
    private function sumAndInsertNumericRecord($result, $metric, $field)
    {
        $total = 0;

        foreach ($result as $row) {
            if (!empty($row[$field])) {
                $total += (int) $row[$field];
            }
        }

        $this->getProcessor()->insertNumericRecord($metric, $total);
    }

    public function aggregateMultipleReports()
    {
        $this->getProcessor()->aggregateNumericMetrics(array(
            self::PAGEPERFORMANCE_TOTAL_NETWORK_TIME,
            self::PAGEPERFORMANCE_TOTAL_NETWORK_HITS,
            self::PAGEPERFORMANCE_TOTAL_SERVER_TIME,
            self::PAGEPERFORMANCE_TOTAL_SERVER_HITS,
            self::PAGEPERFORMANCE_TOTAL_TRANSFER_TIME,
            self::PAGEPERFORMANCE_TOTAL_TRANSFER_HITS,
            self::PAGEPERFORMANCE_TOTAL_DOMPROCESSING_TIME,
            self::PAGEPERFORMANCE_TOTAL_DOMPROCESSING_HITS,
            self::PAGEPERFORMANCE_TOTAL_DOMCOMPLETION_TIME,
            self::PAGEPERFORMANCE_TOTAL_DOMCOMPLETION_HITS,
            self::PAGEPERFORMANCE_TOTAL_ONLOAD_TIME,
            self::PAGEPERFORMANCE_TOTAL_ONLOAD_HITS,
            self::PAGEPERFORMANCE_TOTAL_PAGE_LOAD_TIME,
            self::PAGEPERFORMANCE_TOTAL_PAGE_LOAD_HITS,
        ));
    }

}