PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
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 / Plugin / ComputedMetric.php
matomo / app / core / Plugin Last commit date
ConsoleCommand 1 month ago Dimension 1 month ago API.php 6 months ago AggregatedMetric.php 2 years ago ArchivedMetric.php 1 year ago Archiver.php 1 month ago Categories.php 2 years ago ComponentFactory.php 3 months ago ComputedMetric.php 1 year ago ConsoleCommand.php 1 month ago Controller.php 1 month ago ControllerAdmin.php 2 weeks ago Dependency.php 1 month ago LogTablesProvider.php 2 years ago Manager.php 1 month ago Menu.php 1 month ago MetadataLoader.php 1 month ago Metric.php 1 month ago PluginException.php 1 year ago ProcessedMetric.php 3 months ago ReleaseChannels.php 3 months ago Report.php 1 month ago ReportsProvider.php 2 years ago RequestProcessors.php 4 months ago Segment.php 3 months ago SettingsProvider.php 1 month ago Tasks.php 1 month ago ThemeStyles.php 2 weeks ago ViewDataTable.php 3 months ago Visualization.php 1 year ago WidgetsProvider.php 3 months ago
ComputedMetric.php
231 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik\Plugin;
10
11 use Piwik\Archive\DataTableFactory;
12 use Piwik\Columns\Dimension;
13 use Piwik\Columns\MetricsList;
14 use Piwik\Common;
15 use Piwik\DataTable;
16 use Piwik\DataTable\Row;
17 use Piwik\Metrics\Formatter;
18 use Piwik\Piwik;
19 class ComputedMetric extends \Piwik\Plugin\ProcessedMetric
20 {
21 public const AGGREGATION_AVG = 'avg';
22 public const AGGREGATION_RATE = 'rate';
23 /**
24 * @var string
25 */
26 private $aggregation;
27 /**
28 * @var int
29 */
30 protected $idSite;
31 private $name = '';
32 private $type = '';
33 private $translatedName = '';
34 private $documentation = '';
35 private $metric1 = '';
36 private $metric2 = '';
37 private $category = '';
38 private $metricsList;
39 public function __construct($metric1, $metric2, $aggregation = 'avg')
40 {
41 $nameShort1 = str_replace(array('nb_'), '', $metric1);
42 $nameShort2 = str_replace(array('nb_'), '', $metric2);
43 if ($aggregation === \Piwik\Plugin\ComputedMetric::AGGREGATION_AVG) {
44 $this->name = 'avg_' . $nameShort1 . '_per_' . $nameShort2;
45 } elseif ($aggregation === \Piwik\Plugin\ComputedMetric::AGGREGATION_RATE) {
46 $this->name = $nameShort1 . '_' . $nameShort2 . '_rate';
47 } else {
48 throw new \Exception('Not supported aggregation type');
49 }
50 $this->setMetric1($metric1);
51 $this->setMetric2($metric2);
52 $this->aggregation = $aggregation;
53 }
54 public function getDependentMetrics()
55 {
56 return array($this->metric1, $this->metric2);
57 }
58 public function setCategory($category)
59 {
60 $this->category = $category;
61 return $this;
62 }
63 public function getCategoryId()
64 {
65 return $this->category;
66 }
67 public function setMetric1($metric1)
68 {
69 $this->metric1 = $metric1;
70 return $this;
71 }
72 public function setMetric2($metric2)
73 {
74 $this->metric2 = $metric2;
75 return $this;
76 }
77 public function setDocumentation($documentation)
78 {
79 $this->documentation = $documentation;
80 return $this;
81 }
82 public function setTranslatedName($name)
83 {
84 $this->translatedName = $name;
85 return $this;
86 }
87 public function setType($type)
88 {
89 $this->type = $type;
90 return $this;
91 }
92 public function setName($name)
93 {
94 $this->name = $name;
95 return $this;
96 }
97 public function getName()
98 {
99 return $this->name;
100 }
101 public function compute(Row $row)
102 {
103 $metric1 = $this->getMetric($row, $this->metric1);
104 $metric2 = $this->getMetric($row, $this->metric2);
105 $precision = 2;
106 if ($this->aggregation === self::AGGREGATION_RATE) {
107 $precision = 3;
108 }
109 return Piwik::getQuotientSafe($metric1, $metric2, $precision);
110 }
111 private function getDetectedType()
112 {
113 if (!$this->type) {
114 if ($this->aggregation === self::AGGREGATION_RATE) {
115 $this->type = Dimension::TYPE_PERCENT;
116 } else {
117 $this->type = Dimension::TYPE_NUMBER;
118 // default to number
119 $metric1 = $this->getMetricsList()->getMetric($this->metric1);
120 if ($metric1) {
121 $dimension = $metric1->getDimension();
122 if ($dimension && $dimension->getType() != Dimension::TYPE_BOOL) {
123 $this->type = $dimension->getType();
124 }
125 }
126 }
127 }
128 return $this->type;
129 }
130 public function format($value, Formatter $formatter)
131 {
132 if ($this->aggregation === self::AGGREGATION_RATE) {
133 return $formatter->getPrettyPercentFromQuotient($value);
134 }
135 $type = $this->getDetectedType();
136 switch ($type) {
137 case Dimension::TYPE_MONEY:
138 return $formatter->getPrettyMoney($value, $this->idSite);
139 case Dimension::TYPE_FLOAT:
140 return $formatter->getPrettyNumber($value, $precision = 2);
141 case Dimension::TYPE_NUMBER:
142 return $formatter->getPrettyNumber($value, 1);
143 // we still need to round to have somewhat more accurate result
144 case Dimension::TYPE_DURATION_S:
145 return $formatter->getPrettyTimeFromSeconds(round($value), $displayAsSentence = \true);
146 case Dimension::TYPE_DURATION_MS:
147 $val = round($value / 1000, $value / 1000 > 60 ? 0 : 2);
148 return $formatter->getPrettyTimeFromSeconds($val, $displayAsSentence = \true);
149 case Dimension::TYPE_PERCENT:
150 return $formatter->getPrettyPercentFromQuotient($value);
151 case Dimension::TYPE_BYTE:
152 return $formatter->getPrettySizeFromBytes($value);
153 }
154 return $value;
155 }
156 public function getTranslatedName()
157 {
158 if (!$this->translatedName) {
159 $metric = $this->getMetricsList();
160 $metric1 = $metric->getMetric($this->metric1);
161 $metric2 = $metric->getMetric($this->metric2);
162 if ($this->aggregation === self::AGGREGATION_AVG) {
163 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric && $metric2 && $metric2 instanceof \Piwik\Plugin\ArchivedMetric) {
164 $metric1Name = $metric1->getDimension()->getName();
165 $metric2Name = $metric2->getDimension()->getName();
166 return Piwik::translate('General_ComputedMetricAverage', array($metric1Name, $metric2Name));
167 }
168 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric) {
169 $metric1Name = $metric1->getDimension()->getName();
170 return Piwik::translate('General_AverageX', array($metric1Name));
171 }
172 if ($metric1 && $metric2) {
173 return $metric1->getTranslatedName() . ' per ' . $metric2->getTranslatedName();
174 }
175 return $this->metric1 . ' per ' . $this->metric2;
176 } elseif ($this->aggregation === self::AGGREGATION_RATE) {
177 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric) {
178 return Piwik::translate('General_ComputedMetricRate', array($metric1->getTranslatedName()));
179 } else {
180 return Piwik::translate('General_ComputedMetricRate', array($this->metric1));
181 }
182 }
183 }
184 return $this->translatedName;
185 }
186 public function getDocumentation()
187 {
188 if (!$this->documentation) {
189 $metric = $this->getMetricsList();
190 $metric1 = $metric->getMetric($this->metric1);
191 $metric2 = $metric->getMetric($this->metric2);
192 if ($this->aggregation === self::AGGREGATION_AVG) {
193 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric && $metric2 && $metric2 instanceof \Piwik\Plugin\ArchivedMetric) {
194 return Piwik::translate('General_ComputedMetricAverageDocumentation', array($metric1->getDimension()->getName(), $metric2->getTranslatedName()));
195 }
196 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric) {
197 return Piwik::translate('General_ComputedMetricAverageShortDocumentation', array($metric1->getDimension()->getName()));
198 }
199 return Piwik::translate('General_ComputedMetricAverageDocumentation', array($this->metric1, $this->metric2));
200 } elseif ($this->aggregation === self::AGGREGATION_RATE) {
201 if ($metric1 && $metric1 instanceof \Piwik\Plugin\ArchivedMetric) {
202 return Piwik::translate('General_ComputedMetricRateDocumentation', array($metric1->getDimension()->getNamePlural(), $metric2->getDimension()->getNamePlural()));
203 } else {
204 return Piwik::translate('General_ComputedMetricRateShortDocumentation', array($this->metric1));
205 }
206 }
207 }
208 return $this->documentation;
209 }
210 private function getMetricsList()
211 {
212 if (!$this->metricsList) {
213 $this->metricsList = MetricsList::get();
214 }
215 return $this->metricsList;
216 }
217 public function beforeFormat($report, DataTable $table)
218 {
219 $this->idSite = DataTableFactory::getSiteIdFromMetadata($table);
220 if (empty($this->idSite)) {
221 $this->idSite = Common::getRequestVar('idSite', 0, 'int');
222 }
223 return !empty($this->idSite);
224 // skip formatting if there is no site to get currency info from
225 }
226 public function getSemanticType() : ?string
227 {
228 return $this->getDetectedType();
229 }
230 }
231