Skip to content

Commit

Permalink
Merge pull request #12 from biigle/patch-1
Browse files Browse the repository at this point in the history
Fix month addition/subtraction
  • Loading branch information
mzur authored Apr 30, 2024
2 parents 2855751 + 5ff462d commit 556e01b
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 23 deletions.
8 changes: 5 additions & 3 deletions src/Console/Commands/CountUniqueUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ class CountUniqueUser extends Command
*/
public function handle()
{
$now = Carbon::now()->toImmutable()->settings(['monthOverflow' => false]);

$nbrUser = User::whereBetween('login_at', [
Carbon::now()->subMonth()->startOfMonth(),
Carbon::now()->startOfMonth(),
$now->subMonth()->startOfMonth(),
$now->startOfMonth(),
])->count();

DB::table('kpis_unique_users')->insert([
'date' => Carbon::now()->subMonth()->endOfMonth(),
'date' => $now->subMonth()->endOfMonth(),
'value' => $nbrUser,
]);
}
Expand Down
5 changes: 4 additions & 1 deletion src/Console/Commands/DetermineStorageUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public function handle()
{
$size = $this->getSizeInGB();

$date = Carbon::now()->subMonth()->endOfMonth();
$date = Carbon::now()
->settings(['monthOverflow' => false])
->subMonth()
->endOfMonth();

DB::table('kpis_storage_usage')->insert(['date' => $date, 'value' => $size]);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Http/Controllers/Views/KpiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public function show($idx = 6)
abort(404);
}

$date = Carbon::now()->subMonths(7 - $idx);
$now = Carbon::now()->toImmutable()->settings(['monthOverflow' => false]);
$date = $now->subMonths(7 - $idx);
$year = $date->year;
$month = $date->month;

Expand All @@ -34,7 +35,7 @@ public function show($idx = 6)

$monthOverview = [];
for($i = 6;$i >= 1;$i--) {
$monthOverview[] = Carbon::now()->subMonths($i)->format('M');
$monthOverview[] = $now->subMonths($i)->format('M');
}

return view('kpis::show', [
Expand Down
16 changes: 9 additions & 7 deletions tests/Console/Commands/CountUniqueUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ class CountUniqueUserTest extends TestCase
{
public function testHandle()
{
UserTest::create(['login_at' => Carbon::now()->subMonth()->firstOfMonth()]);
UserTest::create(['login_at' => Carbon::now()->subMonth()]);
UserTest::create(['login_at' => Carbon::now()->subMonth()->endOfMonth()]);
$now = Carbon::now()->toImmutable()->settings(['monthOverflow' => false]);
UserTest::create(['login_at' => $now->subMonth()->firstOfMonth()]);
UserTest::create(['login_at' => $now->subMonth()]);
UserTest::create(['login_at' => $now->subMonth()->endOfMonth()]);

$this->artisan('kpis:count-unique-user')->assertExitCode(0);

$uniqueUsers = DB::table('kpis_unique_users')
->where('date', '=', Carbon::now()->subMonth()->endOfMonth())->pluck('value');
->where('date', '=', $now->subMonth()->endOfMonth())->pluck('value');

$this->assertCount(1, $uniqueUsers);
$this->assertEquals(3, $uniqueUsers[0]);
Expand All @@ -28,13 +29,14 @@ public function testHandle()

public function testLoginWasNotLastMonth()
{
UserTest::create(['login_at' => Carbon::now()->subMonth()->firstOfMonth()]);
UserTest::create(['login_at' => Carbon::now()->subMonths(2)->endOfMonth()]);
$now = Carbon::now()->toImmutable()->settings(['monthOverflow' => false]);
$u1 = UserTest::create(['login_at' => $now->subMonth()->firstOfMonth()]);
$u2 = UserTest::create(['login_at' => $now->subMonths(2)->endOfMonth()]);

$this->artisan('kpis:count-unique-user')->assertExitCode(0);

$uniqueUsers = DB::table('kpis_unique_users')
->where('date', '=', Carbon::now()->subMonth()->endOfMonth())->pluck('value');
->where('date', '=', $now->subMonth()->endOfMonth())->pluck('value');

$this->assertCount(1, $uniqueUsers);
$this->assertEquals(1, $uniqueUsers[0]);
Expand Down
16 changes: 14 additions & 2 deletions tests/Console/Commands/DetermineStorageUsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ public function testHandle()

$this->artisan('kpis:determine-storage-usage')->assertExitCode(0);

$users = DB::table('kpis_storage_usage')->where('date', '=', Carbon::now()->subMonth()->endOfMonth())->pluck('value');
$date = Carbon::now()
->settings(['monthOverflow' => false])
->subMonth()
->endOfMonth();
$users = DB::table('kpis_storage_usage')
->where('date', '=', $date)
->pluck('value');

$this->assertCount(1, $users);
$this->assertEquals(2, $users[0]);
Expand All @@ -44,7 +50,13 @@ public function testEmptyAttributeArrays()

$this->artisan('kpis:determine-storage-usage')->assertExitCode(0);

$users = DB::table('kpis_storage_usage')->where('date', '=', Carbon::now()->subMonth()->endOfMonth())->pluck('value');
$date = Carbon::now()
->settings(['monthOverflow' => false])
->subMonth()
->endOfMonth();
$users = DB::table('kpis_storage_usage')
->where('date', '=', $date)
->pluck('value');

$this->assertCount(1, $users);
$this->assertEquals(0, $users[0]);
Expand Down
10 changes: 8 additions & 2 deletions tests/RequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public function testSaveBigInt(){

public function testGetActions()
{
$date = Carbon::now()->subMonth()->lastOfMonth();
$date = Carbon::now()
->settings(['monthOverflow' => false])
->subMonth()
->lastOfMonth();

DB::table('kpis_actions')->insert(['date' => $date, 'value' => 10]);

Expand All @@ -55,7 +58,10 @@ public function testGetActions()

public function testGetVisits()
{
$date = Carbon::now()->subMonth()->lastOfMonth();
$date = Carbon::now()
->settings(['monthOverflow' => false])
->subMonth()
->lastOfMonth();

DB::table('kpis_visits')->insert(['date' => $date, 'value' => 10]);

Expand Down
5 changes: 4 additions & 1 deletion tests/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class StorageTest extends TestCase
public function testGetStorage()
{

$date = Carbon::now()->subMonth()->lastOfMonth();
$date = Carbon::now()
->settings(['monthOverflow' => false])
->subMonth()
->lastOfMonth();

$noFiles = Storage::getStorageUsage($date->year, $date->month);

Expand Down
14 changes: 9 additions & 5 deletions tests/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
class UserTest extends TestCase
{

public function testGetUser(){

$first = Carbon::now()->subMonth()->firstOfMonth();
$last = Carbon::now()->subMonth()->endOfMonth();
public function testGetUser()
{
$now = Carbon::now()->toImmutable()->settings(['monthOverflow' => false]);
$first = $now->subMonth()->firstOfMonth();
$last = $now->subMonth()->endOfMonth();

$noUserCounted = User::getUser($first->year, $first->month);

Expand All @@ -27,7 +28,10 @@ public function testGetUser(){
}

public function testGetUniqueUser(){
$date = Carbon::now()->subMonth()->endOfMonth();
$date = Carbon::now()
->settings(['monthOverflow' => false])
->subMonth()
->endOfMonth();

$noUserCounted = User::getUniqueUser($date->year, $date->month);

Expand Down

0 comments on commit 556e01b

Please sign in to comment.