File size: 10,411 Bytes
f8ce8bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?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;

/**
 * @api
 * @since 5.0.0
 */
class Record
{
    public const TYPE_NUMERIC = 'numeric';
    public const TYPE_BLOB = 'blob';

    /**
     * @var string
     */
    private $type;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string|null
     */
    private $plugin = null;

    /**
     * @var string|int
     */
    private $columnToSortByBeforeTruncation;

    /**
     * @var int|null
     */
    private $maxRowsInTable;

    /**
     * @var int|null
     */
    private $maxRowsInSubtable;

    /**
     * @var string|null
     */
    private $countOfRecordName = null;

    /**
     * @var bool
     */
    private $countOfRecordNameIsRecursive = false;

    /**
     * @var bool
     */
    private $countOfRecordNameIsForLeafs = false;

    /**
     * @var array|null
     */
    private $columnToRenameAfterAggregation = null;

    /**
     * @var array|null
     */
    private $blobColumnAggregationOps = null;

    /**
     * @var callable|null
     */
    private $multiplePeriodTransform = null;

    /**
     * @var callable|null
     */
    private $aggregatedRecordTransform = null;

    /**
     * @var string|null
     */
    private $builtFromFlatRecord = null;

    /**
     * @var callable|null
     */
    private $flatToHierarchyPathCallback = null;

    /**
     * @var callable|null
     */
    private $legacyHierarchyToFlatReducerCallback = null;

    public static function make($type, $name)
    {
        $record = new Record();
        $record->setType($type);
        $record->setName($name);
        return $record;
    }

    public function setPlugin(?string $plugin): Record
    {
        $this->plugin = $plugin;
        return $this;
    }

    public function setName(string $name): Record
    {
        if (!preg_match('/^[a-zA-Z0-9_-]+$/', $name)) {
            throw new \Exception('Invalid record name: ' . $name . '. Only alphanumeric characters, hyphens and underscores are allowed.');
        }

        $this->name = $name;
        return $this;
    }

    /**
     * @param int|string $columnToSortByBeforeTruncation
     * @return Record
     */
    public function setColumnToSortByBeforeTruncation($columnToSortByBeforeTruncation)
    {
        $this->columnToSortByBeforeTruncation = $columnToSortByBeforeTruncation;
        return $this;
    }

    public function setMaxRowsInTable(?int $maxRowsInTable): Record
    {
        $this->maxRowsInTable = $maxRowsInTable;
        return $this;
    }

    public function setMaxRowsInSubtable(?int $maxRowsInSubtable): Record
    {
        $this->maxRowsInSubtable = $maxRowsInSubtable;
        return $this;
    }

    public function getPlugin(): ?string
    {
        return $this->plugin;
    }

    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @return int|string
     */
    public function getColumnToSortByBeforeTruncation()
    {
        return $this->columnToSortByBeforeTruncation;
    }

    public function getMaxRowsInTable(): ?int
    {
        return $this->maxRowsInTable;
    }

    public function getMaxRowsInSubtable(): ?int
    {
        return $this->maxRowsInSubtable;
    }

    public function setType(string $type): Record
    {
        $this->type = $type;
        return $this;
    }

    public function getType(): string
    {
        return $this->type;
    }

    public function setIsCountOfBlobRecordRows(string $dependentRecordName, bool $isRecursive = false): Record
    {
        $this->countOfRecordName = $dependentRecordName;
        $this->countOfRecordNameIsRecursive = $isRecursive;
        return $this;
    }

    public function setIsCountOfBlobRecordLeafRows(string $dependentRecordName): Record
    {
        $this->countOfRecordName           = $dependentRecordName;
        $this->countOfRecordNameIsForLeafs = true;
        return $this;
    }

    public function getCountOfRecordName(): ?string
    {
        return $this->countOfRecordName;
    }

    public function getCountOfRecordNameIsRecursive(): bool
    {
        return $this->countOfRecordNameIsRecursive;
    }

    public function getCountOfRecordNameIsForLeafs(): bool
    {
        return $this->countOfRecordNameIsForLeafs;
    }

    /**
     * @param array|null $columnToRenameAfterAggregation
     */
    public function setColumnToRenameAfterAggregation(?array $columnToRenameAfterAggregation): Record
    {
        $this->columnToRenameAfterAggregation = $columnToRenameAfterAggregation;
        return $this;
    }

    /**
     * @return array|null
     */
    public function getColumnToRenameAfterAggregation(): ?array
    {
        return $this->columnToRenameAfterAggregation;
    }

    /**
     * @param array|null $blobColumnAggregationOps
     */
    public function setBlobColumnAggregationOps(?array $blobColumnAggregationOps): Record
    {
        $this->blobColumnAggregationOps = $blobColumnAggregationOps;
        return $this;
    }

    /**
     * @return array|null
     */
    public function getBlobColumnAggregationOps(): ?array
    {
        return $this->blobColumnAggregationOps;
    }

    public function setMultiplePeriodTransform(?callable $multiplePeriodTransform): Record
    {
        $this->multiplePeriodTransform = $multiplePeriodTransform;
        return $this;
    }

    public function getMultiplePeriodTransform(): ?callable
    {
        return $this->multiplePeriodTransform;
    }

    /**
     * Sets a transform applied to this blob record's aggregated table during non-day archiving,
     * after the day blobs have been aggregated together (additive columns summed, columns marked
     * 'skip' in the aggregation ops left untouched) and before the table is truncated and stored.
     *
     * Use this for columns that cannot be summed across child periods and must be recomputed from
     * the aggregated additive columns — for example a table-relative ratio, index or score. Mark
     * such a column 'skip' via {@see setBlobColumnAggregationOps()} so it is not summed, then
     * recompute it here. Because the transform runs before truncation, a column it (re)computes can
     * be used as {@see setColumnToSortByBeforeTruncation()}.
     *
     * Only used for non-day periods; the day archive builds the record from logs via the
     * RecordBuilder's aggregate() and should apply any equivalent computation there.
     *
     * Applies on both the standard blob path and the built-from-flat path ({@see setBuiltFromFlatRecord()}):
     * each record's transform runs on that record's own aggregated table, so a flat base record and the
     * hierarchy rebuilt from it are each transformed (the hierarchy after it is built) before being stored.
     *
     * @param callable|null $transform Signature:
     *                                 function (\Piwik\DataTable $table, ArchiveProcessor $archiveProcessor, Record $record): void
     *                                 The callback mutates $table in place.
     */
    public function setAggregatedRecordTransform(?callable $transform): Record
    {
        if (null !== $transform && $this->type !== self::TYPE_BLOB) {
            throw new \InvalidArgumentException('setAggregatedRecordTransform() can only be used with blob records.');
        }

        $this->aggregatedRecordTransform = $transform;
        return $this;
    }

    public function getAggregatedRecordTransform(): ?callable
    {
        return $this->aggregatedRecordTransform;
    }

    /**
     * Marks this blob record as being derived from a flat blob record during non-day aggregation.
     *
     * Use this when day archives store a flat representation and non-day archives should rebuild
     * hierarchy from it. The flat record must be present in getRecordMetadata().
     *
     * @param string $flatRecordName Name of the flat blob record to aggregate first.
     * @param callable $flatToHierarchyPathCallback Callback used when rebuilding hierarchy.
     *                                              Signature: function (Row $flatRow, ArchiveProcessor $archiveProcessor, Record $hierarchicalRecord): ?array
     *                                              Return value is the path of labels to map the flat row into the hierarchy.
     * @param callable|null $legacyHierarchyToFlatReducerCallback Optional callback that can merge legacy hierarchical
     *                                                            aggregates into the flat table when some periods do not
     *                                                            have the flat record yet.
     *                                                            Signature: function (DataTable $legacyHierarchy, DataTable $flatTable, ArchiveProcessor $archiveProcessor, Record $hierarchicalRecord): void
     *                                                            The callback is invoked once per legacy source period hierarchy table.
     */
    public function setBuiltFromFlatRecord(
        string $flatRecordName,
        callable $flatToHierarchyPathCallback,
        ?callable $legacyHierarchyToFlatReducerCallback = null
    ): Record {
        if ($this->type !== self::TYPE_BLOB) {
            throw new \InvalidArgumentException('setBuiltFromFlatRecord() can only be used with blob records.');
        }

        if (!preg_match('/^[a-zA-Z0-9_-]+$/', $flatRecordName)) {
            throw new \InvalidArgumentException('Invalid flat record name: ' . $flatRecordName);
        }

        $this->builtFromFlatRecord = $flatRecordName;
        $this->flatToHierarchyPathCallback = $flatToHierarchyPathCallback;
        $this->legacyHierarchyToFlatReducerCallback = $legacyHierarchyToFlatReducerCallback;

        return $this;
    }

    public function getBuiltFromFlatRecord(): ?string
    {
        return $this->builtFromFlatRecord;
    }

    /**
     * @see setBuiltFromFlatRecord()
     */
    public function getFlatToHierarchyPathCallback(): ?callable
    {
        return $this->flatToHierarchyPathCallback;
    }

    /**
     * @see setBuiltFromFlatRecord()
     */
    public function getLegacyHierarchyToFlatReducerCallback(): ?callable
    {
        return $this->legacyHierarchyToFlatReducerCallback;
    }
}