Skip to content

Commit

Permalink
Add config to keep only recent reports (#354)
Browse files Browse the repository at this point in the history
* Add default config.

* Spec out feature.

* Implement.
  • Loading branch information
jesseleite authored Oct 7, 2024
1 parent 23afc52 commit fd6eb1e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/seo-pro.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
],

'reports' => [
'keep_recent' => 'all',
'queue_chunk_size' => 1000,
],

Expand Down
24 changes: 24 additions & 0 deletions src/Reporting/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ public function save()
'results' => $this->results,
]));

static::deleteOldReports();

return $this;
}

Expand Down Expand Up @@ -556,4 +558,26 @@ public function updateLegacyReport()
->fresh()
->withPages();
}

public static function deleteOldReports()
{
$keep = config('statamic.seo-pro.reports.keep_recent');

if (! is_int($keep)) {
return false;
}

$reports = collect(app('files')->directories(storage_path('statamic/seopro/reports')));

$keepReports = $reports
->keyBy(fn ($path) => pathinfo($path)['basename'])
->sortKeysDesc()
->take($keep);

$reports
->reject(fn ($path) => $keepReports->contains($path))
->each(fn ($path) => app('files')->deleteDirectory($path));

return true;
}
}
69 changes: 69 additions & 0 deletions tests/ReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,75 @@ public function it_can_generate_a_report()
$this->assertCount(10, $this->files->allFiles($this->reportsPath('1/pages')));
}

/** @test */
public function it_doesnt_delete_old_reports_when_generating_by_default()
{
$this->generateEntries(1);

$this->assertFileDoesNotExist($this->reportsPath());

Carbon::setTestNow($now = now());

foreach (range(1, 17) as $i) {
Report::create()->save()->generate();
}

$this->assertCount(17, $this->files->directories($this->reportsPath()));
}

/** @test */
public function it_doesnt_delete_old_reports_when_explicit_all_is_configured()
{
Config::set('statamic.seo-pro.reports.keep_recent', 'all');

$this->generateEntries(1);

$this->assertFileDoesNotExist($this->reportsPath());

Carbon::setTestNow($now = now());

foreach (range(1, 17) as $i) {
Report::create()->save()->generate();
}

$this->assertCount(17, $this->files->directories($this->reportsPath()));
}

/** @test */
public function it_can_delete_old_reports_when_generating()
{
Config::set('statamic.seo-pro.reports.keep_recent', 13);

$this->generateEntries(1);

$this->assertFileDoesNotExist($this->reportsPath());

Carbon::setTestNow($now = now());

foreach (range(1, 17) as $i) {
Report::create()->save()->generate();
}

$this->assertCount(13, $this->files->directories($this->reportsPath()));
$this->assertFileDoesNotExist($this->reportsPath('1'));
$this->assertFileDoesNotExist($this->reportsPath('2'));
$this->assertFileDoesNotExist($this->reportsPath('3'));
$this->assertFileDoesNotExist($this->reportsPath('4'));
$this->assertFileExists($this->reportsPath('5'));
$this->assertFileExists($this->reportsPath('6'));
$this->assertFileExists($this->reportsPath('7'));
$this->assertFileExists($this->reportsPath('8'));
$this->assertFileExists($this->reportsPath('9'));
$this->assertFileExists($this->reportsPath('10'));
$this->assertFileExists($this->reportsPath('11'));
$this->assertFileExists($this->reportsPath('12'));
$this->assertFileExists($this->reportsPath('13'));
$this->assertFileExists($this->reportsPath('14'));
$this->assertFileExists($this->reportsPath('15'));
$this->assertFileExists($this->reportsPath('16'));
$this->assertFileExists($this->reportsPath('17'));
}

/** @test */
public function it_properly_calculates_actionable_count()
{
Expand Down

0 comments on commit fd6eb1e

Please sign in to comment.