Skip to content

Commit

Permalink
Preload AssessmentRecord properties and compress response for all_ass…
Browse files Browse the repository at this point in the history
…essments_for_fund_round_id
  • Loading branch information
Kishan Patel authored and ksp37-dluhc committed Jun 14, 2024
1 parent 0fd98ea commit 099e9b5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
18 changes: 18 additions & 0 deletions api/routes/_helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import copy
import gzip
import json
from uuid import UUID

from config import Config
from db.models.assessment_record.enums import Status
from flask import current_app
from flask import make_response


def compress_response(data):
class UUIDEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, UUID):
return obj.hex
return json.JSONEncoder.default(self, obj)

content = gzip.compress(json.dumps(data, cls=UUIDEncoder).encode("utf8"), 5)
response = make_response(content)
response.headers["Content-length"] = len(content)
response.headers["Content-Encoding"] = "gzip"
return response


def _derive_status(score_map: dict, comment_map: dict, sub_criteria_id: str) -> str:
Expand Down
4 changes: 3 additions & 1 deletion api/routes/assessment_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import List

from api.models.sub_criteria import SubCriteria
from api.routes._helpers import compress_response
from api.routes._helpers import transform_to_assessor_task_list_metadata
from api.routes.subcriterias.get_sub_criteria import (
get_all_subcriteria,
Expand Down Expand Up @@ -89,7 +90,8 @@ def all_assessments_for_fund_round_id(
team_in_place=team_in_place,
joint_application=joint_application,
)
return app_list

return compress_response(app_list)


def sub_criteria(
Expand Down
11 changes: 9 additions & 2 deletions db/queries/assessment_records/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from sqlalchemy.orm import aliased
from sqlalchemy.orm import defer
from sqlalchemy.orm import load_only
from sqlalchemy.orm import selectinload


def get_metadata_for_application(
Expand Down Expand Up @@ -95,7 +96,12 @@ def get_metadata_for_fund_round_id(
statement = (
select(AssessmentRecord)
# Dont load json into memory
.options(defer(AssessmentRecord.jsonb_blob)).where(
.options(
defer(AssessmentRecord.jsonb_blob),
selectinload(AssessmentRecord.qa_complete),
selectinload(AssessmentRecord.flags),
selectinload(AssessmentRecord.tag_associations).selectinload(TagAssociation.tag).selectinload(Tag.tag_type),
).where(
AssessmentRecord.fund_id == fund_id,
AssessmentRecord.round_id == round_id,
AssessmentRecord.is_withdrawn == False, # noqa: E712
Expand Down Expand Up @@ -201,7 +207,8 @@ def get_metadata_for_fund_round_id(
# so it has the double quotes from the json so we have to include them in the comparison
statement = statement.where(func.cast(AssessmentRecord.funding_type, String) == f'"{funding_type}"')

assessment_metadatas = db.session.scalars(statement).all()
raw_results = db.session.execute(statement)
assessment_metadatas = raw_results.unique().scalars().all()

if status != "ALL":
filter_assessments = []
Expand Down

0 comments on commit 099e9b5

Please sign in to comment.