Skip to content

Commit

Permalink
upgrade trueability api to reduce response time for your-exams page (#…
Browse files Browse the repository at this point in the history
…14643)

* upgrade trueability api to reduce response time

* add helpful comments

* fix the comment

* format python
  • Loading branch information
usamabinnadeem-10 authored Jan 13, 2025
1 parent f65090e commit 645b53a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
4 changes: 4 additions & 0 deletions webapp/shop/api/trueability/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def get_assessment_reservations(
group: str = None,
state: List[str] = None,
assessment_state: str = None,
email: str = None,
reservation_ids: List[str] = None,
):
params = {
"ability_screen_id": ability_screen_id,
Expand All @@ -91,6 +93,8 @@ def get_assessment_reservations(
"group": group,
"state[]": state,
"assessment_state[]": assessment_state,
"email": email,
"id[]": reservation_ids,
}
filtered_params = {k: v for k, v in params.items() if v is not None}
uri = "/api/v1/assessment_reservations?" + urlencode(
Expand Down
59 changes: 40 additions & 19 deletions webapp/shop/cred/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,9 @@ def cred_your_exams(
**kwargs,
):
email = flask.request.args.get("email", None)
user = user_info(flask.session)
if not email:
email = user["email"]

agreement_notification = False
confidentiality_agreement_enabled = strtobool(
Expand Down Expand Up @@ -636,19 +639,40 @@ def cred_your_exams(
exams_expired = []

if exam_contracts:
# Fetch all reservations in one API call
try:
reservations_response = (
trueability_api.get_assessment_reservations(
per_page=500,
email=email,
)
)
reservations = {
r["uuid"]: r
for r in reservations_response.get(
"assessment_reservations", []
)
}

except Exception:
reservations = {}

for exam_contract in exam_contracts:
name = exam_contract["cueContext"]["courseID"]
name = EXAM_NAMES.get(name, name)
contract_item_id = (
exam_contract.get("id") or exam_contract["contractItem"]["id"]
)
contract_long_id = exam_contract["contractItem"]["contractID"]

# if exam is scheduled
if "reservation" in exam_contract["cueContext"]:
response = trueability_api.get_assessment_reservation(
exam_contract["cueContext"]["reservation"]["IDs"][-1]
)
if "assessment_reservation" not in response:
reservation_id = exam_contract["cueContext"]["reservation"][
"IDs"
][-1]
reservation = reservations.get(reservation_id)

if not reservation:
exams_expired.append(
{
"name": name,
Expand All @@ -657,20 +681,16 @@ def cred_your_exams(
}
)
continue
else:
r = response.get("assessment_reservation")
timezone = r["user"]["time_zone"]
tz_info = pytz.timezone(timezone)
starts_at = (
datetime.strptime(
r["starts_at"], "%Y-%m-%dT%H:%M:%S.%fZ"
)
.replace(tzinfo=pytz.timezone("UTC"))
.astimezone(tz_info)
)
assessment_id = (
r.get("assessment") and r["assessment"]["id"]
)

r = reservation
timezone = r["user"]["time_zone"]
tz_info = pytz.timezone(timezone)
starts_at = (
datetime.strptime(r["starts_at"], "%Y-%m-%dT%H:%M:%S.%fZ")
.replace(tzinfo=pytz.timezone("UTC"))
.astimezone(tz_info)
)
assessment_id = r.get("assessment") and r["assessment"]["id"]

actions = []
utc = pytz.timezone("UTC")
Expand All @@ -692,6 +712,7 @@ def cred_your_exams(
]
else state
)

# if assessment is provisioned
if assessment_id:
is_in_window = (now > starts_at and now < end) or (
Expand Down Expand Up @@ -783,6 +804,7 @@ def cred_your_exams(
exams_expired.append(
{"name": name, "state": "Expired", "actions": []}
)

# if exam is not used and is not expired
else:
actions = [
Expand Down Expand Up @@ -822,7 +844,6 @@ def cred_your_exams(
# Do not cache this view
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Pragma"] = "no-cache"

return response


Expand Down

0 comments on commit 645b53a

Please sign in to comment.