| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Metrics; |
|
|
| use Piwik\Archive\DataTableFactory; |
| use Piwik\Common; |
| use Piwik\DataTable; |
| use Piwik\NumberFormatter; |
| use Piwik\Piwik; |
| use Piwik\Plugin\ArchivedMetric; |
| use Piwik\Plugin\Metric; |
| use Piwik\Plugin\ProcessedMetric; |
| use Piwik\Plugin\Report; |
| use Piwik\Site; |
| use Piwik\Tracker\GoalManager; |
|
|
| |
| |
| |
| |
| class Formatter |
| { |
| public const PROCESSED_METRICS_FORMATTED_FLAG = 'processed_metrics_formatted'; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getPrettyNumber($value, $precision = 0) |
| { |
| return NumberFormatter::getInstance()->formatNumber($value, $precision); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getPrettyTimeFromSeconds($numberOfSeconds, $displayTimeAsSentence = false, $round = false) |
| { |
| $numberOfSeconds = $round ? (int)$numberOfSeconds : (float)$numberOfSeconds; |
|
|
| $isNegative = false; |
| if ($numberOfSeconds < 0) { |
| $numberOfSeconds = -1 * $numberOfSeconds; |
| $isNegative = true; |
| } |
|
|
| |
| if ($displayTimeAsSentence === false) { |
| $days = floor($numberOfSeconds / 86400); |
| $hours = floor(($reminder = ($numberOfSeconds - $days * 86400)) / 3600); |
| $minutes = floor(($reminder = ($reminder - $hours * 3600)) / 60); |
| $seconds = floor($reminder - $minutes * 60); |
| if ($days == 0) { |
| $time = sprintf("%02s", $hours) . ':' . sprintf("%02s", $minutes) . ':' . sprintf("%02s", $seconds); |
| } else { |
| $time = sprintf(Piwik::translate('Intl_NDays'), $days) . " " . sprintf("%02s", $hours) . ':' . sprintf("%02s", $minutes) . ':' . sprintf("%02s", $seconds); |
| } |
| $centiSeconds = intval($numberOfSeconds * 100) % 100; |
| if ($centiSeconds) { |
| $time .= '.' . sprintf("%02s", $centiSeconds); |
| } |
| if ($isNegative) { |
| $time = '-' . $time; |
| } |
| return $time; |
| } |
| $secondsInYear = 86400 * 365.25; |
|
|
| $years = floor($numberOfSeconds / $secondsInYear); |
| $minusYears = $numberOfSeconds - $years * $secondsInYear; |
| $days = floor($minusYears / 86400); |
|
|
| $minusDays = $numberOfSeconds - $days * 86400; |
| $hours = floor($minusDays / 3600); |
|
|
| $minusDaysAndHours = $minusDays - $hours * 3600; |
| $minutes = floor($minusDaysAndHours / 60); |
|
|
| $seconds = $minusDaysAndHours - $minutes * 60; |
| $precision = ($seconds > 0 && $seconds < 0.01 ? 3 : 2); |
| $seconds = NumberFormatter::getInstance()->formatNumber(round($seconds, $precision), $precision); |
|
|
| if ($years > 0) { |
| $return = sprintf(Piwik::translate('General_YearsDays'), $years, $days); |
| } elseif ($days > 0) { |
| $return = sprintf(Piwik::translate('General_DaysHours'), $days, $hours); |
| } elseif ($hours > 0) { |
| $return = sprintf(Piwik::translate('General_HoursMinutes'), $hours, $minutes); |
| } elseif ($minutes > 0) { |
| $return = sprintf(Piwik::translate('General_MinutesSeconds'), $minutes, $seconds); |
| } else { |
| $return = sprintf(Piwik::translate('Intl_NSecondsShort'), $seconds); |
| } |
|
|
| if ($isNegative) { |
| $return = '-' . $return; |
| } |
|
|
| return $return; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getPrettySizeFromBytes($size, $unit = null, $precision = 1) |
| { |
| if ($size == 0) { |
| return '0 M'; |
| } |
|
|
| list($size, $sizeUnit) = $this->getPrettySizeFromBytesWithUnit($size, $unit, $precision); |
| return $size . " " . $sizeUnit; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function getPrettyMoney($value, $idSite) |
| { |
| $currencySymbol = Site::getCurrencySymbolFor($idSite); |
| return NumberFormatter::getInstance()->formatCurrency($value, $currencySymbol, GoalManager::REVENUE_PRECISION); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| public function getPrettyPercentFromQuotient($value) |
| { |
| return NumberFormatter::getInstance()->formatPercent($value * 100, 4, 0); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function formatMetrics(DataTable $dataTable, ?Report $report = null, $metricsToFormat = null, $formatAll = false) |
| { |
| $metrics = $this->getMetricsToFormat($dataTable, $report); |
| if ( |
| empty($metrics) |
| || $dataTable->getMetadata(self::PROCESSED_METRICS_FORMATTED_FLAG) |
| ) { |
| return; |
| } |
|
|
| $dataTable->setMetadata(self::PROCESSED_METRICS_FORMATTED_FLAG, true); |
|
|
| if ($metricsToFormat !== null) { |
| $metricMatchRegex = $this->makeRegexToMatchMetrics($metricsToFormat); |
| $metrics = array_filter($metrics, function ($metric) use ($metricMatchRegex) { |
| /** @var ProcessedMetric|ArchivedMetric $metric */ |
| return preg_match($metricMatchRegex, $metric->getName()); |
| }); |
| } |
|
|
| foreach ($metrics as $name => $metric) { |
| if (!$metric->beforeFormat($report, $dataTable)) { |
| continue; |
| } |
|
|
| foreach ($dataTable->getRows() as $row) { |
| $columnValue = $row->getColumn($name); |
| if ($columnValue !== false) { |
| $row->setColumn($name, $metric->format($columnValue, $this)); |
| } |
| } |
| } |
|
|
| foreach ($dataTable->getRows() as $row) { |
| $subtable = $row->getSubtable(); |
| if (!empty($subtable)) { |
| $this->formatMetrics($subtable, $report, $metricsToFormat, $formatAll); |
| } |
| $comparisons = $row->getComparisons(); |
| if (!empty($comparisons)) { |
| $this->formatMetrics($comparisons, $report, $metricsToFormat, $formatAll); |
| } |
| } |
|
|
| $idSite = DataTableFactory::getSiteIdFromMetadata($dataTable); |
| if (empty($idSite)) { |
| |
| $idSite = Common::getRequestVar('idSite', 0, 'int'); |
| } |
|
|
| |
| |
| if ($formatAll) { |
| foreach ($dataTable->getRows() as $row) { |
| foreach ($row->getColumns() as $column => $columnValue) { |
| if ( |
| strpos($column, 'revenue') === false |
| || !is_numeric($columnValue) |
| ) { |
| continue; |
| } |
|
|
| if ($columnValue !== false) { |
| $row->setColumn($column, $this->getPrettyMoney($columnValue, $idSite)); |
| } |
| } |
| } |
| } |
| } |
|
|
| protected function getPrettySizeFromBytesWithUnit($size, $unit = null, $precision = 1) |
| { |
| $units = array('B', 'K', 'M', 'G', 'T'); |
| $numUnits = count($units) - 1; |
|
|
| $currentUnit = null; |
| foreach ($units as $idx => $currentUnit) { |
| if ($unit && $unit !== $currentUnit) { |
| $size = $size / 1024; |
| } elseif ($unit && $unit === $currentUnit) { |
| break; |
| } elseif ($size >= 1024 && $idx != $numUnits) { |
| $size = $size / 1024; |
| } else { |
| break; |
| } |
| } |
|
|
| $size = round($size, $precision); |
|
|
| return array($size, $currentUnit); |
| } |
|
|
| private function makeRegexToMatchMetrics($metricsToFormat) |
| { |
| $metricsRegexParts = array(); |
| foreach ($metricsToFormat as $metricFilter) { |
| if ($metricFilter[0] == '/') { |
| $metricsRegexParts[] = '(?:' . substr($metricFilter, 1, strlen($metricFilter) - 2) . ')'; |
| } else { |
| $metricsRegexParts[] = preg_quote($metricFilter); |
| } |
| } |
| return '/^' . implode('|', $metricsRegexParts) . '$/'; |
| } |
|
|
| |
| |
| |
| private function getMetricsToFormat(DataTable $dataTable, ?Report $report = null) |
| { |
| return Report::getMetricsForTable($dataTable, $report, $baseType = 'Piwik\\Plugin\\Metric'); |
| } |
| } |
|
|