Skip to content

Commit

Permalink
feat: added custom date range
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseekn committed Dec 30, 2021
1 parent 14b5ec9 commit 6eb5fa2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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

## 1.0.0 - 201X-XX-XX
## 1.0.2 - 2021-12-30

- initial release
- Added custom date range

## 1.0.1 - 2021-09-25

- Added Carbon package
- Removed unnecessary files and folder

## 1.0.0 - 2021-09-25

- Initial release
33 changes: 33 additions & 0 deletions src/LaravelMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ private static function getMetricsData(string $table, string $column, string $pe
$year = Carbon::now()->year;
$month = Carbon::now()->month;
$week = Carbon::now()->weekOfYear;

if (!in_array($period, [self::TODAY, self::DAY, self::WEEK, self::MONTH, self::YEAR, self::QUATER_YEAR, self::HALF_YEAR])) {
if (!str_contains('~', $period)) return null;

list($start_date, $end_date) = explode('~', $period);
$start_date = Carbon::parse($start_date)->toDateString();
$end_date = Carbon::parse($end_date)->toDateString();

return DB::table($table)
->selectRaw("$type($column) as data")
->whereBetween(DB::raw('date(created_at)'), [$start_date, $end_date])
->where(function ($q) use ($whereRaw) {
if (!is_null($whereRaw)) $q->whereRaw($whereRaw);
})
->first();
}

switch($period) {
case self::TODAY:
Expand Down Expand Up @@ -110,6 +126,23 @@ private static function getTrendsData(string $table, string $column, string $per
$month = Carbon::now()->month;
$week = Carbon::now()->weekOfYear;

if (!in_array($period, [self::TODAY, self::DAY, self::WEEK, self::MONTH, self::YEAR, self::QUATER_YEAR, self::HALF_YEAR])) {
if (!str_contains('~', $period)) return null;

list($start_date, $end_date) = explode('~', $period);

$start_date = Carbon::parse($start_date)->toDateString();
$end_date = Carbon::parse($end_date)->toDateString();

return DB::table($table)
->selectRaw("$type($column) as data, date(created_at) as label")
->whereBetween(DB::raw('date(created_at)'), [$start_date, $end_date])
->where(function ($q) use ($whereRaw) {
if (!is_null($whereRaw)) $q->whereRaw($whereRaw);
})
->first();
}

switch($period) {
case self::TODAY:
return DB::table($table)
Expand Down

0 comments on commit 6eb5fa2

Please sign in to comment.