Skip to content

Commit

Permalink
Merge pull request #23 from stuartcampbell/current-cycle
Browse files Browse the repository at this point in the history
Add endpoint to return the current operating cycle for a facility
  • Loading branch information
danielballan authored Feb 1, 2024
2 parents 7776fe1 + 1490e77 commit 677fe3d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/nsls2api/api/models/facility_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from enum import Enum

import pydantic

class FacilityName(Enum):
nsls2 = "nsls2"
lbms = "lbms"
cfn = "cfn"

class FacilityCyclesResponseModel(pydantic.BaseModel):
facility: str
cycles: list[str]

class FacilityCurrentOperatingCycleResponseModel(pydantic.BaseModel):
facility: str
cycle: str
45 changes: 38 additions & 7 deletions src/nsls2api/api/v1/facility_api.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
import fastapi

from nsls2api.api.models.facility_model import FacilityName
from nsls2api.api.models.facility_model import (
FacilityName,
FacilityCyclesResponseModel,
FacilityCurrentOperatingCycleResponseModel,
)
from nsls2api.services import proposal_service, facility_service

router = fastapi.APIRouter()


# TODO: Add back into schema when implemented.
@router.get(
"/facility/{facility}/cycles/current", response_model=str, include_in_schema=False
"/facility/{facility}/cycles/current",
response_model=FacilityCurrentOperatingCycleResponseModel,
include_in_schema=True,
)
async def get_current_facilty_cycle(facility: FacilityName):
return "2024-1"
async def get_current_operating_cycle(facility: FacilityName):
current_cycle = await facility_service.current_operating_cycle(facility.name)
if current_cycle is None:
return fastapi.responses.JSONResponse(
{
"error": f"No current operating cycle was found for facility {facility.name}"
},
status_code=404,
)

# TODO: Maybe add a health check here
# print("Are we healthy ? ")
# print(await facility_service.is_healthy(facility.name))

response_model = FacilityCurrentOperatingCycleResponseModel(
facility=facility.name, cycle=current_cycle
)

@router.get("/facility/{facility}/cycles", include_in_schema=True)
return response_model


@router.get(
"/facility/{facility}/cycles",
response_model=FacilityCyclesResponseModel,
include_in_schema=True,
)
async def get_facility_cycles(facility: FacilityName):
cycle_list = await facility_service.facility_cycles(facility.name)
if cycle_list is None:
return fastapi.responses.JSONResponse(
{"error": f"No cycles were found for facility {facility.name}"},
status_code=404,
)
data = {"facility": facility.name, "cycles": cycle_list}
return data
response_model = FacilityCyclesResponseModel(
facility=facility.name, cycles=cycle_list
)
return response_model


@router.get("/facility/{facility}/cycle/{cycle}/proposals", include_in_schema=True)
async def get_proposals_for_cycle(facility: FacilityName, cycle: str):
Expand Down
1 change: 1 addition & 0 deletions src/nsls2api/models/cycles.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Cycle(beanie.Document):
name: str
accepting_proposals: Optional[bool]
is_current_operating_cycle: bool = False
active: bool = False
end_date: Optional[datetime.datetime]
facility: str
Expand Down
20 changes: 20 additions & 0 deletions src/nsls2api/services/facility_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,23 @@ async def data_roles_by_user(username: str) -> Optional[list[str]]:
facilities = await Facility.find(In(Facility.data_admins, [username])).to_list()
facility_names = [f.facility_id for f in facilities if f.facility_id is not None]
return facility_names


async def current_operating_cycle(facility: str) -> Optional[str]:
"""
Current Operating Cycle
This method retrieves the current operating cycle for a given facility.
:param facility: The facility name (str).
:return: The current operating cycle (str) or None if no current operating cycle is found.
"""
cycle = await Cycle.find_one(
Cycle.facility == facility,
Cycle.is_current_operating_cycle == True, # noqa: E712
)

if cycle is None:
return None

return cycle.name

0 comments on commit 677fe3d

Please sign in to comment.