Skip to content

Commit

Permalink
Adding to DataArray to handle 3 levels hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Monteiro committed Apr 1, 2020
1 parent e89ebdf commit 6663a72
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions core/DataArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ class DataArray
{
protected $data = array();
protected $dataTwoLevels = array();
protected $dataTreeLevels = array();

public function __construct($data = array(), $dataArrayByLabel = array())
public function __construct($data = array(), $dataArrayByLabel = array(), $dataArrayTree = array())
{
$this->data = $data;
$this->dataTwoLevels = $dataArrayByLabel;
$this->dataTreeLevels = $dataArrayTree;
}

/**
Expand All @@ -44,6 +46,11 @@ public function getDataArrayWithTwoLevels()
return $this->dataTwoLevels;
}

public function getDataArrayWithTreeLevels()
{
return $this->dataTreeLevels;
}

public function sumMetricsVisits($label, $row)
{
if (!isset($this->data[$label])) {
Expand Down Expand Up @@ -329,6 +336,14 @@ public function sumMetricsVisitsPivot($parentLabel, $label, $row)
$this->doSumVisitsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
}

public function sumMetricsVisitsPivotTree($parentLabel, $midLabel, $label, $row)
{
if (!isset($this->dataTreeLevels[$parentLabel][$midLabel][$label])) {
$this->dataTreeLevels[$parentLabel][$midLabel][$label] = static::makeEmptyRow();
}
$this->doSumVisitsMetrics($row, $this->dataTreeLevels[$parentLabel][$midLabel][$label]);
}

public function sumMetricsGoalsPivot($parentLabel, $label, $row)
{
$idGoal = $row['idgoal'];
Expand All @@ -354,18 +369,37 @@ public function sumMetricsEventsPivot($parentLabel, $label, $row)
$this->doSumEventsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
}

public function sumMetricsEventsPivotTree($parentLabel, $midLabel, $label, $row)
{
if (!isset($this->dataTreeLevels[$parentLabel][$midLabel][$label])) {
$this->dataTreeLevels[$parentLabel][$midLabel][$label] = static::makeEmptyRow();
}
$this->doSumEventsMetrics($row, $this->dataTreeLevels[$parentLabel][$midLabel][$label] );
}

public function setRowColumnPivot($parentLabel, $label, $column, $value)
{
$this->dataTwoLevels[$parentLabel][$label][$column] = $value;
}

public function setRowColumnPivotTree($parentLabel, $midLabel, $label, $column, $value)
{
$this->dataTreeLevels[$parentLabel][$midLabel][$label][$column] = $value;
}

public function enrichMetricsWithConversions()
{
$this->enrichWithConversions($this->data);

foreach ($this->dataTwoLevels as &$metricsBySubLabel) {
$this->enrichWithConversions($metricsBySubLabel);
}

foreach ($this->dataTreeLevels as &$metricsByMidLabel) {
foreach ($metricsByMidLabel as &$metricsBySubLabel) {
$this->enrichWithConversions($metricsBySubLabel);
}
}
}

/**
Expand Down Expand Up @@ -426,12 +460,24 @@ public function asDataTable()
{
$dataArray = $this->getDataArray();
$dataArrayTwoLevels = $this->getDataArrayWithTwoLevels();
$dataArrayTreeLevels = $this->getDataArrayWithTreeLevels();

$midTableByLabel = null;
$subtableByLabel = null;

if ( !empty($dataArrayTreeLevels)) {
$midTableByLabel = array();
foreach ($dataArrayTreeLevels as $label => $midTable) {
foreach($midTable as $lastLabel => $lastTable) {
$midTableByLabel[$label][$lastLabel] = DataTable::makeFromIndexedArray($lastTable);
}
}
}

if (!empty($dataArrayTwoLevels)) {
$subtableByLabel = array();
foreach ($dataArrayTwoLevels as $label => $subTable) {
$subtableByLabel[$label] = DataTable::makeFromIndexedArray($subTable);
$subtableByLabel[$label] = DataTable::makeFromIndexedArray($subTable, $midTableByLabel != null && isset($midTableByLabel[$label]) ? $midTableByLabel[$label] : null);
}
}
return DataTable::makeFromIndexedArray($dataArray, $subtableByLabel);
Expand Down

0 comments on commit 6663a72

Please sign in to comment.