|
12 | 12 | use Piwik\Archive; |
13 | 13 | use Piwik\ArchiveProcessor; |
14 | 14 | use Piwik\Common; |
| 15 | +use Piwik\Context; |
15 | 16 | use Piwik\DataTable; |
16 | 17 | use Piwik\DataTable\Row; |
17 | 18 | use Piwik\Piwik; |
@@ -149,6 +150,19 @@ public function buildForNonDayPeriod(ArchiveProcessor $archiveProcessor): void |
149 | 150 | $blobRecordsByName[$blobRecord->getName()] = $blobRecord; |
150 | 151 | } |
151 | 152 |
|
| 153 | + foreach ($blobRecords as $record) { |
| 154 | + $flatRecordName = $record->getBuiltFromFlatRecord(); |
| 155 | + if ( |
| 156 | + empty($flatRecordName) |
| 157 | + || !in_array($record->getName(), $requestedReports) |
| 158 | + || in_array($flatRecordName, $requestedReports) |
| 159 | + ) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + |
| 163 | + $requestedReports[] = $flatRecordName; |
| 164 | + } |
| 165 | + |
152 | 166 | $aggregatedCounts = []; |
153 | 167 |
|
154 | 168 | // make sure if there are requested numeric records that depend on blob records, that the blob records will be archived first |
@@ -483,80 +497,90 @@ protected function aggregateDataTableFromBlobs( |
483 | 497 | ?array $columnsToRenameAfterAggregation, |
484 | 498 | ?array $periodsToInclude = null |
485 | 499 | ): array { |
486 | | - $tableIdToResultRowMapping = []; |
487 | | - $result = new DataTable(); |
488 | | - |
489 | | - if (!empty($columnsAggregationOperation)) { |
490 | | - $result->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnsAggregationOperation); |
491 | | - } |
492 | | - |
493 | | - $hasRows = false; |
494 | | - foreach ($this->querySingleBlobRows($archiveProcessor, $recordName) as $archiveDataRow) { |
495 | | - $period = $archiveDataRow['date1'] . ',' . $archiveDataRow['date2']; |
496 | | - if ($periodsToInclude !== null && !isset($periodsToInclude[$period])) { |
497 | | - continue; |
| 500 | + return $this->executeWithoutRequestedReport(function () use ( |
| 501 | + $archiveProcessor, |
| 502 | + $recordName, |
| 503 | + $columnsAggregationOperation, |
| 504 | + $columnsToRenameAfterAggregation, |
| 505 | + $periodsToInclude |
| 506 | + ) { |
| 507 | + $tableIdToResultRowMapping = []; |
| 508 | + $result = new DataTable(); |
| 509 | + |
| 510 | + if (!empty($columnsAggregationOperation)) { |
| 511 | + $result->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnsAggregationOperation); |
498 | 512 | } |
499 | 513 |
|
500 | | - $hasRows = true; |
501 | | - $tableId = $archiveDataRow['name'] == $recordName ? null : $this->getSubtableIdFromBlobName($archiveDataRow['name']); |
| 514 | + $hasRows = false; |
| 515 | + foreach ($this->querySingleBlobRows($archiveProcessor, $recordName) as $archiveDataRow) { |
| 516 | + $period = $archiveDataRow['date1'] . ',' . $archiveDataRow['date2']; |
| 517 | + if ($periodsToInclude !== null && !isset($periodsToInclude[$period])) { |
| 518 | + continue; |
| 519 | + } |
| 520 | + |
| 521 | + $hasRows = true; |
| 522 | + $tableId = $archiveDataRow['name'] == $recordName ? null : $this->getSubtableIdFromBlobName($archiveDataRow['name']); |
502 | 523 |
|
503 | | - $blobTable = DataTable::fromSerializedArray($archiveDataRow['value']); |
504 | | - $blobTable->filter(function (DataTable $table) use ($archiveProcessor, $columnsToRenameAfterAggregation) { |
505 | | - $archiveProcessor->renameColumnsAfterAggregation($table, $columnsToRenameAfterAggregation); |
506 | | - }); |
| 524 | + $blobTable = DataTable::fromSerializedArray($archiveDataRow['value']); |
| 525 | + $blobTable->filter(function (DataTable $table) use ($archiveProcessor, $columnsToRenameAfterAggregation) { |
| 526 | + $archiveProcessor->renameColumnsAfterAggregation($table, $columnsToRenameAfterAggregation); |
| 527 | + }); |
507 | 528 |
|
508 | | - if ($tableId === null) { |
509 | | - $tableToAddTo = $result; |
510 | | - } elseif (!empty($tableIdToResultRowMapping[$period][$tableId])) { |
511 | | - $rowToAddTo = $tableIdToResultRowMapping[$period][$tableId]; |
512 | | - if (!$rowToAddTo->getIdSubDataTable()) { |
513 | | - $newTable = new DataTable(); |
514 | | - if (!empty($columnsAggregationOperation)) { |
515 | | - $newTable->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnsAggregationOperation); |
| 529 | + if ($tableId === null) { |
| 530 | + $tableToAddTo = $result; |
| 531 | + } elseif (!empty($tableIdToResultRowMapping[$period][$tableId])) { |
| 532 | + $rowToAddTo = $tableIdToResultRowMapping[$period][$tableId]; |
| 533 | + if (!$rowToAddTo->getIdSubDataTable()) { |
| 534 | + $newTable = new DataTable(); |
| 535 | + if (!empty($columnsAggregationOperation)) { |
| 536 | + $newTable->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnsAggregationOperation); |
| 537 | + } |
| 538 | + $rowToAddTo->setSubtable($newTable); |
516 | 539 | } |
517 | | - $rowToAddTo->setSubtable($newTable); |
| 540 | + |
| 541 | + $tableToAddTo = $rowToAddTo->getSubtable(); |
| 542 | + } else { |
| 543 | + Common::destroy($blobTable); |
| 544 | + continue; |
518 | 545 | } |
519 | 546 |
|
520 | | - $tableToAddTo = $rowToAddTo->getSubtable(); |
521 | | - } else { |
522 | | - Common::destroy($blobTable); |
523 | | - continue; |
524 | | - } |
| 547 | + $tableToAddTo->addDataTable($blobTable); |
525 | 548 |
|
526 | | - $tableToAddTo->addDataTable($blobTable); |
| 549 | + foreach ($blobTable->getRows() as $blobTableRow) { |
| 550 | + $label = $blobTableRow->getColumn('label'); |
| 551 | + $subtableId = $blobTableRow->getIdSubDataTable(); |
| 552 | + if (empty($subtableId)) { |
| 553 | + continue; |
| 554 | + } |
527 | 555 |
|
528 | | - foreach ($blobTable->getRows() as $blobTableRow) { |
529 | | - $label = $blobTableRow->getColumn('label'); |
530 | | - $subtableId = $blobTableRow->getIdSubDataTable(); |
531 | | - if (empty($subtableId)) { |
532 | | - continue; |
| 556 | + $rowToAddTo = $tableToAddTo->getRowFromLabel($label); |
| 557 | + if ($rowToAddTo instanceof Row) { |
| 558 | + $tableIdToResultRowMapping[$period][$subtableId] = $rowToAddTo; |
| 559 | + } |
533 | 560 | } |
534 | 561 |
|
535 | | - $rowToAddTo = $tableToAddTo->getRowFromLabel($label); |
536 | | - if ($rowToAddTo instanceof Row) { |
537 | | - $tableIdToResultRowMapping[$period][$subtableId] = $rowToAddTo; |
538 | | - } |
| 562 | + Common::destroy($blobTable); |
539 | 563 | } |
540 | 564 |
|
541 | | - Common::destroy($blobTable); |
542 | | - } |
543 | | - |
544 | | - return [$result, $hasRows]; |
| 565 | + return [$result, $hasRows]; |
| 566 | + }); |
545 | 567 | } |
546 | 568 |
|
547 | 569 | protected function getPeriodsWithRootBlob(ArchiveProcessor $archiveProcessor, string $recordName): array |
548 | 570 | { |
549 | | - $result = []; |
550 | | - foreach ($this->querySingleBlobRows($archiveProcessor, $recordName) as $archiveDataRow) { |
551 | | - if ($archiveDataRow['name'] !== $recordName) { |
552 | | - continue; |
553 | | - } |
| 571 | + return $this->executeWithoutRequestedReport(function () use ($archiveProcessor, $recordName) { |
| 572 | + $result = []; |
| 573 | + foreach ($this->querySingleBlobRows($archiveProcessor, $recordName) as $archiveDataRow) { |
| 574 | + if ($archiveDataRow['name'] !== $recordName) { |
| 575 | + continue; |
| 576 | + } |
554 | 577 |
|
555 | | - $period = $archiveDataRow['date1'] . ',' . $archiveDataRow['date2']; |
556 | | - $result[$period] = true; |
557 | | - } |
| 578 | + $period = $archiveDataRow['date1'] . ',' . $archiveDataRow['date2']; |
| 579 | + $result[$period] = true; |
| 580 | + } |
558 | 581 |
|
559 | | - return $result; |
| 582 | + return $result; |
| 583 | + }); |
560 | 584 | } |
561 | 585 |
|
562 | 586 | protected function querySingleBlobRows(ArchiveProcessor $archiveProcessor, string $recordName): iterable |
@@ -595,6 +619,16 @@ protected function getSubtableIdFromBlobName(string $recordName): ?int |
595 | 619 | return (int) $id; |
596 | 620 | } |
597 | 621 |
|
| 622 | + /** |
| 623 | + * @template T |
| 624 | + * @param callable(): T $callback |
| 625 | + * @return T |
| 626 | + */ |
| 627 | + protected function executeWithoutRequestedReport(callable $callback) |
| 628 | + { |
| 629 | + return Context::executeWithQueryParameters(['requestedReport' => ''], $callback); |
| 630 | + } |
| 631 | + |
598 | 632 | /** |
599 | 633 | * Returns metadata for records primarily used when aggregating over non-day periods. Every numeric/blob |
600 | 634 | * record your RecordBuilder creates should have an associated piece of record metadata. |
|
0 commit comments