Skip to content

Commit

Permalink
Add Combined periods and aggregates methods
Browse files Browse the repository at this point in the history
Update README.md
  • Loading branch information
eliseekn committed Oct 21, 2023
1 parent 9970f90 commit c39ef4b
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-metrics` will be documented in this file

## 2.6.1

- Add Combined periods and aggregates methods

## 2.6

- Add 'fillEmptyDates' method to fill data for empty dates
Expand Down
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ LaravelMetrics::query(...)
LaravelMetrics::query(Order::query())
->count()
->byMonth(12)
->forYear(2023)
->forYear(now()->year)
->labelColumn('status')
->trends();

Expand Down Expand Up @@ -181,6 +181,28 @@ LaravelMetrics::query(...)
->metrics()
```

### Combining periods and aggregates
Combining different time periods and data aggregates can enhance your overall experience. For example :

```php
LaravelMetrics::query(...)
->sumByMonth()
->trends();

LaravelMetrics::query(...)
->counyByMonth(count: 12)
->forYear(now()->year)
->labelColumn('status')
->trends();

LaravelMetrics::query(...)
->countBetween(Carbon::now()->subDays(10)->format('Y-m-d'), Carbon::now()->format('Y-m-d'))
->fillEmptyDates()
->trends();

...
```

## Translations

Days and months names are automatically translated using `config(app.locale)` except 'week' period.
Expand Down
170 changes: 170 additions & 0 deletions src/LaravelMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,176 @@ public function min(string $column): self
return $this->aggregate(Aggregate::MIN->value, $column);
}

protected function countBy(string $period, string $column = 'id', int $count = 0): self
{
return $this
->by($period, $count)
->aggregate(Aggregate::COUNT->value, $column);
}

protected function averageBy(string $period, string $column = 'id', int $count = 0): self
{
return $this
->by($period, $count)
->aggregate(Aggregate::AVERAGE->value, $column);
}

protected function sumBy(string $period, string $column = 'id', int $count = 0): self
{
return $this
->by($period, $count)
->aggregate(Aggregate::SUM->value, $column);
}

protected function maxBy(string $period, string $column = 'id', int $count = 0): self
{
return $this
->by($period, $count)
->aggregate(Aggregate::MAX->value, $column);
}

protected function minBy(string $period, string $column = 'id', int $count = 0): self
{
return $this
->by($period, $count)
->aggregate(Aggregate::MIN->value, $column);
}

public function countByDay(string $column = 'id', int $count = 0): self
{
return $this->countBy(Period::DAY->value, $column, $count);
}

public function countByWeek(string $column = 'id', int $count = 0): self
{
return $this->countBy(Period::WEEK->value, $column, $count);
}

public function countByMonth(string $column = 'id', int $count = 0): self
{
return $this->countBy(Period::MONTH->value, $column, $count);
}

public function countByYear(string $column = 'id', int $count = 0): self
{
return $this->countBy(Period::YEAR->value, $column, $count);
}

public function sumByDay(string $column, int $count = 0): self
{
return $this->sumBy(Period::DAY->value, $column, $count);
}

public function sumByWeek(string $column, int $count = 0): self
{
return $this->sumBy(Period::WEEK->value, $column, $count);
}

public function sumByMonth(string $column, int $count = 0): self
{
return $this->sumBy(Period::MONTH->value, $column, $count);
}

public function sumByYear(string $column, int $count = 0): self
{
return $this->sumBy(Period::YEAR->value, $column, $count);
}

public function averageByDay(string $column, int $count = 0): self
{
return $this->averageBy(Period::DAY->value, $column, $count);
}

public function averageByWeek(string $column, int $count = 0): self
{
return $this->averageBy(Period::WEEK->value, $column, $count);
}

public function averageByMonth(string $column, int $count = 0): self
{
return $this->averageBy(Period::MONTH->value, $column, $count);
}

public function averageByYear(string $column, int $count = 0): self
{
return $this->averageBy(Period::YEAR->value, $column, $count);
}

public function maxByDay(string $column, int $count = 0): self
{
return $this->maxBy(Period::DAY->value, $column, $count);
}

public function maxByWeek(string $column, int $count = 0): self
{
return $this->maxBy(Period::WEEK->value, $column, $count);
}

public function maxByMonth(string $column, int $count = 0): self
{
return $this->maxBy(Period::MONTH->value, $column, $count);
}

public function maxByYear(string $column, int $count = 0): self
{
return $this->maxBy(Period::YEAR->value, $column, $count);
}

public function minByDay(string $column, int $count = 0): self
{
return $this->minBy(Period::DAY->value, $column, $count);
}

public function minByWeek(string $column, int $count = 0): self
{
return $this->minBy(Period::WEEK->value, $column, $count);
}

public function minByMonth(string $column, int $count = 0): self
{
return $this->minBy(Period::MONTH->value, $column, $count);
}

public function minByYear(string $column, int $count = 0): self
{
return $this->minBy(Period::YEAR->value, $column, $count);
}

public function countBetween(array $period, string $column = 'id', string $dateIsoFormat = 'YYYY-MM-DD'): self
{
return $this
->count($column)
->between($period[0], $period[1], $dateIsoFormat);
}

public function sumBetween(array $period, string $column, string $dateIsoFormat = 'YYYY-MM-DD'): self
{
return $this
->sum($column)
->between($period[0], $period[1], $dateIsoFormat);
}

public function averageBetween(array $period, string $column, string $dateIsoFormat = 'YYYY-MM-DD'): self
{
return $this
->average($column)
->between($period[0], $period[1], $dateIsoFormat);
}

public function maxBetween(array $period, string $column, string $dateIsoFormat = 'YYYY-MM-DD'): self
{
return $this
->max($column)
->between($period[0], $period[1], $dateIsoFormat);
}

public function minBetween(array $period, string $column, string $dateIsoFormat = 'YYYY-MM-DD'): self
{
return $this
->min($column)
->between($period[0], $period[1], $dateIsoFormat);
}

public function dateColumn(string $column): self
{
$this->dateColumn = $this->table . '.' . $column;
Expand Down

0 comments on commit c39ef4b

Please sign in to comment.