-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathRecordBuilder.php
More file actions
363 lines (310 loc) · 13.3 KB
/
RecordBuilder.php
File metadata and controls
363 lines (310 loc) · 13.3 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\ArchiveProcessor;
use Piwik\ArchiveProcessor;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Piwik;
/**
* Inherit from this class to define archiving logic for one or more records.
*/
abstract class RecordBuilder
{
/**
* @var int|null
*/
protected $maxRowsInTable;
/**
* @var int|null
*/
protected $maxRowsInSubtable;
/**
* @var string|int|null
*/
protected $columnToSortByBeforeTruncation;
/**
* @var array|null
*/
protected $columnAggregationOps;
/**
* @var array<string|int,string|int>|null
*/
protected $columnToRenameAfterAggregation;
/**
* @param int|null $maxRowsInTable
* @param int|null $maxRowsInSubtable
* @param string|null $columnToSortByBeforeTruncation
* @param array|null $columnAggregationOps
* @param array<string|int,string|int>|null $columnToRenameAfterAggregation
*/
public function __construct(
?int $maxRowsInTable = null,
?int $maxRowsInSubtable = null,
?string $columnToSortByBeforeTruncation = null,
?array $columnAggregationOps = null,
?array $columnToRenameAfterAggregation = null
) {
$this->maxRowsInTable = $maxRowsInTable;
$this->maxRowsInSubtable = $maxRowsInSubtable;
$this->columnToSortByBeforeTruncation = $columnToSortByBeforeTruncation;
$this->columnAggregationOps = $columnAggregationOps;
$this->columnToRenameAfterAggregation = $columnToRenameAfterAggregation;
}
public function isEnabled(ArchiveProcessor $archiveProcessor): bool
{
return true;
}
/**
* Uses the protected `aggregate()` function to build records by aggregating log table data directly, then
* inserts them as archive data.
*
* @param ArchiveProcessor $archiveProcessor
* @return void
*/
public function buildFromLogs(ArchiveProcessor $archiveProcessor): void
{
if (!$this->isEnabled($archiveProcessor)) {
return;
}
$recordsBuilt = $this->getRecordMetadata($archiveProcessor);
$recordMetadataByName = [];
foreach ($recordsBuilt as $recordMetadata) {
if (!($recordMetadata instanceof Record)) {
continue;
}
$recordMetadataByName[$recordMetadata->getName()] = $recordMetadata;
}
$numericRecords = [];
$records = $this->aggregate($archiveProcessor);
foreach ($records as $recordName => $recordValue) {
if (empty($recordMetadataByName[$recordName])) {
if ($recordValue instanceof DataTable) {
Common::destroy($recordValue);
}
continue;
}
if ($recordValue instanceof DataTable) {
$record = $recordMetadataByName[$recordName];
$maxRowsInTable = $record->getMaxRowsInTable() ?? $this->maxRowsInTable;
$maxRowsInSubtable = $record->getMaxRowsInSubtable() ?? $this->maxRowsInSubtable;
$columnToSortByBeforeTruncation = $record->getColumnToSortByBeforeTruncation() ?? $this->columnToSortByBeforeTruncation;
$this->insertBlobRecord($archiveProcessor, $recordName, $recordValue, $maxRowsInTable, $maxRowsInSubtable, $columnToSortByBeforeTruncation);
Common::destroy($recordValue);
} else {
// collect numeric records so we can insert them all at once
$numericRecords[$recordName] = $recordValue;
}
}
unset($records);
if (!empty($numericRecords)) {
$archiveProcessor->insertNumericRecords($numericRecords);
}
}
/**
* Builds records for non-day periods by aggregating day records together, then inserts
* them as archive data.
*
* @param ArchiveProcessor $archiveProcessor
* @return void
*/
public function buildForNonDayPeriod(ArchiveProcessor $archiveProcessor): void
{
if (!$this->isEnabled($archiveProcessor)) {
return;
}
$requestedReports = $archiveProcessor->getParams()->getArchiveOnlyReportAsArray();
$foundRequestedReports = $archiveProcessor->getParams()->getFoundRequestedReports();
$recordsBuilt = $this->getRecordMetadata($archiveProcessor);
$numericRecords = array_filter($recordsBuilt, function (Record $r) {
return $r->getType() == Record::TYPE_NUMERIC;
});
$blobRecords = array_filter($recordsBuilt, function (Record $r) {
return $r->getType() == Record::TYPE_BLOB;
});
$aggregatedCounts = [];
// make sure if there are requested numeric records that depend on blob records, that the blob records will be archived first
foreach ($numericRecords as $record) {
if (
empty($record->getCountOfRecordName())
|| !in_array($record->getName(), $requestedReports)
) {
continue;
}
$dependentRecordName = $record->getCountOfRecordName();
if (!in_array($dependentRecordName, $requestedReports)) {
$requestedReports[] = $dependentRecordName;
}
// we need to aggregate the blob record to get the count, so even if it's found, we must re-aggregate it
// TODO: this could potentially be optimized away, but it would be non-trivial given the current ArchiveProcessor API
$indexInFoundRecords = array_search($dependentRecordName, $foundRequestedReports);
if ($indexInFoundRecords !== false) {
unset($foundRequestedReports[$indexInFoundRecords]);
}
}
foreach ($blobRecords as $record) {
if (
!empty($requestedReports)
&& (!in_array($record->getName(), $requestedReports)
|| in_array($record->getName(), $foundRequestedReports))
) {
continue;
}
$maxRowsInTable = $record->getMaxRowsInTable() ?? $this->maxRowsInTable;
$maxRowsInSubtable = $record->getMaxRowsInSubtable() ?? $this->maxRowsInSubtable;
$columnToSortByBeforeTruncation = $record->getColumnToSortByBeforeTruncation() ?? $this->columnToSortByBeforeTruncation;
$columnToRenameAfterAggregation = $record->getColumnToRenameAfterAggregation() ?? $this->columnToRenameAfterAggregation;
$columnAggregationOps = $record->getBlobColumnAggregationOps() ?? $this->columnAggregationOps;
// only do recursive row counts if there is a numeric record that depends on it
$countRecursiveRows = $countLeafRows = [];
foreach ($numericRecords as $numeric) {
if (
$numeric->getCountOfRecordName() == $record->getName()
) {
if ($numeric->getCountOfRecordNameIsRecursive()) {
$countRecursiveRows[] = $numeric->getCountOfRecordName();
}
if ($numeric->getCountOfRecordNameIsForLeafs()) {
$countLeafRows[] = $numeric->getCountOfRecordName();
}
}
}
$counts = $archiveProcessor->aggregateDataTableRecords(
$record->getName(),
$maxRowsInTable,
$maxRowsInSubtable,
$columnToSortByBeforeTruncation,
$columnAggregationOps,
$columnToRenameAfterAggregation,
$countRecursiveRows,
$countLeafRows
);
$aggregatedCounts = array_merge($aggregatedCounts, $counts);
}
if (!empty($numericRecords)) {
// handle metrics that are aggregated using metric values from child periods
$autoAggregateMetrics = array_filter($numericRecords, function (Record $r) {
return empty($r->getCountOfRecordName());
});
$autoAggregateMetrics = array_map(function (Record $r) {
return $r->getName();
}, $autoAggregateMetrics);
if (!empty($requestedReports)) {
$autoAggregateMetrics = array_filter($autoAggregateMetrics, function ($name) use ($requestedReports, $foundRequestedReports) {
return in_array($name, $requestedReports) && !in_array($name, $foundRequestedReports);
});
}
$autoAggregateMetrics = array_values($autoAggregateMetrics);
if (!empty($autoAggregateMetrics)) {
$archiveProcessor->aggregateNumericMetrics($autoAggregateMetrics, $this->columnAggregationOps);
}
// handle metrics that are set to counts of blob records
$recordCountMetricValues = [];
$recordCountMetrics = array_filter($numericRecords, function (Record $r) {
return !empty($r->getCountOfRecordName());
});
foreach ($recordCountMetrics as $record) {
$dependentRecordName = $record->getCountOfRecordName();
if (empty($aggregatedCounts[$dependentRecordName])) {
continue; // dependent record not archived, so skip this metric
}
$count = $aggregatedCounts[$dependentRecordName];
if ($record->getCountOfRecordNameIsForLeafs()) {
$recordCountMetricValues[$record->getName()] = $count['leafs'];
} elseif ($record->getCountOfRecordNameIsRecursive()) {
$recordCountMetricValues[$record->getName()] = $count['recursive'];
} else {
$recordCountMetricValues[$record->getName()] = $count['level0'];
}
$transform = $record->getMultiplePeriodTransform();
if (!empty($transform)) {
$recordCountMetricValues[$record->getName()] = $transform($recordCountMetricValues[$record->getName()], $count);
}
}
if (!empty($recordCountMetricValues)) {
$archiveProcessor->insertNumericRecords($recordCountMetricValues);
}
}
}
/**
* Returns metadata for records primarily used when aggregating over non-day periods. Every numeric/blob
* record your RecordBuilder creates should have an associated piece of record metadata.
*
* @return Record[]
*/
abstract public function getRecordMetadata(ArchiveProcessor $archiveProcessor): array;
/**
* Derived classes should define this method to aggregate log data for a single day and return the records
* to store indexed by record names.
*
* @return array<string, DataTable|int|float|string> Record values indexed by their record name, eg, `['MyPlugin_MyRecord' => new DataTable()]`
*/
abstract protected function aggregate(ArchiveProcessor $archiveProcessor): array;
protected function insertBlobRecord(
ArchiveProcessor $archiveProcessor,
string $recordName,
DataTable $record,
?int $maxRowsInTable,
?int $maxRowsInSubtable,
?string $columnToSortByBeforeTruncation
): void {
$serialized = $record->getSerialized(
$maxRowsInTable ?: $this->maxRowsInTable,
$maxRowsInSubtable ?: $this->maxRowsInSubtable,
$columnToSortByBeforeTruncation ?: $this->columnToSortByBeforeTruncation
);
$archiveProcessor->insertBlobRecord($recordName, $serialized);
unset($serialized);
}
public function getMaxRowsInTable(): ?int
{
return $this->maxRowsInTable;
}
public function getMaxRowsInSubtable(): ?int
{
return $this->maxRowsInSubtable;
}
public function getColumnToSortByBeforeTruncation(): ?string
{
return $this->columnToSortByBeforeTruncation;
}
public function getPluginName(): string
{
return Piwik::getPluginNameOfMatomoClass(get_class($this));
}
/**
* Returns an extra hint for LogAggregator to add to log aggregation SQL. Can be overridden if you'd
* like the origin hint to have more information.
*
* @return string
*/
public function getQueryOriginHint(): string
{
$recordBuilderName = get_class($this);
$recordBuilderName = explode('\\', $recordBuilderName);
return end($recordBuilderName);
}
/**
* Returns true if at least one of the given reports is handled by this RecordBuilder instance
* when invoked with the given ArchiveProcessor.
*
* @param ArchiveProcessor $archiveProcessor Archiving parameters, like idSite, can influence the list of
* all records a RecordBuilder produces, so it is required here.
* @param string[] $requestedReports The list of requested reports to check for.
* @return bool
*/
public function isBuilderForAtLeastOneOf(ArchiveProcessor $archiveProcessor, array $requestedReports): bool
{
$recordMetadata = $this->getRecordMetadata($archiveProcessor);
foreach ($recordMetadata as $record) {
if (in_array($record->getName(), $requestedReports)) {
return true;
}
}
return false;
}
}