spread heavy workload days
This commit is contained in:
parent
85e2c33c31
commit
6dc2ccbb55
2 changed files with 65 additions and 53 deletions
|
|
@ -18,15 +18,7 @@ class EvenDistributionScheduler
|
|||
$differenceInDays = $startDate->diff($targetDate)->days;
|
||||
$dayCount = $differenceInDays + 1;
|
||||
|
||||
if ($workloadPlacement === WorkloadPlacement::Spread) {
|
||||
return $this->sparseDates(
|
||||
assignmentCount: $assignmentCount,
|
||||
dayCount: $dayCount,
|
||||
startDate: $startDate,
|
||||
);
|
||||
}
|
||||
|
||||
return $this->groupedDates(
|
||||
return $this->distributedDates(
|
||||
assignmentCount: $assignmentCount,
|
||||
dayCount: $dayCount,
|
||||
startDate: $startDate,
|
||||
|
|
@ -37,33 +29,7 @@ class EvenDistributionScheduler
|
|||
/**
|
||||
* @return list<DateTimeImmutable>
|
||||
*/
|
||||
private function sparseDates(
|
||||
int $assignmentCount,
|
||||
int $dayCount,
|
||||
DateTimeImmutable $startDate,
|
||||
): array {
|
||||
if ($assignmentCount === 1) {
|
||||
return [$startDate];
|
||||
}
|
||||
|
||||
$dates = [];
|
||||
for ($assignmentIndex = 0;
|
||||
$assignmentIndex < $assignmentCount;
|
||||
$assignmentIndex++
|
||||
) {
|
||||
$scaledIndex = $assignmentIndex * ($dayCount - 1)
|
||||
/ ($assignmentCount - 1);
|
||||
$dayIndex = (int) floor($scaledIndex + 0.5);
|
||||
$dates[] = $startDate->modify("+{$dayIndex} days");
|
||||
}
|
||||
|
||||
return $dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<DateTimeImmutable>
|
||||
*/
|
||||
private function groupedDates(
|
||||
private function distributedDates(
|
||||
int $assignmentCount,
|
||||
int $dayCount,
|
||||
DateTimeImmutable $startDate,
|
||||
|
|
@ -71,19 +37,18 @@ class EvenDistributionScheduler
|
|||
): array {
|
||||
$assignmentsPerDay = intdiv($assignmentCount, $dayCount);
|
||||
$remainderDayCount = $assignmentCount % $dayCount;
|
||||
$remainderBlockStart = $this->remainderBlockStart(
|
||||
$remainderDayIndexes = $this->remainderDayIndexes(
|
||||
assignmentsPerDay: $assignmentsPerDay,
|
||||
dayCount: $dayCount,
|
||||
remainderDayCount: $remainderDayCount,
|
||||
workloadPlacement: $workloadPlacement,
|
||||
);
|
||||
$remainderDays = array_fill_keys($remainderDayIndexes, true);
|
||||
$dates = [];
|
||||
|
||||
for ($dayIndex = 0; $dayIndex < $dayCount; $dayIndex++) {
|
||||
$assignmentCountForDay = $assignmentsPerDay;
|
||||
if (
|
||||
$dayIndex >= $remainderBlockStart
|
||||
&& $dayIndex < $remainderBlockStart + $remainderDayCount
|
||||
) {
|
||||
if (isset($remainderDays[$dayIndex])) {
|
||||
$assignmentCountForDay++;
|
||||
}
|
||||
|
||||
|
|
@ -96,6 +61,65 @@ class EvenDistributionScheduler
|
|||
return $dates;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<int>
|
||||
*/
|
||||
private function remainderDayIndexes(
|
||||
int $assignmentsPerDay,
|
||||
int $dayCount,
|
||||
int $remainderDayCount,
|
||||
WorkloadPlacement $workloadPlacement,
|
||||
): array {
|
||||
if ($remainderDayCount === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($workloadPlacement === WorkloadPlacement::Spread) {
|
||||
return $this->spreadRemainderDayIndexes(
|
||||
assignmentsPerDay: $assignmentsPerDay,
|
||||
dayCount: $dayCount,
|
||||
remainderDayCount: $remainderDayCount,
|
||||
);
|
||||
}
|
||||
|
||||
$blockStart = $this->remainderBlockStart(
|
||||
dayCount: $dayCount,
|
||||
remainderDayCount: $remainderDayCount,
|
||||
workloadPlacement: $workloadPlacement,
|
||||
);
|
||||
|
||||
return range($blockStart, $blockStart + $remainderDayCount - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<int>
|
||||
*/
|
||||
private function spreadRemainderDayIndexes(
|
||||
int $assignmentsPerDay,
|
||||
int $dayCount,
|
||||
int $remainderDayCount,
|
||||
): array {
|
||||
if ($remainderDayCount === 1) {
|
||||
if ($assignmentsPerDay === 0) {
|
||||
return [0];
|
||||
}
|
||||
|
||||
return [intdiv($dayCount - 1, 2)];
|
||||
}
|
||||
|
||||
$dayIndexes = [];
|
||||
for ($remainderIndex = 0;
|
||||
$remainderIndex < $remainderDayCount;
|
||||
$remainderIndex++
|
||||
) {
|
||||
$scaledIndex = $remainderIndex * ($dayCount - 1)
|
||||
/ ($remainderDayCount - 1);
|
||||
$dayIndexes[] = (int) floor($scaledIndex + 0.5);
|
||||
}
|
||||
|
||||
return $dayIndexes;
|
||||
}
|
||||
|
||||
private function remainderBlockStart(
|
||||
int $dayCount,
|
||||
int $remainderDayCount,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue