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