5
5
use Cachet \Models \Incident ;
6
6
use Cachet \Settings \AppSettings ;
7
7
use Illuminate \Contracts \View \View ;
8
+ use Illuminate \Support \Carbon ;
8
9
use Illuminate \Support \Collection ;
9
10
use Illuminate \View \Component ;
10
11
@@ -17,33 +18,46 @@ public function __construct(private AppSettings $appSettings)
17
18
18
19
public function render (): View
19
20
{
21
+ $ incidentDays = $ this ->appSettings ->incident_days - 1 ;
22
+ $ startDate = Carbon::createFromFormat ('Y-m-d ' , request ('from ' , now ()->toDateString ()));
23
+ $ endDate = $ startDate ->clone ()->subDays ($ incidentDays );
24
+
20
25
return view ('cachet::components.incidents ' , [
21
- 'incidents ' => $ this ->incidents (),
26
+ 'incidents ' => $ this ->incidents (
27
+ $ startDate ,
28
+ $ endDate ,
29
+ $ this ->appSettings ->only_disrupted_days
30
+ ),
31
+ 'from ' => $ startDate ->toDateString (),
32
+ 'to ' => $ endDate ->toDateString (),
33
+ 'nextPeriodFrom ' => $ startDate ->clone ()->subDays ($ incidentDays + 1 )->toDateString (),
34
+ 'nextPeriodTo ' => $ startDate ->clone ()->addDays ($ incidentDays + 1 )->toDateString (),
35
+ 'canPageForward ' => $ startDate ->clone ()->isBefore (now ()),
22
36
]);
23
37
}
24
38
25
- private function incidents (): Collection
39
+ private function incidents (Carbon $ startDate , Carbon $ endDate , bool $ onlyDisruptedDays = false ): Collection
26
40
{
27
- $ startDate = now ()->subDays ($ this ->appSettings ->incident_days - 1 )->startOfDay ();
28
- $ endDate = now ()->endOfDay ();
29
-
30
41
return Incident::query ()
31
42
->with ([
32
43
'components ' ,
33
44
'incidentUpdates ' => fn ($ query ) => $ query ->orderByDesc ('created_at ' ),
34
45
])
35
46
->where ('visible ' , '>= ' , ! auth ()->check ())
36
- ->whereBetween ('occurred_at ' , [$ startDate ->toDateTimeString (), $ endDate ->toDateTimeString ()])
47
+ ->whereBetween ('occurred_at ' , [
48
+ $ endDate ->startOfDay ()->toDateTimeString (),
49
+ $ startDate ->endofDay ()->toDateTimeString (),
50
+ ])
37
51
->orderBy ('occurred_at ' , 'desc ' )
38
52
->get ()
39
53
->groupBy (fn (Incident $ incident ) => $ incident ->occurred_at ?->toDateString())
40
54
->union (
41
55
// Back-fill any missing dates...
42
- collect ($ startDate ->toPeriod ($ endDate ))
56
+ collect ($ endDate ->toPeriod ($ startDate ))
43
57
->keyBy (fn ($ period ) => $ period ->toDateString ())
44
58
->map (fn ($ period ) => collect ())
45
59
)
46
- ->when ($ this -> appSettings -> only_disrupted_days , fn ($ collection ) => $ collection ->filter (fn ($ incidents ) => $ incidents ->isNotEmpty ()))
60
+ ->when ($ onlyDisruptedDays , fn ($ collection ) => $ collection ->filter (fn ($ incidents ) => $ incidents ->isNotEmpty ()))
47
61
->sortKeysDesc ();
48
62
}
49
63
}
0 commit comments