PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / CronArchive / Performance / Logger.php
matomo / app / core / CronArchive / Performance Last commit date
Logger.php 5 years ago Measurement.php 5 years ago
Logger.php
119 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 */
8
9 namespace Piwik\CronArchive\Performance;
10
11 use Piwik\ArchiveProcessor;
12 use Piwik\Common;
13 use Piwik\Config;
14 use Piwik\Timer;
15 use Piwik\Url;
16 use Psr\Log\LoggerInterface;
17
18 class Logger
19 {
20 /**
21 * @var int
22 */
23 private $isEnabled;
24
25 /**
26 * @var LoggerInterface
27 */
28 private $logger;
29
30 /**
31 * @var int
32 */
33 private $archivingRunId;
34
35 public function __construct(Config $config, LoggerInterface $logger = null)
36 {
37 $this->isEnabled = $config->Debug['archiving_profile'] == 1;
38 $this->logger = $logger;
39
40 $this->archivingRunId = $this->getArchivingRunId();
41 if (empty($this->archivingRunId)) {
42 $this->isEnabled = false;
43 }
44 }
45
46 public function logMeasurement($category, $name, ArchiveProcessor\Parameters $activeArchivingParams, Timer $timer)
47 {
48 if (!$this->isEnabled || !$this->logger) {
49 return;
50 }
51
52 $measurement = new Measurement($category, $name, $activeArchivingParams->getSite()->getId(),
53 $activeArchivingParams->getPeriod()->getRangeString(), $activeArchivingParams->getPeriod()->getLabel(),
54 $activeArchivingParams->getSegment()->getString(), $timer->getTime(), $timer->getMemoryLeakValue(),
55 $timer->getPeakMemoryValue());
56
57 $params = array_merge($_GET);
58 unset($params['pid']);
59 unset($params['runid']);
60
61 $this->logger->info("[runid={runid},pid={pid}] {request}: {measurement}", [
62 'pid' => Common::getRequestVar('pid', false),
63 'runid' => $this->getArchivingRunId(),
64 'request' => Url::getQueryStringFromParameters($params),
65 'measurement' => $measurement,
66 ]);
67 }
68
69 public static function getMeasurementsFor($runId, $childPid)
70 {
71 $profilingLogFile = preg_replace('/[\'"]/', '', Config::getInstance()->Debug['archive_profiling_log']);
72 if (!is_readable($profilingLogFile)) {
73 return [];
74 }
75
76 $runId = self::cleanId($runId);
77 $childPid = self::cleanId($childPid);
78
79 $lineIdentifier = "[runid=$runId,pid=$childPid]";
80 $lines = `grep "$childPid" "$profilingLogFile"`;
81 $lines = explode("\n", $lines);
82 $lines = array_map(function ($line) use ($lineIdentifier) {
83 $index = strpos($line, $lineIdentifier);
84 if ($index === false) {
85 return null;
86 }
87 $line = substr($line, $index + strlen($lineIdentifier));
88 return trim($line);
89 }, $lines);
90 $lines = array_filter($lines);
91 $lines = array_map(function ($line) {
92 $parts = explode(":", $line, 2);
93 $parts = array_map('trim', $parts);
94 return $parts;
95 }, $lines);
96
97 $data = [];
98 foreach ($lines as $line) {
99 if (count($line) != 2) {
100 continue;
101 }
102
103 list($request, $measurement) = $line;
104 $data[$request][] = $measurement;
105 }
106 return $data;
107 }
108
109 private function getArchivingRunId()
110 {
111 return Common::getRequestVar('runid', false);
112 }
113
114 private static function cleanId($id)
115 {
116 return preg_replace('/[^a-zA-Z0-9_-]/', '', $id);
117 }
118 }
119