/** * Enhance $simpleDataTable using metadata : * * - remove metrics based on $reportMetadata['metrics'] * - add 0 valued metrics if $simpleDataTable doesn't provide all $reportMetadata['metrics'] * - format metric values to a 'human readable' format * - extract row metadata to a separate Simple $rowsMetadata * * @param int $idSite enables monetary value formatting based on site currency * @param Simple $simpleDataTable * @param array $metadataColumns * @param boolean $hasDimension * @param bool $returnRawMetrics If set to true, the original metrics will be returned * * @return array DataTable $enhancedDataTable filtered metrics with human readable format & Simple $rowsMetadata */ private function handleSimpleDataTable($idSite, $simpleDataTable, $metadataColumns, $hasDimension, $returnRawMetrics = false) { // new DataTable to store metadata $rowsMetadata = new DataTable(); // new DataTable to store 'human readable' values if ($hasDimension) { $enhancedDataTable = new DataTable(); } else { $enhancedDataTable = new Simple(); } foreach ($simpleDataTable->getRows() as $row) { $rowMetrics = $row->getColumns(); // add missing metrics foreach ($metadataColumns as $id => $name) { if (!isset($rowMetrics[$id])) { $row->setColumn($id, 0); $rowMetrics[$id] = 0; } } $enhancedRow = new Row(); $enhancedDataTable->addRow($enhancedRow); foreach ($rowMetrics as $columnName => $columnValue) { // filter metrics according to metadata definition if (isset($metadataColumns[$columnName])) { // generate 'human readable' metric values // if we handle MultiSites.getAll we do not always have the same idSite but different ones for // each site, see https://github.com/piwik/piwik/issues/5006 $idSiteForRow = $idSite; if ($row->getMetadata('idsite') && is_numeric($row->getMetadata('idsite'))) { $idSiteForRow = (int) $row->getMetadata('idsite'); } $prettyValue = MetricsFormatter::getPrettyValue($idSiteForRow, $columnName, $columnValue, $htmlAllowed = false); $enhancedRow->addColumn($columnName, $prettyValue); } elseif ($returnRawMetrics) { if (!isset($columnValue)) { $columnValue = 0; } $enhancedRow->addColumn($columnName, $columnValue); } } // If report has a dimension, extract metadata into a distinct DataTable if ($hasDimension) { $rowMetadata = $row->getMetadata(); $idSubDataTable = $row->getIdSubDataTable(); // Create a row metadata only if there are metadata to insert if (count($rowMetadata) > 0 || !is_null($idSubDataTable)) { $metadataRow = new Row(); $rowsMetadata->addRow($metadataRow); foreach ($rowMetadata as $metadataKey => $metadataValue) { $metadataRow->addColumn($metadataKey, $metadataValue); } if (!is_null($idSubDataTable)) { $metadataRow->addColumn('idsubdatatable', $idSubDataTable); } } } } return array($enhancedDataTable, $rowsMetadata); }
/** * @dataProvider getGetPrettyValueTestCases */ public function testGetPrettyValue($columnName, $value, $expected) { Translate::loadEnglishTranslation(); $access = Access::getInstance(); $access->setSuperUserAccess(true); $idsite = API::getInstance()->addSite("test", "http://test"); $this->assertEquals($expected, MetricsFormatter::getPrettyValue($idsite, $columnName, $value, false, false)); Translate::unloadEnglishTranslation(); }
/** * Enhance $simpleDataTable using metadata : * * - remove metrics based on $reportMetadata['metrics'] * - add 0 valued metrics if $simpleDataTable doesn't provide all $reportMetadata['metrics'] * - format metric values to a 'human readable' format * - extract row metadata to a separate Simple $rowsMetadata * * @param int $idSite enables monetary value formatting based on site currency * @param Simple $simpleDataTable * @param array $metadataColumns * @param boolean $hasDimension * @param bool $returnRawMetrics If set to true, the original metrics will be returned * * @return array DataTable $enhancedDataTable filtered metrics with human readable format & Simple $rowsMetadata */ private function handleSimpleDataTable($idSite, $simpleDataTable, $metadataColumns, $hasDimension, $returnRawMetrics = false) { // new DataTable to store metadata $rowsMetadata = new DataTable(); // new DataTable to store 'human readable' values if ($hasDimension) { $enhancedDataTable = new DataTable(); } else { $enhancedDataTable = new Simple(); } // add missing metrics foreach ($simpleDataTable->getRows() as $row) { $rowMetrics = $row->getColumns(); foreach ($metadataColumns as $id => $name) { if (!isset($rowMetrics[$id])) { $row->addColumn($id, 0); } } } foreach ($simpleDataTable->getRows() as $row) { $enhancedRow = new Row(); $enhancedDataTable->addRow($enhancedRow); $rowMetrics = $row->getColumns(); foreach ($rowMetrics as $columnName => $columnValue) { // filter metrics according to metadata definition if (isset($metadataColumns[$columnName])) { // generate 'human readable' metric values $prettyValue = MetricsFormatter::getPrettyValue($idSite, $columnName, $columnValue, $htmlAllowed = false); $enhancedRow->addColumn($columnName, $prettyValue); } elseif ($returnRawMetrics) { $enhancedRow->addColumn($columnName, $columnValue); } } // If report has a dimension, extract metadata into a distinct DataTable if ($hasDimension) { $rowMetadata = $row->getMetadata(); $idSubDataTable = $row->getIdSubDataTable(); // Create a row metadata only if there are metadata to insert if (count($rowMetadata) > 0 || !is_null($idSubDataTable)) { $metadataRow = new Row(); $rowsMetadata->addRow($metadataRow); foreach ($rowMetadata as $metadataKey => $metadataValue) { $metadataRow->addColumn($metadataKey, $metadataValue); } if (!is_null($idSubDataTable)) { $metadataRow->addColumn('idsubdatatable', $idSubDataTable); } } } } return array($enhancedDataTable, $rowsMetadata); }