[Done] Changed all the model in order to use EndDate and Offset. Pass ProjectTests.
This commit is contained in:
parent
944b968998
commit
76d4babd98
|
|
@ -75,13 +75,7 @@ public function getMosaicPath()
|
|||
public function getMosaicList(): Collection
|
||||
{
|
||||
return collect(Storage::files($this->getMosaicPath()))
|
||||
->filter(fn($file) => Str::endsWith($file, '.tif'))
|
||||
->sortByDesc(function ($file) {
|
||||
$parts = explode('_', str_replace('.tif', '', $file));
|
||||
$week = $parts[1];
|
||||
$year = $parts[2];
|
||||
return $year.sprintf('%02d', $week);
|
||||
})
|
||||
->filter(fn($filename) => Str::endsWith($filename, '.tif'))
|
||||
->values();
|
||||
}
|
||||
|
||||
|
|
@ -136,10 +130,10 @@ public function allMosaicsPresent(Carbon $endDate): bool
|
|||
{
|
||||
// end date is in the future
|
||||
if ($endDate->isFuture()) {
|
||||
throw new \Exception('Mosaic can be generated for the future. Change the end date.');
|
||||
throw new \Exception('Mosaic can\'t be generated for the future. Change the end date.');
|
||||
}
|
||||
|
||||
$mosaicsNotPresentInFilesystem = $this->getMosaicFilenameListByEndDate($endDate)
|
||||
$mosaicsNotPresentInFilesystem = collect($this->getMosaicFilenameListByEndDate($endDate))
|
||||
->filter(function ($filename) {
|
||||
return !$this->getMosaicList()->contains(function ($mosaicFilename) use ($filename) {
|
||||
// TODO check the value of the week leading 0
|
||||
|
|
@ -156,21 +150,11 @@ public function allMosaicsPresent(Carbon $endDate): bool
|
|||
throw new \Exception($message);
|
||||
}
|
||||
|
||||
public static function getMosaicFilenameListByEndDate(Carbon $endDate): Collection
|
||||
public static function getMosaicFilenameListByEndDate(Carbon $endDate, int $offset=7): \Generator
|
||||
{
|
||||
$result = collect([]);
|
||||
for ($i = 0; $i < 4; $i++) {
|
||||
$week = $endDate->weekOfYear;
|
||||
$year = $endDate->year;
|
||||
if ($week === 53) {
|
||||
$year--;
|
||||
yield(ProjectMosaic::getFilenameByPeriod($endDate->copy()->subDays($offset*$i),$offset));
|
||||
}
|
||||
|
||||
$result->add(sprintf('week_%02d_%04d.tif', $week, $year));
|
||||
|
||||
$endDate = $endDate->subWeek();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getMosiacList()
|
||||
|
|
@ -298,8 +282,8 @@ public function scheduleReport(?Carbon $endDate = null, ?int $offset = null)
|
|||
$offset = null;
|
||||
}
|
||||
|
||||
$endDate = $year ?? Carbon::yesterday();
|
||||
$offset = $week ?? 1;
|
||||
$endDate ??= Carbon::yesterday();
|
||||
$offset ??= 7;
|
||||
Bus::chain([
|
||||
Bus::batch($this->getFileDownloadsFor($endDate, $offset)),
|
||||
Bus::batch($this->getMosaicsFor($endDate, $offset)),
|
||||
|
|
@ -309,7 +293,7 @@ public function scheduleReport(?Carbon $endDate = null, ?int $offset = null)
|
|||
return "done";
|
||||
}
|
||||
|
||||
public function getReportFor(Carbon $endDate, $offset, $sendMail = false): ProjectReportGeneratorJob
|
||||
public function getReportFor(Carbon $endDate, int $offset = 7, $sendMail = false): ProjectReportGeneratorJob
|
||||
{
|
||||
$report = $this->reports()->create([
|
||||
'name' => 'Report of the '.$endDate->format('d-m-Y').' from the past '.$offset.' days',
|
||||
|
|
@ -321,7 +305,7 @@ public function getReportFor(Carbon $endDate, $offset, $sendMail = false): Proje
|
|||
return (new ProjectReportGeneratorJob($report, $sendMail));
|
||||
}
|
||||
|
||||
public function getFileDownloadsFor(Carbon $endDate, $offset):array
|
||||
public function getFileDownloadsFor(Carbon $endDate, int $offset=7):array
|
||||
{
|
||||
$startOfRange = (clone $endDate)->subdays(4*$offset-1);
|
||||
$dateRange = CarbonPeriod::create($startOfRange, $endDate);
|
||||
|
|
@ -332,7 +316,7 @@ public function getFileDownloadsFor(Carbon $endDate, $offset):array
|
|||
->toArray();
|
||||
}
|
||||
|
||||
public function getMosaicsFor(Carbon $endDate, $offset= 7): array
|
||||
public function getMosaicsFor(Carbon $endDate, int $offset= 7): array
|
||||
{
|
||||
logger(sprintf('in Get Mosaics for Period %s with %d offset', $endDate->format('Y-m-d'),$offset));
|
||||
return collect(range(0, 3))
|
||||
|
|
|
|||
|
|
@ -25,14 +25,10 @@ class ProjectMosaic extends Model
|
|||
'status'
|
||||
];
|
||||
|
||||
public static function getFilenameForYearAndWeek($year, $startWeekNumber)
|
||||
{
|
||||
return sprintf('week_%02d_%d.tif', $startWeekNumber, $year);
|
||||
}
|
||||
|
||||
public static function getFilenameByPeriod(Carbon $endDate,int $offset)
|
||||
{
|
||||
return sprintf('Period_%s_%s.tif', $endDate->format('Y-m-d'), $endDate->copy()->subDays($offset)->format('Y-m-d'));
|
||||
return sprintf('period_%s_%s.tif',(clone $endDate)->subdays($offset-1)->format('Y-m-d'),$endDate->format('Y-m-d'));
|
||||
}
|
||||
|
||||
public function project()
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ public function getFileNameAttribute()
|
|||
}
|
||||
public static function getFileName(Carbon $endDate, int $offset): string
|
||||
{
|
||||
return 'Period_'.$endDate->copy()->subDays($offset).'_'.$endDate;
|
||||
return 'period_'.$endDate->copy()->subDays($offset).'_'.$endDate;
|
||||
}
|
||||
|
||||
public function getFullPathName()
|
||||
|
|
|
|||
|
|
@ -24,19 +24,20 @@ class ProjectMosaicTest extends TestCase
|
|||
* @test
|
||||
* @dataProvider filenameProvider
|
||||
* */
|
||||
public function it_should_return_the_correct_attachment_path($year,$week, $expected)
|
||||
public function it_should_return_the_correct_attachment_path($endDate,$offset, $expected)
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
ProjectMosaic::getFilenameForYearAndWeek($year, $week)
|
||||
ProjectMosaic::getFilenameByPeriod($endDate,$offset)
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
public static function filenameProvider(){
|
||||
return [
|
||||
[2022, 1, 'week_01_2022.tif'],
|
||||
[2022, 10, 'week_10_2022.tif'],
|
||||
[new Carbon('2022-01-01'), 10, 'period_2021-12-22_2022-01-01.tif'],
|
||||
[new Carbon('2022-10-25'),7, 'period_2022-10-18_2022-10-25.tif'],
|
||||
];
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,10 +80,9 @@ public function when_not_all_mosaics_are_present_it_should_return_an_exception()
|
|||
|
||||
$this->seed();
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Missing mosaics: week_52_2022.tif, week_51_2021.tif, week_50_2021.tif, week_49_2021.tif');
|
||||
$this->expectExceptionMessage('Missing mosaics: period_2021-12-26_2022-01-01.tif, period_2021-12-19_2021-12-25.tif, period_2021-12-12_2021-12-18.tif, period_2021-12-05_2021-12-11.tif');
|
||||
$project = Project::find(1);
|
||||
$lastDate = Carbon::parse('2022-01-01');
|
||||
//$lastDate->getWeekOfYear();
|
||||
$project->allMosaicsPresent($lastDate);
|
||||
}
|
||||
|
||||
|
|
@ -92,13 +91,13 @@ public function when_all_mosaics_are_present_it_should_return_true()
|
|||
{
|
||||
// TODO CHeck with Martin the Leading Zero test
|
||||
$project = Mockery::mock(Project::class)->makePartial();
|
||||
$lastDate = Carbon::parse('2021-01-22');
|
||||
$lastDate = Carbon::parse('2022-01-01');
|
||||
$project->shouldReceive('getMosaicList')->andReturn(
|
||||
collect([
|
||||
"chemba/weekly_mosaic/week_53_2020.tif",
|
||||
"chemba/weekly_mosaic/week_03_2021.tif",
|
||||
"chemba/weekly_mosaic/week_02_2021.tif",
|
||||
"chemba/weekly_mosaic/week_01_2021.tif",
|
||||
"chemba/weekly_mosaic/period_2021-12-26_2022-01-01.tif",
|
||||
"chemba/weekly_mosaic/period_2021-12-19_2021-12-25.tif",
|
||||
"chemba/weekly_mosaic/period_2021-12-12_2021-12-18.tif",
|
||||
"chemba/weekly_mosaic/period_2021-12-05_2021-12-11.tif",
|
||||
]));
|
||||
|
||||
$this->assertTrue($project->allMosaicsPresent($lastDate));
|
||||
|
|
@ -108,16 +107,14 @@ public function when_all_mosaics_are_present_it_should_return_true()
|
|||
public function when_not_mosaics_are_present_it_should_throw_an_exception_listing_the_missing_mosiacs()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Missing mosaics: week_53_2020.tif');
|
||||
$this->expectExceptionMessage('Missing mosaics: period_2020-12-05_2020-12-11.tif');
|
||||
$project = Mockery::mock(Project::class)->makePartial();
|
||||
$lastDate = Carbon::parse('2021-01-01');
|
||||
$project->shouldReceive('getMosaicList')->andReturn(
|
||||
collect([
|
||||
"chemba/weekly_mosaic/week_52_2020.tif",
|
||||
"chemba/weekly_mosaic/week_51_2020.tif",
|
||||
"chemba/weekly_mosaic/week_50_2020.tif",
|
||||
"chemba/weekly_mosaic/week_49_2021.tif",
|
||||
"chemba/weekly_mosaic/week_48_2021.tif",
|
||||
"chemba/weekly_mosaic/period_2020-12-26_2021-01-01.tif",
|
||||
"chemba/weekly_mosaic/period_2020-12-19_2020-12-25.tif",
|
||||
"chemba/weekly_mosaic/period_2020-12-12_2020-12-18.tif",
|
||||
]));
|
||||
|
||||
$project->allMosaicsPresent($lastDate);
|
||||
|
|
@ -127,7 +124,7 @@ public function when_not_mosaics_are_present_it_should_throw_an_exception_listin
|
|||
public function when_all_mosaics_are_present_is_called_with_future_date_it_should_throw_an_expection()
|
||||
{
|
||||
$this->expectException(\Exception::class);
|
||||
$this->expectExceptionMessage('Mosaic can be generated for the future. Change the end date.');
|
||||
$this->expectExceptionMessage('Mosaic can\'t be generated for the future. Change the end date.');
|
||||
$this->seed();
|
||||
$project = Project::find(1);
|
||||
Carbon::setTestNow(Carbon::parse('2020-01-01'));
|
||||
|
|
@ -143,14 +140,14 @@ public function getMosiacFileListByEndDate_should_return_four_filenames()
|
|||
/* @var Project $project */
|
||||
$project = Project::find(1);
|
||||
$lastDate = Carbon::parse('2021-01-01');
|
||||
$list = $project::getMosaicFilenameListByEndDate($lastDate);
|
||||
$list = iterator_to_array($project::getMosaicFilenameListByEndDate($lastDate));
|
||||
$this->assertCount(4, $list);
|
||||
$this->assertEquals([
|
||||
"week_53_2020.tif",
|
||||
"week_52_2020.tif",
|
||||
"week_51_2020.tif",
|
||||
"week_50_2020.tif",
|
||||
], $list->toArray());
|
||||
"period_2020-12-26_2021-01-01.tif",
|
||||
"period_2020-12-19_2020-12-25.tif",
|
||||
"period_2020-12-12_2020-12-18.tif",
|
||||
"period_2020-12-05_2020-12-11.tif",
|
||||
], $list);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
|
@ -162,8 +159,8 @@ public function when_getFileDownloadsFor_is_called_it_returns_a_collection_of_se
|
|||
'download_path' => 'project_download_path',
|
||||
]);
|
||||
|
||||
$downloads = $project->getFileDownloadsFor(new Carbon('2024-06-18'),20);
|
||||
$this->assertCount(60, $downloads);
|
||||
$downloads = $project->getFileDownloadsFor(new Carbon('2024-06-18'),7);
|
||||
$this->assertCount(28, $downloads);
|
||||
collect($downloads)->each(fn($job) => $this->assertInstanceOf(ProjectDownloadTiffJob::class, $job));
|
||||
}
|
||||
|
||||
|
|
@ -176,7 +173,7 @@ public function when_getMosaicsFor_is_called_it_returns_a_collection_of_seven_do
|
|||
'download_path' => 'project_download_path',
|
||||
]);
|
||||
|
||||
$mosaics = $project->getMosaicsFor(2023, 2);
|
||||
$mosaics = $project->getMosaicsFor(new Carbon('2023-01-01'));
|
||||
$this->assertCount(4, $mosaics);
|
||||
collect($mosaics)->each(fn($job) => $this->assertInstanceOf(ProjectMosiacGeneratorJob::class, $job));
|
||||
}
|
||||
|
|
@ -190,7 +187,7 @@ public function when_getReport_is_called_it_returns_a_jobs()
|
|||
'download_path' => 'project_download_path',
|
||||
]);
|
||||
|
||||
$job = $project->getReportFor(2023, 2);
|
||||
$job = $project->getReportFor(new Carbon('2023-01-01'));
|
||||
|
||||
$this->assertInstanceOf(ProjectReportGeneratorJob::class, $job);
|
||||
}
|
||||
|
|
@ -198,15 +195,17 @@ public function when_getReport_is_called_it_returns_a_jobs()
|
|||
/** @test */
|
||||
public function it_can_create_a_chain_of_batches_that_result_in_a_report()
|
||||
{
|
||||
/* @var Project $project*/
|
||||
$project = Project::create([
|
||||
'name' => 'project_name',
|
||||
'download_path' => 'project_download_path',
|
||||
]);
|
||||
|
||||
Bus::fake();
|
||||
$project->scheduleReport(2023, 50);
|
||||
$project->scheduleReport(new Carbon('2023-01-01'),7);
|
||||
Bus::assertChained([
|
||||
Bus::chainedBatch(function (PendingBatch $batch) {
|
||||
$batch;
|
||||
return $batch->jobs->count() === 28;
|
||||
}),
|
||||
Bus::chainedBatch(function (PendingBatch $batch) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue