Skip to content

Commit

Permalink
Refs #472 Custom Events Archiver and API
Browse files Browse the repository at this point in the history
  • Loading branch information
mattab committed Apr 9, 2014
1 parent df8fc0f commit c3d7826
Show file tree
Hide file tree
Showing 18 changed files with 3,257 additions and 139 deletions.
3 changes: 3 additions & 0 deletions core/ArchiveProcessor/PluginsArchiver.php
Expand Up @@ -93,6 +93,9 @@ public function callAggregateAllPlugins($visits, $visitsConverted)
/** @var Archiver $archiver */
$archiver = new $archiverClass($this->archiveProcessor);

if(!$archiver->isEnabled()) {
continue;
}
if($this->shouldProcessReportsForPlugin($pluginName)) {
if($this->isSingleSiteDayArchive) {
$archiver->aggregateDayReport();
Expand Down
46 changes: 45 additions & 1 deletion core/DataArray.php
Expand Up @@ -204,6 +204,43 @@ static protected function makeEmptyActionRow()
);
}

public function sumMetricsEvents($label, $row)
{
if (!isset($this->data[$label])) {
$this->data[$label] = self::makeEmptyEventRow();
}
$this->doSumEventsMetrics($row, $this->data[$label], $onlyMetricsAvailableInActionsTable = true);
}

static protected function makeEmptyEventRow()
{
return array(
Metrics::INDEX_NB_UNIQ_VISITORS => 0,
Metrics::INDEX_NB_VISITS => 0,
Metrics::INDEX_EVENT_NB_HITS => 0,
Metrics::INDEX_EVENT_SUM_EVENT_VALUE => 0,
Metrics::INDEX_EVENT_MIN_EVENT_VALUE => 0,
Metrics::INDEX_EVENT_MAX_EVENT_VALUE => 0,
);
}

const EVENT_VALUE_PRECISION = 2;

/**
* @param array $newRowToAdd
* @param array $oldRowToUpdate
* @return void
*/
protected function doSumEventsMetrics($newRowToAdd, &$oldRowToUpdate)
{
$oldRowToUpdate[Metrics::INDEX_NB_VISITS] += $newRowToAdd[Metrics::INDEX_NB_VISITS];
$oldRowToUpdate[Metrics::INDEX_NB_UNIQ_VISITORS] += $newRowToAdd[Metrics::INDEX_NB_UNIQ_VISITORS];
$oldRowToUpdate[Metrics::INDEX_EVENT_NB_HITS] += $newRowToAdd[Metrics::INDEX_EVENT_NB_HITS];
$oldRowToUpdate[Metrics::INDEX_EVENT_SUM_EVENT_VALUE] += round($newRowToAdd[Metrics::INDEX_EVENT_SUM_EVENT_VALUE], self::EVENT_VALUE_PRECISION);
$oldRowToUpdate[Metrics::INDEX_EVENT_MAX_EVENT_VALUE] = (float)max($newRowToAdd[Metrics::INDEX_EVENT_MAX_EVENT_VALUE], $oldRowToUpdate[Metrics::INDEX_EVENT_MAX_EVENT_VALUE]);
$oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE] = (float)min($newRowToAdd[Metrics::INDEX_EVENT_MIN_EVENT_VALUE], $oldRowToUpdate[Metrics::INDEX_EVENT_MIN_EVENT_VALUE]);
}

/**
* Generic function that will sum all columns of the given row, at the specified label's row.
*
Expand Down Expand Up @@ -252,6 +289,14 @@ public function sumMetricsActionsPivot($parentLabel, $label, $row)
$this->doSumVisitsMetrics($row, $this->dataTwoLevels[$parentLabel][$label], $onlyMetricsAvailableInActionsTable = true);
}

public function sumMetricsEventsPivot($parentLabel, $label, $row)
{
if (!isset($this->dataTwoLevels[$parentLabel][$label])) {
$this->dataTwoLevels[$parentLabel][$label] = $this->makeEmptyEventRow();
}
$this->doSumEventsMetrics($row, $this->dataTwoLevels[$parentLabel][$label]);
}

public function setRowColumnPivot($parentLabel, $label, $column, $value)
{
$this->dataTwoLevels[$parentLabel][$label][$column] = $value;
Expand Down Expand Up @@ -336,5 +381,4 @@ public function asDataTable()
}
return DataTable::makeFromIndexedArray($dataArray, $subtableByLabel);
}

}
10 changes: 10 additions & 0 deletions core/Plugin/Archiver.php
Expand Up @@ -120,4 +120,14 @@ protected function getLogAggregator()
{
return $this->getProcessor()->getLogAggregator();
}

/**
* Whether this Archiver should be used or not.
*
* @return bool
*/
public function isEnabled()
{
return true;
}
}
85 changes: 85 additions & 0 deletions plugins/Events/API.php
@@ -0,0 +1,85 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\Events;

use Piwik\Archive;
use Piwik\DataTable\Row;
use Piwik\DataTable;
use Piwik\Metrics;
use Piwik\Piwik;

/**
* Custom Events API
*
* @package Events
* @method static \Piwik\Plugins\Events\API getInstance()
*/
class API extends \Piwik\Plugin\API
{
protected function getDataTable($name, $idSite, $period, $date, $segment, $expanded = false, $idSubtable = null)
{
Piwik::checkUserHasViewAccess($idSite);
$dataTable = Archive::getDataTableFromArchive($name, $idSite, $period, $date, $segment, $expanded, $idSubtable);
$dataTable->filter('Sort', array(Metrics::INDEX_NB_VISITS));
$dataTable->queueFilter('ReplaceColumnNames');
$dataTable->queueFilter('ReplaceSummaryRowLabel');
$dataTable->filter(function (DataTable $table) {
$row = $table->getRowFromLabel(Archiver::EVENT_NAME_NOT_SET);
if($row) {
$row->setColumn('label', Piwik::translate(Archiver::EVENT_NAME_NOT_SET));
}
});
return $dataTable;
}

public function getCategory($idSite, $period, $date, $segment = false, $expanded = false)
{
return $this->getDataTable(Archiver::EVENTS_CATEGORY_ACTION_RECORD_NAME, $idSite, $period, $date, $segment, $expanded);
}

public function getAction($idSite, $period, $date, $segment = false, $expanded = false)
{
return $this->getDataTable(Archiver::EVENTS_ACTION_NAME_RECORD_NAME, $idSite, $period, $date, $segment, $expanded);
}

public function getName($idSite, $period, $date, $segment = false, $expanded = false)
{
return $this->getDataTable(Archiver::EVENTS_NAME_ACTION_RECORD_NAME, $idSite, $period, $date, $segment, $expanded);
}

public function getActionFromCategoryId($idSite, $period, $date, $idSubtable, $segment = false)
{
return $this->getDataTable(Archiver::EVENTS_CATEGORY_ACTION_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $idSubtable);
}

public function getNameFromCategoryId($idSite, $period, $date, $idSubtable, $segment = false)
{
return $this->getDataTable(Archiver::EVENTS_CATEGORY_NAME_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $idSubtable);
}

public function getCategoryFromActionId($idSite, $period, $date, $idSubtable, $segment = false)
{
return $this->getDataTable(Archiver::EVENTS_ACTION_CATEGORY_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $idSubtable);
}

public function getNameFromActionId($idSite, $period, $date, $idSubtable, $segment = false)
{
return $this->getDataTable(Archiver::EVENTS_ACTION_NAME_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $idSubtable);
}

public function getActionFromNameId($idSite, $period, $date, $idSubtable, $segment = false)
{
return $this->getDataTable(Archiver::EVENTS_NAME_ACTION_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $idSubtable);
}

public function getCategoryFromNameId($idSite, $period, $date, $idSubtable, $segment = false)
{
return $this->getDataTable(Archiver::EVENTS_NAME_CATEGORY_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = false, $idSubtable);
}
}

0 comments on commit c3d7826

Please sign in to comment.