diff --git a/core/DataTable.php b/core/DataTable.php index 4d7abc40ac893ae6ac84afb0c4fa2e09c0001a5f..2d94c53ee42f4ad459ff0981e5704f2f57e7f473 100644 --- a/core/DataTable.php +++ b/core/DataTable.php @@ -734,6 +734,7 @@ class Piwik_DataTable * serialized DataTable contained in this DataTable. * We save DataTable in serialized format in the Database. * Each row of this returned PHP array will be a row in the DB table. + * At the end of the method execution, the dataTable may be truncated (if $maximum* parameters are set). * * The keys of the array are very important as they are used to define the DataTable * @@ -771,7 +772,6 @@ class Piwik_DataTable { throw new Exception("Maximum recursion level of ".self::MAXIMUM_DEPTH_LEVEL_ALLOWED. " reached. You have probably set a DataTable_Row with an associated DataTable which belongs already to its parent hierarchy."); } - if( !is_null($maximumRowsInDataTable) ) { $filter = new Piwik_DataTable_Filter_AddSummaryRow($this, $maximumRowsInDataTable - 1); @@ -779,7 +779,7 @@ class Piwik_DataTable // For each row, get the serialized row // If it is associated to a sub table, get the serialized table recursively ; - // but returns all serialized tables and subtable in an array of 1 dimension! + // but returns all serialized tables and subtable in an array of 1 dimension $aSerializedDataTable = array(); foreach($this->rows as $row) { @@ -814,7 +814,7 @@ class Piwik_DataTable * * The function creates all the necessary DataTable_Row * - * @param string Serialized string of a datatable + * @param string string of serialized datatable * @return void */ public function addRowsFromSerializedArray( $stringSerialized ) @@ -1000,14 +1000,17 @@ class Piwik_DataTable $cleanRow = array(); foreach($array as $label => $row) { + // TODO I think this requirement is not true anymore: // we make sure that the label column is first in the list! - // important for the UI javascript mainly... + // important for the UI javascript mainly... + // array_merge doesn't work here as it reindex the numeric value // see the test testMergeArray in PHP_Related.test.php + $cleanRow[Piwik_DataTable_Row::DATATABLE_ASSOCIATED] = null; $cleanRow[Piwik_DataTable_Row::COLUMNS] = array('label' => $label) + $row; if(!is_null($subtablePerLabel) // some rows of this table don't have subtables - // (for examplecase of the campaign without keywords ) + // (for example case of campaigns without keywords) && isset($subtablePerLabel[$label]) ) { diff --git a/core/DataTable/Filter/AddSummaryRow.php b/core/DataTable/Filter/AddSummaryRow.php index b8d956dd43203a638d0c2efdc87f3c5f04bd9ac0..f5763ffa073d7e5ec677a4b414e3d4c51f7e54d6 100644 --- a/core/DataTable/Filter/AddSummaryRow.php +++ b/core/DataTable/Filter/AddSummaryRow.php @@ -43,7 +43,7 @@ class Piwik_DataTable_Filter_AddSummaryRow extends Piwik_DataTable_Filter protected function filter() { $filter = new Piwik_DataTable_Filter_Sort($this->table, $this->columnToSortByBeforeTruncating, 'desc'); - + $rows = $this->table->getRows(); $count = $this->table->getRowsCount(); $newRow = new Piwik_DataTable_Row(); @@ -61,6 +61,7 @@ class Piwik_DataTable_Filter_AddSummaryRow extends Piwik_DataTable_Filter } } $newRow->addColumn('label', $this->labelSummaryRow); + $filter = new Piwik_DataTable_Filter_Limit($this->table, 0, $this->startRowToSummarize); $this->table->addSummaryRow($newRow); } diff --git a/core/DataTable/Filter/Limit.php b/core/DataTable/Filter/Limit.php index b292a39aeb7fddef58dcd5196c4e6aab13a9c50e..d59ba38e297277f9ed25b299659dea664bcf318a 100644 --- a/core/DataTable/Filter/Limit.php +++ b/core/DataTable/Filter/Limit.php @@ -35,7 +35,6 @@ class Piwik_DataTable_Filter_Limit extends Piwik_DataTable_Filter $limit = -1; } $this->limit = $limit; - $this->filter(); } @@ -45,8 +44,10 @@ class Piwik_DataTable_Filter_Limit extends Piwik_DataTable_Filter $rowsCount = $table->getRowsCount(); // we delete from 0 to offset - $table->deleteRowsOffset( 0, $this->offset ); - + if($this->offset > 0) + { + $table->deleteRowsOffset( 0, $this->offset ); + } // at this point the array has offset less elements. We delete from limit to the end if( $this->limit >= 0 ) { diff --git a/core/DataTable/Renderer/Console.php b/core/DataTable/Renderer/Console.php index 59ea9bc55f4d25794c2cfd53490b59b5d2b615e6..d85a42dadff210a97c65b2e34f82cbe8f09b181a 100644 --- a/core/DataTable/Renderer/Console.php +++ b/core/DataTable/Renderer/Console.php @@ -75,6 +75,7 @@ class Piwik_DataTable_Renderer_Console extends Piwik_DataTable_Renderer break; } if(is_string($value)) $value = "'$value'"; + elseif(is_array($value)) $value = var_export($value, true); $columns[] = "'$column' => $value"; } @@ -87,10 +88,8 @@ class Piwik_DataTable_Renderer_Console extends Piwik_DataTable_Renderer $metadata = array(); foreach($row->getMetadata() as $name => $value) { - if(is_string($value)) - { - $value = "'$value'"; - } + if(is_string($value)) $value = "'$value'"; + elseif(is_array($value)) $value = var_export($value, true); $metadata[] = "'$name' => $value"; } $metadata = implode(", ", $metadata); diff --git a/core/DataTable/Row.php b/core/DataTable/Row.php index 119eac3797aa8b20f82b4ce8344f1bf7ce64aedf..9c5edfdd33b6b51617ef67472a29c6b8c8eb6993 100644 --- a/core/DataTable/Row.php +++ b/core/DataTable/Row.php @@ -107,16 +107,15 @@ class Piwik_DataTable_Row foreach($this->getColumns() as $column => $value) { if(is_string($value)) $value = "'$value'"; + elseif(is_array($value)) $value = var_export($value, true); $columns[] = "'$column' => $value"; } $columns = implode(", ", $columns); $metadata = array(); foreach($this->getMetadata() as $name => $value) { - if(is_string($value)) - { - $value = "'$value'"; - } + if(is_string($value)) $value = "'$value'"; + elseif(is_array($value)) $value = var_export($value, true); $metadata[] = "'$name' => $value"; } $metadata = implode(", ", $metadata); @@ -332,20 +331,39 @@ class Piwik_DataTable_Row { foreach($rowToSum->getColumns() as $name => $value) { - if($name != 'label' - && Piwik::isNumeric($value)) + if($name != 'label') { - $current = $this->getColumn($name); - if($current === false) + if(Piwik::isNumeric($value)) { - $current = 0; + $current = $this->getColumn($name); + if($current === false) + { + $current = 0; + } + $this->setColumn( $name, $current + $value); + } + elseif(is_array($value)) + { + $current = $this->getColumn($name); + $newValue = array(); + if($current == false) + { + $newValue = $value; + } + else + { + $newValue = $current; + foreach($value as $arrayIndex => $arrayValue) + { + $newValue[$arrayIndex] += $arrayValue; + } + } + $this->setColumn($name, $newValue); } - $this->setColumn( $name, $current + $value); } } } - /** * Helper function to test if two rows are equal. * @@ -366,7 +384,6 @@ class Piwik_DataTable_Row uksort($cols1, 'strnatcasecmp'); uksort($cols2, 'strnatcasecmp'); - if($cols1 != $cols2) { return false; diff --git a/misc/TODO b/misc/TODO index 6a431144574c504f79df5a1e67055b2f98b322f5..e52dab9f6823a53d298b2ce2e634684d64532652 100644 --- a/misc/TODO +++ b/misc/TODO @@ -1,5 +1,6 @@ Following up show all columns new UI element ==== +- passe en année, actions puis pages => c'est vide !!! - pages columns: first should be unique page views, sorted by unique pageviews - exclude all population doesn't work for actions - post 29 There seems not to be a way to export the pages information widget. I wanted to download the entry pages to see where my visitors entered my site from, but there is no option to do so. diff --git a/tests/config_test.php b/tests/config_test.php index 676d4e4d6b1857e60b3825fddb5b374d7a413b0c..4b8d6b85f346fa11ccc5e7731d89db79e278c15d 100755 --- a/tests/config_test.php +++ b/tests/config_test.php @@ -72,7 +72,6 @@ require_once 'Zend/Db/Table.php'; require_once 'FrontController.php'; require_once 'Config.php'; require_once 'Timer.php'; -require_once 'API/APIable.php'; require_once 'Access.php'; require_once 'Log.php'; require_once 'core/Piwik.php'; diff --git a/tests/core/DataTable.test.php b/tests/core/DataTable.test.php index f54315e412e5c3ebef26eae1b95c42fbd7f5c443..16960ae4886d29bbccfc65104f48d1251f1c4e57 100644 --- a/tests/core/DataTable.test.php +++ b/tests/core/DataTable.test.php @@ -255,7 +255,8 @@ class Test_Piwik_DataTable extends UnitTestCase 'test_float3'=> 1.5, 'test_stringint'=> "145", "test" => 'string fake', - 'super'=>array('this column has an array value, amazing') + 'super'=>array('this column has an array string that will be 0 when algorithm sums the value'), + 'integerArrayToSum'=>array( 1=>1, 2=>10.0), ); $metadata = array('logo'=> 'piwik.png', 'super'=>array('this column has an array value, amazing')); @@ -271,23 +272,22 @@ class Test_Piwik_DataTable extends UnitTestCase 'test_float2'=> 14.5, 'test_stringint'=> "5", 0925824 => 'toto', - 'super'=>array('this column has geagaean array value, amazing')); + 'super'=>array('this column has geagaean array value, amazing'), + 'integerArrayToSum'=>array( 1=> 5, 2=>5.5), + ); $finalRow = new Piwik_DataTable_Row( array(Piwik_DataTable_Row::COLUMNS => $columns2)); - $finalRow->sumRow($row1); - - $columnsWanted = array('test_int'=> 150, 'test_float'=> 150.0, 'test_float2'=> 14.5, 'test_float3'=> 1.5, - 'test_stringint'=> "150", //add also strings!! - 'super'=>array('this column has geagaean array value, amazing'), + 'test_stringint'=> 150, //add also strings!! + 'super'=>array(0), + 'integerArrayToSum' => array( 1=> 6, 2=>15.5), 0925824 => 'toto', ); $rowWanted = new Piwik_DataTable_Row( array(Piwik_DataTable_Row::COLUMNS => $columnsWanted)); - $this->assertTrue( Piwik_DataTable_Row::isEqual($rowWanted, $finalRow)); } diff --git a/tests/core/PHP_Related.test.php b/tests/core/PHP_Related.test.php index ae6f25ef989b6d667d90343d77d729918eea1ce9..99c8ee092b3a6da5835d0a1135f7d9a140419e48 100644 --- a/tests/core/PHP_Related.test.php +++ b/tests/core/PHP_Related.test.php @@ -24,6 +24,51 @@ class Test_PHP_Related extends UnitTestCase { } + // conclusion: + // - it's ok to have big holes in your array index values + // - obvious: it's not ok to index array by strings when you can do magic and index with int + function oldtest_memoryUsageArrayIncreasingIndexOrJumps() + { + ini_set('memory_limit','200M'); + Piwik::createConfigObject(); + Piwik::createLogObject(); + //test array[0] array[1] array[2] + //VS + // test array[0] array[100] array[200] + // same memory usage? hash + echo "start ". __FUNCTION__ . "<br>"; + Piwik::printMemoryUsage(); + $timer = new Piwik_Timer(); + $testId = 2; + if($testId == 1) + { + echo "start incrementing index<br>"; + $array = array(); + for($i = 0; $i<1000000;$i++) { + $array[$i] = 1; + } + } + elseif($testId == 2) + { + echo "start indexing by strings<br>"; + $array = array(); + for($i = 0; $i<1000000;$i++) { + $array[$i."_".$i] = 1; + } + } + elseif($testId == 3) + { + echo "start jumping index<br>"; + for($i = 0; $i<1000000;$i++) { + $array[$i*100] = 1; + } + } + echo $timer . "<br>"; + echo "size serialized:". Piwik::getPrettySizeFromBytes(strlen(serialize($array))). "<br>"; + Piwik::printMemoryUsage(); + echo "end ". __FUNCTION__ . "<br>"; + } + function test_versionTrailingZero() { $this->assertTrue(version_compare('0.1','0.01') == 0); @@ -75,9 +120,7 @@ class Test_PHP_Related extends UnitTestCase public function test_staticAttr() { // use this trick to read the static attribute of the class - // $class::$methodsNotToPublish doesn't work $vars = get_class_vars("test_staticAttr"); - $this->assertEqual( $vars['a'], 'testa' ); }