Skip to content

Commit

Permalink
Add PostgreSQL support
Browse files Browse the repository at this point in the history
Update CHANGELOG.md
Update README.md
  • Loading branch information
eliseekn committed Dec 29, 2023
1 parent f82c4ce commit 94e93d6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.8.0

- Add PostgreSQL support

## 2.7.4

- Add "from" period to set custom startDate end use the current date as endDate for between period
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ composer require eliseekn/laravel-metrics
```

## Features
- MySQL support
- MySQL and PostgreSQL support
- Verbose query builder
- Custom columns and table definition
- Days and months translation with Carbon
Expand Down Expand Up @@ -120,11 +120,11 @@ Order::metrics()
### Types of periods
```php
LaravelMetrics::query(...)
->byDay(int $count = 0) // or
->byWeek(int $count = 0) // or
->byMonth(int $count = 0) // or
->byYear(int $count = 0) // or
->between(string $startDate, string $endDate, string $dateIsoFormat)
->byDay(int $count = 0) //or
->byWeek(int $count = 0) //or
->byMonth(int $count = 0) //or
->byYear(int $count = 0) //or
->between(string $startDate, string $endDate, string $dateIsoFormat) //or
->from(string $date, string $dateIsoFormat)
```

Expand Down Expand Up @@ -158,17 +158,17 @@ LaravelMetrics::query(...)
### Types of aggregates
```php
LaravelMetrics::query(...)
->count(string $column = 'id') // or
->average(string $column) // or
->sum(string $column) // or
->max(string $column) // or
->count(string $column = 'id') //or
->average(string $column) //or
->sum(string $column) //or
->max(string $column) //or
->min(string $column)
```

### Types of data
```php
LaravelMetrics::query(...)
->trends() // or
->trends() //or
->metrics()
```

Expand Down
29 changes: 24 additions & 5 deletions src/DatesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,31 @@ protected function getMonthPeriod(): array

protected function formatPeriod(string $period): string
{
$driver = $this->builder->getConnection()->getDriverName();

if ($driver === 'mysql') {
return match ($period) {
Period::DAY->value => "weekday($this->dateColumn)",
Period::WEEK->value => "week($this->dateColumn)",
Period::MONTH->value => "month($this->dateColumn)",
default => "year($this->dateColumn)",
};
}

if ($driver === 'pgsql') {
return match ($period) {
Period::DAY->value => "EXTRACT(DOW FROM $this->dateColumn)",
Period::WEEK->value => "EXTRACT(WEEK FROM $this->dateColumn)",
Period::MONTH->value => "EXTRACT(MONTH FROM $this->dateColumn)",
default => "EXTRACT(YEAR FROM $this->dateColumn)",
};
}

return match ($period) {
Period::DAY->value => "weekday($this->dateColumn)",
Period::WEEK->value => "week($this->dateColumn)",
Period::MONTH->value => "month($this->dateColumn)",
Period::YEAR->value => "year($this->dateColumn)",
default => '',
Period::DAY->value => "strftime('%w', $this->dateColumn)",
Period::WEEK->value => "strftime('%W', $this->dateColumn)",
Period::MONTH->value => "strftime('%m', $this->dateColumn)",
default => "strftime('%Y', $this->dateColumn)",
};
}

Expand Down
1 change: 1 addition & 0 deletions src/LaravelMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function byYear(int $count = 0): self
{
return $this->by(Period::YEAR->value, $count);
}

public function between(string $start, string $end, string $dateIsoFormat = 'YYYY-MM-DD'): self
{
$this->checkDateFormat([$start, $end]);
Expand Down

0 comments on commit 94e93d6

Please sign in to comment.