2020
2121# Add parent directory to path for lib imports
2222sys .path .insert (0 , str (PROJECT_ROOT ))
23- from lib .muni_lib import download_muni_image , detect_muni_status , read_cache , write_cache , write_cached_image
23+ from lib .muni_lib import download_muni_image , detect_muni_status , read_cache , write_cache , write_cached_image , calculate_best_status
2424from lib .notifiers import notify_status_change
2525
2626# Configuration
@@ -111,41 +111,26 @@ def check_status(should_write_cache=False):
111111 'timestamp' : datetime .now ().isoformat ()
112112 }
113113
114- # Read existing cache to get previous statuses
114+ # Read existing cache to get previous statuses and best_status
115115 statuses = []
116- previous_status = None
116+ previous_best_status = None
117117 cache_data = read_cache ()
118118 if cache_data :
119- # Get previous statuses from cache
120- if 'statuses' in cache_data and len (cache_data ['statuses' ]) > 0 :
121- previous_status = cache_data ['statuses' ][0 ]['status' ]
122- # Keep existing statuses (will be trimmed below)
119+ if 'statuses' in cache_data :
123120 statuses = cache_data ['statuses' ][:]
121+ if 'best_status' in cache_data :
122+ previous_best_status = cache_data ['best_status' ]
124123
125124 # Add new status at the front
126125 statuses .insert (0 , new_status )
127126
127+ # Calculate best status using shared function
128+ # This ensures webapp, RSS, and Bluesky all show the same status
129+ best_status = calculate_best_status (statuses , window_size = 3 )
130+
128131 # Keep only last 3 statuses (~1.5 min window at 30s intervals)
129- # This filters out brief transient issues
130132 statuses = statuses [:3 ]
131133
132- # Determine best status (most optimistic)
133- # Priority: green > yellow > red
134- status_priority = {'green' : 3 , 'yellow' : 2 , 'red' : 1 }
135- best_status_value = max ([s ['status' ] for s in statuses ], key = lambda x : status_priority .get (x , 0 ))
136-
137- # Find the most recent entry with the best status
138- # This ensures we use the most recent delay info if status is yellow
139- best_status = None
140- for s in statuses : # statuses[0] is most recent
141- if s ['status' ] == best_status_value :
142- best_status = s
143- break
144-
145- # Fallback to most recent if somehow not found
146- if best_status is None :
147- best_status = statuses [0 ]
148-
149134 # Write cache with status history
150135 cache_data = {
151136 'statuses' : statuses ,
@@ -166,16 +151,18 @@ def check_status(should_write_cache=False):
166151 else :
167152 print (f"\n Cache write failed" )
168153
169- # Notify all channels if status changed
170- current_status = detection ['status' ]
171- if previous_status is not None and current_status != previous_status :
172- print (f"\n Status changed: { previous_status } -> { current_status } " )
173- delay_summaries = detection .get ('detection' , {}).get ('delay_summaries' , [])
154+ # Notify all channels if BEST status changed
155+ # This ensures notifications match what the webapp shows
156+ current_best = best_status ['status' ]
157+ previous_best = previous_best_status ['status' ] if previous_best_status else None
158+ if previous_best is not None and current_best != previous_best :
159+ print (f"\n Best status changed: { previous_best } -> { current_best } " )
160+ delay_summaries = best_status .get ('detection' , {}).get ('delay_summaries' , [])
174161 notify_results = notify_status_change (
175- status = current_status ,
176- previous_status = previous_status ,
162+ status = current_best ,
163+ previous_status = previous_best ,
177164 delay_summaries = delay_summaries ,
178- timestamp = new_status ['timestamp' ]
165+ timestamp = best_status ['timestamp' ]
179166 )
180167 for channel , result in notify_results .items ():
181168 if result ['success' ]:
0 commit comments