/** * Queries archive tables for data and returns the result. * @param array|string $archiveNames * @param $archiveDataType * @param null|int $idSubtable * @return Archive\DataCollection */ private function get($archiveNames, $archiveDataType, $idSubtable = null) { if (!is_array($archiveNames)) { $archiveNames = array($archiveNames); } // apply idSubtable if ($idSubtable !== null && $idSubtable != self::ID_SUBTABLE_LOAD_ALL_SUBTABLES) { foreach ($archiveNames as &$name) { $name = $this->appendIdsubtable($name, $idSubtable); } } $result = new Archive\DataCollection($archiveNames, $archiveDataType, $this->params->getIdSites(), $this->params->getPeriods(), $defaultRow = null); $archiveIds = $this->getArchiveIds($archiveNames); if (empty($archiveIds)) { return $result; } $loadAllSubtables = $idSubtable == self::ID_SUBTABLE_LOAD_ALL_SUBTABLES; $archiveData = ArchiveSelector::getArchiveData($archiveIds, $archiveNames, $archiveDataType, $loadAllSubtables); foreach ($archiveData as $row) { // values are grouped by idsite (site ID), date1-date2 (date range), then name (field name) $idSite = $row['idsite']; $periodStr = $row['date1'] . "," . $row['date2']; if ($archiveDataType == 'numeric') { $value = $this->formatNumericValue($row['value']); } else { $value = $this->uncompress($row['value']); $result->addMetadata($idSite, $periodStr, 'ts_archived', $row['ts_archived']); } $resultRow =& $result->get($idSite, $periodStr); $resultRow[$row['name']] = $value; } return $result; }
private function assertArchiveBlob(PiwikArchive\DataCollection $dataCollection, $date, $expectedBlob) { $dateIndex = $date . ',' . $date; $dataArrays = $dataCollection->get(1, $dateIndex); if (!empty($expectedBlob) && 0 !== reset($expectedBlob)) { $this->assertNotEmpty($dataArrays['_metadata']['ts_archived']); $dataArrays['_metadata']['ts_archived'] = true; unset($dataArrays['_metadata']); } $this->assertEquals($expectedBlob, $dataArrays); }
/** * @param $data * @return DataTable\Simple */ private function makeFromMetricsArray($data) { $table = new DataTable\Simple(); if (!empty($data)) { $table->setAllTableMetadata(DataCollection::getDataRowMetadata($data)); DataCollection::removeMetadataFromDataRow($data); $table->addRow(new Row(array(Row::COLUMNS => $data))); } else { // if we're querying numeric data, we couldn't find any, and we're only // looking for one metric, add a row w/ one column w/ value 0. this is to // ensure that the PHP renderer outputs 0 when only one column is queried. // w/o this code, an empty array would be created, and other parts of Piwik // would break. if (count($this->dataNames) == 1 && $this->dataType == 'numeric') { $name = reset($this->dataNames); $table->addRow(new Row(array(Row::COLUMNS => array($name => 0)))); } } $result = $table; return $result; }
/** * Queries archive tables for data and returns the result. * @param array|string $archiveNames * @param $archiveDataType * @param null|int $idSubtable * @return Archive\DataCollection */ protected function get($archiveNames, $archiveDataType, $idSubtable = null) { if (!is_array($archiveNames)) { $archiveNames = array($archiveNames); } // apply idSubtable if ($idSubtable !== null && $idSubtable != self::ID_SUBTABLE_LOAD_ALL_SUBTABLES) { // this is also done in ArchiveSelector. It should be actually only done in ArchiveSelector but DataCollection // does require to have the subtableId appended. Needs to be changed in refactoring to have it only in one // place. $dataNames = array(); foreach ($archiveNames as $name) { $dataNames[] = ArchiveSelector::appendIdsubtable($name, $idSubtable); } } else { $dataNames = $archiveNames; } $result = new Archive\DataCollection($dataNames, $archiveDataType, $this->params->getIdSites(), $this->params->getPeriods(), $defaultRow = null); $archiveIds = $this->getArchiveIds($archiveNames); if (empty($archiveIds)) { return $result; } $archiveData = ArchiveSelector::getArchiveData($archiveIds, $archiveNames, $archiveDataType, $idSubtable); $isNumeric = $archiveDataType == 'numeric'; foreach ($archiveData as $row) { // values are grouped by idsite (site ID), date1-date2 (date range), then name (field name) $periodStr = $row['date1'] . ',' . $row['date2']; if ($isNumeric) { $row['value'] = $this->formatNumericValue($row['value']); } else { $result->addMetadata($row['idsite'], $periodStr, DataTable::ARCHIVED_DATE_METADATA_NAME, $row['ts_archived']); } $result->set($row['idsite'], $periodStr, $row['name'], $row['value']); } return $result; }