93 lines
2.8 KiB
PHP
93 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Models\Department;
|
|
use App\Models\Feedback;
|
|
use Filament\Widgets\ChartWidget;
|
|
use Illuminate\Contracts\Support\Htmlable;
|
|
|
|
class HandlerPerformanceWidget extends ChartWidget
|
|
{
|
|
protected int|string|array $columnSpan = 'full';
|
|
|
|
public function getHeading(): string | Htmlable | null
|
|
{
|
|
return __('app.widget_avg_time_by_handler');
|
|
}
|
|
|
|
public function getDescription(): string | Htmlable | null
|
|
{
|
|
return __('app.widget_avg_time_by_handler_desc');
|
|
}
|
|
|
|
protected function getType(): string
|
|
{
|
|
return 'bar';
|
|
}
|
|
|
|
protected function getData(): array
|
|
{
|
|
$user = auth()->user();
|
|
|
|
$query = Feedback::query()
|
|
->whereIn('status', ['resolved', 'closed'])
|
|
->whereNotNull('assigned_to');
|
|
|
|
if ($user->hasRole('manager') && ! $user->hasRole('admin')) {
|
|
$deptIds = Department::where('manager_id', $user->id)->pluck('id');
|
|
$query->whereIn('current_department_id', $deptIds);
|
|
}
|
|
|
|
$handlers = (clone $query)
|
|
->select('assigned_to')
|
|
->selectRaw('AVG(strftime("%s", updated_at) - strftime("%s", created_at)) / 60 as avg_minutes')
|
|
->groupBy('assigned_to')
|
|
->with('assignedTo')
|
|
->get();
|
|
|
|
$labels = $handlers->map(fn ($h) => $h->assignedTo?->name ?? __('app.widget_unknown'))->toArray();
|
|
$data = $handlers->map(fn ($h) => round($h->avg_minutes, 1))->toArray();
|
|
|
|
$maxMinutes = max($data) ?: 1;
|
|
$colors = collect($data)->map(function ($minutes) use ($maxMinutes) {
|
|
$ratio = $minutes / $maxMinutes;
|
|
if ($ratio > 0.8) {
|
|
return 'rgba(220, 38, 38, 0.8)';
|
|
} elseif ($ratio > 0.5) {
|
|
return 'rgba(217, 119, 6, 0.8)';
|
|
}
|
|
return 'rgba(26, 86, 219, 0.8)';
|
|
})->toArray();
|
|
|
|
$borderColors = collect($data)->map(function ($minutes) use ($maxMinutes) {
|
|
$ratio = $minutes / $maxMinutes;
|
|
if ($ratio > 0.8) {
|
|
return 'rgb(220, 38, 38)';
|
|
} elseif ($ratio > 0.5) {
|
|
return 'rgb(217, 119, 6)';
|
|
}
|
|
return 'rgb(26, 86, 219)';
|
|
})->toArray();
|
|
|
|
return [
|
|
'datasets' => [
|
|
[
|
|
'label' => __('app.widget_avg_time_minutes'),
|
|
'data' => $data,
|
|
'backgroundColor' => $colors,
|
|
'borderColor' => $borderColors,
|
|
'borderWidth' => 1,
|
|
'borderRadius' => 6,
|
|
],
|
|
],
|
|
'labels' => $labels,
|
|
];
|
|
}
|
|
|
|
public static function canView(): bool
|
|
{
|
|
return auth()->user()?->hasPermissionTo('view-dashboard-widgets') ?? false;
|
|
}
|
|
}
|