Exemplo n.º 1
0
 /**
  * Merge the columns of two data tables.
  * Manipulates the first table.
  *
  * @param DataTable|DataTable\Map $table1 The table to eventually filter.
  * @param DataTable|DataTable\Map $table2 Whether to delete rows with no visits or not.
  */
 public function mergeDataTables($table1, $table2)
 {
     // handle table arrays
     if ($table1 instanceof DataTable\Map && $table2 instanceof DataTable\Map) {
         $subTables2 = $table2->getDataTables();
         foreach ($table1->getDataTables() as $index => $subTable1) {
             if (!array_key_exists($index, $subTables2)) {
                 // occurs when archiving starts on dayN and continues into dayN+1, see https://github.com/piwik/piwik/issues/5168#issuecomment-50959925
                 continue;
             }
             $subTable2 = $subTables2[$index];
             $this->mergeDataTables($subTable1, $subTable2);
         }
         return;
     }
     $firstRow2 = $table2->getFirstRow();
     if (!$firstRow2 instanceof Row) {
         return;
     }
     $firstRow1 = $table1->getFirstRow();
     if (empty($firstRow1)) {
         $firstRow1 = $table1->addRow(new Row());
     }
     foreach ($firstRow2->getColumns() as $metric => $value) {
         $firstRow1->setColumn($metric, $value);
     }
 }
Exemplo n.º 2
0
 private function createInstanceWithDataTableMaps()
 {
     $dataTable = new DataTable\Map();
     $subDataTableMap1 = $this->createInstanceWithDataTables();
     $dataTable->addTable($subDataTableMap1, 'subArray1');
     $subDataTableMap2 = $this->createInstanceWithDataTables();
     $dataTable->addTable($subDataTableMap2, 'subArray2');
     return $dataTable;
 }
 /**
  * Manipulates child DataTables of a DataTable\Map. See @manipulate for more info.
  *
  * @param DataTable\Map $dataTable
  * @return DataTable\Map
  */
 protected function manipulateDataTableMap($dataTable)
 {
     $result = $dataTable->getEmptyClone();
     foreach ($dataTable->getDataTables() as $tableLabel => $childTable) {
         $newTable = $this->manipulate($childTable);
         $result->addTable($newTable, $tableLabel);
     }
     return $result;
 }
Exemplo n.º 4
0
 /**
  * Computes the output of the given array of data tables
  *
  * @param DataTable\Map $map data tables to render
  * @param string $prefix prefix to output before table data
  * @return string
  */
 protected function renderDataTableMap(DataTable\Map $map, $prefix)
 {
     $output = "Set<hr />";
     $prefix = $prefix . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
     foreach ($map->getDataTables() as $descTable => $table) {
         $output .= $prefix . "<b>" . $descTable . "</b><br />";
         $output .= $prefix . $this->renderTable($table, $prefix . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
         $output .= "<hr />";
     }
     return $output;
 }
Exemplo n.º 5
0
 private function getSeriesData($rowLabel, $columnName, DataTable\Map $dataTable)
 {
     $seriesData = array();
     foreach ($dataTable->getDataTables() as $childTable) {
         // get the row for this label (use the first if $rowLabel is false)
         if ($rowLabel === false) {
             $row = $childTable->getFirstRow();
         } else {
             $row = $childTable->getRowFromLabel($rowLabel);
         }
         // get series data point. defaults to 0 if no row or no column value.
         if ($row === false) {
             $seriesData[] = 0;
         } else {
             $seriesData[] = $row->getColumn($columnName) ?: 0;
         }
     }
     return $seriesData;
 }
Exemplo n.º 6
0
 /**
  * @param DataTable\Map $dataTableMap
  * @param string $columnToPlot
  *
  * @return array
  * @throws \Exception
  */
 protected function getValuesFromDataTableMap($dataTableMap, $columnToPlot)
 {
     $dataTableMap->applyQueuedFilters();
     $values = array();
     foreach ($dataTableMap->getDataTables() as $table) {
         if ($table->getRowsCount() > 1) {
             throw new Exception("Expecting only one row per DataTable");
         }
         $value = 0;
         $onlyRow = $table->getFirstRow();
         if (false !== $onlyRow) {
             if (!empty($columnToPlot)) {
                 $value = $onlyRow->getColumn($columnToPlot);
             } else {
                 $columns = $onlyRow->getColumns();
                 $value = current($columns);
             }
         }
         $values[] = $value;
     }
     return $values;
 }
Exemplo n.º 7
0
 public function test_renderDataTable_shouldRenderDataTableMaps()
 {
     $map = new DataTable\Map();
     $dataTable = new DataTable();
     $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
     $dataTable2 = new DataTable();
     $dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
     $map->addTable($dataTable, 'table1');
     $map->addTable($dataTable2, 'table2');
     $response = $this->builder->renderDataTable($map);
     $expected = array('table1' => array(array('nb_visits' => 5, 'nb_random' => 10)), 'table2' => array(array('nb_visits' => 3, 'nb_random' => 6)));
     $this->assertEquals($expected, $response);
 }
Exemplo n.º 8
0
 /**
  * Enhance a $dataTable using metadata :
  *
  * - remove metrics based on $reportMetadata['metrics']
  * - add 0 valued metrics if $dataTable doesn't provide all $reportMetadata['metrics']
  * - format metric values to a 'human readable' format
  * - extract row metadata to a separate Simple|Set : $rowsMetadata
  * - translate metric names to a separate array : $columns
  *
  * @param int $idSite enables monetary value formatting based on site currency
  * @param \Piwik\DataTable\Map|\Piwik\DataTable\Simple $dataTable
  * @param array $reportMetadata
  * @param bool $showRawMetrics
  * @param bool|null $formatMetrics
  * @return array Simple|Set $newReport with human readable format & array $columns list of translated column names & Simple|Set $rowsMetadata
  */
 private function handleTableReport($idSite, $dataTable, &$reportMetadata, $showRawMetrics = false, $formatMetrics = null)
 {
     $hasDimension = isset($reportMetadata['dimension']);
     $columns = @$reportMetadata['metrics'] ?: array();
     if ($hasDimension) {
         $columns = array_merge(array('label' => $reportMetadata['dimension']), $columns);
     }
     if (isset($reportMetadata['processedMetrics']) && is_array($reportMetadata['processedMetrics'])) {
         $processedMetricsAdded = Metrics::getDefaultProcessedMetrics();
         foreach ($reportMetadata['processedMetrics'] as $processedMetricId => $processedMetricTranslation) {
             // this processed metric can be displayed for this report
             if ($processedMetricTranslation && $processedMetricId !== $processedMetricTranslation) {
                 $columns[$processedMetricId] = $processedMetricTranslation;
             } elseif (isset($processedMetricsAdded[$processedMetricId])) {
                 // for instance in case 'nb_visits' => 'nb_visits' we will translate it
                 $columns[$processedMetricId] = $processedMetricsAdded[$processedMetricId];
             }
         }
     }
     // Display the global Goal metrics
     if (isset($reportMetadata['metricsGoal'])) {
         $metricsGoalDisplay = array('revenue');
         // Add processed metrics to be displayed for this report
         foreach ($metricsGoalDisplay as $goalMetricId) {
             if (isset($reportMetadata['metricsGoal'][$goalMetricId])) {
                 $columns[$goalMetricId] = $reportMetadata['metricsGoal'][$goalMetricId];
             }
         }
     }
     $columns = $this->hideShowMetrics($columns);
     $totals = array();
     // $dataTable is an instance of Set when multiple periods requested
     if ($dataTable instanceof DataTable\Map) {
         // Need a new Set to store the 'human readable' values
         $newReport = new DataTable\Map();
         $newReport->setKeyName("prettyDate");
         // Need a new Set to store report metadata
         $rowsMetadata = new DataTable\Map();
         $rowsMetadata->setKeyName("prettyDate");
         // Process each Simple entry
         foreach ($dataTable->getDataTables() as $simpleDataTable) {
             $this->removeEmptyColumns($columns, $reportMetadata, $simpleDataTable);
             list($enhancedSimpleDataTable, $rowMetadata) = $this->handleSimpleDataTable($idSite, $simpleDataTable, $columns, $hasDimension, $showRawMetrics, $formatMetrics);
             $enhancedSimpleDataTable->setAllTableMetadata($simpleDataTable->getAllTableMetadata());
             $period = $simpleDataTable->getMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX)->getLocalizedLongString();
             $newReport->addTable($enhancedSimpleDataTable, $period);
             $rowsMetadata->addTable($rowMetadata, $period);
             $totals = $this->aggregateReportTotalValues($simpleDataTable, $totals);
         }
     } else {
         $this->removeEmptyColumns($columns, $reportMetadata, $dataTable);
         list($newReport, $rowsMetadata) = $this->handleSimpleDataTable($idSite, $dataTable, $columns, $hasDimension, $showRawMetrics, $formatMetrics);
         $totals = $this->aggregateReportTotalValues($dataTable, $totals);
     }
     return array($newReport, $columns, $rowsMetadata, $totals);
 }
 /**
  * Creates a Set from an array index.
  *
  * @param array $index @see DataCollection
  * @param array $resultIndices @see make
  * @param array $keyMetadata The metadata to add to the table when it's created.
  * @return DataTable\Map
  */
 private function createDataTableMapFromIndex($index, $resultIndices, $keyMetadata = array())
 {
     $resultIndexLabel = reset($resultIndices);
     $resultIndex = key($resultIndices);
     array_shift($resultIndices);
     $result = new DataTable\Map();
     $result->setKeyName($resultIndexLabel);
     foreach ($index as $label => $value) {
         $keyMetadata[$resultIndex] = $label;
         if (empty($resultIndices)) {
             $newTable = $this->createDataTable($value, $keyMetadata);
         } else {
             $newTable = $this->createDataTableMapFromIndex($value, $resultIndices, $keyMetadata);
         }
         $result->addTable($newTable, $this->prettifyIndexLabel($resultIndex, $label));
     }
     return $result;
 }
Exemplo n.º 10
0
    public function test_renderDataTable_shouldRenderDataTableMaps()
    {
        $map = new DataTable\Map();
        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
        $dataTable2 = new DataTable();
        $dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
        $map->addTable($dataTable, 'table1');
        $map->addTable($dataTable2, 'table2');
        $response = $this->builder->renderDataTable($map);
        $this->assertEquals('<table id="MultiSites_getAll" border="1">
<thead>
	<tr>
		<th>_defaultKeyName</th>
		<th>nb_visits</th>
		<th>nb_random</th>
	</tr>
</thead>
<tbody>
	<tr>
		<td>table1</td>
		<td>5</td>
		<td>10</td>
	</tr>
	<tr>
		<td>table2</td>
		<td>3</td>
		<td>6</td>
	</tr>
</tbody>
</table>
', $response);
    }
Exemplo n.º 11
0
    public function test_renderDataTable_shouldRenderDataTableMaps()
    {
        $map = new DataTable\Map();
        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
        $dataTable2 = new DataTable();
        $dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
        $map->addTable($dataTable, 'table1');
        $map->addTable($dataTable2, 'table2');
        $response = $this->builder->renderDataTable($map);
        $this->assertEquals('<?xml version="1.0" encoding="utf-8" ?>
<results>
	<result defaultKeyName="table1">
		<row>
			<nb_visits>5</nb_visits>
			<nb_random>10</nb_random>
		</row>
	</result>
	<result defaultKeyName="table2">
		<row>
			<nb_visits>3</nb_visits>
			<nb_random>6</nb_random>
		</row>
	</result>
</results>', $response);
    }
Exemplo n.º 12
0
 /**
  * Filter a DataTable instance. See @filter for more info.
  *
  * @param DataTable\Simple|DataTable\Map $dataTable
  * @return mixed
  */
 protected function manipulateDataTable($dataTable)
 {
     $result = $dataTable->getEmptyClone();
     foreach ($this->labels as $labelIndex => $label) {
         $row = null;
         foreach ($this->getLabelVariations($label) as $labelVariation) {
             $labelVariation = explode(self::SEPARATOR_RECURSIVE_LABEL, $labelVariation);
             $row = $this->doFilterRecursiveDescend($labelVariation, $dataTable);
             if ($row) {
                 if ($this->addLabelIndex) {
                     $row->setMetadata(self::FLAG_IS_ROW_EVOLUTION, $labelIndex);
                 }
                 $result->addRow($row);
                 break;
             }
         }
     }
     return $result;
 }
Exemplo n.º 13
0
 /**
  * Common filters for all Actions API
  *
  * @param DataTable|DataTable\Simple|DataTable\Map $dataTable
  * @param bool $expanded
  */
 protected function filterActionsDataTable($dataTable, $expanded = false)
 {
     // Must be applied before Sort in this case, since the DataTable can contain both int and strings indexes
     // (in the transition period between pre 1.2 and post 1.2 datatable structure)
     $dataTable->filter('ReplaceColumnNames');
     $dataTable->filter('Sort', array('nb_visits', 'desc', $naturalSort = false, $expanded));
     $dataTable->queueFilter('ReplaceSummaryRowLabel');
 }
Exemplo n.º 14
0
 /**
  * @param DataTable|DataTable\Map $dataTable
  * @param $fieldsToGet
  * @param $sitesToProblablyAdd
  */
 private function addMissingWebsites($dataTable, $fieldsToGet, $sitesToProblablyAdd)
 {
     $siteIdsInDataTable = array();
     foreach ($dataTable->getRows() as $row) {
         /** @var DataTable\Row $row */
         $siteIdsInDataTable[] = $row->getColumn('label');
     }
     foreach ($sitesToProblablyAdd as $site) {
         if (!in_array($site['idsite'], $siteIdsInDataTable)) {
             $siteRow = array_combine($fieldsToGet, array_pad(array(), count($fieldsToGet), 0));
             $siteRow['label'] = (int) $site['idsite'];
             $dataTable->addRowFromSimpleArray($siteRow);
         }
     }
 }
Exemplo n.º 15
0
 private function makeMergedTableWithPeriodAndSiteIndex($index, $resultIndices, $useSimpleDataTable, $isNumeric)
 {
     $map = new DataTable\Map();
     $map->setKeyName($resultIndices[self::TABLE_METADATA_PERIOD_INDEX]);
     // we save all tables of the map in this array to be able to add rows fast
     $tables = array();
     foreach ($this->periods as $range => $period) {
         // as the resulting table is "merged", we do only set Period metedata and no metadata for site. Instead each
         // row will have an idsite metadata entry.
         $metadata = array(self::TABLE_METADATA_PERIOD_INDEX => $period);
         if ($useSimpleDataTable) {
             $table = new DataTable\Simple();
         } else {
             $table = new DataTable();
         }
         $table->setAllTableMetadata($metadata);
         $map->addTable($table, $this->prettifyIndexLabel(self::TABLE_METADATA_PERIOD_INDEX, $range));
         $tables[$range] = $table;
     }
     foreach ($index as $idsite => $table) {
         $rowMeta = array('idsite' => $idsite);
         foreach ($table as $range => $row) {
             if (!empty($row)) {
                 $tables[$range]->addRow(new Row(array(Row::COLUMNS => $row, Row::METADATA => $rowMeta)));
             } elseif ($isNumeric) {
                 $tables[$range]->addRow(new Row(array(Row::COLUMNS => $this->defaultRow, Row::METADATA => $rowMeta)));
             }
         }
     }
     return $map;
 }
Exemplo n.º 16
0
 /**
  * Computes the output for the given data table array
  *
  * @param Map $table
  * @param array $array
  * @param string $prefixLines
  * @return string
  */
 protected function renderDataTableMap($table, $array, $prefixLines = "")
 {
     // CASE 1
     //array
     //  'day1' => string '14' (length=2)
     //  'day2' => string '6' (length=1)
     $firstTable = current($array);
     if (!is_array($firstTable)) {
         $xml = '';
         $nameDescriptionAttribute = $table->getKeyName();
         foreach ($array as $valueAttribute => $value) {
             if (empty($value)) {
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\" />\n";
             } elseif ($value instanceof Map) {
                 $out = $this->renderTable($value, true);
                 //TODO somehow this code is not tested, cover this case
                 $xml .= "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\">\n{$out}</result>\n";
             } else {
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\">" . self::formatValueXml($value) . "</result>\n";
             }
         }
         return $xml;
     }
     $subTables = $table->getDataTables();
     $firstTable = current($subTables);
     // CASE 2
     //array
     //  'day1' =>
     //    array
     //      'nb_uniq_visitors' => string '18'
     //      'nb_visits' => string '101'
     //  'day2' =>
     //    array
     //      'nb_uniq_visitors' => string '28'
     //      'nb_visits' => string '11'
     if ($firstTable instanceof Simple) {
         $xml = '';
         $nameDescriptionAttribute = $table->getKeyName();
         foreach ($array as $valueAttribute => $dataTableSimple) {
             if (count($dataTableSimple) == 0) {
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\" />\n";
             } else {
                 if (is_array($dataTableSimple)) {
                     $dataTableSimple = "\n" . $this->renderDataTableSimple($dataTableSimple, $prefixLines . "\t") . $prefixLines . "\t";
                 }
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\">" . $dataTableSimple . "</result>\n";
             }
         }
         return $xml;
     }
     // CASE 3
     //array
     //  'day1' =>
     //    array
     //      0 =>
     //        array
     //          'label' => string 'phpmyvisites'
     //          'nb_uniq_visitors' => int 11
     //          'nb_visits' => int 13
     //      1 =>
     //        array
     //          'label' => string 'phpmyvisits'
     //          'nb_uniq_visitors' => int 2
     //          'nb_visits' => int 2
     //  'day2' =>
     //    array
     //      0 =>
     //        array
     //          'label' => string 'piwik'
     //          'nb_uniq_visitors' => int 121
     //          'nb_visits' => int 130
     //      1 =>
     //        array
     //          'label' => string 'piwik bis'
     //          'nb_uniq_visitors' => int 20
     //          'nb_visits' => int 120
     if ($firstTable instanceof DataTable) {
         $xml = '';
         $nameDescriptionAttribute = $table->getKeyName();
         foreach ($array as $keyName => $arrayForSingleDate) {
             $dataTableOut = $this->renderDataTable($arrayForSingleDate, $prefixLines . "\t");
             if (empty($dataTableOut)) {
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$keyName}\" />\n";
             } else {
                 $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$keyName}\">\n";
                 $xml .= $dataTableOut;
                 $xml .= $prefixLines . "\t</result>\n";
             }
         }
         return $xml;
     }
     if ($firstTable instanceof Map) {
         $xml = '';
         $tables = $table->getDataTables();
         $nameDescriptionAttribute = $table->getKeyName();
         foreach ($tables as $valueAttribute => $tableInArray) {
             $out = $this->renderTable($tableInArray, true, $prefixLines . "\t");
             $xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\">\n" . $out . $prefixLines . "\t</result>\n";
         }
         return $xml;
     }
     return '';
 }
Exemplo n.º 17
0
    public function test_renderDataTable_shouldRenderDataTableMaps()
    {
        $map = new DataTable\Map();
        $dataTable = new DataTable();
        $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
        $dataTable2 = new DataTable();
        $dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
        $map->addTable($dataTable, 'table1');
        $map->addTable($dataTable2, 'table2');
        $response = $this->builder->renderDataTable($map);
        $this->assertEquals('defaultKeyName,nb_visits,nb_random
table1,5,10
table2,3,6', $response);
    }
Exemplo n.º 18
0
 public function test_hasDataTableUsers_shouldHandleADataTableMap()
 {
     $map = new DataTable\Map();
     $map->addTable(new DataTable(), 'label1');
     $map->addTable(new DataTable(), 'label2');
     $map->addTable($this->getDataTableWithoutUsersColumn(), 'label3');
     $this->assertNotDataTableHasUsers($map);
     $map->addTable($this->getDataTableWithZeroUsers(), 'label4');
     $map->addTable(new DataTable(), 'label5');
     $this->assertNotDataTableHasUsers($map);
     $map->addTable($this->getDataTableWithUsers(), 'label6');
     $this->assertDataTableHasUsers($map);
 }
Exemplo n.º 19
0
 private function generateOverviewReport($method, $idSite, $period, $date, $segment, array $defaultParams)
 {
     $tableManager = DataTable\Manager::getInstance();
     /** @var DataTable[] $tables */
     $tables = array();
     foreach ($this->getOverviewReports() as $reportId => $reportParams) {
         if (!empty($reportParams)) {
             foreach ($defaultParams as $key => $defaultParam) {
                 if (!array_key_exists($key, $reportParams)) {
                     $reportParams[$key] = $defaultParam;
                 }
             }
         }
         $firstTableId = $tableManager->getMostRecentTableId();
         $table = $this->requestApiMethod($method, $idSite, $period, $date, $reportId, $segment, $reportParams);
         $reportTableIds[] = $table->getId();
         $tableManager->deleteTablesExceptIgnored($reportTableIds, $firstTableId);
         $tables[] = $table;
     }
     $map = new DataTable\Map();
     foreach ($tables as $table) {
         $map->addTable($table, $table->getMetadata('reportName'));
     }
     return $map;
 }
Exemplo n.º 20
0
 /**
  * Computes the output of the given data table array
  *
  * @param DataTable\Map $table
  * @param array $allColumns
  * @return string
  */
 protected function renderDataTableMap($table, &$allColumns = array())
 {
     $str = '';
     foreach ($table->getDataTables() as $currentLinePrefix => $dataTable) {
         $returned = explode("\n", $this->renderTable($dataTable, $allColumns));
         // get rid of the columns names
         $returned = array_slice($returned, 1);
         // case empty datatable we dont print anything in the CSV export
         // when in xml we would output <result date="2008-01-15" />
         if (!empty($returned)) {
             foreach ($returned as &$row) {
                 $row = $currentLinePrefix . $this->separator . $row;
             }
             $str .= "\n" . implode("\n", $returned);
         }
     }
     // prepend table key to column list
     $allColumns = array_merge(array($table->getKeyName() => true), $allColumns);
     // add header to output string
     $str = $this->getHeaderLine(array_keys($allColumns)) . $str;
     return $str;
 }
Exemplo n.º 21
0
    public function test_renderDataTable_shouldRenderDataTableMaps_IfKeynameIsDate()
    {
        $map = new DataTable\Map();
        $map->setKeyName('date');
        $_GET['period'] = 'day';
        $response = $this->builder->renderDataTable($map);
        unset($_GET['period']);
        $response = preg_replace(array('/<pubDate>(.*)<\\/pubDate>/', '/<lastBuildDate>(.*)<\\/lastBuildDate>/'), '', $response);
        $this->assertEquals('<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>piwik statistics - RSS</title>
    <link>http://piwik.org</link>
    <description>Piwik RSS feed</description>
    
    <generator>piwik</generator>
    <language>en</language>
    	</channel>
</rss>', $response);
    }
Exemplo n.º 22
0
 /**
  * Aggregates the DataTable\Map into the destination $aggregated
  * @param $map
  * @param $aggregated
  */
 protected function aggregatedDataTableMapsAsOne(Map $map, DataTable $aggregated)
 {
     foreach ($map->getDataTables() as $tableToAggregate) {
         if ($tableToAggregate instanceof Map) {
             $this->aggregatedDataTableMapsAsOne($tableToAggregate, $aggregated);
         } else {
             $aggregated->addDataTable($tableToAggregate, $this->isAggregateSubTables());
         }
     }
 }
Exemplo n.º 23
0
 public function test_renderDataTable_shouldRenderDataTableMaps()
 {
     $map = new DataTable\Map();
     $dataTable = new DataTable();
     $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
     $dataTable2 = new DataTable();
     $dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
     $map->addTable($dataTable, 'table1');
     $map->addTable($dataTable2, 'table2');
     $response = $this->jsonBuilder->renderDataTable($map);
     $this->assertEquals('{"table1":[{"nb_visits":5,"nb_random":10}],"table2":[{"nb_visits":3,"nb_random":6}]}', $response);
     $this->assertNoJsonError($response);
 }
Exemplo n.º 24
0
 protected function _getDataTableMap_containsDataTableMap_simpleOneRow()
 {
     $table = new DataTable\Map();
     $table->setKeyName('parentArrayKey');
     $table->addTable($this->_getDataTableSimpleOneRowMapTest(), 'idSite');
     return $table;
 }
Exemplo n.º 25
0
 /**
  * Returns a new DataTable\Map w/o any child DataTables, but with
  * the same key name as this instance.
  *
  * @return Map
  */
 public function getEmptyClone()
 {
     $dataTableMap = new Map();
     $dataTableMap->setKeyName($this->getKeyName());
     return $dataTableMap;
 }
Exemplo n.º 26
0
 /** Get row evolution for a multiple labels */
 private function getMultiRowEvolution(DataTable\Map $dataTable, $metadata, $apiModule, $apiAction, $labels, $column, $legendAppendMetric = true, $labelUseAbsoluteUrl = true)
 {
     if (!isset($metadata['metrics'][$column])) {
         // invalid column => use the first one that's available
         $metrics = array_keys($metadata['metrics']);
         $column = reset($metrics);
     }
     // get the processed label and logo (if any) for every requested label
     $actualLabels = $logos = array();
     foreach ($labels as $labelIdx => $label) {
         foreach ($dataTable->getDataTables() as $table) {
             $labelRow = $this->getRowEvolutionRowFromLabelIdx($table, $labelIdx);
             if ($labelRow) {
                 $actualLabels[$labelIdx] = $this->getRowUrlForEvolutionLabel($labelRow, $apiModule, $apiAction, $labelUseAbsoluteUrl);
                 $logos[$labelIdx] = $labelRow->getMetadata('logo');
                 if (!empty($actualLabels[$labelIdx])) {
                     break;
                 }
             }
         }
         if (empty($actualLabels[$labelIdx])) {
             $actualLabels[$labelIdx] = $this->cleanOriginalLabel($label);
         }
     }
     // convert rows to be array($column.'_'.$labelIdx => $value) as opposed to
     // array('label' => $label, 'column' => $value).
     $dataTableMulti = $dataTable->getEmptyClone();
     foreach ($dataTable->getDataTables() as $tableLabel => $table) {
         $newRow = new Row();
         foreach ($labels as $labelIdx => $label) {
             $row = $this->getRowEvolutionRowFromLabelIdx($table, $labelIdx);
             $value = 0;
             if ($row) {
                 $value = $row->getColumn($column);
                 $value = floatVal(str_replace(',', '.', $value));
             }
             if ($value == '') {
                 $value = 0;
             }
             $newLabel = $column . '_' . (int) $labelIdx;
             $newRow->addColumn($newLabel, $value);
         }
         $newTable = $table->getEmptyClone();
         if (!empty($labels)) {
             // only add a row if the row has data (no labels === no data)
             $newTable->addRow($newRow);
         }
         $dataTableMulti->addTable($newTable, $tableLabel);
     }
     // the available metrics for the report are returned as metadata / columns
     $metadata['columns'] = $metadata['metrics'];
     // metadata / metrics should document the rows that are compared
     // this way, UI code can be reused
     $metadata['metrics'] = array();
     foreach ($actualLabels as $labelIndex => $label) {
         if ($legendAppendMetric) {
             $label .= ' (' . $metadata['columns'][$column] . ')';
         }
         $metricName = $column . '_' . $labelIndex;
         $metadata['metrics'][$metricName] = SafeDecodeLabel::decodeLabelSafe($label);
         if (!empty($logos[$labelIndex])) {
             $metadata['logos'][$metricName] = $logos[$labelIndex];
         }
     }
     $this->enhanceRowEvolutionMetaData($metadata, $dataTableMulti);
     return array('column' => $column, 'reportData' => $dataTableMulti, 'metadata' => $metadata);
 }
Exemplo n.º 27
0
 /**
  * Common filters for all Actions API
  *
  * @param DataTable|DataTable\Simple|DataTable\Map $dataTable
  */
 private function filterActionsDataTable($dataTable)
 {
     // Must be applied before Sort in this case, since the DataTable can contain both int and strings indexes
     // (in the transition period between pre 1.2 and post 1.2 datatable structure)
     $dataTable->filter('Piwik\\Plugins\\Actions\\DataTable\\Filter\\Actions');
     return $dataTable;
 }
 /**
  * @param DataTable|DataTable\Map $dataTable
  * @param $visualization
  */
 protected function initChartObjectData($dataTable, $visualization)
 {
     // We apply a filter to the DataTable, decoding the label column (useful for keywords for example)
     $dataTable->filter('ColumnCallbackReplace', array('label', 'urldecode'));
     $xLabels = $dataTable->getColumn('label');
     $columnNames = $this->properties['columns_to_display'];
     if (($labelColumnIndex = array_search('label', $columnNames)) !== false) {
         unset($columnNames[$labelColumnIndex]);
     }
     $columnNameToTranslation = $columnNameToValue = array();
     foreach ($columnNames as $columnName) {
         $columnNameToTranslation[$columnName] = @$this->properties['translations'][$columnName];
         $columnNameToValue[$columnName] = $dataTable->getColumn($columnName);
     }
     $visualization->dataTable = $dataTable;
     $visualization->properties = $this->properties;
     $visualization->setAxisXLabels($xLabels);
     $visualization->setAxisYValues($columnNameToValue);
     $visualization->setAxisYLabels($columnNameToTranslation);
     $units = $this->getUnitsForColumnsToDisplay();
     $visualization->setAxisYUnits($units);
 }
Exemplo n.º 29
0
 /**
  * @param DataTable|DataTable\Map $dataTable
  * @param $visualization
  */
 protected function initChartObjectData($dataTable, $visualization)
 {
     $xLabels = $dataTable->getColumn('label');
     $columnNames = $this->properties['columns_to_display'];
     if (($labelColumnIndex = array_search('label', $columnNames)) !== false) {
         unset($columnNames[$labelColumnIndex]);
     }
     $columnNameToTranslation = $columnNameToValue = array();
     foreach ($columnNames as $columnName) {
         $columnNameToTranslation[$columnName] = @$this->properties['translations'][$columnName];
         $columnNameToValue[$columnName] = $dataTable->getColumn($columnName);
     }
     $visualization->dataTable = $dataTable;
     $visualization->properties = $this->properties;
     $visualization->setAxisXLabels($xLabels);
     $visualization->setAxisYValues($columnNameToValue);
     $visualization->setAxisYLabels($columnNameToTranslation);
     $units = $this->getUnitsForColumnsToDisplay();
     $visualization->setAxisYUnits($units);
 }