Skip to content

Commit

Permalink
Add forMonth and forYear methods
Browse files Browse the repository at this point in the history
Update README.md
  • Loading branch information
eliseekn committed Aug 25, 2023
1 parent e37dcf4 commit 4c7d5d2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ LaravelMetrics::query(Order::query())
->byYear()
->metrics();

// generate total count of the product for the current day of the current week
// generate total count of the product for the current day of the current month
LaravelMetrics::query(Product::query())
->count()
->byDay(1)
Expand All @@ -56,7 +56,7 @@ LaravelMetrics::query(Product::query())
->byWeek()
->metrics();

// generate trends of count of posts for the current week
// generate trends of count of posts for the current month
// by using a custom query and a custom date column
LaravelMetrics::query(
Post::query()->where('user_id', auth()->id())
Expand All @@ -74,6 +74,14 @@ LaravelMetrics::query(
->between('2020-05-01', '2022-08-21')
->trends();

// generate total count of the orders for the current year
// by using a custom label column
LaravelMetrics::query(Order::query())
->count()
->byMonth(12)
->labelColumn('status')
->trends();

// generate total count of the orders for the current year
// by using a custom label column
LaravelMetrics::query(Order::query())
Expand All @@ -100,6 +108,26 @@ $count > 1 => for an interval of day, week, month or year from the $count value
$period = 'day', 'week', 'month' or 'year'
```

#### Notes
Periods are typically defined for the current month and/or year. However, you also have the option to define a specific month or year using dedicated methods. For example:
```php
// generate total count of the orders for the year 2023
//// by using a custom label column
LaravelMetrics::query(Order::query())
->count()
->byMonth(12)
->forYear(2023)
->labelColumn('status')
->trends();

// generate total count of the product for the current day of the month february
LaravelMetrics::query(Product::query())
->count()
->byDay(1)
->forMonth(2)
->metrics();
```

### Types of aggregates
```php
->count(string $column = 'id')
Expand Down
14 changes: 12 additions & 2 deletions src/LaravelMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ public function between(string $start, string $end): self
return $this;
}

public function forMonth(int $month): self
{
$this->month = $month;
return $this;
}

public function forYear(int $year): self
{
$this->year = $year;
return $this;
}

public function aggregate(string $aggregate, string $column): self
{
$aggregate = strtolower($aggregate);
Expand Down Expand Up @@ -158,7 +170,6 @@ protected function metricsData(): mixed
->selectRaw($this->asData("$this->aggregate($this->column)"))
->whereYear($this->dateColumn, $this->year)
->whereMonth($this->dateColumn, $this->month)
->where(DB::raw($this->formatPeriod(Period::WEEK->value)), $this->week)
->when($this->count === 1, function (QueryBuilder $query) {
return $query->where(DB::raw("day($this->dateColumn)"), $this->day);
})
Expand Down Expand Up @@ -233,7 +244,6 @@ protected function trendsData(): Collection
->selectRaw($this->asData("$this->aggregate($this->column)") . ", " . $this->asLabel(Period::DAY->value))
->whereYear($this->dateColumn, $this->year)
->whereMonth($this->dateColumn, $this->month)
->where(DB::raw($this->formatPeriod(Period::WEEK->value)), $this->week)
->when($this->count === 1, function (QueryBuilder $query) {
return $query->where(DB::raw("day($this->dateColumn)"), $this->day);
})
Expand Down

0 comments on commit 4c7d5d2

Please sign in to comment.