Skip to content

Added configurable date column #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,46 @@ class MyProjectableModel extends Model
];
}
```
If you want to use a different date field from your Model instead of created_at then do the following :
1) Make sure the field is casted to Carbon

```php
use App\Models\Projections\MyProjection;
use TimothePearce\TimeSeries\Projectable;

class MyProjectableModel extends Model
{
use Projectable;

protected $casts = [
'other_date_time' => 'datetime:Y-m-d H:00',
];

protected array $projections = [
MyProjection::class,
];
}
```
2) Add the dateColumn field in your Projection
```php
namespace App\Models\Projections;

use Illuminate\Database\Eloquent\Model;
use TimothePearce\TimeSeries\Contracts\ProjectionContract;
use TimothePearce\TimeSeries\Models\Projection;

class MyProjection extends Projection implements ProjectionContract
{
/**
* The projected periods.
*/
public array $periods = [];

public string $dateColumn = 'other_date_time';
....

```


### Implement a Projection

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"php": "^8.0"
},
"require-dev": {
"brianium/paratest": "^6.11",
"nunomaduro/collision": "^6.0",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^7.0",
Expand Down
12 changes: 9 additions & 3 deletions src/Projector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@

class Projector
{
public string $dateColumn = 'created_at';

public function __construct(
protected Model $projectedModel,
protected string $projectionName,
protected string $eventName,
) {
$projection = (new $this->projectionName());
if (isset($projection->dateColumn)) {
$this->dateColumn = $projection->dateColumn;
}
}

/**
Expand Down Expand Up @@ -104,7 +110,7 @@ private function findProjection(string $period): Projection|null
['projection_name', $this->projectionName],
['key', $this->hasKey() ? $this->key() : null],
['period', $period],
['start_date', app(TimeSeries::class)->resolveFloorDate($this->projectedModel->created_at, $period)],
['start_date', app(TimeSeries::class)->resolveFloorDate($this->projectedModel->{$this->dateColumn}, $period)],
]);
}

Expand All @@ -117,7 +123,7 @@ private function createProjection(string $period): void
'projection_name' => $this->projectionName,
'key' => $this->hasKey() ? $this->key() : null,
'period' => $period,
'start_date' => app(TimeSeries::class)->resolveFloorDate($this->projectedModel->created_at, $period),
'start_date' => app(TimeSeries::class)->resolveFloorDate($this->projectedModel->{$this->dateColumn}, $period),
'content' => $this->mergeProjectedContent((new $this->projectionName())->defaultContent(), $period),
]);
}
Expand Down Expand Up @@ -193,7 +199,7 @@ private function resolveCallableMethod(array $content, string $period): array
*/
private function resolveStartDate(string $periodType, int $quantity): Carbon
{
$startDate = $this->projectedModel->created_at->floorUnit($periodType, $quantity);
$startDate = $this->projectedModel->{$this->dateColumn}->floorUnit($periodType, $quantity);

if (in_array($periodType, ['week', 'weeks'])) {
$startDate->startOfWeek(config('time-series.beginning_of_the_week'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace TimothePearce\TimeSeries\Tests\Models\Projections;

use Illuminate\Database\Eloquent\Model;
use TimothePearce\TimeSeries\Contracts\ProjectionContract;
use TimothePearce\TimeSeries\Models\Projection;

class TableReservationPerDiningDayProjection extends Projection implements ProjectionContract
{
/**
* Lists the time intervals used to compute the projections.
*/
public array $periods = ['1 day'];

public string $dateColumn = 'reservation_date';

/**
* The default projection content.
*/
public function defaultContent(): array
{
return [
'total_people' => 0,
'number_reservations' => 0,
];
}

/**
* Computes the content when a projectable model is created.
*/
public function projectableCreated(array $content, Model $model): array
{
return [
'total_people' => $content['total_people'] += $model->number_people,
'number_reservations' => $content['number_reservations'] + 1,
];
}

/**
* Computes the content when a projectable model is deleted.
*/
public function projectableDeleted(array $content, Model $model): array
{
return [
'total_people' => $content['total_people'] -= $model->number_people,
'number_reservations' => $content['number_reservations'] - 1,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace TimothePearce\TimeSeries\Tests\Models\Projections;

use Illuminate\Database\Eloquent\Model;
use TimothePearce\TimeSeries\Contracts\ProjectionContract;
use TimothePearce\TimeSeries\Models\Projection;

class TableReservationPerDiningDayProjectionWithKey extends TableReservationPerDiningDayProjection
{
/**
* The key used to query the projection.
*/
public static function key(Model $model): string
{
return (string)$model->table_id;
}
}
14 changes: 14 additions & 0 deletions tests/Models/Projections/TableReservationPerMadeDayProjection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace TimothePearce\TimeSeries\Tests\Models\Projections;

use Illuminate\Database\Eloquent\Model;
use TimothePearce\TimeSeries\Contracts\ProjectionContract;
use TimothePearce\TimeSeries\Models\Projection;

class TableReservationPerMadeDayProjection extends TableReservationPerDiningDayProjection
{
public function __construct() {
$this->dateColumn = 'reservation_made_date';
}
}
29 changes: 29 additions & 0 deletions tests/Models/TableReservation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace TimothePearce\TimeSeries\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use TimothePearce\TimeSeries\Models\Traits\Projectable;
use TimothePearce\TimeSeries\Tests\Models\Projections\TableReservationPerMadeDayProjection;
use TimothePearce\TimeSeries\Tests\Models\Projections\TableReservationPerDiningDayProjection;

class TableReservation extends Model
{
use HasFactory;
use Projectable;
use SoftDeletes;

protected $casts = [
'reservation_date' => 'datetime:Y-m-d',
'reservation_made_date' => 'datetime:Y-m-d H:00',
];
/**
* The projections list.
*/
protected array $projections = [
TableReservationPerMadeDayProjection::class,
TableReservationPerDiningDayProjection::class,
];
}
Loading
Loading