dashboard in better style 2

This commit is contained in:
2026-06-21 21:51:18 +03:30
parent eb4eff8002
commit eec7001c2f
2 changed files with 56 additions and 0 deletions
@@ -379,4 +379,59 @@ class DashboardController extends Controller
}
return response()->json(array_values($send));
}
public function orderPeakHours(Request $request)
{
$days = (int) $request->input('days', 30);
$days = min(max($days, 7), 90);
$start = now()->subDays($days)->startOfDay();
$rows = Order::query()
->where('sts', 2)
->where('type', 2)
->where('created_at', '>=', $start)
->select([
DB::raw('HOUR(created_at) as hour'),
'direction',
DB::raw('COUNT(*) as count'),
])
->groupBy('hour', 'direction')
->get();
$hours = [];
for ($h = 0; $h < 24; $h++) {
$hours[$h] = ['hour' => $h, 'buy' => 0, 'sell' => 0, 'total' => 0];
}
foreach ($rows as $row) {
$h = (int) $row->hour;
if ($h < 0 || $h > 23) {
continue;
}
$count = (int) $row->count;
if ((int) $row->direction === 1) {
$hours[$h]['buy'] += $count;
} elseif ((int) $row->direction === 2) {
$hours[$h]['sell'] += $count;
}
$hours[$h]['total'] += $count;
}
$peakHour = 0;
$peakCount = 0;
foreach ($hours as $slot) {
if ($slot['total'] > $peakCount) {
$peakCount = $slot['total'];
$peakHour = $slot['hour'];
}
}
return response()->json([
'hours' => array_values($hours),
'peak_hour' => $peakCount > 0 ? $peakHour : null,
'peak_count' => $peakCount,
'days' => $days,
'total_orders' => array_sum(array_column($hours, 'total')),
]);
}
}