You can add row metadata with this method.
public addRowsFromArray ( array $array ) | ||
$array | array | Array with the following structure array( // row1 array( Row::COLUMNS => array( col1_name => value1, col2_name => value2, ...), Row::METADATA => array( metadata1_name => value1, ...), // see Row ), // row2 array( ... ), ) |
public function setUp() { $this->currentTable = new DataTable(); $this->currentTable->addRowsFromArray(array(array(Row::COLUMNS => array('label' => 'val1', 'nb_visits' => 120)), array(Row::COLUMNS => array('label' => 'val2', 'nb_visits' => 70)), array(Row::COLUMNS => array('label' => 'val3', 'nb_visits' => 90)), array(Row::COLUMNS => array('label' => 'val4', 'nb_visits' => 99)), array(Row::COLUMNS => array('label' => 'val5', 'nb_visits' => 0)), array(Row::COLUMNS => array('label' => 'val6', 'nb_visits' => 0)), array(Row::COLUMNS => array('label' => 'val7', 'nb_visits' => 134)), array(Row::COLUMNS => array('label' => 'val8', 'nb_visits' => 100)), array(Row::COLUMNS => array('label' => 'val9', 'nb_visits' => 7)), array(Row::COLUMNS => array('label' => 'val10', 'nb_visits' => 89)))); $this->pastTable = new DataTable(); $this->pastTable->addRowsFromArray(array(array(Row::COLUMNS => array('label' => 'val1', 'nb_visits' => 102)), array(Row::COLUMNS => array('label' => 'val102', 'nb_visits' => 29)), array(Row::COLUMNS => array('label' => 'val4', 'nb_visits' => 120)), array(Row::COLUMNS => array('label' => 'val6', 'nb_visits' => 313)), array(Row::COLUMNS => array('label' => 'val109', 'nb_visits' => 0)), array(Row::COLUMNS => array('label' => 'val8', 'nb_visits' => 140)), array(Row::COLUMNS => array('label' => 'val9', 'nb_visits' => 72)), array(Row::COLUMNS => array('label' => 'val107', 'nb_visits' => 415)), array(Row::COLUMNS => array('label' => 'val10', 'nb_visits' => 0)))); $this->table = new DataTable(); }
private function filterTable($withUser = 5) { $dataTable = new DataTable(); $dataTable->addRowsFromArray(array(array(Row::COLUMNS => array('label' => 'val1', Metrics::INDEX_NB_USERS => 0)), array(Row::COLUMNS => array('label' => 'val2')), array(Row::COLUMNS => array('label' => 'val2 5w ö?', Metrics::INDEX_NB_USERS => $withUser)))); $dataTable->filter($this->filter, array($idSite = 1, $period = 'day', $date = 'today')); return $dataTable->getColumn(Metrics::INDEX_NB_USERS); }
public function testRangeCheckOnMetadata() { $table = new DataTable(); $table->addRowsFromArray(array(array(Row::COLUMNS => array('label' => 'foo'), Row::METADATA => array('count' => 5)), array(Row::COLUMNS => array('label' => 'bar'), Row::METADATA => array('count' => 10)), array(Row::COLUMNS => array('label' => 'bar'), Row::METADATA => array('count' => 15)))); $filter = new RangeCheck($table, 'count', 0, 10); $filter->filter($table); $this->assertEquals(array(5, 10, 10), $table->getRowsMetadata('count')); }
public function test_filter() { $dataTable = new DataTable(); $dataTable->addRowsFromArray(array(array(Row::COLUMNS => array('label' => 'val1', 'nb_visits' => 120)), array(Row::COLUMNS => array('nb_visits' => 90)), array(Row::COLUMNS => array('label' => 'val2 5w ö?', 'nb_visits' => 99)), array(Row::COLUMNS => array('label' => Archiver::LABEL_CUSTOM_VALUE_NOT_DEFINED, 'nb_visits' => 99)))); $dataTable->filter($this->filter, array($idDimension = 5)); $metadata = $dataTable->getRowsMetadata('segment'); $expected = array('dimension5==val1', false, 'dimension5==val2+5w+%C3%B6%3F', 'dimension5=='); $this->assertSame($expected, $metadata); }
/** * @group Core */ public function testRangeCheckNormalDataTableNonIntegerValues() { $table = new DataTable(); $table->addRowsFromArray(array(array(Row::COLUMNS => array('label' => 'ask', 'count' => '3')), array(Row::COLUMNS => array('label' => 'nintendo', 'count' => 'test')), array(Row::COLUMNS => array('label' => 'test', 'count' => 0x1232)), array(Row::COLUMNS => array('label' => 'piwik', 'count' => 0x5)), array(Row::COLUMNS => array('label' => 'google', 'count' => '9test')), array(Row::COLUMNS => array('label' => 'yahoo', 'count' => 'test4')))); $filter = new RangeCheck($table, 'count', 3.97, 10); $filter->filter($table); $expectedOrder = array(3.97, 3.97, 10, 5, '9test', 3.97); $this->assertEquals($expectedOrder, $table->getColumn('count')); }
/** * Returns a data table for testing * @return DataTable */ protected function getTable() { $subtableAskPath1 = new DataTable(); $subtableAskPath1->addRowsFromArray(array(array(Row::COLUMNS => array('label' => 'path1-index-page.html')), array(Row::COLUMNS => array('label' => 'another-page')))); $subtableAsk = new DataTable(); $subtableAsk->addRowsFromArray(array(array(Row::COLUMNS => array('label' => 'path1'), Row::DATATABLE_ASSOCIATED => $subtableAskPath1), array(Row::COLUMNS => array('label' => 'index.html')))); $table = new DataTable(); $rows = array(array(Row::COLUMNS => array('label' => 'http://www.ask.com'), Row::DATATABLE_ASSOCIATED => $subtableAsk), array(Row::COLUMNS => array('label' => 'yahoo'))); $table->addRowsFromArray($rows); return $table; }
/** * Test to exclude low population filter * * @group Core */ public function testFilterLowpop1() { $idcol = Row::COLUMNS; $table = new DataTable(); $rows = array(array($idcol => array('label' => 'google', 'nb_visits' => 897)), array($idcol => array('label' => 'ask', 'nb_visits' => -152)), array($idcol => array('label' => 'piwik', 'nb_visits' => 1.5)), array($idcol => array('label' => 'piwik2', 'nb_visits' => 1.4)), array($idcol => array('label' => 'yahoo', 'nb_visits' => 154)), array($idcol => array('label' => 'amazon', 'nb_visits' => 30)), array($idcol => array('label' => '238949', 'nb_visits' => 0)), array($idcol => array('label' => 'Q*(%&*', 'nb_visits' => 1)), array($idcol => array('label' => 'Q*(%&*2', 'nb_visits' => -1.5))); $table->addRowsFromArray($rows); $expectedtable = new DataTable(); $rows = array(array($idcol => array('label' => 'google', 'nb_visits' => 897)), array($idcol => array('label' => 'piwik', 'nb_visits' => 1.5)), array($idcol => array('label' => 'piwik2', 'nb_visits' => 1.4)), array($idcol => array('label' => 'yahoo', 'nb_visits' => 154)), array($idcol => array('label' => 'amazon', 'nb_visits' => 30))); $expectedtable->addRowsFromArray($rows); $filter = new ExcludeLowPopulation($table, 'nb_visits', 1.4); $filter->filter($table); $this->assertTrue(DataTable::isEqual($table, $expectedtable)); }
/** * Test to filter a column with a pattern * * _Pattern * @dataProvider getTestData */ public function testFilterPattern($pattern, $expectedRows) { $table = new DataTable(); $idcol = Row::COLUMNS; $rows = array(array($idcol => array('label' => 'google')), array($idcol => array('label' => 'ask')), array($idcol => array('label' => 'piwik')), array($idcol => array('label' => 'yahoo')), array(Row::METADATA => array('label' => 'amazon')), array($idcol => array('label' => '2389752/47578949')), array($idcol => array('label' => 'Q*(%&*("$&%*(&"$*")"))'))); $table->addRowsFromArray($rows); $rowIds = array_keys($rows); $rowToDelete = array_diff($rowIds, $expectedRows); $expectedtable = clone $table; $expectedtable->deleteRows($rowToDelete); $filteredTable = clone $table; $filteredTable->filter('Pattern', array('label', $pattern)); $this->assertEquals($expectedtable->getRows(), $filteredTable->getRows()); }
/** * DATA OF DATATABLE_ARRAY * ------------------------- */ protected function _getDataTableMapTest() { $array1 = array(array(Row::COLUMNS => array('label' => 'Google', 'nb_uniq_visitors' => 11, 'nb_visits' => 11), Row::METADATA => array('url' => 'http://www.google.com', 'logo' => './plugins/Referrers/images/searchEngines/www.google.com.png')), array(Row::COLUMNS => array('label' => 'Yahoo!', 'nb_uniq_visitors' => 15, 'nb_visits' => 151), Row::METADATA => array('url' => 'http://www.yahoo.com', 'logo' => './plugins/Referrers/images/searchEngines/www.yahoo.com.png'))); $table1 = new DataTable(); $table1->addRowsFromArray($array1); $array2 = array(array(Row::COLUMNS => array('label' => 'Google1©', 'nb_uniq_visitors' => 110, 'nb_visits' => 110), Row::METADATA => array('url' => 'http://www.google.com1', 'logo' => './plugins/Referrers/images/searchEngines/www.google.com.png1')), array(Row::COLUMNS => array('label' => 'Yahoo!1', 'nb_uniq_visitors' => 150, 'nb_visits' => 1510), Row::METADATA => array('url' => 'http://www.yahoo.com1', 'logo' => './plugins/Referrers/images/searchEngines/www.yahoo.com.png1'))); $table2 = new DataTable(); $table2->addRowsFromArray($array2); $table3 = new DataTable(); $table = new DataTable\Map(); $table->setKeyName('testKey'); $table->addTable($table1, 'date1'); $table->addTable($table2, 'date2'); $table->addTable($table3, 'date3'); return $table; }
private function getDataTable() { $dataTable = new DataTable(); $dataTable->addRowsFromArray(array(array(Row::COLUMNS => array('label' => 'ten', 'visits' => 10)), array(Row::COLUMNS => array('label' => '3test', 'visits' => 33)), array(Row::COLUMNS => array('label' => 'ninety', 'visits' => 90)), array(Row::COLUMNS => array('label' => '3test', 'visits' => 65)), array(Row::COLUMNS => array('label' => '3teste', 'visits' => 67)), array(Row::COLUMNS => array('label' => 'hundred', 'visits' => 100)))); return $dataTable; }
protected function _getDataTable2ForTest() { $rows = $this->_getRowsDataTable2ForTest(); $table = new DataTable(); $table->addRowsFromArray($rows); return $table; }
/** * Test to filter a column with a offset, limit 3 * * @group Core */ public function testFilterOffsetLimit3() { $table = new DataTable(); $idcol = Row::COLUMNS; $rows = array(array($idcol => array('label' => 'google')), array($idcol => array('label' => 'ask')), array($idcol => array('label' => 'piwik')), array($idcol => array('label' => 'yahoo')), array($idcol => array('label' => 'amazon')), array($idcol => array('label' => '238975247578949')), array($idcol => array('label' => 'Q*(%&*("$&%*(&"$*")"))'))); $table->addRowsFromArray($rows); $expectedtable = new DataTable(); $filter = new Limit($table, 8, 15); $filter->filter($table); $this->assertEquals(array_values($expectedtable->getRows()), array_values($table->getRows())); }
private function createTestDataTable() { $result = new DataTable(); $result->addRowsFromArray(array(array(Row::COLUMNS => array('label' => 'row1', 'col1' => 1)), array(Row::COLUMNS => array('label' => 'row2', 'col1' => 2)))); return $result; }
private function getRow3Subtable() { $table = new DataTable(); $table->addRowsFromArray(array(new Row(array(Row::COLUMNS => array('label' => 'col 2', 'nb_visits' => 7, 'nb_actions' => 8))), new Row(array(Row::COLUMNS => array('label' => 'col 3', 'nb_visits' => 9, 'nb_actions' => 31))), new Row(array(Row::COLUMNS => array('label' => 'col 4', 'nb_visits' => 32, 'nb_actions' => 33))))); return $table; }
public function matchVisitorsEmail() { $table = new DataTable(); $visitors = Db::fetchAll("\tSELECT DISTINCT idvisitor FROM " . Common::prefixTable("snoopy_visitors")); //$visitors_to_score = \Piwik\API\Request::processRequest('Snoopy.getVisitorEmail', array('idvisitor' => $visitor)); foreach ($visitors as $visitor) { $email = \Piwik\API\Request::processRequest('SnoopyBehavioralScoring.getVisitorEmail', array('idvisitor' => $visitor['idvisitor'])); //var_dump($email->getRows()); $table->addRowsFromArray($email->getRows()); } return $table; }
/** * Test to sort by visit * * @group Core */ public function testFilterSortNumeric() { $idcol = Row::COLUMNS; $table = new DataTable(); $rows = array(array($idcol => array('label' => 'google', 'nb_visits' => 897)), array($idcol => array('label' => 'ask', 'nb_visits' => -152)), array($idcol => array('label' => 'piwik', 'nb_visits' => 1.5)), array($idcol => array('label' => 'yahoo', 'nb_visits' => 154)), array($idcol => array('label' => 'amazon', 'nb_visits' => 30)), array($idcol => array('label' => '238949', 'nb_visits' => 0)), array($idcol => array('label' => 'Q*(%&*', 'nb_visits' => 1))); $table->addRowsFromArray($rows); $expectedtable = new DataTable(); $rows = array(array($idcol => array('label' => 'ask', 'nb_visits' => -152)), array($idcol => array('label' => '238949', 'nb_visits' => 0)), array($idcol => array('label' => 'Q*(%&*', 'nb_visits' => 1)), array($idcol => array('label' => 'piwik', 'nb_visits' => 1.5)), array($idcol => array('label' => 'amazon', 'nb_visits' => 30)), array($idcol => array('label' => 'yahoo', 'nb_visits' => 154)), array($idcol => array('label' => 'google', 'nb_visits' => 897))); $expectedtable->addRowsFromArray($rows); $expectedtableReverse = new DataTable(); $expectedtableReverse->addRowsFromArray(array_reverse($rows)); $filter = new Sort($table, 'nb_visits', 'asc'); $filter->filter($table); $this->assertTrue(DataTable::isEqual($table, $expectedtable)); $filter = new Sort($table, 'nb_visits', 'desc'); $filter->filter($table); $this->assertTrue(DataTable::isEqual($table, $expectedtableReverse)); }
public function test_sortingArrayValues_doesNotError() { $table = new DataTable(); $table->addRowsFromArray(array(array(Row::COLUMNS => array('label' => 'ask', 'count_array' => array(100, 1, 2))), array(Row::COLUMNS => array('label' => 'nintendo', 'count_array' => array(0, 'hello'))), array(Row::COLUMNS => array('label' => 'yahoo', 'count_array' => array(10, 'test'))))); $tableOriginal = clone $table; $filter = new Sort($table, 'count_array', 'desc'); $filter->filter($table); $this->assertTrue(DataTable::isEqual($tableOriginal, $table)); }