Skip to content

Commit

Permalink
An alternate approach to controlling how oak is initialized on startu…
Browse files Browse the repository at this point in the history
…p to see how it behaves on gcp
  • Loading branch information
kevinschaper committed Aug 11, 2023
1 parent a3cc25a commit 5bd2501
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion backend/src/monarch_py/api/association.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def _get_associations(
pagination: PaginationParams = Depends(),
) -> AssociationResults:
"""Retrieves all associations for a given entity, or between two entities."""
response = solr.get_associations(
response = solr().get_associations(
category=category,
predicate=predicate,
subject=subject,
Expand Down
10 changes: 8 additions & 2 deletions backend/src/monarch_py/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class Settings(BaseSettings):

settings = Settings()

solr = SolrImplementation(base_url=settings.solr_url)

oak = OakImplementation().init_semsim()
@lru_cache(maxsize=1)
def solr():
return SolrImplementation(settings.solr_url)


@lru_cache(maxsize=1)
def oak():
return OakImplementation().init_semsim()
4 changes: 2 additions & 2 deletions backend/src/monarch_py/api/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def _get_entity(
Returns:
Node: Entity details for the specified id
"""
response = solr.get_entity(id, extra=True)
response = solr().get_entity(id, extra=True)
if response is None:
raise HTTPException(status_code=404, detail="Entity not found")
return Node(**response.__dict__) # This is an odd consequence of how Node extends Entity
Expand Down Expand Up @@ -59,7 +59,7 @@ def _association_table(
Returns:
AssociationResults: Association table data for the specified entity and association type
"""
response = solr.get_association_table(
response = solr().get_association_table(
entity=id, category=category, q=query, offset=pagination.offset, limit=pagination.limit
)
return response
2 changes: 1 addition & 1 deletion backend/src/monarch_py/api/histopheno.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@router.get("/{id}")
async def _get_histopheno(id) -> HistoPheno:
"""Retrieves the entity with the specified id"""
response = solr.get_histopheno(id)
response = solr().get_histopheno(id)
if response is None:
raise HTTPException(status_code=404, detail="Entity not found")

Expand Down
9 changes: 8 additions & 1 deletion backend/src/monarch_py/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
from monarch_py.api import association, entity, histopheno, search, semsim
from monarch_py.api.config import oak

PREFIX = "/v3/api"

app = FastAPI(
docs_url="/v3/docs",
redoc_url="/v3/redoc",
on_startup=[],
)


@app.on_event('startup')
async def initialize_app():
oak()

app.include_router(entity.router, prefix=f"{PREFIX}/entity")
app.include_router(association.router, prefix=f"{PREFIX}/association")
app.include_router(search.router, prefix=PREFIX)
Expand Down
4 changes: 2 additions & 2 deletions backend/src/monarch_py/api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def search(
EntityResults
"""
facet_fields = ["category", "in_taxon_label"]
response = solr.search(
response = solr().search(
q=q,
category=category,
in_taxon_label=taxon,
Expand All @@ -53,5 +53,5 @@ async def autocomplete(q: str) -> SearchResults:
Returns:
SearchResults
"""
response = solr.autocomplete(q=q)
response = solr().autocomplete(q=q)
return response
4 changes: 2 additions & 2 deletions backend/src/monarch_py/api/semsim.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def _compare(
subjects: {subjects.split(',')}
objects: {objects.split(',')}
""")
results = oak.compare(
results = oak().compare(
subjects=subjects.split(","),
objects=objects.split(","),
)
Expand All @@ -48,4 +48,4 @@ def _post_compare(
}
</pre>
"""
return oak.compare(subjects, objects)
return oak().compare(subjects, objects)

0 comments on commit 5bd2501

Please sign in to comment.