add month year group for graph data
This commit is contained in:
@@ -7,9 +7,7 @@ use App\Http\Resources\RainfallCollection;
|
||||
use App\Http\Resources\RainfallResource;
|
||||
use App\Models\Rainfall;
|
||||
use Carbon\Carbon;
|
||||
use Carbon\CarbonPeriod;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class RainfallController extends Controller
|
||||
{
|
||||
@@ -88,48 +86,28 @@ class RainfallController extends Controller
|
||||
$data = $request->validate([
|
||||
'start' => 'date',
|
||||
'end' => 'date',
|
||||
'period' => ['regex:/^day|week|month|year$/'],
|
||||
]);
|
||||
|
||||
$rainfalls = $request->user()
|
||||
->rainfalls()
|
||||
->whereBetween('date', [$data['start'], $data['end']])
|
||||
->orderBy('date')
|
||||
->get()
|
||||
->groupBy('date');
|
||||
->get();
|
||||
|
||||
$results = collect();
|
||||
$index = 0;
|
||||
foreach ($rainfalls as $date => $rainfall) {
|
||||
if ($index === 0 && $date !== $data['start']) {
|
||||
[$results, $index] = $this->addEmptyDays($results, $index, $data['start'], (new Carbon($date))->subDay());
|
||||
} elseif ($index > 0 && (new Carbon($results->last()['date']))->addDay()->format('Y-m-d') !== $date) {
|
||||
[$results, $index] = $this->addEmptyDays($results, $index, (new Carbon($results->last()['date']))->addDay(), (new Carbon($date))->subDay());
|
||||
$results = [];
|
||||
$currentDate = $data['start'];
|
||||
while ($currentDate <= $data['end']) {
|
||||
$key = Rainfall::getDateKey($currentDate, $data['period']);
|
||||
if (! isset($results[$key])) {
|
||||
$keyData = Rainfall::getDateKeyData($currentDate, $data);
|
||||
$results[$key] = [...$keyData, 'value' => 0];
|
||||
}
|
||||
|
||||
$results->push([
|
||||
'id' => $index++,
|
||||
'date' => $date,
|
||||
'value' => $rainfall->sum('value'),
|
||||
]);
|
||||
$results[$key]['value'] += $rainfalls->where('date', new Carbon($currentDate))->sum('value');
|
||||
$currentDate = (new Carbon($currentDate))->addDay()->format('Y-m-d');
|
||||
}
|
||||
|
||||
if ($results->isNotEmpty() && $results->last()['date'] !== $data['end']) {
|
||||
[$results] = $this->addEmptyDays($results, $index, (new Carbon($results->last()['date']))->addDay(), $data['end']);
|
||||
}
|
||||
|
||||
return response()->json($results);
|
||||
}
|
||||
|
||||
private function addEmptyDays(Collection $results, int $index, Carbon|string $start, Carbon|string $end)
|
||||
{
|
||||
foreach (CarbonPeriod::create($start, $end) as $date) {
|
||||
$results->push([
|
||||
'id' => $index++,
|
||||
'date' => $date->format('Y-m-d'),
|
||||
'value' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
return [$results, $index];
|
||||
return response()->json(array_values($results));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
@@ -10,6 +11,21 @@ class Rainfall extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
const MONTHS = [
|
||||
1 => 'Janvier',
|
||||
2 => 'Février',
|
||||
3 => 'Mars',
|
||||
4 => 'Avril',
|
||||
5 => 'Mai',
|
||||
6 => 'Juin',
|
||||
7 => 'Juillet',
|
||||
8 => 'Août',
|
||||
9 => 'Septembre',
|
||||
10 => 'Octobre',
|
||||
11 => 'Novembre',
|
||||
12 => 'Décembre',
|
||||
];
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
protected $casts = [
|
||||
@@ -20,4 +36,51 @@ class Rainfall extends Model
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public static function getDateKey(string $date, string $period)
|
||||
{
|
||||
$date = new Carbon($date);
|
||||
if ($period === 'year') {
|
||||
return $date->year;
|
||||
} elseif ($period === 'month') {
|
||||
return $date->year * 100 + $date->month;
|
||||
} elseif ($period === 'week') {
|
||||
return $date->year * 100 + $date->week;
|
||||
}
|
||||
|
||||
return $date->format('Y-m-d');
|
||||
}
|
||||
|
||||
public static function getDateKeyData(string $date, array $data): array
|
||||
{
|
||||
$date = new Carbon($date);
|
||||
$start = new Carbon($data['start']);
|
||||
$endDate = new Carbon($data['end']);
|
||||
$startDate = $date->format('Y-m-d 00:00:00');
|
||||
|
||||
if ($data['period'] === 'year') {
|
||||
$days = $start->year === $endDate->year ? $start->diffInDays($endDate)
|
||||
: (($date->year === $endDate->year || $start->year === $date->year)
|
||||
? $date->diffInDays($date->year === $endDate->year ? $endDate : $start->endOfYear()) + 1
|
||||
: $date->endOfYear()->day);
|
||||
$label = $date->year;
|
||||
} elseif ($data['period'] === 'month') {
|
||||
$days = ($date->month === $endDate->month || $date->month === $start->month)
|
||||
? $date->diffInDays($date->month === $endDate->month ? $endDate : $start->endOfMonth()) + 1
|
||||
: $date->endOfMonth()->day;
|
||||
$label = Rainfall::MONTHS[$date->month].' '.$date->year;
|
||||
} elseif ($data['period'] === 'week') {
|
||||
$days = $date->week === $endDate->week ? $date->diffInDays($endDate) + 1 : 7;
|
||||
$label = 'semaine '.$date->week;
|
||||
} else {
|
||||
$days = 1;
|
||||
$label = $date->format('d/m/Y');
|
||||
}
|
||||
|
||||
return [
|
||||
'start' => $startDate,
|
||||
'days' => $days,
|
||||
'label' => $label,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,11 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasApiTokens, HasFactory, Notifiable;
|
||||
use HasApiTokens, HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
|
||||
Reference in New Issue
Block a user