Skip to content

Commit

Permalink
Fixed week aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed May 22, 2024
1 parent eb810fc commit 280d778
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
22 changes: 17 additions & 5 deletions app/Service/TimeEntryAggregationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;

class TimeEntryAggregationService
{
Expand Down Expand Up @@ -175,18 +176,19 @@ public function fillGapsInTimeGroups(array $data, TimeEntryAggregationType $grou
return $data;
} else {
$format = match ($interval) {
TimeEntryAggregationTypeInterval::Day => 'Y-m-d',
TimeEntryAggregationTypeInterval::Week => 'Y-m-d H:i:s',
TimeEntryAggregationTypeInterval::Day, TimeEntryAggregationTypeInterval::Week => 'Y-m-d',
TimeEntryAggregationTypeInterval::Month => 'Y-m',
TimeEntryAggregationTypeInterval::Year => 'Y',
};
$slots = $this->timeSlotsBetween($start, $end, $timezone, $startOfWeek, $interval, $format);
$foundEntries = [];
$filledData = [];
foreach ($slots as $slot) {
$foundDataSet = null;
foreach ($data as $item) {
if ($item['key'] === $slot) {
$foundDataSet = $item;
$foundEntries[] = $item['key'];
break;
}
}
Expand Down Expand Up @@ -219,6 +221,16 @@ public function fillGapsInTimeGroups(array $data, TimeEntryAggregationType $grou
}
}

if (count($foundEntries) !== count($data)) {
foreach ($data as $item) {
if (! in_array($item['key'], $foundEntries, true)) {
Log::error('Problem with filling gaps in time groups', [
'item' => $item,
]);
}
}
}

return $filledData;
}
}
Expand All @@ -233,11 +245,11 @@ private function getGroupByQuery(TimeEntryAggregationType $group, string $timezo
} else {
$dateWithTimeZone = 'start';
}
$startOfWeek = Carbon::now()->setTimezone($timezone)->startOfWeek($startOfWeek->carbonWeekDay())->utc()->toDateTimeString();
$startOfWeek = Carbon::now()->setTimezone($timezone)->startOfWeek($startOfWeek->carbonWeekDay())->toDateTimeString();
if ($group === TimeEntryAggregationType::Day) {
return 'date('.$dateWithTimeZone.')';
} elseif ($group === TimeEntryAggregationType::Week) {
return "to_char(date_bin('7 days', ".$dateWithTimeZone.", timestamp '".$startOfWeek."'), 'YYYY-MM-DD HH24:MI:SS')";
return "to_char(date_bin('7 days', ".$dateWithTimeZone.", timestamp '".$startOfWeek."'), 'YYYY-MM-DD')";
} elseif ($group === TimeEntryAggregationType::Month) {
return 'to_char('.$dateWithTimeZone.', \'YYYY-MM\')';
} elseif ($group === TimeEntryAggregationType::Year) {
Expand Down Expand Up @@ -268,7 +280,7 @@ public function timeSlotsBetween(Carbon $start, Carbon $end, string $timezone, W
if ($interval === TimeEntryAggregationTypeInterval::Day) {
$current->startOfDay();
} elseif ($interval === TimeEntryAggregationTypeInterval::Week) {
$current->startOfWeek($startOfWeek->carbonWeekDay())->utc();
$current->startOfWeek($startOfWeek->carbonWeekDay());
} elseif ($interval === TimeEntryAggregationTypeInterval::Month) {
$current->startOfMonth();
} elseif ($interval === TimeEntryAggregationTypeInterval::Year) {
Expand Down
18 changes: 9 additions & 9 deletions tests/Unit/Endpoint/Api/V1/TimeEntryEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,8 @@ public function test_aggregate_endpoint_groups_by_one_group(): void
$data = $this->createUserWithPermission([
'time-entries:view:all',
]);
$week1 = Carbon::now()->timezone($data->user->timezone)->startOfWeek($data->user->week_start->carbonWeekDay())->utc();
$week2 = Carbon::now()->timezone($data->user->timezone)->subWeeks(2)->startOfWeek($data->user->week_start->carbonWeekDay())->utc();
$week1 = Carbon::now()->timezone($data->user->timezone)->startOfWeek($data->user->week_start->carbonWeekDay());
$week2 = Carbon::now()->timezone($data->user->timezone)->subWeeks(2)->startOfWeek($data->user->week_start->carbonWeekDay());
$timeEntry1Week1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($week1->copy()->addDays(1), 10)->create();
$timeEntry2Week1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($week1->copy()->addDays(2), 10)->create();
$timeEntry1Week2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($week2->copy()->addDays(3), 10)->create();
Expand All @@ -661,14 +661,14 @@ public function test_aggregate_endpoint_groups_by_one_group(): void
'grouped_type' => 'week',
'grouped_data' => [
0 => [
'key' => $week2->format('Y-m-d H:i:s'),
'key' => $week2->format('Y-m-d'),
'seconds' => 20,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
],
1 => [
'key' => $week1->format('Y-m-d H:i:s'),
'key' => $week1->format('Y-m-d'),
'seconds' => 20,
'cost' => 0,
'grouped_type' => null,
Expand All @@ -685,8 +685,8 @@ public function test_aggregate_endpoint_groups_by_one_group_with_fill_gaps_argum
$data = $this->createUserWithPermission([
'time-entries:view:all',
]);
$laterWeekEnd = Carbon::now()->timezone($data->user->timezone)->endOfWeek($data->user->week_start->toEndOfWeek()->carbonWeekDay())->utc();
$earlierWeekStart = Carbon::now()->timezone($data->user->timezone)->subWeeks(2)->startOfWeek($data->user->week_start->carbonWeekDay())->utc();
$laterWeekEnd = Carbon::now()->timezone($data->user->timezone)->endOfWeek($data->user->week_start->toEndOfWeek()->carbonWeekDay());
$earlierWeekStart = Carbon::now()->timezone($data->user->timezone)->subWeeks(2)->startOfWeek($data->user->week_start->carbonWeekDay());

$timeEntry1 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($laterWeekEnd->copy()->subDays(1), 10)->create();
$timeEntry2 = TimeEntry::factory()->forOrganization($data->organization)->forMember($data->member)->startWithDuration($laterWeekEnd->copy()->subDays(2), 10)->create();
Expand All @@ -712,21 +712,21 @@ public function test_aggregate_endpoint_groups_by_one_group_with_fill_gaps_argum
'grouped_type' => 'week',
'grouped_data' => [
0 => [
'key' => '2024-05-05 22:00:00',
'key' => $earlierWeekStart->startOfWeek($data->user->week_start->carbonWeekDay())->format('Y-m-d'),
'seconds' => 20,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
],
1 => [
'key' => '2024-05-12 22:00:00',
'key' => $laterWeekEnd->copy()->subWeek()->startOfWeek($data->user->week_start->carbonWeekDay())->format('Y-m-d'),
'seconds' => 0,
'cost' => 0,
'grouped_type' => null,
'grouped_data' => null,
],
2 => [
'key' => '2024-05-19 22:00:00',
'key' => $laterWeekEnd->startOfWeek($data->user->week_start->carbonWeekDay())->format('Y-m-d'),
'seconds' => 20,
'cost' => 0,
'grouped_type' => null,
Expand Down

0 comments on commit 280d778

Please sign in to comment.