Skip to content

Commit

Permalink
Fix date function by database driver
Browse files Browse the repository at this point in the history
Fix metrics negative variation value display
  • Loading branch information
eliseekn committed Apr 21, 2024
1 parent 61fdd30 commit 60f605e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .idea/laravel-metrics.iml

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

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

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

## 2.9.3

- Fix date function by database driver
- Fix metrics negative variation value display

## 2.9.2

- Rebase branch 2.9.0-beta-1 onto 2.x
Expand Down
11 changes: 11 additions & 0 deletions src/DatesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ protected function formatPeriod(string $period): string
};
}

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

return match ($driver) {
'mysql' => "date($this->dateColumn)",
'pgsql' => "TO_CHAR($this->dateColumn, 'YYYY-MM-DD')",
'default' => "strftime('%Y-%m-%d', $this->dateColumn)",
};
}

protected function formatDate(array $data): array
{
return array_map(function ($datum) {
Expand Down
8 changes: 4 additions & 4 deletions src/LaravelMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ protected function metricsData(): mixed
if (is_array($this->period)) {
return $this->builder
->selectRaw($this->asData())
->whereBetween(DB::raw("date($this->dateColumn)"), [$this->period[0], $this->period[1]])
->whereBetween(DB::raw($this->formatDateColumn()), [$this->period[0], $this->period[1]])
->first();
}

Expand Down Expand Up @@ -516,8 +516,8 @@ protected function trendsData(): Collection
{
if (is_array($this->period)) {
return $this->builder
->selectRaw($this->asData().', '.$this->asLabel("date($this->dateColumn)", false))
->whereBetween(DB::raw("date($this->dateColumn)"), [$this->period[0], $this->period[1]])
->selectRaw($this->asData().', '.$this->asLabel($this->formatDateColumn(), false))
->whereBetween(DB::raw($this->formatDateColumn()), [$this->period[0], $this->period[1]])
->groupBy('label')
->orderBy('label')
->get();
Expand Down Expand Up @@ -713,7 +713,7 @@ public function metricsWithVariations(int $previousCount, string $previousPeriod
} elseif ($value < 0) {
$result['variation'] = [
'type' => 'decrease',
'value' => $value,
'value' => abs($value),
];
}

Expand Down

0 comments on commit 60f605e

Please sign in to comment.