-
Notifications
You must be signed in to change notification settings - Fork 0
/
Archiver.php
158 lines (133 loc) · 6.11 KB
/
Archiver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* Copyright (C) Piwik PRO - All rights reserved.
*
* Using this code requires that you first get a license from Piwik PRO.
* Unauthorized copying of this file, via any medium is strictly prohibited.
*
* @link http://piwik.pro
*/
namespace Piwik\Plugins\NewspaperReporting;
use Piwik\Config;
use Piwik\DataArray;
use Piwik\Metrics;
/**
* Class Archiver
* @package Piwik\Plugins\NewspaperReporting
*
* Archiver is class processing raw data into ready ro read reports.
* It must implement two methods for aggregating daily reports
* aggregateDayReport() and other for summing daily reports into periods
* like week, month, year or custom range aggregateMultipleReports().
*
* For more detailed information about Archiver please visit Piwik developer guide
* http://developer.piwik.org/api-reference/Piwik/Plugin/Archiver
*/
class Archiver extends \Piwik\Plugin\Archiver
{
/**
* It is a good practice to store your archive names (reports stored in database)
* in Archiver class constants. You can define as many record names as you want
* for your plugin.
*
* Also important thing is that record name must be prefixed with plugin name.
*
* This is only an example record name, so feel free to change it to suit your needs.
*/
const NEWSPAPERREPORTING_PAYWALL_ARCHIVE_RECORD = "NewspaperReporting_paywall_archive_record";
const NEWSPAPERREPORTING_ARTICLE_ARCHIVE_RECORD = "NewspaperReporting_article_archive_record";
const LABEL_CUSTOM_VALUE_NOT_DEFINED = "NewspaperReporting_value_not_defined";
/**
* @var DataArray
*/
protected $paywallDataArray;
/**
* @var DataArray
*/
protected $articleDataArray;
protected $maximumRowsInDataTableLevelZero;
protected $maximumRowsInSubDataTable;
function __construct($processor)
{
parent::__construct($processor);
$this->maximumRowsInDataTableLevelZero = Config::getInstance()->General['datatable_archiving_maximum_rows_custom_variables'];
$this->maximumRowsInSubDataTable = Config::getInstance()->General['datatable_archiving_maximum_rows_subtable_custom_variables'];
}
public function aggregateDayReport()
{
$this->paywallDataArray = new DataArray();
$this->articleDataArray = new DataArray();
$settings = new \Piwik\Plugins\NewspaperReporting\Settings();
$paywallCvarId = $settings->paywallId->getValue();
$articleCvarId = $settings->articleId->getValue();
$this->aggregatePaywallVariables($paywallCvarId, $articleCvarId);
$this->aggregateArticleVariables($articleCvarId);
$paywallTable = $this->paywallDataArray->asDataTable();
$articleTable = $this->articleDataArray->asDataTable();
$paywallBlob = $paywallTable->getSerialized(
$this->maximumRowsInDataTableLevelZero,
$this->maximumRowsInSubDataTable,
$columnToSort = Metrics::INDEX_NB_VISITS
);
$articleBlob = $articleTable->getSerialized(
$this->maximumRowsInDataTableLevelZero,
$this->maximumRowsInSubDataTable,
$columnToSort = Metrics::INDEX_NB_VISITS
);
$this->getProcessor()->insertBlobRecord(self::NEWSPAPERREPORTING_PAYWALL_ARCHIVE_RECORD, $paywallBlob);
$this->getProcessor()->insertBlobRecord(self::NEWSPAPERREPORTING_ARTICLE_ARCHIVE_RECORD, $articleBlob);
}
protected function aggregateArticleVariables($articleSlot)
{
$articleKeyField = "custom_var_k" . $articleSlot;
$articleValueField = "custom_var_v" . $articleSlot;
$articleWhere = "%s.$articleKeyField != ''";
$articleDimensions = array($articleKeyField, $articleValueField);
$articleQuery = $this->getLogAggregator()->queryActionsByDimension($articleDimensions, $articleWhere);
while ($articleRow = $articleQuery->fetch()) {
$articleValue = $this->cleanCustomVarValue($articleRow[$articleValueField]);
$this->articleDataArray->sumMetricsActions($articleValue, $articleRow);
}
}
protected function aggregatePaywallVariables($paywallSlot, $articleSlot)
{
$paywallKeyField = "custom_var_k" . $paywallSlot;
$paywallValueField = "custom_var_v" . $paywallSlot;
$paywallWhere = "%s.$paywallKeyField != ''";
$paywallDimensions = array($paywallKeyField, $paywallValueField, 'idvisit');
$articleKeyField = "custom_var_k" . $articleSlot;
$articleValueField = "custom_var_v" . $articleSlot;
$paywallQuery = $this->getLogAggregator()->queryVisitsByDimension($paywallDimensions, $paywallWhere);
while ($paywallRow = $paywallQuery->fetch()) {
$paywallValue = $this->cleanCustomVarValue($paywallRow[$paywallValueField]);
$this->paywallDataArray->sumMetricsVisits($paywallValue, $paywallRow);
$articleWhere = "%s.{$articleKeyField} != '' AND %s.idvisit = {$paywallRow['idvisit']}";
$articleDimensions = array($articleKeyField, $articleValueField);
$articleQuery = $this->getLogAggregator()->queryActionsByDimension($articleDimensions, $articleWhere);
while ($articleRow = $articleQuery->fetch()) {
$articleValue = $this->cleanCustomVarValue($articleRow[$articleValueField]);
$this->paywallDataArray->sumMetricsActionsPivot($paywallValue, $articleValue, $articleRow);
}
}
}
protected function cleanCustomVarValue($value)
{
if (strlen($value)) {
return $value;
}
return self::LABEL_CUSTOM_VALUE_NOT_DEFINED;
}
public function aggregateMultipleReports()
{
$columnsAggregationOperation = null;
$this->getProcessor()->aggregateDataTableRecords(
array(self::NEWSPAPERREPORTING_PAYWALL_ARCHIVE_RECORD, self::NEWSPAPERREPORTING_ARTICLE_ARCHIVE_RECORD),
$this->maximumRowsInDataTableLevelZero,
$this->maximumRowsInSubDataTable,
$columnToSort = Metrics::INDEX_NB_VISITS,
$columnsAggregationOperation,
$columnsToRenameAfterAggregation = null,
$countRowsRecursive = array()
);
}
}