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