Skip to content

Commit

Permalink
Feat #5
Browse files Browse the repository at this point in the history
Add 'fillEmptyDates' method to fill data for empty dates
Fix get trends from 'between' method when using custom label
Update README.md
  • Loading branch information
eliseekn committed Oct 4, 2023
1 parent 8e00583 commit 1fc3b37
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 16 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

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

## 2.6

- Add 'fillEmptyDates' method to fill data for empty dates
- Fix get trends from 'between' method when using custom label

## 2.4.1

- Fix undefined array key 'data' exception when trends are empty
- Update README.md

## 2.4.0

Expand Down Expand Up @@ -34,7 +38,6 @@ All notable changes to `laravel-metrics` will be documented in this file

## 1.0.4 - 2022-01-04

- Update README.md
- Add demo project link

## 1.0.3 - 2021-12-30
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ LaravelMetrics::query(...)
->byWeek(int $count = 0) // or
->byMonth(int $count = 0) // or
->byYear(int $count = 0) // or
->between(string $startDate, string $endDate, string $isoFormat)
->between(string $startDate, string $endDate, string $dateIsoFormat)
```

**Note :** Periods are defined for the current day, week, month or year by default. However, you can define a specific value using dedicated methods. For example:
Expand Down Expand Up @@ -154,6 +154,16 @@ LaravelMetrics::query(...)
->forYear(int $year)
```

**Note :** Make sure to employ the 'fillEmptyDates' method when utilizing the 'between' period to automatically populate any missing dates with a default value. For example:

```php
LaravelMetrics::query(...)
->count()
->between(Carbon::now()->subDays(10)->format('Y-m-d'), Carbon::now()->format('Y-m-d'))
->fillEmptyDates()
->trends();
```

### Types of aggregates
```php
LaravelMetrics::query(...)
Expand Down
83 changes: 70 additions & 13 deletions src/LaravelMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class LaravelMetrics
protected int $month;
protected int $day;
protected int $week;
protected string $isoFormat = 'YYYY-MM-DD';
protected string $dateIsoFormat = 'YYYY-MM-DD';
protected bool $fillEmptyDates = false;
protected int $emptyDatesData = 0;

public function __construct(protected Builder|QueryBuilder $builder)
{
Expand Down Expand Up @@ -92,11 +94,11 @@ public function byYear(int $count = 0): self
return $this->by(Period::YEAR->value, $count);
}

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

Expand Down Expand Up @@ -174,6 +176,13 @@ public function labelColumn(string $column): self
return $this;
}

public function fillEmptyDates(int $value = 0): self
{
$this->fillEmptyDates = true;
$this->emptyDatesData = $value;
return $this;
}

protected function metricsData(): mixed
{
if (is_array($this->period)) {
Expand Down Expand Up @@ -328,6 +337,46 @@ protected function asLabel(?string $label = null, bool $format = true): string
return $label . " as label";
}

protected function generateDateRange($startDate, $endDate)
{
$startDate = Carbon::parse($startDate);
$endDate = Carbon::parse($endDate);

$dateRange = [];
$currentDate = $startDate;

while ($currentDate <= $endDate) {
$dateRange[] = $currentDate->format('Y-m-d');
$currentDate->addDay();
}

return $dateRange;
}

protected function fillEmptyDatesFromCollection(Collection $data): Collection
{
$dateRange = $this->generateDateRange($this->period[0], $this->period[1]);
$mergedData = [];

foreach ($dateRange as $date) {
$dataForDate = $data->where('label', $date)->first();

if ($dataForDate) {
$mergedData[] = [
'label' => $dataForDate->label,
'data' => $dataForDate->data
];
} else {
$mergedData[] = [
'label' => $date,
'data' => $this->emptyDatesData
];
}
}

return collect($mergedData);
}

/**
* Generate metrics data
*/
Expand All @@ -342,18 +391,22 @@ public function metrics(): mixed
*/
public function trends(): array
{
$trendsData = !is_null($this->period)
? $this->formatDate($this->trendsData())
: $this->trendsData();
$trendsData = $this->trendsData();

if ($this->fillEmptyDates) {
$trendsData = $this->fillEmptyDatesFromCollection($trendsData);
}

$trendsData = $this->formatDate($trendsData);

$result = [
'labels' => [],
'data' => []
];

foreach ($trendsData as $data) {
$result['labels'][] = $data->label;
$result['data'][] = $data->data;
$result['labels'][] = $data['label'];
$result['data'][] = $data['data'];
}

return $result;
Expand Down Expand Up @@ -419,16 +472,20 @@ protected function formatPeriod(string $period): string
protected function formatDate(Collection $data): Collection
{
return $data->map(function ($datum) {
if (!is_numeric($datum['label']) && !DateTime::createFromFormat('Y-m-d',$datum['label'])) {
return $datum;
}

if ($this->period === Period::MONTH->value) {
$datum->label = Carbon::parse($this->year . '-' . $datum->label)->locale(self::locale())->monthName;
$datum['label'] = Carbon::parse($this->year . '-' . $datum['label'])->locale(self::locale())->monthName;
} elseif ($this->period === Period::DAY->value) {
$datum->label = Carbon::parse($this->year . '-' . $this->month . '-' . $datum->label)->locale(self::locale())->dayName;
$datum['label'] = Carbon::parse($this->year . '-' . $this->month . '-' . $datum['label'])->locale(self::locale())->dayName;
} elseif ($this->period === Period::WEEK->value) {
$datum->label = 'Week ' . $datum->label;
$datum['label'] = 'Week ' . $datum['label'];
} elseif ($this->period === Period::YEAR->value) {
$datum->label = intval($datum->label);
$datum['label'] = intval($datum['label']);
} else {
$datum->label = Carbon::parse($datum->label)->locale(self::locale())->isoFormat($this->isoFormat);
$datum['label'] = Carbon::parse($datum['label'])->locale(self::locale())->isoFormat($this->dateIsoFormat);
}

return $datum;
Expand Down

0 comments on commit 1fc3b37

Please sign in to comment.