diff --git a/app/Service/DashboardService.php b/app/Service/DashboardService.php index 869b53d0..7bb58687 100644 --- a/app/Service/DashboardService.php +++ b/app/Service/DashboardService.php @@ -47,18 +47,21 @@ class DashboardService { $result = []; $windowSize = 24 / $windows; + $end = Carbon::now($timeZone)->endOfDay()->subHours(3)->utc()->toDateTimeString(); + $start = Carbon::now($timeZone)->subDays($days)->startOfDay()->utc()->toDateTimeString(); + $date = Carbon::now($timeZone)->startOfDay(); - $end = $date->copy()->endOfDay()->utc()->toDateTimeString(); - $start = $end; for ($i = 0; $i < $days; $i++) { + $dateString = $date->format('Y-m-d'); $tempDate = $date->copy(); - $start = $tempDate->utc()->toDateTimeString(); + $start = $tempDate->copy()->utc()->toDateTimeString(); $tempWindows = []; for ($j = 0; $j < $windows; $j++) { - $tempWindow = $tempDate->addHours($windowSize)->utc()->toDateTimeString(); + $tempWindow = $tempDate->utc()->toDateTimeString(); $tempWindows[] = $tempWindow; + $tempDate->addHours($windowSize); } - $result[$date->format('Y-m-d')] = $tempWindows; + $result[$dateString] = $tempWindows; $date->subDay(); } @@ -420,14 +423,14 @@ class DashboardService SELECT time_ranges.start, EXTRACT(epoch FROM sum(LEAST(time_ranges."end", coalesce(time_entries."end", :now::timestamp)) - GREATEST(time_ranges.start, time_entries.start))) AS aggregate FROM ( SELECT time_range_starts.start AS start, time_range_starts.start + interval \'3 hours\' AS "end" - FROM generate_series(:start_time_ranges::timestamp, :end_time_ranges::timestamp, interval \'3 hours\') as time_range_starts (start) + FROM generate_series(:start_time_ranges::timestamp, :end_time_ranges::timestamp + interval \'3 hours\', interval \'3 hours\') as time_range_starts (start) ) time_ranges JOIN time_entries ON time_entries.start < time_ranges."end" - AND coalesce(time_entries."end", :now::timestamp) > time_ranges.start - where time_entries.user_id = :user_id and + AND coalesce(time_entries."end", :now::timestamp) > time_ranges.start + WHERE time_entries.user_id = :user_id and time_entries.organization_id = :organization_id - GROUP BY time_ranges.start - ORDER BY time_ranges.start + GROUP BY time_ranges.start + ORDER BY time_ranges.start ', [ 'start_time_ranges' => $lastDaysSplitInWindows['start'], 'end_time_ranges' => $lastDaysSplitInWindows['end'], diff --git a/tests/Unit/Service/DashboardServiceTest.php b/tests/Unit/Service/DashboardServiceTest.php index 69b0cd97..9212b77e 100644 --- a/tests/Unit/Service/DashboardServiceTest.php +++ b/tests/Unit/Service/DashboardServiceTest.php @@ -536,18 +536,18 @@ class DashboardServiceTest extends TestCase public function test_last_seven_days_returns_spend_time_in_the_last_seven_days_aggregated_in_three_hour_blocks(): void { // Arrange - $now = Carbon::create(2024, 4, 17, 12, 0, 0, 'Europe/Vienna'); + $now = Carbon::create(2024, 4, 17, 12, 0, 0, 'Europe/Vienna')->utc(); $this->travelTo($now); $organization = Organization::factory()->create(); $user = User::factory()->create([ 'timezone' => 'Europe/Vienna', ]); $timeEntryOverWholePeriod = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([ - 'start' => now('Europe/Vienna')->subDays(7)->startOfDay()->subMinute()->utc(), - 'end' => now('Europe/Vienna')->endOfDay()->addMinute()->utc(), // TODO: addMinute should not be necessary + 'start' => now('Europe/Vienna')->subDays(7)->startOfDay()->utc(), + 'end' => now('Europe/Vienna')->endOfDay()->addSecond()->utc(), // TODO: fix problem with last second ]); $timeEntryOverWholePeriodWithoutEnd = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([ - 'start' => now('Europe/Vienna')->subDays(7)->startOfDay()->subMinute()->utc(), + 'start' => now('Europe/Vienna')->subDays(7)->startOfDay()->utc(), 'end' => null, ]); $timeEntry1Task1 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([ @@ -562,16 +562,16 @@ class DashboardServiceTest extends TestCase $this->assertSame([ 0 => [ 'date' => '2024-04-17', - 'duration' => 115800, + 'duration' => 130200, 'history' => [ 0 => 21600, 1 => 21600, - 2 => 22200, - 3 => 18000, + 2 => 21600, + 3 => 22200, 4 => 10800, 5 => 10800, - 6 => 10800, // TODO - 7 => 0, // TODO + 6 => 10800, + 7 => 10800, ], ], 1 => [