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

Logger.php « Performance « CronArchive « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 302968b99f77a82e6709a5795999f259fae7b5a2 (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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\CronArchive\Performance;

use Piwik\ArchiveProcessor;
use Piwik\Common;
use Piwik\Config;
use Piwik\Timer;
use Piwik\Url;
use Psr\Log\LoggerInterface;

class Logger
{
    /**
     * @var int
     */
    private $isEnabled;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var int
     */
    private $archivingRunId;

    public function __construct(Config $config, LoggerInterface $logger = null)
    {
        $this->isEnabled = $config->Debug['archiving_profile'] == 1;
        $this->logger = $logger;

        $this->archivingRunId = $this->getArchivingRunId();
        if (empty($this->archivingRunId)) {
            $this->isEnabled = false;
        }
    }

    public function logMeasurement($category, $name, ArchiveProcessor\Parameters $activeArchivingParams, Timer $timer)
    {
        if (!$this->isEnabled || !$this->logger) {
            return;
        }

        $measurement = new Measurement($category, $name, $activeArchivingParams->getSite()->getId(),
            $activeArchivingParams->getPeriod()->getRangeString(), $activeArchivingParams->getPeriod()->getLabel(),
            $activeArchivingParams->getSegment()->getString(), $timer->getTime(), $timer->getMemoryLeakValue(),
            $timer->getPeakMemoryValue());

        $params = array_merge($_GET);
        unset($params['pid']);
        unset($params['runid']);

        $this->logger->info("[runid={runid},pid={pid}] {request}: {measurement}", [
            'pid' => Common::getRequestVar('pid', false),
            'runid' => $this->getArchivingRunId(),
            'request' => Url::getQueryStringFromParameters($params),
            'measurement' => $measurement,
        ]);
    }

    public static function getMeasurementsFor($runId, $childPid)
    {
        $profilingLogFile = preg_replace('/[\'"]/', '', Config::getInstance()->Debug['archive_profiling_log']);
        if (!is_readable($profilingLogFile)) {
            return [];
        }

        $runId = self::cleanId($runId);
        $childPid = self::cleanId($childPid);

        $lineIdentifier = "[runid=$runId,pid=$childPid]";
        $lines = `grep "$childPid" "$profilingLogFile"`;
        $lines = explode("\n", $lines);
        $lines = array_map(function ($line) use ($lineIdentifier) {
            $index = strpos($line, $lineIdentifier);
            if ($index === false) {
                return null;
            }
            $line = substr($line, $index + strlen($lineIdentifier));
            return trim($line);
        }, $lines);
        $lines = array_filter($lines);
        $lines = array_map(function ($line) {
            $parts = explode(":", $line, 2);
            $parts = array_map('trim', $parts);
            return $parts;
        }, $lines);

        $data = [];
        foreach ($lines as $line) {
            if (count($line) != 2) {
                continue;
            }

            list($request, $measurement) = $line;
            $data[$request][] = $measurement;
        }
        return $data;
    }

    private function getArchivingRunId()
    {
        return Common::getRequestVar('runid', false);
    }

    private static function cleanId($id)
    {
        return preg_replace('/[^a-zA-Z0-9_-]/', '', $id);
    }
}