@marcoplus Can you wrap your code with 3 ` please so that it will be readable. :)
Feb 2, 2023
19
Level 6
Laravel count the first three and for each hour
I am following a post, I managed to receive the response from the server with the code that I took and adapted but I don't understand why I only count the totals and they are not divided, I have to count the first three for each hour and say up, while all the others say down, every hour repeat the same operation, how could i do it?
$uptimeData = Ticket::where('created_at', '>=', '2023-01-01 00:00:00')
->where('created_at', '<=', '2023-01-031 00:00:00')
->orderBy('created_at', 'asc')
->select('ticket_Id', 'created_at')
->get();
$intervals = \Carbon\CarbonInterval::hours(1)->toPeriod('2023-01-01 00:00:00', '2023-01-31 00:00:00');
$uptimeDataTimeline = $uptimeData->groupBy(function ($item, $key) use ($intervals) {
$date = Carbon::parse($item->created_at);
foreach ($intervals as $key => $interval) {
if ($date->hour == Carbon::parse($interval)->addHours(1)->hour) {
$actualHour1 = Carbon::parse($interval)->hour;
if (strlen($actualHour1) == 1) $actualHour1 = "0$actualHour1";
return $date->format("Y-m-d $actualHour1:00:00");
}
else if ($date->hour == Carbon::parse($interval)->addHours(1)->hour) {
$actualHour2 = Carbon::parse($interval)->subHours(1)->hour;
if (strlen($actualHour2) == 1) $actualHour2 = "0$actualHour2";
return $date->format("Y-m-d $actualHour2:00:00");
}
}
return $date->format('Y-m-d H:00:00');
});
$uptimeDataTimeline = $uptimeDataTimeline->map(function($checksInPeriod, $key){
$down = 0;
$up = 0;
$total = 0;
$uptime = 0;
$fill = '#1fc777';
foreach($checksInPeriod as $key => $value){
$total++;
if(strtotime($value['ticket_Id']) == 'down') $down++;
if(strtotime($value['ticket_Id']) == 'up') $up++;
}
$uptime = floatval(number_format(round($up / $total, 5) * 100, 2, '.',','));
if ($uptime < 100) $fill = '#9deab8';
if ($uptime < 99) $fill = '#fbaa49';
if ($uptime < 98) $fill = '#e0465e';
return [
'total_ticket_Id' => $total,
'down_ticket_Id' => $down,
'up_ticket_Id' => $up,
'uptime' => $uptime,
'fill' => $fill
];
});
Illuminate\Support\Collection {#3861 ▼ // app/Http/Controllers/TicketsController.php:112
#items: array:492 [▶]
#escapeWhenCastingToString: false
}
Level 55
It's a little hard to understand the error as it's not formatted correctly but I have modified your code a little bit so try this:
$uptimeDataTimeline = $uptimeDataTimeline->map(function($checksInPeriod, $key){
$down = 0;
$up = 0;
$total = 0;
$uptime = 0;
$fill = '#1fc777';
// You have an issue with your loop:
foreach($checksInPeriod as $key => $value){
$total++;
if ($total <= 3) {
$up++;
} else {
$down++;
}
}
$uptime = floatval(number_format(round($up / $total, 5) * 100, 2, '.',','));
if ($uptime < 100) $fill = '#9deab8';
if ($uptime < 99) $fill = '#fbaa49';
if ($uptime < 98) $fill = '#e0465e';
return [
'total_ticket_Id' => $total,
'down_ticket_Id' => $down,
'up_ticket_Id' => $up,
'uptime' => $uptime,
'fill' => $fill
];
});
1 like
Please or to participate in this conversation.