ArchivingStatus.php
6 months ago
BlobTableAggregator.php
2 weeks ago
Loader.php
2 weeks ago
LoaderLock.php
2 weeks ago
Parameters.php
2 weeks ago
PluginsArchiver.php
2 weeks ago
PluginsArchiverException.php
2 years ago
Record.php
2 weeks ago
RecordBuilder.php
2 weeks ago
Rules.php
2 weeks ago
BlobTableAggregator.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\ArchiveProcessor; |
| 10 | |
| 11 | use Piwik\Common; |
| 12 | use Piwik\DataTable; |
| 13 | use Piwik\DataTable\Row; |
| 14 | /** |
| 15 | * Internal helper for aggregating blob rows into a DataTable. |
| 16 | * |
| 17 | * @internal |
| 18 | */ |
| 19 | final class BlobTableAggregator |
| 20 | { |
| 21 | /** |
| 22 | * @param iterable<array{name: string, date1: string, date2: string, value: string}> $archiveDataRows |
| 23 | * @param callable(DataTable):void $renameColumnsCallback |
| 24 | * @param callable(array{name: string, date1: string, date2: string, value: string}):bool|null $shouldIncludeRow |
| 25 | * @param callable(string, int):void|null $onMissingParentTable |
| 26 | * @return array{0: DataTable, 1: bool} |
| 27 | */ |
| 28 | public static function aggregateBlobRows(iterable $archiveDataRows, string $recordName, ?array $columnsAggregationOperation, callable $renameColumnsCallback, ?callable $shouldIncludeRow = null, ?callable $onMissingParentTable = null) : array |
| 29 | { |
| 30 | // maps period & subtable ID in database to the Row instance in $result that subtable should be added to |
| 31 | // [$row['date1'].','.$row['date2']][$tableId] = $row in $result |
| 32 | $tableIdToResultRowMapping = []; |
| 33 | $result = new DataTable(); |
| 34 | $hasRows = \false; |
| 35 | if (!empty($columnsAggregationOperation)) { |
| 36 | $result->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnsAggregationOperation); |
| 37 | } |
| 38 | foreach ($archiveDataRows as $archiveDataRow) { |
| 39 | if ($shouldIncludeRow !== null && !$shouldIncludeRow($archiveDataRow)) { |
| 40 | continue; |
| 41 | } |
| 42 | $hasRows = \true; |
| 43 | $period = $archiveDataRow['date1'] . ',' . $archiveDataRow['date2']; |
| 44 | $tableId = $archiveDataRow['name'] === $recordName ? null : self::parseSubtableIdFromBlobName($archiveDataRow['name']); |
| 45 | $blobTable = DataTable::fromSerializedArray($archiveDataRow['value']); |
| 46 | $blobTable->filter(function (DataTable $table) use($renameColumnsCallback) { |
| 47 | $renameColumnsCallback($table); |
| 48 | }); |
| 49 | if ($tableId === null) { |
| 50 | $tableToAddTo = $result; |
| 51 | } elseif (empty($tableIdToResultRowMapping[$period][$tableId])) { |
| 52 | if ($onMissingParentTable !== null) { |
| 53 | $onMissingParentTable($period, $tableId); |
| 54 | } |
| 55 | Common::destroy($blobTable); |
| 56 | continue; |
| 57 | } else { |
| 58 | $rowToAddTo = $tableIdToResultRowMapping[$period][$tableId]; |
| 59 | if (!$rowToAddTo->getIdSubDataTable()) { |
| 60 | $newTable = new DataTable(); |
| 61 | if (!empty($columnsAggregationOperation)) { |
| 62 | $newTable->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnsAggregationOperation); |
| 63 | } |
| 64 | $rowToAddTo->setSubtable($newTable); |
| 65 | } |
| 66 | $tableToAddTo = $rowToAddTo->getSubtable(); |
| 67 | } |
| 68 | $tableToAddTo->addDataTable($blobTable); |
| 69 | foreach ($blobTable->getRows() as $blobTableRow) { |
| 70 | $label = $blobTableRow->getColumn('label'); |
| 71 | $subtableId = $blobTableRow->getIdSubDataTable(); |
| 72 | if (empty($subtableId)) { |
| 73 | continue; |
| 74 | } |
| 75 | $rowToAddTo = $tableToAddTo->getRowFromLabel($label); |
| 76 | if ($rowToAddTo instanceof Row) { |
| 77 | $tableIdToResultRowMapping[$period][$subtableId] = $rowToAddTo; |
| 78 | } |
| 79 | } |
| 80 | Common::destroy($blobTable); |
| 81 | } |
| 82 | return [$result, $hasRows]; |
| 83 | } |
| 84 | public static function parseSubtableIdFromBlobName(string $recordName) : ?int |
| 85 | { |
| 86 | $parts = explode('_', $recordName); |
| 87 | $id = end($parts); |
| 88 | if (!is_numeric($id)) { |
| 89 | return null; |
| 90 | } |
| 91 | return (int) $id; |
| 92 | } |
| 93 | } |
| 94 |