Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into test/accessingTheOpenApi
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGoodall committed Oct 11, 2023
2 parents 6fb5ac2 + f8facec commit 3da94dd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
22 changes: 18 additions & 4 deletions application/data_access/entity_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_entity_query(


def get_entity_count(dataset: Optional[str] = None):
sql = select(EntityOrm.dataset, func.count(func.distinct(EntityOrm.entity)))
sql = select(EntityOrm.dataset, func.count(EntityOrm.entity))
sql = sql.group_by(EntityOrm.dataset)
if dataset is not None:
sql = sql.filter(EntityOrm.dataset == dataset)
Expand All @@ -70,21 +70,35 @@ def get_entity_search(parameters: dict):
params = normalised_params(parameters)

with get_context_session() as session:
query_args = [EntityOrm, func.count(EntityOrm.entity).over().label("count")]
count: int
entities: [EntityModel]

# get count
query_args = [func.count(EntityOrm.entity).label("count")]
query = session.query(*query_args)
query = _apply_base_filters(query, params)
query = _apply_date_filters(query, params)
query = _apply_location_filters(session, query, params)
query = _apply_period_option_filter(query, params)
query = _apply_limit_and_pagination_filters(query, params)

entities = query.all()
if entities:
count = entities[0].count
else:
count = 0

entities = [entity_factory(entity.EntityOrm) for entity in entities]
# get entities
query_args = [EntityOrm]
query = session.query(*query_args)
query = _apply_base_filters(query, params)
query = _apply_date_filters(query, params)
query = _apply_location_filters(session, query, params)
query = _apply_period_option_filter(query, params)
query = _apply_limit_and_pagination_filters(query, params)

entities = query.all()
entities = [entity_factory(entity_orm) for entity_orm in entities]

return {"params": params, "count": count, "entities": entities}


Expand Down
2 changes: 1 addition & 1 deletion application/templates/components/entity-card/macro.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% set propertiesArr = [
{ "key": "Dataset", "value": e["dataset"] },
{ "key": "Reference", "value": e["reference"] },
{ "key": "period", "value": e['end-date'] | convert_to_current_or_historical },
{ "key": "Period", "value": e['end-date'] | convert_to_current_or_historical },
]
%}

Expand Down
9 changes: 9 additions & 0 deletions changeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,12 @@
### Why was this change made?
- To keep track of changes made to the project
***

<br />

## 10-10-2023
### What's new
- Optimized the queries in get_entity_count and get_entity_search
### Why was this change made?
- The production database was using too much local storage performing some of the most common queries.
***

0 comments on commit 3da94dd

Please sign in to comment.