Skip to content

Commit

Permalink
Merge pull request #9 from gajosu/add-custom-date-column
Browse files Browse the repository at this point in the history
Add Support for Custom Date Column
  • Loading branch information
sakanjo authored Jun 7, 2024
2 parents ad54ee4 + 4a4dc95 commit 9ab5d75
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Metrics/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ abstract class Metric

protected bool $withGrowthRate = false;

protected ?string $dateColumn = null;

protected GrowthRateType $growthRateType = GrowthRateType::Percentage;

/**
Expand Down Expand Up @@ -111,9 +113,16 @@ public function getRanges(): array
return $this->ranges;
}

public function dateColumn(string $dateColumn): static
{
$this->dateColumn = $dateColumn;

return $this;
}

protected function getDateColumn(): string
{
return $this->query->getModel()->getCreatedAtColumn();
return $this->dateColumn ?? $this->query->getModel()->getCreatedAtColumn();
}

protected function resolveBetween(array $range): array
Expand Down
35 changes: 35 additions & 0 deletions tests/src/Value/CustomDateColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Support\Facades\Date;
use SaKanjo\EasyMetrics\Metrics\Value;
use SaKanjo\EasyMetrics\Tests\Models\User;
use SaKanjo\EasyMetrics\Tests\TestCase;

use function PHPUnit\Framework\assertEquals;

uses(TestCase::class);

it('shows correct data for count method with custom date column', function () {
User::factory()->create([
'email_verified_at' => Date::now()->subDays(2),
]);

User::factory()->create([
'email_verified_at' => Date::now()->subDays(10),
]);

User::factory()->create([
'email_verified_at' => Date::now()->subDays(20),
]);

User::factory()->create([
'email_verified_at' => null,
]);

$data = Value::make(User::class)
->dateColumn('email_verified_at')
->range(15)
->count();

assertEquals(2, $data);
});

0 comments on commit 9ab5d75

Please sign in to comment.