Added guard hourly patrol chart
This commit is contained in:
249
app/Filament/Widgets/GuardPatrolHourlyChart.php
Normal file
249
app/Filament/Widgets/GuardPatrolHourlyChart.php
Normal file
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Models\CheckPointName;
|
||||
use App\Models\CheckPointTime;
|
||||
use App\Models\GuardPatrolEntry;
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
|
||||
class GuardPatrolHourlyChart extends ChartWidget
|
||||
{
|
||||
protected static ?string $heading = 'Chart';
|
||||
|
||||
protected static ?string $maxHeight = '330px';
|
||||
|
||||
protected int|string|array $columnSpan = 12;
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$selectedPlant = session('selected_plant');
|
||||
$selectedDate = session('selected_date');
|
||||
$selectedGuardName = session('selected_name');
|
||||
$selectedTime = session('selected_time');
|
||||
$validSessions = session('valid_sessions', []);
|
||||
|
||||
if (empty($selectedTime) || !isset($validSessions[$selectedTime]) || !$validSessions[$selectedTime]) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (!$selectedPlant || !$selectedDate || !$selectedGuardName || empty($selectedTime)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$query = GuardPatrolEntry::where('guard_patrol_entries.plant_id', $selectedPlant)
|
||||
->where('guard_patrol_entries.guard_name_id', $selectedGuardName)
|
||||
->whereDate('guard_patrol_entries.patrol_time', $selectedDate);
|
||||
|
||||
if (!empty($selectedTime))
|
||||
{
|
||||
[$startTime, $endTime] = explode(' - ', $selectedTime);
|
||||
$startDateTime = Carbon::parse($selectedDate . ' ' . $startTime);
|
||||
$endDateTime = Carbon::parse($selectedDate . ' ' . $endTime);
|
||||
$query->whereBetween('guard_patrol_entries.patrol_time', [$startDateTime, $endDateTime]);
|
||||
}
|
||||
$patrols = $query->orderBy('guard_patrol_entries.patrol_time')->get();
|
||||
|
||||
if ($patrols->count() < 2)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
$checkpointNames = CheckPointName::where('plant_id', $selectedPlant)
|
||||
->whereIn('id', $patrols->pluck('check_point_name_id')->unique())
|
||||
->orderBy('id') // or another column
|
||||
->pluck('name', 'id');
|
||||
|
||||
$nameToSequence = [];
|
||||
$sequence = 1;
|
||||
foreach ($checkpointNames as $id => $name)
|
||||
{
|
||||
$nameToSequence[$name] = $sequence++;
|
||||
}
|
||||
|
||||
$sequenceNumbers = array_values($nameToSequence);
|
||||
|
||||
$cushioningData = CheckPointTime::where('plant_id', $selectedPlant)
|
||||
->whereIn('sequence_number', $sequenceNumbers)
|
||||
->get(['sequence_number', 'min_cushioning', 'max_cushioning'])
|
||||
->keyBy('sequence_number');
|
||||
|
||||
$numSequences = count($cushioningData);
|
||||
|
||||
|
||||
$intervals = [];
|
||||
$labels = [];
|
||||
$colors = [];
|
||||
|
||||
for ($i = 0; $i < $patrols->count() - 1; $i++)
|
||||
{
|
||||
// Calculate interval
|
||||
$current = Carbon::parse($patrols[$i]->patrol_time);
|
||||
$next = Carbon::parse($patrols[$i+1]->patrol_time);
|
||||
$interval = $next->diffInMinutes($current, true);
|
||||
$intervals[] = $interval;
|
||||
|
||||
$labels[] = "Seq " . ($i + 1);
|
||||
$sequenceNumber = ($i % $numSequences) + 1;
|
||||
$cushioning = $cushioningData[$sequenceNumber] ?? null;
|
||||
|
||||
if (!$cushioning) {
|
||||
$colors[] = '#cccccc';
|
||||
continue;
|
||||
}
|
||||
|
||||
$min = $cushioning->min_cushioning;
|
||||
$max = $cushioning->max_cushioning;
|
||||
|
||||
if ($interval < $min) {
|
||||
$colors[] = '#ffd700';
|
||||
} elseif ($interval <= $max) {
|
||||
$colors[] = '#4caf50';
|
||||
} else {
|
||||
$colors[] = '#ff4c4c';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return [
|
||||
'labels' => $labels,
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => 'Interval (minutes)',
|
||||
'data' => $intervals,
|
||||
'backgroundColor' => $colors,
|
||||
'borderColor' => '#333',
|
||||
'borderWidth' => 1,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
// $colors = [];
|
||||
// $intervals = [];
|
||||
|
||||
// for ($i = 0; $i < $patrols->count() - 1; $i++) {
|
||||
// $current = Carbon::parse($patrols[$i]->patrol_time);
|
||||
// $next = Carbon::parse($patrols[$i+1]->patrol_time);
|
||||
// $interval = $next->diffInMinutes($current, true);
|
||||
// $intervals[] = $interval;
|
||||
// }
|
||||
|
||||
// $numSequences = count($sequenceNumbers);
|
||||
|
||||
// for ($i = 0; $i < $patrols->count() - 1; $i++)
|
||||
// {
|
||||
// $sequenceNumber = $sequenceNumbers[$i % $numSequences];
|
||||
// $cushioning = $cushioningData[$sequenceNumber] ?? null;
|
||||
|
||||
// if (!$cushioning) {
|
||||
// $colors[] = '#cccccc';
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $min = $cushioning->min_cushioning;
|
||||
// $max = $cushioning->max_cushioning;
|
||||
// $interval = $intervals[$i];
|
||||
|
||||
// if ($interval < $min) {
|
||||
// $colors[] = '#ffd700';
|
||||
// } elseif ($interval <= $max) {
|
||||
// $colors[] = '#4caf50';
|
||||
// } else {
|
||||
// $colors[] = '#ff4c4c';
|
||||
// }
|
||||
// }
|
||||
|
||||
// $labels = [];
|
||||
// for ($i = 0; $i < count($intervals); $i++) {
|
||||
// $labels[] = "Seq " . ($i + 1);
|
||||
// }
|
||||
|
||||
// return [
|
||||
// 'labels' => $labels,
|
||||
// 'datasets' => [
|
||||
// [
|
||||
// 'label' => 'Interval (minutes)',
|
||||
// 'data' => $intervals,
|
||||
// 'backgroundColor' => $colors,
|
||||
// 'borderColor' => '#333',
|
||||
// 'borderWidth' => 1,
|
||||
// ],
|
||||
// ],
|
||||
// ];
|
||||
|
||||
|
||||
// $colors = [];
|
||||
|
||||
// $intervals = [];
|
||||
// for ($i = 0; $i < $patrols->count() - 1; $i++) {
|
||||
// $current = Carbon::parse($patrols[$i]->patrol_time);
|
||||
// $next = Carbon::parse($patrols[$i+1]->patrol_time);
|
||||
// $interval = $next->diffInMinutes($current, true);
|
||||
// $intervals[] = $interval;
|
||||
// }
|
||||
|
||||
// for ($i = 0; $i < $patrols->count() - 1; $i++)
|
||||
// {
|
||||
// $checkpointId = $patrols[$i+1]->check_point_name_id;
|
||||
// $name = $checkpointNames[$checkpointId] ?? null;
|
||||
// $sequenceNumber = $nameToSequence[$name] ?? null;
|
||||
|
||||
// if (!$sequenceNumber)
|
||||
// {
|
||||
// $colors[] = '#cccccc';
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $cushioning = $cushioningData[$sequenceNumber] ?? null;
|
||||
|
||||
// if (!$cushioning)
|
||||
// {
|
||||
// $colors[] = '#cccccc';
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $min = $cushioning->min_cushioning;
|
||||
// $max = $cushioning->max_cushioning;
|
||||
// $interval = $intervals[$i];
|
||||
|
||||
// if ($interval < $min)
|
||||
// {
|
||||
// $colors[] = '#ffd700';
|
||||
// } elseif ($interval <= $max)
|
||||
// {
|
||||
// $colors[] = '#4caf50';
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// $colors[] = '#ff4c4c';
|
||||
// }
|
||||
// }
|
||||
|
||||
// $labels = [];
|
||||
// for ($i = 0; $i < count($intervals); $i++)
|
||||
// {
|
||||
// $labels[] = "Seq " . ($i + 1);
|
||||
// }
|
||||
|
||||
// return [
|
||||
// 'labels' => $labels,
|
||||
// 'datasets' => [
|
||||
// [
|
||||
// 'label' => 'Interval (minutes)',
|
||||
// 'data' => $intervals,
|
||||
// 'backgroundColor' => $colors,
|
||||
// 'borderColor' => '#333',
|
||||
// 'borderWidth' => 1,
|
||||
// ],
|
||||
// ],
|
||||
// ];
|
||||
}
|
||||
|
||||
protected function getType(): string
|
||||
{
|
||||
return 'bar';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user