@@ -74,30 +74,42 @@ public function baseQuery(Request $request): Builder
7474 ->whereHas ('device ' , function ($ query ): void {
7575 $ query ->where ('disabled ' , 0 );
7676 })
77- ->when ($ from_ts || $ to_ts , function ($ query ) use ($ from_ts , $ to_ts ): void {
78- $ query -> where ( function ( $ q ) use ( $ from_ts , $ to_ts ): void {
79- // Outage starts within range
80- $ this ->applyDateRangeCondition ($ q , 'going_down ' , $ from_ts , $ to_ts );
81-
82- // OR outage ends within range (if it has ended)
83- $ q -> orWhere (function ($ subQuery ) use ($ from_ts , $ to_ts ): void {
84- $ subQuery -> whereNotNull ( ' up_again ' );
85- $ this ->applyDateRangeCondition ($ subQuery , 'up_again ' , $ from_ts , $ to_ts );
77+ ->when ($ from_ts || $ to_ts , function ($ query ) use ($ request , $ from_ts , $ to_ts ): void {
78+ if ( $ request -> status === ' current ' ) {
79+ // Current outages: only filter by start date within range
80+ $ this ->applyDateRangeCondition ($ query , 'going_down ' , $ from_ts , $ to_ts );
81+ } elseif ( $ request -> status === ' previous ' ) {
82+ // Previous outages: both start AND end must be within range
83+ $ query -> where (function ($ q ) use ($ from_ts , $ to_ts ): void {
84+ $ this -> applyDateRangeCondition ( $ q , ' going_down ' , $ from_ts , $ to_ts );
85+ $ this ->applyDateRangeCondition ($ q , 'up_again ' , $ from_ts , $ to_ts );
8686 });
87-
88- // OR outage spans the entire range (started before, still ongoing or ended after)
89- $ q ->orWhere (function ($ subQuery ) use ($ from_ts , $ to_ts ): void {
90- if ($ from_ts ) {
91- $ subQuery ->where ('going_down ' , '< ' , $ from_ts );
92- }
93- if ($ to_ts ) {
94- $ subQuery ->where (function ($ q ) use ($ to_ts ): void {
95- $ q ->whereNotNull ('up_again ' ) // Still ongoing
96- ->orWhere ('up_again ' , '> ' , $ to_ts ); // Ended after range
97- });
98- }
87+ } else {
88+ // All outages: use original logic (any overlap with range)
89+ $ query ->where (function ($ q ) use ($ from_ts , $ to_ts ): void {
90+ // Outage starts within range
91+ $ this ->applyDateRangeCondition ($ q , 'going_down ' , $ from_ts , $ to_ts );
92+
93+ // OR outage ends within range (if it has ended)
94+ $ q ->orWhere (function ($ subQuery ) use ($ from_ts , $ to_ts ): void {
95+ $ subQuery ->whereNotNull ('up_again ' );
96+ $ this ->applyDateRangeCondition ($ subQuery , 'up_again ' , $ from_ts , $ to_ts );
97+ });
98+
99+ // OR outage spans the entire range (started before, still ongoing or ended after)
100+ $ q ->orWhere (function ($ subQuery ) use ($ from_ts , $ to_ts ): void {
101+ if ($ from_ts ) {
102+ $ subQuery ->where ('going_down ' , '< ' , $ from_ts );
103+ }
104+ if ($ to_ts ) {
105+ $ subQuery ->where (function ($ q ) use ($ to_ts ): void {
106+ $ q ->whereNull ('up_again ' ) // Still ongoing
107+ ->orWhere ('up_again ' , '> ' , $ to_ts ); // Ended after range
108+ });
109+ }
110+ });
99111 });
100- });
112+ }
101113 })
102114 ->when ($ request ->status === 'current ' , function ($ query ): void {
103115 $ query ->whereNull ('up_again ' );
0 commit comments