Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/ExamplePlugin')
-rw-r--r--plugins/ExamplePlugin/API.php16
-rw-r--r--plugins/ExamplePlugin/Archiver.php83
-rw-r--r--plugins/ExamplePlugin/ExamplePlugin.php14
3 files changed, 113 insertions, 0 deletions
diff --git a/plugins/ExamplePlugin/API.php b/plugins/ExamplePlugin/API.php
index 44d67cd80c..88f6249ecb 100644
--- a/plugins/ExamplePlugin/API.php
+++ b/plugins/ExamplePlugin/API.php
@@ -8,6 +8,7 @@
namespace Piwik\Plugins\ExamplePlugin;
+use Piwik\Archive;
use Piwik\DataTable;
use Piwik\DataTable\Row;
@@ -54,4 +55,19 @@ class API extends \Piwik\Plugin\API
return $table;
}
+
+ /**
+ * Returns the example metric we archive in Archiver.php.
+ * @param int $idSite
+ * @param string $period
+ * @param string $date
+ * @param bool|string $segment
+ * @return DataTable
+ */
+ public function getExampleArchivedMetric($idSite, $period, $date, $segment = false)
+ {
+ $archive = Archive::build($idSite, $period, $date, $segment);
+ $dataTable = $archive->getDataTableFromNumeric([Archiver::EXAMPLEPLUGIN_METRIC_NAME, Archiver::EXAMPLEPLUGIN_CONST_METRIC_NAME]);
+ return $dataTable;
+ }
}
diff --git a/plugins/ExamplePlugin/Archiver.php b/plugins/ExamplePlugin/Archiver.php
index f507c6087a..13455ce6eb 100644
--- a/plugins/ExamplePlugin/Archiver.php
+++ b/plugins/ExamplePlugin/Archiver.php
@@ -8,6 +8,13 @@
namespace Piwik\Plugins\ExamplePlugin;
+use Piwik\ArchiveProcessor;
+use Piwik\Container\StaticContainer;
+use Piwik\Date;
+use Piwik\Option;
+use Piwik\Sequence;
+use Psr\Log\LoggerInterface;
+
/**
* Class Archiver
*
@@ -31,6 +38,27 @@ class Archiver extends \Piwik\Plugin\Archiver
* This is only an example record name, so feel free to change it to suit your needs.
*/
const EXAMPLEPLUGIN_ARCHIVE_RECORD = "ExamplePlugin_archive_record";
+ const EXAMPLEPLUGIN_METRIC_NAME = 'ExamplePlugin_example_metric';
+ const EXAMPLEPLUGIN_CONST_METRIC_NAME = 'ExamplePlugin_example_metric2';
+
+ private $daysFrom = '2016-07-08';
+
+ /**
+ * @var string
+ */
+ private $requestedReport = null;
+
+ public function __construct(ArchiveProcessor $processor)
+ {
+ parent::__construct($processor);
+
+ $this->requestedReport = $processor->getParams()->getArchiveOnlyReport();
+ if ($this->requestedReport) {
+ $processor->getParams()->setIsPartialArchive(true);
+ }
+
+ $this->createSequence();
+ }
public function aggregateDayReport()
{
@@ -45,6 +73,25 @@ class Archiver extends \Piwik\Plugin\Archiver
* $visitorReport = $visitorMetrics->getSerialized();
* $this->getProcessor()->insertBlobRecord(self::EXAMPLEPLUGIN_ARCHIVE_RECORD, $visitorReport);
*/
+
+ if ($this->isArchiving(self::EXAMPLEPLUGIN_METRIC_NAME)) {
+ // insert a test numeric metric that is the difference in days between the day we're archiving and
+ // $this->daysFrom.
+ $daysFrom = Date::factory($this->daysFrom);
+ $date = $this->getProcessor()->getParams()->getPeriod()->getDateStart();
+
+ $differenceInSeconds = $daysFrom->getTimestamp() - $date->getTimestamp();
+ $differenceInDays = round($differenceInSeconds / 86400);
+
+ $this->getProcessor()->insertNumericRecord(self::EXAMPLEPLUGIN_METRIC_NAME, $differenceInDays);
+ }
+
+ if ($this->isArchiving(self::EXAMPLEPLUGIN_CONST_METRIC_NAME)) {
+ $archiveCount = $this->incrementArchiveCount();
+ $archiveCount = 50 + $archiveCount;
+ $archiveCount += 5 - ($archiveCount % 5); // round up to nearest 5 multiple to avoid random test failures
+ $this->getProcessor()->insertNumericRecord(self::EXAMPLEPLUGIN_CONST_METRIC_NAME, $archiveCount);
+ }
}
public function aggregateMultipleReports()
@@ -57,5 +104,41 @@ class Archiver extends \Piwik\Plugin\Archiver
*
* $this->getProcessor()->aggregateDataTableRecords(self::EXAMPLEPLUGIN_ARCHIVE_RECORD);
*/
+
+ $reports = [];
+ if ($this->isArchiving(self::EXAMPLEPLUGIN_METRIC_NAME)) {
+ $reports[] = self::EXAMPLEPLUGIN_METRIC_NAME;
+ }
+ if ($this->isArchiving(self::EXAMPLEPLUGIN_CONST_METRIC_NAME)) {
+ $reports[] = self::EXAMPLEPLUGIN_CONST_METRIC_NAME;
+ }
+ $this->getProcessor()->aggregateNumericMetrics($reports);
+ }
+
+ private function incrementArchiveCount()
+ {
+ $sequence = new Sequence('ExamplePlugin_archiveCount');
+ $result = $sequence->getNextId();
+ return $result;
+ }
+
+ private function isArchiving(string $reportName)
+ {
+ return empty($this->requestedReport) || $this->requestedReport == $reportName;
+ }
+
+ private function createSequence()
+ {
+ $sequence = new Sequence('ExamplePlugin_archiveCount');
+ if (!$sequence->exists()) {
+ for ($i = 0; $i < 100; ++$i) {
+ try {
+ $sequence->create();
+ break;
+ } catch (\Exception $ex) {
+ // ignore
+ }
+ }
+ }
}
}
diff --git a/plugins/ExamplePlugin/ExamplePlugin.php b/plugins/ExamplePlugin/ExamplePlugin.php
index f974975c42..8b737f5784 100644
--- a/plugins/ExamplePlugin/ExamplePlugin.php
+++ b/plugins/ExamplePlugin/ExamplePlugin.php
@@ -10,4 +10,18 @@ namespace Piwik\Plugins\ExamplePlugin;
class ExamplePlugin extends \Piwik\Plugin
{
+ public function registerEvents()
+ {
+ return [
+ 'CronArchive.getArchivingAPIMethodForPlugin' => 'getArchivingAPIMethodForPlugin',
+ ];
+ }
+
+ // support archiving just this plugin via core:archive
+ public function getArchivingAPIMethodForPlugin(&$method, $plugin)
+ {
+ if ($plugin == 'ExamplePlugin') {
+ $method = 'ExamplePlugin.getExampleArchivedMetric';
+ }
+ }
}