diff --git a/.idea/laravel-metrics.iml b/.idea/laravel-metrics.iml index 9acac01..4d75f98 100644 --- a/.idea/laravel-metrics.iml +++ b/.idea/laravel-metrics.iml @@ -3,8 +3,8 @@ - + diff --git a/CHANGELOG.md b/CHANGELOG.md index 39ba228..9153195 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/DatesFunctions.php b/src/DatesFunctions.php index 416f4cc..5fa3043 100644 --- a/src/DatesFunctions.php +++ b/src/DatesFunctions.php @@ -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) { diff --git a/src/LaravelMetrics.php b/src/LaravelMetrics.php index 3bb0e13..3439b6c 100644 --- a/src/LaravelMetrics.php +++ b/src/LaravelMetrics.php @@ -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(); } @@ -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(); @@ -713,7 +713,7 @@ public function metricsWithVariations(int $previousCount, string $previousPeriod } elseif ($value < 0) { $result['variation'] = [ 'type' => 'decrease', - 'value' => $value, + 'value' => abs($value), ]; }