2525def check_overdue_maintenance ():
2626 """
2727 Check for overdue maintenance across all rigs and maintenance types.
28-
28+
2929 Returns:
3030 list: List of dictionaries containing overdue maintenance information
3131 """
3232 overdue_items = []
3333 current_date = datetime .now ().date ()
34-
34+
3535 # Get all maintenance types and their intervals
3636 maintenance_types = rig_maintenance .MaintenanceType .fetch (as_dict = True )
37-
37+
3838 # Get all locations (rigs)
39- locations = lab .Location .fetch (as_dict = True )
40-
39+ queries = [
40+ 'system_type = "rig"' ,
41+ 'acquisition_type in ("behavior", "electrophysiology", "2photon")' ,
42+ "(location_description is not NULL and length(trim(location_description)) > 0)" ,
43+ ]
44+
45+ merged_queries = " and " .join (queries )
46+
47+ locations = (lab .Location & merged_queries ).fetch (as_dict = True )
48+
4149 print (f"Checking maintenance status as of { current_date } " )
4250 print ("=" * 60 )
43-
51+
4452 for location in locations :
4553 location_name = location ['location' ]
4654 print (f"\n Checking rig: { location_name } " )
47- print ("-" * 40 )
48-
55+ print ("-" * 61 )
56+
4957 for mtype in maintenance_types :
5058 maintenance_type = mtype ['maintenance_type' ]
5159 interval_days = mtype ['interval_days' ]
52-
60+
5361 # Find the most recent maintenance record for this rig and type
54- recent_maintenance = (rig_maintenance .RigMaintenance &
55- {'location' : location_name ,
62+ recent_maintenance = (rig_maintenance .RigMaintenance &
63+ {'location' : location_name ,
5664 'maintenance_type' : maintenance_type }).fetch (
5765 'maintenance_date' , order_by = 'maintenance_date DESC' , limit = 1 )
58-
66+
5967 if len (recent_maintenance ) == 0 :
6068 # No maintenance record exists
6169 overdue_items .append ({
@@ -67,11 +75,11 @@ def check_overdue_maintenance():
6775 'status' : 'NO_RECORD' ,
6876 'message' : f'No maintenance record found for { maintenance_type } '
6977 })
70- print (f" { maintenance_type :.<30 } NO RECORD FOUND ❌" )
78+ print (f" { maintenance_type :.<40 } NO RECORD FOUND ❌" )
7179 else :
7280 last_maintenance_date = recent_maintenance [0 ]
7381 days_since_last = (current_date - last_maintenance_date ).days
74-
82+
7583 if days_since_last > interval_days :
7684 # Maintenance is overdue
7785 days_overdue = days_since_last - interval_days
@@ -90,14 +98,14 @@ def check_overdue_maintenance():
9098 # Maintenance is up to date
9199 days_until_due = interval_days - days_since_last
92100 print (f" { maintenance_type :.<30} OK ({ days_until_due } days until due) ✅" )
93-
101+
94102 return overdue_items
95103
96104
97105def print_summary (overdue_items ):
98106 """
99107 Print a summary of overdue maintenance items.
100-
108+
101109 Args:
102110 overdue_items (list): List of overdue maintenance items
103111 """
@@ -106,20 +114,20 @@ def print_summary(overdue_items):
106114 print ("🎉 All maintenance is up to date!" )
107115 print ("=" * 60 )
108116 return
109-
117+
110118 print ("\n " + "=" * 60 )
111119 print ("⚠️ OVERDUE MAINTENANCE SUMMARY" )
112120 print ("=" * 60 )
113-
121+
114122 # Group by status
115123 no_record_items = [item for item in overdue_items if item ['status' ] == 'NO_RECORD' ]
116124 overdue_items_list = [item for item in overdue_items if item ['status' ] == 'OVERDUE' ]
117-
125+
118126 if no_record_items :
119127 print (f"\n 📋 RIGS WITH NO MAINTENANCE RECORDS ({ len (no_record_items )} items):" )
120128 for item in no_record_items :
121129 print (f" • { item ['location' ]} - { item ['maintenance_type' ]} " )
122-
130+
123131 if overdue_items_list :
124132 print (f"\n 🚨 OVERDUE MAINTENANCE ({ len (overdue_items_list )} items):" )
125133 # Sort by days overdue (most overdue first)
@@ -128,7 +136,7 @@ def print_summary(overdue_items):
128136 print (f" • { item ['location' ]} - { item ['maintenance_type' ]} : "
129137 f"{ item ['days_overdue' ]} days overdue "
130138 f"(last done: { item ['last_maintenance' ]} )" )
131-
139+
132140 print (f"\n Total items requiring attention: { len (overdue_items )} " )
133141 print ("=" * 60 )
134142
@@ -138,19 +146,19 @@ def main():
138146 try :
139147 print ("Rig Maintenance Status Checker" )
140148 print ("=" * 60 )
141-
149+
142150 # Check for overdue maintenance
143151 overdue_items = check_overdue_maintenance ()
144-
152+
145153 # Print summary
146154 print_summary (overdue_items )
147-
155+
148156 # Exit with error code if there are overdue items
149157 if overdue_items :
150158 sys .exit (1 )
151159 else :
152160 sys .exit (0 )
153-
161+
154162 except Exception as e :
155163 print (f"Error during maintenance check: { e } " )
156164 sys .exit (1 )
0 commit comments