diff options
author | mattab <matthieu.aubry@gmail.com> | 2013-06-10 13:00:42 +0400 |
---|---|---|
committer | mattab <matthieu.aubry@gmail.com> | 2013-06-16 12:10:27 +0400 |
commit | c17139710a43b55b5f48377e6281db6ae602957c (patch) | |
tree | 42fda2ab71074d8d273fab1c444a1fec53957df9 /plugins/CustomVariables/Archiver.php | |
parent | 7fdd382b2880f291752e33ec6f6f44a5dbb21dec (diff) |
refactoring / improvements of Archiveprocessing (in progress)
such as removing duplicate code and a lot of refactoring, the code is now much more readable!
Diffstat (limited to 'plugins/CustomVariables/Archiver.php')
-rw-r--r-- | plugins/CustomVariables/Archiver.php | 106 |
1 files changed, 40 insertions, 66 deletions
diff --git a/plugins/CustomVariables/Archiver.php b/plugins/CustomVariables/Archiver.php index f5df35ba38..15cc2fdf23 100644 --- a/plugins/CustomVariables/Archiver.php +++ b/plugins/CustomVariables/Archiver.php @@ -13,8 +13,11 @@ class Piwik_CustomVariables_Archiver extends Piwik_PluginsArchiver { const LABEL_CUSTOM_VALUE_NOT_DEFINED = "Value not defined"; const CUSTOM_VARIABLE_RECORD_NAME = 'CustomVariables_valueByName'; - protected $metricsByKey = array(); - protected $metricsByKeyAndValue = array(); + + /** + * @var Piwik_DataArray + */ + protected $dataArray; protected $maximumRowsInDataTableLevelZero; protected $maximumRowsInSubDataTable; protected $newEmptyRow; @@ -28,15 +31,15 @@ class Piwik_CustomVariables_Archiver extends Piwik_PluginsArchiver public function archiveDay() { + $this->dataArray = new Piwik_DataArray(); + for ($i = 1; $i <= Piwik_Tracker::MAX_CUSTOM_VARIABLES; $i++) { $this->aggregateCustomVariable($i); } $this->removeVisitsMetricsFromActionsAggregate(); - $this->getProcessor()->enrichMetricsWithConversions($this->metricsByKey); - $this->getProcessor()->enrichPivotMetricsWithConversions($this->metricsByKeyAndValue); - - $table = $this->getProcessor()->getDataTableWithSubtablesFromArraysIndexedByLabel($this->metricsByKeyAndValue, $this->metricsByKey); + $this->dataArray->enrichMetricsWithConversions(); + $table = $this->getProcessor()->getDataTableFromDataArray($this->dataArray); $blob = $table->getSerialized( $this->maximumRowsInDataTableLevelZero, $this->maximumRowsInSubDataTable, $columnToSort = Piwik_Archive::INDEX_NB_VISITS @@ -55,21 +58,34 @@ class Piwik_CustomVariables_Archiver extends Piwik_PluginsArchiver $query = $this->getProcessor()->queryVisitsByDimension($dimensions, $where); $this->aggregateFromVisits($query, $keyField, $valueField); - $query = $this->getProcessor()->queryActionsByDimension($dimensions, $where); + // IF we query Custom Variables scope "page" either: Product SKU, Product Name, + // then we also query the "Product page view" price which was possibly recorded. + $additionalSelects = false; + // FIXMEA + if (in_array($slot, array(3,4,5))) { + $additionalSelects = array( $this->getSelectAveragePrice() ); + } + $query = $this->getProcessor()->queryActionsByDimension($dimensions, $where, $additionalSelects); $this->aggregateFromActions($query, $keyField, $valueField); $query = $this->getProcessor()->queryConversionsByDimension($dimensions, $where); $this->aggregateFromConversions($query, $keyField, $valueField); } + protected function getSelectAveragePrice() + { + return Piwik_ArchiveProcessing_Day::getSqlRevenue("AVG(log_link_visit_action.custom_var_v2)") + . " as `" . Piwik_Archive::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED . "`"; + } + protected function aggregateFromVisits($query, $keyField, $valueField) { while ($row = $query->fetch()) { - $value = $row[$valueField]; - $value = $this->cleanCustomVarValue($value); - $key = $row[$keyField]; - $this->aggregateVisit($key, $value, $row); + $value = $this->cleanCustomVarValue($row[$valueField]); + + $this->dataArray->sumMetricsVisits($key, $row); + $this->dataArray->sumMetricsVisitsPivot($key, $value, $row); } } @@ -81,35 +97,18 @@ class Piwik_CustomVariables_Archiver extends Piwik_PluginsArchiver return self::LABEL_CUSTOM_VALUE_NOT_DEFINED; } - protected function aggregateVisit($key, $value, $row) - { - if (!isset($this->metricsByKey[$key])) { - $this->metricsByKey[$key] = $this->getProcessor()->makeEmptyRow(); - } - if (!isset($this->metricsByKeyAndValue[$key][$value])) { - $this->metricsByKeyAndValue[$key][$value] = $this->getProcessor()->makeEmptyRow(); - } - - $this->getProcessor()->sumMetrics($row, $this->metricsByKey[$key]); - $this->getProcessor()->sumMetrics($row, $this->metricsByKeyAndValue[$key][$value]); - } protected function aggregateFromActions($query, $keyField, $valueField) { while ($row = $query->fetch()) { $key = $row[$keyField]; - $value = $row[$valueField]; - $value = $this->cleanCustomVarValue($value); - $this->aggregateAction($key, $value, $row); - } - } + $value = $this->cleanCustomVarValue($row[$valueField]); - protected function aggregateAction($key, $value, $row) - { - $alreadyAggregated = $this->aggregateEcommerceCategories($key, $value, $row); - if (!$alreadyAggregated) { - $this->aggregateActionByKeyAndValue($key, $value, $row); - $this->aggregateActionByKey($key, $row); + $alreadyAggregated = $this->aggregateEcommerceCategories($key, $value, $row); + if (!$alreadyAggregated) { + $this->aggregateActionByKeyAndValue($key, $value, $row); + $this->dataArray->sumMetricsActions($key, $row); + } } } @@ -146,17 +145,14 @@ class Piwik_CustomVariables_Archiver extends Piwik_PluginsArchiver protected function aggregateActionByKeyAndValue($key, $value, $row) { - if (!isset($this->metricsByKeyAndValue[$key][$value])) { - $this->metricsByKeyAndValue[$key][$value] = $this->getProcessor()->makeEmptyActionRow(); - } - $this->getProcessor()->sumMetrics($row, $this->metricsByKeyAndValue[$key][$value], $onlyMetricsAvailableInActionsTable = true); + $this->dataArray->sumMetricsActionsPivot($key, $value, $row); if ($this->isReservedKey($key)) { // Price tracking on Ecommerce product/category pages: // the average is returned from the SQL query so the price is not "summed" like other metrics $index = Piwik_Archive::INDEX_ECOMMERCE_ITEM_PRICE_VIEWED; if (!empty($row[$index])) { - $this->metricsByKeyAndValue[$key][$value][$index] = (float)$row[$index]; + $this->dataArray->setRowColumnPivot($key, $value, $index, (float)$row[$index]); } } } @@ -166,13 +162,6 @@ class Piwik_CustomVariables_Archiver extends Piwik_PluginsArchiver return in_array($key, Piwik_CustomVariables_API::getReservedCustomVariableKeys()); } - protected function aggregateActionByKey($key, $row) - { - if (!isset($this->metricsByKey[$key])) { - $this->metricsByKey[$key] = $this->getProcessor()->makeEmptyActionRow(); - } - $this->getProcessor()->sumMetrics($row, $this->metricsByKey[$key], $onlyMetricsAvailableInActionsTable = true); - } protected function aggregateFromConversions($query, $keyField, $valueField) { @@ -182,32 +171,17 @@ class Piwik_CustomVariables_Archiver extends Piwik_PluginsArchiver while ($row = $query->fetch()) { $key = $row[$keyField]; $value = $this->cleanCustomVarValue($row[$valueField]); - $idGoal = $row['idgoal']; - $this->aggregateConversion($key, $value, $idGoal, $row); - } - } - - protected function aggregateConversion($key, $value, $idGoal, $row) - { - if (!isset($this->metricsByKey[$key][Piwik_Archive::INDEX_GOALS][$idGoal])) { - $this->metricsByKey[$key][Piwik_Archive::INDEX_GOALS][$idGoal] = $this->getProcessor()->makeEmptyGoalRow($idGoal); - } - if (!isset($this->metricsByKeyAndValue[$key][$value][Piwik_Archive::INDEX_GOALS][$idGoal])) { - $this->metricsByKeyAndValue[$key][$value][Piwik_Archive::INDEX_GOALS][$idGoal] = $this->getProcessor()->makeEmptyGoalRow($idGoal); + $this->dataArray->sumMetricsGoals($key, $row); + $this->dataArray->sumMetricsGoalsPivot($key, $value, $row); } - - $this->getProcessor()->sumGoalMetrics($row, $this->metricsByKey[$key][Piwik_Archive::INDEX_GOALS][$idGoal]); - $this->getProcessor()->sumGoalMetrics($row, $this->metricsByKeyAndValue[$key][$value][Piwik_Archive::INDEX_GOALS][$idGoal]); } protected function removeVisitsMetricsFromActionsAggregate() { - $emptyActionRow = $this->getProcessor()->makeEmptyActionRow(); - - foreach ($this->metricsByKey as $key => &$row) { - $isActionRowAggregate = (count($row) == count($emptyActionRow)); + $dataArray = &$this->dataArray->getDataArray(); + foreach ($dataArray as $key => &$row) { if (!self::isReservedKey($key) - && $isActionRowAggregate + && Piwik_DataArray::isRowActions($row) ) { unset($row[Piwik_Archive::INDEX_NB_UNIQ_VISITORS]); unset($row[Piwik_Archive::INDEX_NB_VISITS]); |