Skip to content

Commit

Permalink
Applied ruff code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartcampbell committed Jan 9, 2025
1 parent 6cc9b9c commit 3c60336
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 40 deletions.
4 changes: 3 additions & 1 deletion src/nsls2api/api/v1/facility_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ async def get_proposals_for_cycle(facility: FacilityName, cycle: str):
proposal_list = await proposal_service.fetch_proposals_for_cycle(cycle, facility)
if proposal_list is None:
return fastapi.responses.JSONResponse(
{"error": f"No proposals were found for cycle {cycle} for facility {facility.name}"},
{
"error": f"No proposals were found for cycle {cycle} for facility {facility.name}"
},
status_code=404,
)
model = CycleProposalList(
Expand Down
22 changes: 13 additions & 9 deletions src/nsls2api/api/v1/jobs_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ async def sync_proposal_types(facility: FacilityName = FacilityName.nsls2):
"/sync/proposals/cycle/{cycle}",
dependencies=[Depends(get_current_user)],
include_in_schema=SYNC_ROUTES_IN_SCHEMA,
tags=["sync"], deprecated=True,
tags=["sync"],
deprecated=True,
)
async def sync_proposals_for_cycle(request: Request, cycle: str,
facility: FacilityName = FacilityName.nsls2) -> BackgroundJob:
async def sync_proposals_for_cycle(
request: Request, cycle: str, facility: FacilityName = FacilityName.nsls2
) -> BackgroundJob:
sync_params = JobSyncParameters(cycle=cycle, facility=facility)
job = await background_service.create_background_job(
JobActions.synchronize_proposals_for_cycle,
Expand All @@ -100,8 +102,9 @@ async def sync_proposals_for_cycle(request: Request, cycle: str,
include_in_schema=SYNC_ROUTES_IN_SCHEMA,
tags=["sync"],
)
async def sync_proposals_for_facility_cycle(request: Request,
facility: FacilityName, cycle: str) -> BackgroundJob:
async def sync_proposals_for_facility_cycle(
request: Request, facility: FacilityName, cycle: str
) -> BackgroundJob:
sync_params = JobSyncParameters(cycle=cycle, facility=facility)
job = await background_service.create_background_job(
JobActions.synchronize_proposals_for_cycle,
Expand All @@ -125,12 +128,13 @@ async def sync_cycles(facility: FacilityName = FacilityName.nsls2):
@router.get(
"/sync/update-cycles/{facility}",
include_in_schema=SYNC_ROUTES_IN_SCHEMA,
tags=["sync"], summary="Updates the local (nsls2core DB) cycle <-> proposal mapping"
tags=["sync"],
summary="Updates the local (nsls2core DB) cycle <-> proposal mapping",
)
async def sync_update_cycles(
request: fastapi.Request,
facility: FacilityName = FacilityName.nsls2,
cycle: Optional[str] = None,
request: fastapi.Request,
facility: FacilityName = FacilityName.nsls2,
cycle: Optional[str] = None,
):
sync_params = JobSyncParameters(facility=facility, sync_source=JobSyncSource.PASS)

Expand Down
1 change: 0 additions & 1 deletion src/nsls2api/api/v1/proposal_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from http.client import HTTPException
from typing import Annotated

import fastapi
Expand Down
5 changes: 3 additions & 2 deletions src/nsls2api/services/pass_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ async def get_saf_from_proposal(
return saf_list


async def get_commissioning_proposals_by_year(year: str, facility_name: FacilityName = FacilityName.nsls2) -> Optional[list[PassProposal]]:

async def get_commissioning_proposals_by_year(
year: str, facility_name: FacilityName = FacilityName.nsls2
) -> Optional[list[PassProposal]]:
pass_facility = await facility_service.pass_id_for_facility(facility_name)
if not pass_facility:
error_message: str = f"Facility {facility_name} does not have a PASS ID."
Expand Down
42 changes: 23 additions & 19 deletions src/nsls2api/services/proposal_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ async def recently_updated(count=5, beamline: str | None = None):
# return result


async def fetch_proposals_for_cycle(cycle_name: str, facility_name: FacilityName = FacilityName.nsls2) -> list[str]:
cycle = await Cycle.find_one(Cycle.name == cycle_name, Cycle.facility == facility_name)
async def fetch_proposals_for_cycle(
cycle_name: str, facility_name: FacilityName = FacilityName.nsls2
) -> list[str]:
cycle = await Cycle.find_one(
Cycle.name == cycle_name, Cycle.facility == facility_name
)
if cycle is None:
raise LookupError(f"Cycle {cycle} not found in local database.")
return cycle.proposals
Expand Down Expand Up @@ -151,13 +155,13 @@ async def search_proposals(search_text: str) -> Optional[list[Proposal]]:

# Get a list of proposals that match the given criteria
async def fetch_proposals(
proposal_id: list[str] | None = None,
beamline: list[str] | None = None,
cycle: list[str] | None = None,
facility: list[str] | None = None,
page_size: int = 10,
page: int = 1,
include_directories: bool = False,
proposal_id: list[str] | None = None,
beamline: list[str] | None = None,
cycle: list[str] | None = None,
facility: list[str] | None = None,
page_size: int = 10,
page: int = 1,
include_directories: bool = False,
) -> Optional[list[ProposalFullDetails]]:
query = []

Expand Down Expand Up @@ -202,7 +206,7 @@ async def fetch_proposals(


async def proposal_type_description_from_pass_type_id(
pass_type_id: int,
pass_type_id: int,
) -> Optional[str]:
proposal_type = await ProposalType.find_one(
ProposalType.pass_id == str(pass_type_id)
Expand Down Expand Up @@ -245,7 +249,7 @@ async def fetch_users_on_proposal(proposal_id: str) -> Optional[list[User]]:


async def fetch_usernames_from_proposal(
proposal_id: str,
proposal_id: str,
) -> Optional[list[str]]:
proposal = await proposal_by_id(proposal_id)

Expand Down Expand Up @@ -304,18 +308,18 @@ async def has_valid_cycle(proposal: Proposal):
# If we don't have any cycles listed and this is not a commissioning
# proposal then the cycle information is invalid
return not (
(len(proposal.cycles) == 0)
and (
proposal.pass_type_id != 300005
or proposal.type == "Beamline Commissioning (beamline staff only)"
)
(len(proposal.cycles) == 0)
and (
proposal.pass_type_id != 300005
or proposal.type == "Beamline Commissioning (beamline staff only)"
)
)


async def is_commissioning(proposal: Proposal):
return (
proposal.pass_type_id == "300005"
or proposal.type == "Beamline Commissioning (beamline staff only)"
proposal.pass_type_id == "300005"
or proposal.type == "Beamline Commissioning (beamline staff only)"
)


Expand Down Expand Up @@ -439,7 +443,7 @@ async def generate_fake_proposal_id() -> int:


async def generate_fake_test_proposal(
facility_name: FacilityName = FacilityName.nsls2, add_specific_user=None
facility_name: FacilityName = FacilityName.nsls2, add_specific_user=None
) -> Optional[Proposal]:
"""
Generates a fake test proposal.
Expand Down
29 changes: 21 additions & 8 deletions src/nsls2api/services/sync_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,23 @@ async def synchronize_proposal_from_pass(proposal_id: str) -> None:
logger.debug(f"Response: {response}")


async def update_proposals_with_cycle(cycle_name: str, facility_name: FacilityName = FacilityName.nsls2) -> None:
async def update_proposals_with_cycle(
cycle_name: str, facility_name: FacilityName = FacilityName.nsls2
) -> None:
"""
Update the cycle <-> proposals mapping for the given cycle.
:param cycle_name: The name of the cycle to process proposals for.
:type cycle_name: str
"""

proposal_list = await proposal_service.fetch_proposals_for_cycle(cycle_name, facility_name=facility_name)
proposal_list = await proposal_service.fetch_proposals_for_cycle(
cycle_name, facility_name=facility_name
)

logger.info(f"Found {len(proposal_list)} proposals for {facility_name} cycle {cycle_name}.")
logger.info(
f"Found {len(proposal_list)} proposals for {facility_name} cycle {cycle_name}."
)

for proposal_id in proposal_list:
# Add the cycle to the Proposal object
Expand All @@ -352,22 +358,29 @@ async def worker_synchronize_proposal_from_pass(proposal_id: str) -> None:
)


async def worker_synchronize_proposals_for_cycle_from_pass(cycle: str,
facility_name: FacilityName = FacilityName.nsls2) -> None:
async def worker_synchronize_proposals_for_cycle_from_pass(
cycle: str, facility_name: FacilityName = FacilityName.nsls2
) -> None:
start_time = datetime.datetime.now()

cycle_year = await facility_service.cycle_year(cycle, facility_name=facility_name)

proposals = await proposal_service.fetch_proposals_for_cycle(cycle, facility_name=facility_name)
logger.info(f"Synchronizing {len(proposals)} proposals for facility {facility_name} in {cycle} cycle.")
proposals = await proposal_service.fetch_proposals_for_cycle(
cycle, facility_name=facility_name
)
logger.info(
f"Synchronizing {len(proposals)} proposals for facility {facility_name} in {cycle} cycle."
)

for proposal_id in proposals:
logger.info(f"Synchronizing proposal {proposal_id}.")
await synchronize_proposal_from_pass(proposal_id)

commissioning_proposals: list[
PassProposal
] = await pass_service.get_commissioning_proposals_by_year(cycle_year, facility_name=facility_name)
] = await pass_service.get_commissioning_proposals_by_year(
cycle_year, facility_name=facility_name
)
logger.info(
f"Synchronizing {len(proposals)} commissioning proposals for the year {cycle_year}."
)
Expand Down

0 comments on commit 3c60336

Please sign in to comment.