Skip to content

Commit a43cb15

Browse files
Fix to keep resume import from import_start_time and continue import pending dates, #PG-3704
1 parent db07709 commit a43cb15

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

Commands/ImportGA4Reports.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,21 @@ protected function executeImpl() : int
198198
$output->writeln(LogToSingleFileProcessor::$cliOutputPrefix . "Importing the following date ranges in order: " . $dateRangesText);
199199
// NOTE: date ranges to reimport are handled first, then we go back to the main import (which could be
200200
// continuous)
201+
202+
// If no date ranges to re-import and import reached till startDate, check if futureDates need to be imported or not and update the start and EdnDate
203+
if (count($dateRangesToImport) == 1 && !empty($status['last_day_archived']) && $status['last_day_archived'] === $status['import_range_start'] && $dates[1]->isLater(Date::factory('yesterday')) && !empty($status['import_start_time'])) {
204+
$dateRangesToImport[0][0] = Date::factory($status['import_start_time'])->subDay(1);
205+
$status['do_not_import_latest_dates_first'] = true;
206+
$status['main_import_progress'] = $dateRangesToImport[0][0]->toString();
207+
$importStatus->saveStatus($status);
208+
}
209+
201210
foreach (array_values($dateRangesToImport) as $index => $datesToImport) {
202211
$status = $importStatus->getImportStatus($idSite);
212+
$isDoNotImportLatestDatesFirst = !empty($status['do_not_import_latest_dates_first']);
213+
if ($isDoNotImportLatestDatesFirst) {
214+
$status['last_date_imported'] = null;
215+
}
203216
// can change in the meantime, so we refetch
204217
if (!is_array($datesToImport) || count($datesToImport) != 2) {
205218
$output->writeln(LogToSingleFileProcessor::$cliOutputPrefix . "Found broken entry in date ranges to import (entry #{$index}) with improper type, skipping.");
@@ -218,7 +231,9 @@ protected function executeImpl() : int
218231
if (!empty($lastDateImported) && $isFutureDateImport) {
219232
$startDate = Date::factory($status['future_resume_date']);
220233
} else {
221-
if (!empty($lastDateImported) && Date::factory($lastDateImported)->subDay(1)->isEarlier($endDate)) {
234+
if ($isDoNotImportLatestDatesFirst) {
235+
$startDate = Date::factory($lastDateImported)->addDay(1);
236+
} elseif (!empty($lastDateImported) && Date::factory($lastDateImported)->subDay(1)->isEarlier($endDate)) {
222237
$endDate = Date::factory($lastDateImported)->subDay(1);
223238
}
224239
}
@@ -227,15 +242,15 @@ protected function executeImpl() : int
227242
$importStatus->removeReImportEntry($idSite, $datesToImport);
228243
continue;
229244
}
230-
if ($endDate->isEarlier($startDate)) {
245+
if ($endDate->isEarlier($startDate) && !$isDoNotImportLatestDatesFirst) {
231246
$output->writeln(LogToSingleFileProcessor::$cliOutputPrefix . "(Entry #{$index}) is finished, moving on.");
232247
$importStatus->removeReImportEntry($idSite, $datesToImport);
233248
continue;
234249
}
235250
$output->writeln(LogToSingleFileProcessor::$cliOutputPrefix . "Importing reports for date range {$startDate} - {$endDate} from GA property {$property}.");
236251
try {
237252
$importer->setIsMainImport($isMainImport);
238-
$aborted = $importer->import($idSite, $property, $startDate, $endDate, $lock, '', $streamIds);
253+
$aborted = $importer->import($idSite, $property, $startDate, $endDate, $lock, '', $streamIds, $isDoNotImportLatestDatesFirst);
239254
if ($aborted == -1) {
240255
$shouldFinishImportIfNothingLeft = \false;
241256
}

ImportStatus.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ public function getImportedDateRange($idSite)
7070
}
7171
return $dates;
7272
}
73-
public function dayImportFinished($idSite, Date $date, $isMainImport = \true)
73+
public function dayImportFinished($idSite, Date $date, $isMainImport = \true, $skipRecentDateImportFirst = false)
7474
{
7575
$status = $this->getImportStatus($idSite);
7676
$status['status'] = self::STATUS_ONGOING;
77-
if (empty($status['last_date_imported']) || !Date::factory($status['last_date_imported'])->isEarlier($date) || !empty($status['future_resume_date']) && Date::factory($status['last_date_imported'])->isEarlier($date)) {
77+
if (empty($status['last_date_imported']) || $skipRecentDateImportFirst || !Date::factory($status['last_date_imported'])->isEarlier($date) || !empty($status['future_resume_date']) && Date::factory($status['last_date_imported'])->isEarlier($date)) {
7878
$status['last_date_imported'] = $date->toString();
7979
$this->setImportedDateRange($idSite, $startDate = null, $date);
8080
if ($isMainImport) {

ImporterGA4.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ private function importCustomVariableSlots()
300300
$command .= ' customvariables:set-max-custom-variables ' . $numCustomVarSlots;
301301
passthru($command);
302302
}
303-
public function import($idSite, $propertyId, Date $start, Date $end, Lock $lock, $segment = '', $streamIds = [])
303+
public function import($idSite, $propertyId, Date $start, Date $end, Lock $lock, $segment = '', $streamIds = [], $skipRecentDateImportFirst = false)
304304
{
305305
$date = null;
306306
try {
@@ -314,7 +314,11 @@ public function import($idSite, $propertyId, Date $start, Date $end, Lock $lock,
314314
}
315315
$recordImporters = $this->getRecordImporters($idSite, $propertyId, $streamIds);
316316
$site = new Site($idSite);
317-
$dates = $this->getRecentDatesToImport($start, $endPlusOne, Date::today()->getTimestamp());
317+
if (!$skipRecentDateImportFirst) {
318+
$dates = $this->getRecentDatesToImport($start, $endPlusOne, Date::today()->getTimestamp());
319+
} else {
320+
$dates = $this->getDatesToImport($start, $end);
321+
}
318322
foreach ($dates as $date) {
319323
if ($date->isToday() || $date->isLater(Date::yesterday())) {
320324
$this->logger->info("Encountered Future Date while Importing data for GA4 Property {propertyID} for date {date}, the import would be stopped", ['viewId' => $propertyId, 'date' => $date->toString()]);
@@ -328,7 +332,7 @@ public function import($idSite, $propertyId, Date $start, Date $end, Lock $lock,
328332
// force delete all tables in case they aren't all freed
329333
\Piwik\DataTable\Manager::getInstance()->deleteAll();
330334
}
331-
$this->importStatus->dayImportFinished($idSite, $date, $this->isMainImport);
335+
$this->importStatus->dayImportFinished($idSite, $date, $this->isMainImport, $skipRecentDateImportFirst);
332336
}
333337
$this->importStatus->finishImportIfNothingLeft($idSite);
334338
unset($recordImporters);
@@ -591,4 +595,18 @@ public function getRecentDatesToImport(Date $startDate, Date $endPlusOne, $thres
591595
}
592596
return $dates;
593597
}
598+
599+
/**
600+
* @param Date $startDate
601+
* @param Date $endDate
602+
* @return array of dates between $startDate and $endDate
603+
*/
604+
public function getDatesToImport(Date $startDate, Date $endDate)
605+
{
606+
$dates = [];
607+
for ($date = $startDate; $date->getTimestamp() <= $endDate->getTimestamp(); $date = $date->addDay(1)) {
608+
array_push($dates, $date);
609+
}
610+
return $dates;
611+
}
594612
}

tests/Integration/ImporterTestGA4.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ public function test_getRecentDatesToImport_Past()
6565
}
6666
$this->assertEquals(['2019-07-13', '2019-07-12', '2019-07-11', '2019-07-10', '2019-07-09', '2019-07-08', '2019-07-07'], $processed);
6767
}
68+
public function test_getDatesToImport()
69+
{
70+
$startDate = Date::factory('2022-07-07');
71+
$endDate = Date::factory('2022-07-13');
72+
$dates = $this->importer->getDatesToImport($startDate, $endDate);
73+
$processed = [];
74+
foreach ($dates as $dateObj) {
75+
$processed[] = $dateObj->toString();
76+
}
77+
$this->assertEquals(['2022-07-07', '2022-07-08', '2022-07-09', '2022-07-10', '2022-07-11', '2022-07-12', '2022-07-13'], $processed);
78+
}
6879
public function makeMockService()
6980
{
7081
return new \Piwik\Plugins\GoogleAnalyticsImporter\tests\Integration\MockGoogleServiceAnalytics($this);

0 commit comments

Comments
 (0)