Refactored the way incidents are pulled out of the database

Incidents are now pulled out via the database, grouped together via php, then missing days are added to the data
This commit is contained in:
Adam Lavin 2015-04-20 23:28:53 +01:00
parent 70a07e6d19
commit 4c4a32da3d
2 changed files with 14 additions and 13 deletions

View File

@ -74,23 +74,24 @@ class HomeController extends AbstractController
$metrics = Metric::where('display_chart', 1)->get();
}
$allIncidents = [];
$daysToShow = Setting::get('app_incident_days') ?: 7;
$incidentDays = range(0, $daysToShow);
$dateFormat = Setting::get('date_format') ?: 'jS F Y';
foreach ($incidentDays as $i) {
$allIncidents = Incident::notScheduled()->whereBetween('created_at', [
$startDate->copy()->subDays($daysToShow)->format('Y-m-d').' 00:00:00',
$startDate->format('Y-m-d').' 23:59:59',
])->orderBy('created_at', 'desc')->get()->groupBy(function(Incident $incident) use ($dateFormat) {
return $incident->created_at->format($dateFormat);
});
// Add in days that have no incidents
foreach($incidentDays as $i) {
$date = $startDate->copy()->subDays($i);
$incidents = Incident::notScheduled()->whereBetween('created_at', [
$date->format('Y-m-d').' 00:00:00',
$date->format('Y-m-d').' 23:59:59',
])->orderBy('created_at', 'desc')->get();
$allIncidents[] = [
'date' => (new Date($date->toDateString()))->format($dateFormat),
'incidents' => $incidents,
];
if (!isset($allIncidents[$date->format($dateFormat)])) {
$allIncidents[$date->format($dateFormat)] = [];
}
}
// Scheduled maintenance code.

View File

@ -34,8 +34,8 @@
@endif
<h1>{{ trans('cachet.incidents.past') }}</h1>
@foreach($allIncidents as $incidents)
@include('partials.incidents', $incidents)
@foreach($allIncidents as $date => $incidents)
@include('partials.incidents', compact('date', 'incidents'))
@endforeach
<hr>