PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/DataArray.php

https://github.com/CodeYellowBV/piwik
PHP | 396 lines | 271 code | 39 blank | 86 comment | 32 complexity | f7ea1c2efcdd0da77a6cf3003c28babc 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;
  10. use Exception;
  11. use Piwik\Tracker\GoalManager;
  12. /**
  13. * The DataArray is a data structure used to aggregate datasets,
  14. * ie. sum arrays made of rows made of columns,
  15. * data from the logs is stored in a DataArray before being converted in a DataTable
  16. *
  17. */
  18. class DataArray
  19. {
  20. protected $data = array();
  21. protected $dataTwoLevels = array();
  22. public function __construct($data = array(), $dataArrayByLabel = array())
  23. {
  24. $this->data = $data;
  25. $this->dataTwoLevels = $dataArrayByLabel;
  26. }
  27. /**
  28. * This returns the actual raw data array
  29. *
  30. * @return array
  31. */
  32. public function &getDataArray()
  33. {
  34. return $this->data;
  35. }
  36. public function getDataArrayWithTwoLevels()
  37. {
  38. return $this->dataTwoLevels;
  39. }
  40. public function sumMetricsVisits($label, $row)
  41. {
  42. if (!isset($this->data[$label])) {
  43. $this->data[$label] = self::makeEmptyRow();
  44. }
  45. $this->doSumVisitsMetrics($row, $this->data[$label]);
  46. }
  47. /**
  48. * Returns an empty row containing default metrics
  49. *
  50. * @return array
  51. */
  52. static public function makeEmptyRow()
  53. {
  54. return array(Metrics::INDEX_NB_UNIQ_VISITORS => 0,
  55. Metrics::INDEX_NB_VISITS => 0,
  56. Metrics::INDEX_NB_ACTIONS => 0,
  57. Metrics::INDEX_MAX_ACTIONS => 0,
  58. Metrics::INDEX_SUM_VISIT_LENGTH => 0,
  59. Metrics::INDEX_BOUNCE_COUNT => 0,
  60. Metrics::INDEX_NB_VISITS_CONVERTED => 0,
  61. );
  62. }
  63. /**
  64. * Adds the given row $newRowToAdd to the existing $oldRowToUpdate passed by reference
  65. * The rows are php arrays Name => value
  66. *
  67. * @param array $newRowToAdd
  68. * @param array $oldRowToUpdate
  69. * @param bool $onlyMetricsAvailableInActionsTable
  70. *
  71. * @return void
  72. */
  73. protected function doSumVisitsMetrics($newRowToAdd, &$oldRowToUpdate, $onlyMetricsAvailableInActionsTable = false)
  74. {
  75. // Pre 1.2 format: string indexed rows are returned from the DB
  76. // Left here for Backward compatibility with plugins doing custom SQL queries using these metrics as string
  77. if (!isset($newRowToAdd[Metrics::INDEX_NB_VISITS])) {
  78. $oldRowToUpdate[Metrics::INDEX_NB_VISITS] += $newRowToAdd['nb_visits'];
  79. $oldRowToUpdate[Metrics::INDEX_NB_ACTIONS] += $newRowToAdd['nb_actions'];
  80. $oldRowToUpdate[Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd['nb_uniq_visitors'];
  81. if ($onlyMetricsAvailableInActionsTable) {
  82. return;
  83. }
  84. $oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS] = (float)max($newRowToAdd['max_actions'], $oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS]);
  85. $oldRowToUpdate[Metrics::INDEX_SUM_VISIT_LENGTH] += $newRowToAdd['sum_visit_length'];
  86. $oldRowToUpdate[Metrics::INDEX_BOUNCE_COUNT] += $newRowToAdd['bounce_count'];
  87. $oldRowToUpdate[Metrics::INDEX_NB_VISITS_CONVERTED] += $newRowToAdd['nb_visits_converted'];
  88. return;
  89. }
  90. $oldRowToUpdate[Metrics::INDEX_NB_VISITS] += $newRowToAdd[Metrics::INDEX_NB_VISITS];
  91. $oldRowToUpdate[Metrics::INDEX_NB_ACTIONS] += $newRowToAdd[Metrics::INDEX_NB_ACTIONS];
  92. $oldRowToUpdate[Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[Metrics::INDEX_NB_UNIQ_VISITORS];
  93. if ($onlyMetricsAvailableInActionsTable) {
  94. return;
  95. }
  96. // In case the existing Row had no action metrics (eg. Custom Variable XYZ with "visit" scope)
  97. // but the new Row has action metrics (eg. same Custom Variable XYZ this time with a "page" scope)
  98. if(!isset($oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS])) {
  99. $toZero = array(Metrics::INDEX_MAX_ACTIONS,
  100. Metrics::INDEX_SUM_VISIT_LENGTH,
  101. Metrics::INDEX_BOUNCE_COUNT,
  102. Metrics::INDEX_NB_VISITS_CONVERTED);
  103. foreach($toZero as $metric) {
  104. $oldRowToUpdate[$metric] = 0;
  105. }
  106. }
  107. $oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS] = (float)max($newRowToAdd[Metrics::INDEX_MAX_ACTIONS], $oldRowToUpdate[Metrics::INDEX_MAX_ACTIONS]);
  108. $oldRowToUpdate[Metrics::INDEX_SUM_VISIT_LENGTH] += $newRowToAdd[Metrics::INDEX_SUM_VISIT_LENGTH];
  109. $oldRowToUpdate[Metrics::INDEX_BOUNCE_COUNT] += $newRowToAdd[Metrics::INDEX_BOUNCE_COUNT];
  110. $oldRowToUpdate[Metrics::INDEX_NB_VISITS_CONVERTED] += $newRowToAdd[Metrics::INDEX_NB_VISITS_CONVERTED];
  111. }
  112. public function sumMetricsGoals($label, $row)
  113. {
  114. $idGoal = $row['idgoal'];
  115. if (!isset($this->data[$label][Metrics::INDEX_GOALS][$idGoal])) {
  116. $this->data[$label][Metrics::INDEX_GOALS][$idGoal] = self::makeEmptyGoalRow($idGoal);
  117. }
  118. $this->doSumGoalsMetrics($row, $this->data[$label][Metrics::INDEX_GOALS][$idGoal]);
  119. }
  120. /**
  121. * @param $idGoal
  122. * @return array
  123. */
  124. protected static function makeEmptyGoalRow($idGoal)
  125. {
  126. if ($idGoal > GoalManager::IDGOAL_ORDER) {
  127. return array(Metrics::INDEX_GOAL_NB_CONVERSIONS => 0,
  128. Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 0,
  129. Metrics::INDEX_GOAL_REVENUE => 0,
  130. );
  131. }
  132. if ($idGoal == GoalManager::IDGOAL_ORDER) {
  133. return array(Metrics::INDEX_GOAL_NB_CONVERSIONS => 0,
  134. Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 0,
  135. Metrics::INDEX_GOAL_REVENUE => 0,
  136. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL => 0,
  137. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX => 0,
  138. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING => 0,
  139. Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT => 0,
  140. Metrics::INDEX_GOAL_ECOMMERCE_ITEMS => 0,
  141. );
  142. }
  143. // idGoal == GoalManager::IDGOAL_CART
  144. return array(Metrics::INDEX_GOAL_NB_CONVERSIONS => 0,
  145. Metrics::INDEX_GOAL_NB_VISITS_CONVERTED => 0,
  146. Metrics::INDEX_GOAL_REVENUE => 0,
  147. Metrics::INDEX_GOAL_ECOMMERCE_ITEMS => 0,
  148. );
  149. }
  150. /**
  151. *
  152. * @param $newRowToAdd
  153. * @param $oldRowToUpdate
  154. */
  155. protected function doSumGoalsMetrics($newRowToAdd, &$oldRowToUpdate)
  156. {
  157. $oldRowToUpdate[Metrics::INDEX_GOAL_NB_CONVERSIONS] += $newRowToAdd[Metrics::INDEX_GOAL_NB_CONVERSIONS];
  158. $oldRowToUpdate[Metrics::INDEX_GOAL_NB_VISITS_CONVERTED] += $newRowToAdd[Metrics::INDEX_GOAL_NB_VISITS_CONVERTED];
  159. $oldRowToUpdate[Metrics::INDEX_GOAL_REVENUE] += $newRowToAdd[Metrics::INDEX_GOAL_REVENUE];
  160. // Cart & Order
  161. if (isset($oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_ITEMS])) {
  162. $oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_ITEMS] += $newRowToAdd[Metrics::INDEX_GOAL_ECOMMERCE_ITEMS];
  163. // Order only
  164. if (isset($oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL])) {
  165. $oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL] += $newRowToAdd[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SUBTOTAL];
  166. $oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX] += $newRowToAdd[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_TAX];
  167. $oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING] += $newRowToAdd[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_SHIPPING];
  168. $oldRowToUpdate[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT] += $newRowToAdd[Metrics::INDEX_GOAL_ECOMMERCE_REVENUE_DISCOUNT];
  169. }
  170. }
  171. }
  172. public function sumMetricsActions($label, $row)
  173. {
  174. if (!isset($this->data[$label])) {
  175. $this->data[$label] = self::makeEmptyActionRow();
  176. }
  177. $this->doSumVisitsMetrics($row, $this->data[$label], $onlyMetricsAvailableInActionsTable = true);
  178. }
  179. static protected function makeEmptyActionRow()
  180. {
  181. return array(
  182. Metrics::INDEX_NB_UNIQ_VISITORS => 0,
  183. Metrics::INDEX_NB_VISITS => 0,
  184. Metrics::INDEX_NB_ACTIONS => 0,
  185. );
  186. }
  187. public function sumMetricsEvents($label, $row)
  188. {
  189. if (!isset($this->data[$label])) {
  190. $this->data[$label] = self::makeEmptyEventRow();
  191. }
  192. $this->doSumEventsMetrics($row, $this->data[$label], $onlyMetricsAvailableInActionsTable = true);
  193. }
  194. static protected function makeEmptyEventRow()
  195. {
  196. return array(
  197. Metrics::INDEX_NB_UNIQ_VISITORS => 0,
  198. Metrics::INDEX_NB_VISITS => 0,
  199. Metrics::INDEX_EVENT_NB_HITS => 0,
  200. Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE => 0,
  201. Metrics::INDEX_EVENT_SUM_EVENT_VALUE => 0,
  202. Metrics::INDEX_EVENT_MIN_EVENT_VALUE => 0,
  203. Metrics::INDEX_EVENT_MAX_EVENT_VALUE => 0,
  204. );
  205. }
  206. const EVENT_VALUE_PRECISION = 2;
  207. /**
  208. * @param array $newRowToAdd
  209. * @param array $oldRowToUpdate
  210. * @return void
  211. */
  212. protected function doSumEventsMetrics($newRowToAdd, &$oldRowToUpdate)
  213. {
  214. $oldRowToUpdate[Metrics::INDEX_NB_VISITS] += $newRowToAdd[Metrics::INDEX_NB_VISITS];
  215. $oldRowToUpdate[Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[Metrics::INDEX_NB_UNIQ_VISITORS];
  216. $oldRowToUpdate[Metrics::INDEX_EVENT_NB_HITS] += $newRowToAdd[Metrics::INDEX_EVENT_NB_HITS];
  217. $oldRowToUpdate[Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE] += $newRowToAdd[Metrics::INDEX_EVENT_NB_HITS_WITH_VALUE];
  218. $newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE] = round($newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE], self::EVENT_VALUE_PRECISION);
  219. $oldRowToUpdate[Metrics::INDEX_EVENT_SUM_EVENT_VALUE] += $newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE];
  220. $oldRowToUpdate[Metrics::INDEX_EVENT_MAX_EVENT_VALUE] = round(max($newRowToAdd[Metrics::INDEX_EVENT_MAX_EVENT_VALUE], $oldRowToUpdate[Metrics::INDEX_EVENT_MAX_EVENT_VALUE]), self::EVENT_VALUE_PRECISION);
  221. // Update minimum only if it is set
  222. if($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] !== false) {
  223. if($oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] === false) {
  224. $oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = round($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE], self::EVENT_VALUE_PRECISION);
  225. } else {
  226. $oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = round(min($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE], $oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE]), self::EVENT_VALUE_PRECISION);
  227. }
  228. }
  229. }
  230. /**
  231. * Generic function that will sum all columns of the given row, at the specified label's row.
  232. *
  233. * @param $label
  234. * @param $row
  235. * @throws Exception if the the data row contains non numeric values
  236. */
  237. public function sumMetrics($label, $row)
  238. {
  239. foreach ($row as $columnName => $columnValue) {
  240. if (empty($columnValue)) {
  241. continue;
  242. }
  243. if (empty($this->data[$label][$columnName])) {
  244. $this->data[$label][$columnName] = 0;
  245. }
  246. if (!is_numeric($columnValue)) {
  247. throw new Exception("DataArray->sumMetricsPivot expects rows of numeric values, non numeric found: " . var_export($columnValue, true) . " for column $columnName");
  248. }
  249. $this->data[$label][$columnName] += $columnValue;
  250. }
  251. }
  252. public function sumMetricsVisitsPivot($parentLabel, $label, $row)
  253. {
  254. if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
  255. $this->dataTwoLevels[$parentLabel][$label] = self::makeEmptyRow();
  256. }
  257. $this->doSumVisitsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
  258. }
  259. public function sumMetricsGoalsPivot($parentLabel, $label, $row)
  260. {
  261. $idGoal = $row['idgoal'];
  262. if (!isset($this->dataTwoLevels[$parentLabel][$label][Metrics::INDEX_GOALS][$idGoal])) {
  263. $this->dataTwoLevels[$parentLabel][$label][Metrics::INDEX_GOALS][$idGoal] = self::makeEmptyGoalRow($idGoal);
  264. }
  265. $this->doSumGoalsMetrics($row, $this->dataTwoLevels[$parentLabel][$label][Metrics::INDEX_GOALS][$idGoal]);
  266. }
  267. public function sumMetricsActionsPivot($parentLabel, $label, $row)
  268. {
  269. if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
  270. $this->dataTwoLevels[$parentLabel][$label] = $this->makeEmptyActionRow();
  271. }
  272. $this->doSumVisitsMetrics($row, $this->dataTwoLevels[$parentLabel][$label], $onlyMetricsAvailableInActionsTable = true);
  273. }
  274. public function sumMetricsEventsPivot($parentLabel, $label, $row)
  275. {
  276. if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
  277. $this->dataTwoLevels[$parentLabel][$label] = $this->makeEmptyEventRow();
  278. }
  279. $this->doSumEventsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
  280. }
  281. public function setRowColumnPivot($parentLabel, $label, $column, $value)
  282. {
  283. $this->dataTwoLevels[$parentLabel][$label][$column] = $value;
  284. }
  285. public function enrichMetricsWithConversions()
  286. {
  287. $this->enrichWithConversions($this->data);
  288. foreach ($this->dataTwoLevels as &$metricsBySubLabel) {
  289. $this->enrichWithConversions($metricsBySubLabel);
  290. }
  291. }
  292. /**
  293. * Given an array of stats, it will process the sum of goal conversions
  294. * and sum of revenue and add it in the stats array in two new fields.
  295. *
  296. * @param array $data Passed by reference, two new columns
  297. * will be added: total conversions, and total revenue, for all goals for this label/row
  298. */
  299. protected function enrichWithConversions(&$data)
  300. {
  301. foreach ($data as $label => &$values) {
  302. if (!isset($values[Metrics::INDEX_GOALS])) {
  303. continue;
  304. }
  305. // When per goal metrics are processed, general 'visits converted' is not meaningful because
  306. // it could differ from the sum of each goal conversions
  307. unset($values[Metrics::INDEX_NB_VISITS_CONVERTED]);
  308. $revenue = $conversions = 0;
  309. foreach ($values[Metrics::INDEX_GOALS] as $idgoal => $goalValues) {
  310. // Do not sum Cart revenue since it is a lost revenue
  311. if ($idgoal >= GoalManager::IDGOAL_ORDER) {
  312. $revenue += $goalValues[Metrics::INDEX_GOAL_REVENUE];
  313. $conversions += $goalValues[Metrics::INDEX_GOAL_NB_CONVERSIONS];
  314. }
  315. }
  316. $values[Metrics::INDEX_NB_CONVERSIONS] = $conversions;
  317. // 25.00 recorded as 25
  318. if (round($revenue) == $revenue) {
  319. $revenue = round($revenue);
  320. }
  321. $values[Metrics::INDEX_REVENUE] = $revenue;
  322. // if there are no "visit" column, we force one to prevent future complications
  323. // eg. This helps the setDefaultColumnsToDisplay() call
  324. if(!isset($values[Metrics::INDEX_NB_VISITS])) {
  325. $values[Metrics::INDEX_NB_VISITS] = 0;
  326. }
  327. }
  328. }
  329. /**
  330. * Returns true if the row looks like an Action metrics row
  331. *
  332. * @param $row
  333. * @return bool
  334. */
  335. static public function isRowActions($row)
  336. {
  337. return (count($row) == count(self::makeEmptyActionRow())) && isset($row[Metrics::INDEX_NB_ACTIONS]);
  338. }
  339. /**
  340. * Converts array to a datatable
  341. *
  342. * @return \Piwik\DataTable
  343. */
  344. public function asDataTable()
  345. {
  346. $dataArray = $this->getDataArray();
  347. $dataArrayTwoLevels = $this->getDataArrayWithTwoLevels();
  348. $subtableByLabel = null;
  349. if (!empty($dataArrayTwoLevels)) {
  350. $subtableByLabel = array();
  351. foreach ($dataArrayTwoLevels as $label => $subTable) {
  352. $subtableByLabel[$label] = DataTable::makeFromIndexedArray($subTable);
  353. }
  354. }
  355. return DataTable::makeFromIndexedArray($dataArray, $subtableByLabel);
  356. }
  357. }