PageRenderTime 113ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Archive/DataCollection.php

https://github.com/CodeYellowBV/piwik
PHP | 336 lines | 123 code | 36 blank | 177 comment | 21 complexity | b7e7f58584cebefeacc14e63cf99741b MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Archive;
  10. use Exception;
  11. use Piwik\DataTable;
  12. /**
  13. * This class is used to hold and transform archive data for the Archive class.
  14. *
  15. * Archive data is loaded into an instance of this type, can be indexed by archive
  16. * metadata (such as the site ID, period string, etc.), and can be transformed into
  17. * DataTable and Map instances.
  18. */
  19. class DataCollection
  20. {
  21. const METADATA_CONTAINER_ROW_KEY = '_metadata';
  22. /**
  23. * The archive data, indexed first by site ID and then by period date range. Eg,
  24. *
  25. * array(
  26. * '0' => array(
  27. * array(
  28. * '2012-01-01,2012-01-01' => array(...),
  29. * '2012-01-02,2012-01-02' => array(...),
  30. * )
  31. * ),
  32. * '1' => array(
  33. * array(
  34. * '2012-01-01,2012-01-01' => array(...),
  35. * )
  36. * )
  37. * )
  38. *
  39. * Archive data can be either a numeric value or a serialized string blob. Every
  40. * piece of archive data is associated by it's archive name. For example,
  41. * the array(...) above could look like:
  42. *
  43. * array(
  44. * 'nb_visits' => 1,
  45. * 'nb_actions' => 2
  46. * )
  47. *
  48. * There is a special element '_metadata' in data rows that holds values treated
  49. * as DataTable metadata.
  50. */
  51. private $data = array();
  52. /**
  53. * The whole list of metric/record names that were used in the archive query.
  54. *
  55. * @var array
  56. */
  57. private $dataNames;
  58. /**
  59. * The type of data that was queried for (ie, "blob" or "numeric").
  60. *
  61. * @var string
  62. */
  63. private $dataType;
  64. /**
  65. * The default values to use for each metric/record name that's being queried
  66. * for.
  67. *
  68. * @var array
  69. */
  70. private $defaultRow;
  71. /**
  72. * The list of all site IDs that were queried for.
  73. *
  74. * @var array
  75. */
  76. private $sitesId;
  77. /**
  78. * The list of all periods that were queried for. Each period is associated with
  79. * the period's range string. Eg,
  80. *
  81. * array(
  82. * '2012-01-01,2012-01-31' => new Period(...),
  83. * '2012-02-01,2012-02-28' => new Period(...),
  84. * )
  85. *
  86. * @var \Piwik\Period[]
  87. */
  88. private $periods;
  89. /**
  90. * Constructor.
  91. *
  92. * @param array $dataNames @see $this->dataNames
  93. * @param string $dataType @see $this->dataType
  94. * @param array $sitesId @see $this->sitesId
  95. * @param \Piwik\Period[] $periods @see $this->periods
  96. * @param array $defaultRow @see $this->defaultRow
  97. */
  98. public function __construct($dataNames, $dataType, $sitesId, $periods, $defaultRow = null)
  99. {
  100. $this->dataNames = $dataNames;
  101. $this->dataType = $dataType;
  102. if ($defaultRow === null) {
  103. $defaultRow = array_fill_keys($dataNames, 0);
  104. }
  105. $this->sitesId = $sitesId;
  106. foreach ($periods as $period) {
  107. $this->periods[$period->getRangeString()] = $period;
  108. }
  109. $this->defaultRow = $defaultRow;
  110. }
  111. /**
  112. * Returns a reference to the data for a specific site & period. If there is
  113. * no data for the given site ID & period, it is set to the default row.
  114. *
  115. * @param int $idSite
  116. * @param string $period eg, '2012-01-01,2012-01-31'
  117. */
  118. public function &get($idSite, $period)
  119. {
  120. if (!isset($this->data[$idSite][$period])) {
  121. $this->data[$idSite][$period] = $this->defaultRow;
  122. }
  123. return $this->data[$idSite][$period];
  124. }
  125. /**
  126. * Adds a new metadata to the data for specific site & period. If there is no
  127. * data for the given site ID & period, it is set to the default row.
  128. *
  129. * Note: Site ID and period range string are two special types of metadata. Since
  130. * the data stored in this class is indexed by site & period, this metadata is not
  131. * stored in individual data rows.
  132. *
  133. * @param int $idSite
  134. * @param string $period eg, '2012-01-01,2012-01-31'
  135. * @param string $name The metadata name.
  136. * @param mixed $value The metadata name.
  137. */
  138. public function addMetadata($idSite, $period, $name, $value)
  139. {
  140. $row = & $this->get($idSite, $period);
  141. $row[self::METADATA_CONTAINER_ROW_KEY][$name] = $value;
  142. }
  143. /**
  144. * Returns archive data as an array indexed by metadata.
  145. *
  146. * @param array $resultIndices An array mapping metadata names to pretty labels
  147. * for them. Each archive data row will be indexed
  148. * by the metadata specified here.
  149. *
  150. * Eg, array('site' => 'idSite', 'period' => 'Date')
  151. * @return array
  152. */
  153. public function getIndexedArray($resultIndices)
  154. {
  155. $indexKeys = array_keys($resultIndices);
  156. $result = $this->createOrderedIndex($indexKeys);
  157. foreach ($this->data as $idSite => $rowsByPeriod) {
  158. foreach ($rowsByPeriod as $period => $row) {
  159. // FIXME: This hack works around a strange bug that occurs when getting
  160. // archive IDs through ArchiveProcessing instances. When a table
  161. // does not already exist, for some reason the archive ID for
  162. // today (or from two days ago) will be added to the Archive
  163. // instances list. The Archive instance will then select data
  164. // for periods outside of the requested set.
  165. // working around the bug here, but ideally, we need to figure
  166. // out why incorrect idarchives are being selected.
  167. if (empty($this->periods[$period])) {
  168. continue;
  169. }
  170. $this->putRowInIndex($result, $indexKeys, $row, $idSite, $period);
  171. }
  172. }
  173. return $result;
  174. }
  175. /**
  176. * Returns archive data as a DataTable indexed by metadata. Indexed data will
  177. * be represented by Map instances.
  178. *
  179. * @param array $resultIndices An array mapping metadata names to pretty labels
  180. * for them. Each archive data row will be indexed
  181. * by the metadata specified here.
  182. *
  183. * Eg, array('site' => 'idSite', 'period' => 'Date')
  184. * @return DataTable|DataTable\Map
  185. */
  186. public function getDataTable($resultIndices)
  187. {
  188. $dataTableFactory = new DataTableFactory(
  189. $this->dataNames, $this->dataType, $this->sitesId, $this->periods, $this->defaultRow);
  190. $index = $this->getIndexedArray($resultIndices);
  191. return $dataTableFactory->make($index, $resultIndices);
  192. }
  193. /**
  194. * Returns archive data as a DataTable indexed by metadata. Indexed data will
  195. * be represented by Map instances. Each DataTable will have
  196. * its subtable IDs set.
  197. *
  198. * This function will only work if blob data was loaded and only one record
  199. * was loaded (not including subtables of the record).
  200. *
  201. * @param array $resultIndices An array mapping metadata names to pretty labels
  202. * for them. Each archive data row will be indexed
  203. * by the metadata specified here.
  204. *
  205. * Eg, array('site' => 'idSite', 'period' => 'Date')
  206. * @param int|null $idSubTable The subtable to return.
  207. * @param int|null $depth max depth for subtables.
  208. * @param bool $addMetadataSubTableId Whether to add the DB subtable ID as metadata
  209. * to each datatable, or not.
  210. * @throws Exception
  211. * @return DataTable|DataTable\Map
  212. */
  213. public function getExpandedDataTable($resultIndices, $idSubTable = null, $depth = null, $addMetadataSubTableId = false)
  214. {
  215. if ($this->dataType != 'blob') {
  216. throw new Exception("DataCollection: cannot call getExpandedDataTable with "
  217. . "{$this->dataType} data types. Only works with blob data.");
  218. }
  219. if (count($this->dataNames) !== 1) {
  220. throw new Exception("DataCollection: cannot call getExpandedDataTable with "
  221. . "more than one record.");
  222. }
  223. $dataTableFactory = new DataTableFactory(
  224. $this->dataNames, 'blob', $this->sitesId, $this->periods, $this->defaultRow);
  225. $dataTableFactory->expandDataTable($depth, $addMetadataSubTableId);
  226. $dataTableFactory->useSubtable($idSubTable);
  227. $index = $this->getIndexedArray($resultIndices);
  228. return $dataTableFactory->make($index, $resultIndices);
  229. }
  230. /**
  231. * Returns metadata for a data row.
  232. *
  233. * @param array $data The data row.
  234. * @return array
  235. */
  236. public static function getDataRowMetadata($data)
  237. {
  238. if (isset($data[self::METADATA_CONTAINER_ROW_KEY])) {
  239. return $data[self::METADATA_CONTAINER_ROW_KEY];
  240. } else {
  241. return array();
  242. }
  243. }
  244. /**
  245. * Removes all table metadata from a data row.
  246. *
  247. * @param array $data The data row.
  248. */
  249. public static function removeMetadataFromDataRow(&$data)
  250. {
  251. unset($data[self::METADATA_CONTAINER_ROW_KEY]);
  252. }
  253. /**
  254. * Creates an empty index using a list of metadata names. If the 'site' and/or
  255. * 'period' metadata names are supplied, empty rows are added for every site/period
  256. * that was queried for.
  257. *
  258. * Using this function ensures consistent ordering in the indexed result.
  259. *
  260. * @param array $metadataNamesToIndexBy List of metadata names to index archive data by.
  261. * @return array
  262. */
  263. private function createOrderedIndex($metadataNamesToIndexBy)
  264. {
  265. $result = array();
  266. if (!empty($metadataNamesToIndexBy)) {
  267. $metadataName = array_shift($metadataNamesToIndexBy);
  268. if ($metadataName == DataTableFactory::TABLE_METADATA_SITE_INDEX) {
  269. $indexKeyValues = array_values($this->sitesId);
  270. } else if ($metadataName == DataTableFactory::TABLE_METADATA_PERIOD_INDEX) {
  271. $indexKeyValues = array_keys($this->periods);
  272. }
  273. foreach ($indexKeyValues as $key) {
  274. $result[$key] = $this->createOrderedIndex($metadataNamesToIndexBy);
  275. }
  276. }
  277. return $result;
  278. }
  279. /**
  280. * Puts an archive data row in an index.
  281. */
  282. private function putRowInIndex(&$index, $metadataNamesToIndexBy, $row, $idSite, $period)
  283. {
  284. $currentLevel = & $index;
  285. foreach ($metadataNamesToIndexBy as $metadataName) {
  286. if ($metadataName == DataTableFactory::TABLE_METADATA_SITE_INDEX) {
  287. $key = $idSite;
  288. } else if ($metadataName == DataTableFactory::TABLE_METADATA_PERIOD_INDEX) {
  289. $key = $period;
  290. } else {
  291. $key = $row[self::METADATA_CONTAINER_ROW_KEY][$metadataName];
  292. }
  293. if (!isset($currentLevel[$key])) {
  294. $currentLevel[$key] = array();
  295. }
  296. $currentLevel = & $currentLevel[$key];
  297. }
  298. $currentLevel = $row;
  299. }
  300. }