ConsoleCommand
2 years ago
Dimension
3 months ago
API.php
6 months ago
AggregatedMetric.php
2 years ago
ArchivedMetric.php
1 year ago
Archiver.php
6 months ago
Categories.php
2 years ago
ComponentFactory.php
3 months ago
ComputedMetric.php
1 year ago
ConsoleCommand.php
3 months ago
Controller.php
3 months ago
ControllerAdmin.php
3 months ago
Dependency.php
1 year ago
LogTablesProvider.php
2 years ago
Manager.php
3 months ago
Menu.php
3 months ago
MetadataLoader.php
1 year ago
Metric.php
3 months ago
PluginException.php
1 year ago
ProcessedMetric.php
3 months ago
ReleaseChannels.php
3 months ago
Report.php
3 months ago
ReportsProvider.php
2 years ago
RequestProcessors.php
4 months ago
Segment.php
3 months ago
SettingsProvider.php
3 months ago
Tasks.php
3 months ago
ThemeStyles.php
6 months 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 |