Skip to content

Commit 4351834

Browse files
Merge pull request #199 from HarikaBishai/feature/automate-cycle-update
Added current cycle automation changes to cycles sync process
2 parents abae964 + 0ea694d commit 4351834

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/nsls2api/services/facility_service.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ async def facility_cycles(facility: str) -> Optional[list[str]]:
8989
cycle_list = [c.name for c in cycles if c.name is not None]
9090
return cycle_list
9191

92+
async def facility_cycle_by_date(facility: FacilityName, date: datetime.datetime) -> Optional[Cycle]:
93+
"""
94+
Find the cycle for a facility that contains the given date.
95+
96+
:param facility: The facility name (FacilityName).
97+
:param date: The date to search for (datetime).
98+
:return: The matching Cycle or None.
99+
"""
100+
cycle = await Cycle.find_one(
101+
Cycle.facility == facility.value,
102+
Cycle.start_date <= date,
103+
Cycle.end_date >= date
104+
)
105+
return cycle if cycle else None
106+
92107

93108
async def facility_by_pass_id(pass_user_facility_id: str) -> Optional[Facility]:
94109
"""

src/nsls2api/services/sync_service.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,11 @@ async def worker_synchronize_dataadmins(skip_beamlines=False) -> None:
8181
f"Beamline Data Admin permissions synchronized in {time_taken.total_seconds():,.2f} seconds"
8282
)
8383

84-
8584
async def worker_synchronize_cycles_from_pass(
8685
facility_name: FacilityName = FacilityName.nsls2,
8786
) -> None:
8887
"""
89-
This method synchronizes the cycles for a facility from PASS.
90-
91-
:param facility_name: The facility name (FacilityName).
88+
Synchronize cycles for a facility from PASS and set the current cycle.
9289
"""
9390
start_time = datetime.datetime.now()
9491

@@ -136,7 +133,7 @@ async def worker_synchronize_cycles_from_pass(
136133
response_type=UpdateResponse.NEW_DOCUMENT,
137134
)
138135

139-
# Now let's update the list of proposals for this cycle
136+
# Update proposals for this cycle
140137
proposals_list = await pass_service.get_proposals_allocated_by_cycle(
141138
cycle.name, facility=facility_name
142139
)
@@ -147,6 +144,23 @@ async def worker_synchronize_cycles_from_pass(
147144
updated_cycle.last_updated = datetime.datetime.now()
148145
await updated_cycle.save()
149146

147+
# --- Set current operating cycle from today's date ---
148+
today = datetime.datetime.now()
149+
found_cycle = await facility_service.facility_cycle_by_date(facility_name, today)
150+
if not found_cycle:
151+
logger.warning(
152+
f"No cycle found for today's date in facility {facility_name}"
153+
)
154+
else:
155+
logger.info(
156+
f"Found cycle {found_cycle.name} for today's date in facility {facility_name}"
157+
)
158+
# Set the current operating cycle for the facility
159+
await facility_service.set_current_operating_cycle(facility_name, found_cycle.name)
160+
logger.info(
161+
f"Set current operating cycle for {facility_name} to {found_cycle.name}"
162+
)
163+
150164
time_taken = datetime.datetime.now() - start_time
151165
logger.info(
152166
f"Cycle information (for {facility_name}) synchronized in {time_taken.total_seconds():,.2f} seconds"

0 commit comments

Comments
 (0)