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
ArchivedMetric.php
191 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\Common; |
| 14 | use Piwik\DataTable; |
| 15 | use Piwik\Metrics\Formatter; |
| 16 | use Piwik\Piwik; |
| 17 | class ArchivedMetric extends \Piwik\Plugin\Metric |
| 18 | { |
| 19 | public const AGGREGATION_COUNT = 'count(%s)'; |
| 20 | public const AGGREGATION_COUNT_PREFIX = 'nb_'; |
| 21 | public const AGGREGATION_SUM = 'sum(%s)'; |
| 22 | public const AGGREGATION_SUM_PREFIX = 'sum_'; |
| 23 | public const AGGREGATION_MAX = 'max(%s)'; |
| 24 | public const AGGREGATION_MAX_PREFIX = 'max_'; |
| 25 | public const AGGREGATION_MIN = 'min(%s)'; |
| 26 | public const AGGREGATION_MIN_PREFIX = 'min_'; |
| 27 | public const AGGREGATION_UNIQUE = 'count(distinct %s)'; |
| 28 | public const AGGREGATION_UNIQUE_PREFIX = 'nb_uniq_'; |
| 29 | public const AGGREGATION_COUNT_WITH_NUMERIC_VALUE = 'sum(if(%s > 0, 1, 0))'; |
| 30 | public const AGGREGATION_COUNT_WITH_NUMERIC_VALUE_PREFIX = 'nb_with_'; |
| 31 | /** |
| 32 | * @var string |
| 33 | */ |
| 34 | private $aggregation; |
| 35 | /** |
| 36 | * @var int |
| 37 | */ |
| 38 | protected $idSite; |
| 39 | private $name = ''; |
| 40 | private $type = ''; |
| 41 | private $translatedName = ''; |
| 42 | private $documentation = ''; |
| 43 | private $dbTable = ''; |
| 44 | private $category = ''; |
| 45 | private $query = ''; |
| 46 | /** |
| 47 | * @var Dimension |
| 48 | */ |
| 49 | private $dimension; |
| 50 | public function __construct(Dimension $dimension, $aggregation = \false) |
| 51 | { |
| 52 | if (!empty($aggregation) && strpos($aggregation, '%s') === \false) { |
| 53 | throw new \Exception(sprintf('The given aggregation for %s.%s needs to include a %%s for the column name', $dimension->getDbTableName(), $dimension->getColumnName())); |
| 54 | } |
| 55 | $this->setDimension($dimension); |
| 56 | $this->setDbTable($dimension->getDbTableName()); |
| 57 | $this->aggregation = $aggregation; |
| 58 | } |
| 59 | public function getAggregation() |
| 60 | { |
| 61 | return $this->aggregation; |
| 62 | } |
| 63 | public function setDimension($dimension) |
| 64 | { |
| 65 | $this->dimension = $dimension; |
| 66 | return $this; |
| 67 | } |
| 68 | public function getDimension() |
| 69 | { |
| 70 | return $this->dimension; |
| 71 | } |
| 72 | public function setCategory($category) |
| 73 | { |
| 74 | $this->category = $category; |
| 75 | return $this; |
| 76 | } |
| 77 | public function getCategoryId() |
| 78 | { |
| 79 | return $this->category; |
| 80 | } |
| 81 | public function setDbTable($dbTable) |
| 82 | { |
| 83 | $this->dbTable = $dbTable; |
| 84 | return $this; |
| 85 | } |
| 86 | public function setDocumentation($documentation) |
| 87 | { |
| 88 | $this->documentation = $documentation; |
| 89 | return $this; |
| 90 | } |
| 91 | public function setTranslatedName($name) |
| 92 | { |
| 93 | $this->translatedName = $name; |
| 94 | return $this; |
| 95 | } |
| 96 | public function setType($type) |
| 97 | { |
| 98 | $this->type = $type; |
| 99 | return $this; |
| 100 | } |
| 101 | public function setName($name) |
| 102 | { |
| 103 | $this->name = $name; |
| 104 | return $this; |
| 105 | } |
| 106 | public function setQuery($query) |
| 107 | { |
| 108 | $this->query = $query; |
| 109 | return $this; |
| 110 | } |
| 111 | public function getName() |
| 112 | { |
| 113 | return $this->name; |
| 114 | } |
| 115 | public function format($value, Formatter $formatter) |
| 116 | { |
| 117 | switch ($this->type) { |
| 118 | case Dimension::TYPE_BOOL: |
| 119 | return $formatter->getPrettyNumber($value); |
| 120 | case Dimension::TYPE_ENUM: |
| 121 | return $formatter->getPrettyNumber($value); |
| 122 | case Dimension::TYPE_MONEY: |
| 123 | return $formatter->getPrettyMoney($value, $this->idSite); |
| 124 | case Dimension::TYPE_FLOAT: |
| 125 | return $formatter->getPrettyNumber((float) $value, $precision = 2); |
| 126 | case Dimension::TYPE_NUMBER: |
| 127 | return $formatter->getPrettyNumber($value); |
| 128 | case Dimension::TYPE_DURATION_S: |
| 129 | return $formatter->getPrettyTimeFromSeconds($value, $displayAsSentence = \true); |
| 130 | case Dimension::TYPE_DURATION_MS: |
| 131 | $val = round($value / 1000, $value / 1000 > 60 ? 0 : 2); |
| 132 | return $formatter->getPrettyTimeFromSeconds($val, $displayAsSentence = \true); |
| 133 | case Dimension::TYPE_PERCENT: |
| 134 | return $formatter->getPrettyPercentFromQuotient($value); |
| 135 | case Dimension::TYPE_BYTE: |
| 136 | return $formatter->getPrettySizeFromBytes($value); |
| 137 | } |
| 138 | return $value; |
| 139 | } |
| 140 | public function getType() |
| 141 | { |
| 142 | return $this->type; |
| 143 | } |
| 144 | public function getTranslatedName() |
| 145 | { |
| 146 | if (!empty($this->translatedName)) { |
| 147 | return Piwik::translate($this->translatedName); |
| 148 | } |
| 149 | return $this->translatedName; |
| 150 | } |
| 151 | public function getDependentMetrics() |
| 152 | { |
| 153 | return array($this->getName()); |
| 154 | } |
| 155 | public function getDocumentation() |
| 156 | { |
| 157 | return $this->documentation; |
| 158 | } |
| 159 | public function getDbTableName() |
| 160 | { |
| 161 | return $this->dbTable; |
| 162 | } |
| 163 | public function getQuery() |
| 164 | { |
| 165 | if ($this->query) { |
| 166 | return $this->query; |
| 167 | } |
| 168 | $column = $this->dbTable . '.' . $this->dimension->getColumnName(); |
| 169 | if ($this->dimension->getSqlSegment()) { |
| 170 | $column = str_replace($this->dimension->getDbTableName(), $this->dbTable, $this->dimension->getSqlSegment()); |
| 171 | } |
| 172 | if (!empty($this->aggregation)) { |
| 173 | return sprintf($this->aggregation, $column); |
| 174 | } |
| 175 | return $column; |
| 176 | } |
| 177 | public function beforeFormat($report, DataTable $table) |
| 178 | { |
| 179 | $this->idSite = DataTableFactory::getSiteIdFromMetadata($table); |
| 180 | if (empty($this->idSite)) { |
| 181 | $this->idSite = Common::getRequestVar('idSite', 0, 'int'); |
| 182 | } |
| 183 | return !empty($this->idSite); |
| 184 | // skip formatting if there is no site to get currency info from |
| 185 | } |
| 186 | public function getSemanticType() : ?string |
| 187 | { |
| 188 | return $this->type; |
| 189 | } |
| 190 | } |
| 191 |