PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
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 6 years ago Measurement.php 6 years ago
Logger.php
120 lines
1 <?php
2 /**
3 * Piwik - 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\Option;
15 use Piwik\Timer;
16 use Piwik\Url;
17 use Psr\Log\LoggerInterface;
18
19 class Logger
20 {
21 /**
22 * @var int
23 */
24 private $isEnabled;
25
26 /**
27 * @var LoggerInterface
28 */
29 private $logger;
30
31 /**
32 * @var int
33 */
34 private $archivingRunId;
35
36 public function __construct(Config $config, LoggerInterface $logger = null)
37 {
38 $this->isEnabled = $config->Debug['archiving_profile'] == 1;
39 $this->logger = $logger;
40
41 $this->archivingRunId = $this->getArchivingRunId();
42 if (empty($this->archivingRunId)) {
43 $this->isEnabled = false;
44 }
45 }
46
47 public function logMeasurement($category, $name, ArchiveProcessor\Parameters $activeArchivingParams, Timer $timer)
48 {
49 if (!$this->isEnabled || !$this->logger) {
50 return;
51 }
52
53 $measurement = new Measurement($category, $name, $activeArchivingParams->getSite()->getId(),
54 $activeArchivingParams->getPeriod()->getRangeString(), $activeArchivingParams->getPeriod()->getLabel(),
55 $activeArchivingParams->getSegment()->getString(), $timer->getTime(), $timer->getMemoryLeakValue(),
56 $timer->getPeakMemoryValue());
57
58 $params = array_merge($_GET);
59 unset($params['pid']);
60 unset($params['runid']);
61
62 $this->logger->info("[runid={runid},pid={pid}] {request}: {measurement}", [
63 'pid' => Common::getRequestVar('pid', false),
64 'runid' => $this->getArchivingRunId(),
65 'request' => Url::getQueryStringFromParameters($params),
66 'measurement' => $measurement,
67 ]);
68 }
69
70 public static function getMeasurementsFor($runId, $childPid)
71 {
72 $profilingLogFile = preg_replace('/[\'"]/', '', Config::getInstance()->Debug['archive_profiling_log']);
73 if (!is_readable($profilingLogFile)) {
74 return [];
75 }
76
77 $runId = self::cleanId($runId);
78 $childPid = self::cleanId($childPid);
79
80 $lineIdentifier = "[runid=$runId,pid=$childPid]";
81 $lines = `grep "$childPid" "$profilingLogFile"`;
82 $lines = explode("\n", $lines);
83 $lines = array_map(function ($line) use ($lineIdentifier) {
84 $index = strpos($line, $lineIdentifier);
85 if ($index === false) {
86 return null;
87 }
88 $line = substr($line, $index + strlen($lineIdentifier));
89 return trim($line);
90 }, $lines);
91 $lines = array_filter($lines);
92 $lines = array_map(function ($line) {
93 $parts = explode(":", $line, 2);
94 $parts = array_map('trim', $parts);
95 return $parts;
96 }, $lines);
97
98 $data = [];
99 foreach ($lines as $line) {
100 if (count($line) != 2) {
101 continue;
102 }
103
104 list($request, $measurement) = $line;
105 $data[$request][] = $measurement;
106 }
107 return $data;
108 }
109
110 private function getArchivingRunId()
111 {
112 return Common::getRequestVar('runid', false);
113 }
114
115 private static function cleanId($id)
116 {
117 return preg_replace('/[^a-zA-Z0-9_-]/', '', $id);
118 }
119 }
120