fix 🐛 error sentry on version 7.3
This commit is contained in:
parent
69e1d855de
commit
e41f3e8a67
2 changed files with 69 additions and 33 deletions
|
|
@ -72,29 +72,43 @@ class StatsController extends Controller
|
|||
* - total of specific url from a brand
|
||||
* - total of all urls for each brand
|
||||
*/
|
||||
$datasets = match (true) {
|
||||
!$requestValidated['brand'] => $this->buildDatasets(
|
||||
if (!$requestValidated['brand']) {
|
||||
$datasets = $this->buildDatasets(
|
||||
Brand::all(),
|
||||
$periodInterval,
|
||||
$requestValidated['period'],
|
||||
fn($brand, $date, $period) => $this->getCallsSum($brand, $date, $period),
|
||||
fn($brand) => $brand->name
|
||||
),
|
||||
!$requestValidated['url'] => $this->buildDatasets(
|
||||
function ($brand, $date, $period) {
|
||||
return $this->getCallsSum($brand, $date, $period);
|
||||
},
|
||||
function ($brand) {
|
||||
return $brand->name;
|
||||
}
|
||||
);
|
||||
} elseif (!$requestValidated['url']) {
|
||||
$datasets = $this->buildDatasets(
|
||||
Brand::find($requestValidated['brand'])->urls,
|
||||
$periodInterval,
|
||||
$requestValidated['period'],
|
||||
fn($url, $date, $period) => $this->getCallsSum($url, $date, $period),
|
||||
fn($url) => $url->value
|
||||
),
|
||||
default => $this->buildDatasets(
|
||||
function ($url, $date, $period) {
|
||||
return $this->getCallsSum($url, $date, $period);
|
||||
},
|
||||
function ($url) {
|
||||
return $url->value;
|
||||
}
|
||||
);
|
||||
} else {
|
||||
$datasets = $this->buildDatasets(
|
||||
[Url::find($requestValidated['url'])],
|
||||
$periodInterval,
|
||||
$requestValidated['period'],
|
||||
fn($url, $date, $period) => $this->getCallsSum($url, $date, $period),
|
||||
fn($url) => $url->value
|
||||
),
|
||||
}; //end match
|
||||
function ($url, $date, $period) {
|
||||
return $this->getCallsSum($url, $date, $period);
|
||||
},
|
||||
function ($url) {
|
||||
return $url->value;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Get the type of graph when there are several data or only one.
|
||||
$chartType = count(Arr::first($datasets)['data'] ?? []) > 1 ? 'line' : 'bar';
|
||||
|
|
@ -157,15 +171,21 @@ class StatsController extends Controller
|
|||
return UrlStats::query()
|
||||
->when(
|
||||
$period === PeriodEnum::day()->value,
|
||||
fn($query) => $query->whereDate('date', $date)
|
||||
function ($query) use ($date) {
|
||||
return $query->whereDate('date', $date);
|
||||
}
|
||||
)
|
||||
->when(
|
||||
$period === PeriodEnum::week()->value,
|
||||
fn($query) => $query->whereBetween('date', [$date->copy()->startOfWeek(), $date->copy()->endOfWeek()])
|
||||
function ($query) use ($date) {
|
||||
return $query->whereBetween('date', [$date->copy()->startOfWeek(), $date->copy()->endOfWeek()]);
|
||||
}
|
||||
)
|
||||
->when(
|
||||
$period === PeriodEnum::month()->value,
|
||||
fn($query) => $query->whereBetween('date', [$date->copy()->startOfMonth(), $date->copy()->endOfMonth()])
|
||||
function ($query) use ($date) {
|
||||
return $query->whereBetween('date', [$date->copy()->startOfMonth(), $date->copy()->endOfMonth()]);
|
||||
}
|
||||
)
|
||||
->whereIn('url_id', $urlIds)
|
||||
->sum('calls');
|
||||
|
|
@ -240,17 +260,22 @@ class StatsController extends Controller
|
|||
private function getChartLabels(Carbon $date, string $period): array
|
||||
{
|
||||
// Match given period with enum names.
|
||||
return match ($period) {
|
||||
$this->getEnumName(PeriodEnum::week()) => array_unique([sprintf(
|
||||
'Du %s au %s',
|
||||
$date->startOfWeek()->format('Y-m-d'),
|
||||
$date->endOfWeek()->format('Y-m-d')
|
||||
)]),
|
||||
$this->getEnumName(PeriodEnum::month()) => array_unique([
|
||||
Str::ucfirst($date->monthName)
|
||||
]),
|
||||
default => array_unique([$date->format('d-m-Y')]),
|
||||
};
|
||||
switch ($period) {
|
||||
case $this->getEnumName(PeriodEnum::week()):
|
||||
return array_unique([sprintf(
|
||||
'Du %s au %s',
|
||||
$date->startOfWeek()->format('Y-m-d'),
|
||||
$date->endOfWeek()->format('Y-m-d')
|
||||
)]);
|
||||
|
||||
case $this->getEnumName(PeriodEnum::month()):
|
||||
return array_unique([
|
||||
Str::ucfirst($date->monthName)
|
||||
]);
|
||||
|
||||
default:
|
||||
return array_unique([$date->format('d-m-Y')]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -34,11 +34,22 @@ class StatsRequest extends FormRequest
|
|||
$adjustedEndDate = $endDate->greaterThan($now) ? $now : $endDate;
|
||||
|
||||
// Get start & end dates by period.
|
||||
[$adjustedStart, $adjustedEnd] = match ($this->input('period')) {
|
||||
'week' => [$startDate->copy()->startOfWeek(), $endDate->copy()->endOfWeek()],
|
||||
'month' => [$startDate->copy()->startOfMonth(), $endDate->copy()->endOfMonth()],
|
||||
default => [$startDate, $adjustedEndDate],
|
||||
};
|
||||
switch ($this->input('period')) {
|
||||
case 'week':
|
||||
$adjustedStart = $startDate->copy()->startOfWeek();
|
||||
$adjustedEnd = $endDate->copy()->endOfWeek();
|
||||
break;
|
||||
|
||||
case 'month':
|
||||
$adjustedStart = $startDate->copy()->startOfMonth();
|
||||
$adjustedEnd = $endDate->copy()->endOfMonth();
|
||||
break;
|
||||
|
||||
default:
|
||||
$adjustedStart = $startDate;
|
||||
$adjustedEnd = $adjustedEndDate;
|
||||
break;
|
||||
}
|
||||
|
||||
$this->merge([
|
||||
'url' => Url::find($urlId) ? $urlId : null,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue