Skip to content

Commit

Permalink
make calls to base and experimental systems concurrently
Browse files Browse the repository at this point in the history
  • Loading branch information
jueri committed Dec 5, 2024
1 parent a261366 commit 468eff7
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
3 changes: 1 addition & 2 deletions web/app/api/rankings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from app.services.session_service import create_new_session
from app.services.system_service import get_least_served_system
from app.services.profile_service import profile_route
from flask import jsonify, request, current_app
from flask import jsonify, request
from pytz import timezone
import asyncio

Expand Down Expand Up @@ -75,7 +75,6 @@ def ranking_from_db(rid):


@api.route("/ranking", methods=["GET"])
@profile_route
def ranking():
"""Produce a ranking for current session
Expand Down
7 changes: 6 additions & 1 deletion web/app/db_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
def init_async_session(app):
database_uri = app.config["SQLALCHEMY_DATABASE_URI"]
database_uri = database_uri.replace("postgresql", "postgresql+asyncpg")
# database_uri = database_uri.replace("sqlite", "sqlite+aiosqlite")
if app.config["TESTING"]:
database_uri = database_uri.replace("sqlite:///", "sqlite+aiosqlite:///")

print(f"Using database URI: {database_uri}")

engine = create_async_engine(database_uri, echo=False)

async_session_factory = sessionmaker(
bind=engine, class_=AsyncSession, expire_on_commit=False
)
app.extensions["async_session"] = async_session_factory
print(f"Async session initialized: {async_session}")
print(f"Async session initialized: {async_session_factory}")
81 changes: 43 additions & 38 deletions web/app/services/ranking_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import docker
from app.extensions import cache
from app.models import Result, System, db
from app.models import Result, System
from app.services.interleave_service import interleave_rankings
from flask import current_app
from pytz import timezone
Expand Down Expand Up @@ -38,9 +38,35 @@ async def request_results_from_conatiner(container_name, query, rpp, page):
return content.json()


def extract_hits(result, container_name, type):
# Extract hits list from result and allow custom fields for docid and hits_path
docid_name = current_app.config["SYSTEMS_CONFIG"][container_name].get(
"docid", "docid"
)
hits_path = current_app.config["SYSTEMS_CONFIG"][container_name].get("hits_path")

if hits_path is None:
hits = result["itemlist"]
else:
hits = hits_path.find(result)[0].value

if isinstance(hits[0], dict):
item_dict = {
i + 1: {"docid": hits[i][docid_name], "type": type}
for i in range(0, len(hits))
}
else:
item_dict = {
i + 1: {"docid": hits[i], "type": type} for i in range(0, len(hits))
}

return item_dict, hits


async def query_system(container_name, query, rpp, page, session_id, type="EXP"):
"""query a system with a given query and return the ranking and the result"""
current_app.logger.debug(f'Produce ranking with system: "{container_name}"...')
current_app.logger.debug(f'Produce ranking with system: "{container_name}"')

q_date = datetime.now(tz).replace(tzinfo=None, microsecond=0)
ts_start = time.time()

Expand All @@ -60,52 +86,33 @@ async def query_system(container_name, query, rpp, page, session_id, type="EXP")
await session.commit()

result = await request_results_from_conatiner(container_name, query, rpp, page)
item_dict, hits = extract_hits(result, container_name, type)

# calc query execution time in ms
ts_end = time.time()
q_time = round((ts_end - ts_start) * 1000)

# Extract hits list from result and allow custom fields for docid and hits_path
docid_name = current_app.config["SYSTEMS_CONFIG"][container_name].get(
"docid", "docid"
)
hits_path = current_app.config["SYSTEMS_CONFIG"][container_name].get("hits_path")

if hits_path is None:
hits = result["itemlist"]
else:
hits = hits_path.find(result)[0].value

if isinstance(hits[0], dict):
item_dict = {
i + 1: {"docid": hits[i][docid_name], "type": type}
for i in range(0, len(hits))
}
else:
item_dict = {
i + 1: {"docid": hits[i], "type": type} for i in range(0, len(hits))
}

# Save the ranking to the database
async with async_session_factory() as session:
# Save the ranking to the database
system_id = await session.execute(
select(System).where(System.name == container_name)
)
system_id = system_id.scalar()

ranking = Result(
session_id=session_id,
system_id=system_id.id,
type="RANK",
q=query,
q_date=q_date,
q_time=q_time,
num_found=len(hits),
page=page,
rpp=rpp,
items=item_dict,
)

async with async_session_factory() as session:
ranking = Result(
session_id=session_id,
system_id=system_id.id,
type="RANK",
q=query,
q_date=q_date,
q_time=q_time,
num_found=len(hits),
page=page,
rpp=rpp,
items=item_dict,
)
session.add(ranking)
await session.commit()

Expand Down Expand Up @@ -190,9 +197,7 @@ def build_simple_response(ranking_obj):

# @cache.memoize(timeout=600)
async def make_ranking(container_name, query, rpp, page, session_id):

if current_app.config["INTERLEAVE"]:

current_app.logger.warning("Started gathering")
baseline, experimental = await asyncio.gather(
query_system(
Expand Down
3 changes: 2 additions & 1 deletion web/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ Flask-APScheduler==1.13.1
requests-mock==1.12.1
jsonpath-ng==1.6.1
Flask-Caching==2.3.0
httpx
httpx
aiosqlite

0 comments on commit 468eff7

Please sign in to comment.