Example #1
0
 private function createInstanceWithDataTableMaps()
 {
     $dataTable = new DataTable\Map();
     $subDataTableMap1 = $this->createInstanceWithDataTables();
     $dataTable->addTable($subDataTableMap1, 'subArray1');
     $subDataTableMap2 = $this->createInstanceWithDataTables();
     $dataTable->addTable($subDataTableMap2, 'subArray2');
     return $dataTable;
 }
 /**
  * @expectedException \Exception
  * @expectedExceptionMessage RSS feeds can be generated for one specific website
  */
 public function test_renderDataTable_shouldFail_IfKeynameIsNotDate()
 {
     $map = new DataTable\Map();
     $dataTable = new DataTable();
     $dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
     $map->addTable($dataTable, 'table1');
     $map->addTable($dataTable, 'table2');
     $this->builder->renderDataTable($map);
 }
Example #3
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);
    }
Example #4
0
 protected function _getDataTableMap_containsDataTableMap_simpleOneRow()
 {
     $table = new DataTable\Map();
     $table->setKeyName('parentArrayKey');
     $table->addTable($this->_getDataTableSimpleOneRowMapTest(), 'idSite');
     return $table;
 }
 /**
  * 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);
 }
Example #6
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);
 }
Example #7
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;
 }
 /**
  * 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;
 }
 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;
 }
Example #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('defaultKeyName,nb_visits,nb_random
table1,5,10
table2,3,6', $response);
    }
    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);
    }
Example #12
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);
 }
Example #13
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);
 }