Skip to content

Commit

Permalink
Add custom label column definition
Browse files Browse the repository at this point in the history
Move periods and aggregates constants to enums
Update README.md
  • Loading branch information
eliseekn committed Aug 24, 2023
1 parent 977d965 commit 23a1c03
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 83 deletions.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ composer require eliseekn/laravel-metrics
## Features
- MySQL support
- Verbose query builder
- Custom date column definition
- Days and months names translation
- Custom columns definition support
- Days and months names translation with Carbon

## Usage
Import the `Eliseekn\LaravelMetrics\LaravelMetrics` class in your controller and use it as follows :
Expand Down Expand Up @@ -57,7 +57,7 @@ LaravelMetrics::query(Product::query())
->metrics();

// generate trends of count of posts for the current week
// by using a custom query and a specific date column (published_at)
// by using a custom query and a custom date column
LaravelMetrics::query(
Post::query()->where('user_id', auth()->id())
)
Expand All @@ -73,6 +73,14 @@ LaravelMetrics::query(
->count()
->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();
```

### Types of periods
Expand All @@ -81,8 +89,8 @@ LaravelMetrics::query(
->byWeek(int $count = 0)
->byMonth(int $count = 0)
->byYear(int $count = 0)
->by(string $period, $count = 0)
->between($startDate, $endDate)
->between(string $startDate, string $endDate)
->by(string $period, int $count = 0)
```

```php
Expand All @@ -94,11 +102,12 @@ $period = 'day', 'week', 'month' or 'year'

### Types of aggregates
```php
->count('column')
->average('column')
->sum('column')
->max('column')
->min('column')
->count(string $column = 'id')
->average(string $column)
->sum(string $column)
->max(string $column)
->min(string $column)
->aggregate(string $aggregate, string $column)
```

### Types of data
Expand Down
18 changes: 18 additions & 0 deletions src/Enums/Aggregate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

namespace Eliseekn\LaravelMetrics\Enums;

enum Aggregate: string
{
case COUNT = 'count';
case AVERAGE = 'avg';
case SUM = 'sum';
case MAX = 'max';
case MIN = 'min';

public static function values(): array
{
return array_column(self::cases(), 'value');
}
}
17 changes: 17 additions & 0 deletions src/Enums/Period.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace Eliseekn\LaravelMetrics\Enums;

enum Period: string
{
case DAY = 'day';
case WEEK = 'week';
case MONTH = 'month';
case YEAR = 'year';

public static function values(): array
{
return array_column(self::cases(), 'value');
}
}
16 changes: 16 additions & 0 deletions src/Exceptions/InvalidAggregateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace Eliseekn\LaravelMetrics\Exceptions;

use Exception;

/**
* This exception occurs when date format is invalid
*/
class InvalidAggregateException extends Exception
{
public function __construct() {
parent::__construct('Invalid aggregate value. Valid aggregate is count, sum, max, min or avg');
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidPeriodException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
class InvalidPeriodException extends Exception
{
public function __construct() {
parent::__construct('Invalid period value. Valid period is day, month or year');
parent::__construct('Invalid period value. Valid period is day, week, month or year');
}
}
Loading

0 comments on commit 23a1c03

Please sign in to comment.