ArchiveInvalidator
1 year ago
ArchiveInvalidator.php
1 year ago
ArchivePurger.php
1 year ago
ArchiveQuery.php
1 year ago
ArchiveQueryFactory.php
1 year ago
ArchiveState.php
2 years ago
Chunk.php
1 year ago
DataCollection.php
1 year ago
DataTableFactory.php
1 year ago
Parameters.php
2 years ago
ArchiveState.php
84 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 | declare (strict_types=1); |
| 10 | namespace Piwik\Archive; |
| 11 | |
| 12 | use Piwik\DataAccess\ArchiveWriter; |
| 13 | use Piwik\DataTable; |
| 14 | use Piwik\Date; |
| 15 | use Piwik\Period\Range; |
| 16 | use Piwik\Site; |
| 17 | class ArchiveState |
| 18 | { |
| 19 | public const COMPLETE = 'complete'; |
| 20 | public const INCOMPLETE = 'incomplete'; |
| 21 | public const INVALIDATED = 'invalidated'; |
| 22 | /** |
| 23 | * @param array{date1: string, date2: string, idsite: string, ts_archived: string} $archiveData |
| 24 | * @param array<string, array<int>> $archiveIds archives ids indexed by period |
| 25 | * @param array<int, array<string, array<int, int>>> $archiveStates archive states indexed by site and period |
| 26 | */ |
| 27 | public function addMetadataToResultCollection(\Piwik\Archive\DataCollection $collection, array $archiveData, array $archiveIds, array $archiveStates) : void |
| 28 | { |
| 29 | $periodsTsArchived = []; |
| 30 | foreach ($archiveData as $archive) { |
| 31 | $idSite = $archive['idsite']; |
| 32 | $period = $archive['date1'] . ',' . $archive['date2']; |
| 33 | $periodsTsArchived[$idSite][$period] = $archive['ts_archived']; |
| 34 | } |
| 35 | foreach ($periodsTsArchived as $idSite => $periods) { |
| 36 | $site = new Site($idSite); |
| 37 | foreach ($periods as $period => $tsArchived) { |
| 38 | $state = $this->checkArchiveStates($site, $period, $archiveIds, $archiveStates); |
| 39 | $range = new Range('day', $period); |
| 40 | $state = $this->checkTsArchived($state, $site, $range, $tsArchived); |
| 41 | if (null === $state) { |
| 42 | // do not set metadata, if no state was determined, |
| 43 | // to avoid generating unexpected default rows |
| 44 | continue; |
| 45 | } |
| 46 | $collection->addMetadata($idSite, $period, DataTable::ARCHIVE_STATE_METADATA_NAME, $state); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | /** |
| 51 | * @param array<string, array<int>> $archiveIds |
| 52 | * @param array<int, array<string, array<int, int>>> $archiveStates |
| 53 | */ |
| 54 | private function checkArchiveStates(Site $site, string $period, array $archiveIds, array $archiveStates) : ?string |
| 55 | { |
| 56 | $idSite = $site->getId(); |
| 57 | $availableStates = array_intersect_key($archiveStates[$idSite][$period] ?? [], array_flip($archiveIds[$period] ?? [])); |
| 58 | if ([] === $availableStates) { |
| 59 | // do not determine state if no archives were used |
| 60 | return null; |
| 61 | } |
| 62 | if (in_array(ArchiveWriter::DONE_INVALIDATED, $availableStates)) { |
| 63 | // archive has been invalidated |
| 64 | return self::INVALIDATED; |
| 65 | } |
| 66 | // all archives not invalidated should be complete |
| 67 | // includes DONE_OK, DONE_OK_TEMPORARY and DONE_PARTIAL |
| 68 | return self::COMPLETE; |
| 69 | } |
| 70 | private function checkTsArchived(?string $state, Site $site, Range $range, string $tsArchived) : ?string |
| 71 | { |
| 72 | if (self::COMPLETE !== $state) { |
| 73 | // only archives detected as complete can be archived before range end |
| 74 | return $state; |
| 75 | } |
| 76 | $rangeEndTimestamp = $range->getDateTimeEnd()->setTimezone($site->getTimezone())->getTimestamp(); |
| 77 | $tsArchivedTimestamp = Date::factory($tsArchived)->getTimestamp(); |
| 78 | if ($tsArchivedTimestamp <= $rangeEndTimestamp) { |
| 79 | return self::INCOMPLETE; |
| 80 | } |
| 81 | return $state; |
| 82 | } |
| 83 | } |
| 84 |