Fixed bugs in lastSevenDays chart

This commit is contained in:
Constantin Graf
2024-04-24 16:30:26 +02:00
committed by Constantin Graf
parent 267adf52ca
commit 8136f630c9
2 changed files with 22 additions and 19 deletions

View File

@@ -47,18 +47,21 @@ class DashboardService
{ {
$result = []; $result = [];
$windowSize = 24 / $windows; $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(); $date = Carbon::now($timeZone)->startOfDay();
$end = $date->copy()->endOfDay()->utc()->toDateTimeString();
$start = $end;
for ($i = 0; $i < $days; $i++) { for ($i = 0; $i < $days; $i++) {
$dateString = $date->format('Y-m-d');
$tempDate = $date->copy(); $tempDate = $date->copy();
$start = $tempDate->utc()->toDateTimeString(); $start = $tempDate->copy()->utc()->toDateTimeString();
$tempWindows = []; $tempWindows = [];
for ($j = 0; $j < $windows; $j++) { for ($j = 0; $j < $windows; $j++) {
$tempWindow = $tempDate->addHours($windowSize)->utc()->toDateTimeString(); $tempWindow = $tempDate->utc()->toDateTimeString();
$tempWindows[] = $tempWindow; $tempWindows[] = $tempWindow;
$tempDate->addHours($windowSize);
} }
$result[$date->format('Y-m-d')] = $tempWindows; $result[$dateString] = $tempWindows;
$date->subDay(); $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 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 ( FROM (
SELECT time_range_starts.start AS start, time_range_starts.start + interval \'3 hours\' AS "end" 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 ) time_ranges
JOIN time_entries ON time_entries.start < time_ranges."end" JOIN time_entries ON time_entries.start < time_ranges."end"
AND coalesce(time_entries."end", :now::timestamp) > time_ranges.start AND coalesce(time_entries."end", :now::timestamp) > time_ranges.start
where time_entries.user_id = :user_id and WHERE time_entries.user_id = :user_id and
time_entries.organization_id = :organization_id time_entries.organization_id = :organization_id
GROUP BY time_ranges.start GROUP BY time_ranges.start
ORDER BY time_ranges.start ORDER BY time_ranges.start
', [ ', [
'start_time_ranges' => $lastDaysSplitInWindows['start'], 'start_time_ranges' => $lastDaysSplitInWindows['start'],
'end_time_ranges' => $lastDaysSplitInWindows['end'], 'end_time_ranges' => $lastDaysSplitInWindows['end'],

View File

@@ -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 public function test_last_seven_days_returns_spend_time_in_the_last_seven_days_aggregated_in_three_hour_blocks(): void
{ {
// Arrange // 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); $this->travelTo($now);
$organization = Organization::factory()->create(); $organization = Organization::factory()->create();
$user = User::factory()->create([ $user = User::factory()->create([
'timezone' => 'Europe/Vienna', 'timezone' => 'Europe/Vienna',
]); ]);
$timeEntryOverWholePeriod = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([ $timeEntryOverWholePeriod = 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' => now('Europe/Vienna')->endOfDay()->addMinute()->utc(), // TODO: addMinute should not be necessary 'end' => now('Europe/Vienna')->endOfDay()->addSecond()->utc(), // TODO: fix problem with last second
]); ]);
$timeEntryOverWholePeriodWithoutEnd = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([ $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, 'end' => null,
]); ]);
$timeEntry1Task1 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([ $timeEntry1Task1 = TimeEntry::factory()->forUser($user)->forOrganization($organization)->create([
@@ -562,16 +562,16 @@ class DashboardServiceTest extends TestCase
$this->assertSame([ $this->assertSame([
0 => [ 0 => [
'date' => '2024-04-17', 'date' => '2024-04-17',
'duration' => 115800, 'duration' => 130200,
'history' => [ 'history' => [
0 => 21600, 0 => 21600,
1 => 21600, 1 => 21600,
2 => 22200, 2 => 21600,
3 => 18000, 3 => 22200,
4 => 10800, 4 => 10800,
5 => 10800, 5 => 10800,
6 => 10800, // TODO 6 => 10800,
7 => 0, // TODO 7 => 10800,
], ],
], ],
1 => [ 1 => [