Skip to content

Commit

Permalink
Type performance
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWatzinger committed Oct 24, 2023
1 parent 03b2cad commit 1553dbd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
8 changes: 7 additions & 1 deletion openatlas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ def before_request() -> None:
g.cidoc_classes = CidocClass.get_all(session['language'])
g.properties = CidocProperty.get_all(session['language'])
g.classes = OpenatlasClass.get_all()
g.types = Type.get_all()
with_count = False
if request.path.startswith('/type') or \
request.path.startswith('/api/type_tree/') or (
request.path.startswith('/entity/') and
request.path.split('/entity/')[1].isdigit()):
with_count = True
g.types = Type.get_all(with_count)
g.reference_systems = ReferenceSystem.get_all()
g.view_class_mapping = view_class_mapping
g.class_view_mapping = OpenatlasClass.get_class_view_mapping()
Expand Down
5 changes: 3 additions & 2 deletions openatlas/api/endpoints/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ def get() -> Union[tuple[Resource, int], Response]:

@staticmethod
def get_type_tree() -> dict[int, Any]:
return {id_: GetTypeTree.serialize_to_json(type_)
for id_, type_ in Type.get_all().items()}
return {
id_: GetTypeTree.serialize_to_json(type_) for id_, type_
in g.types.items()}

@staticmethod
def serialize_to_json(type_: Type) -> dict[str, Any]:
Expand Down
28 changes: 16 additions & 12 deletions openatlas/database/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
class Type:

@staticmethod
def get_types() -> list[dict[str, Any]]:
g.cursor.execute(
"""
def get_types(with_count: bool) -> list[dict[str, Any]]:
sql = f"""
SELECT
e.id,
e.name,
Expand All @@ -18,8 +17,9 @@ def get_types() -> list[dict[str, Any]]:
e.created,
e.modified,
es.id AS super_id,
COUNT(l2.id) AS count,
COUNT(l3.id) AS count_property,
{'COUNT(l2.id)' if with_count else '0'} AS count,
{'COUNT(l3.id)' if with_count else '0'} AS count_property,
COALESCE(to_char(e.begin_from, 'yyyy-mm-dd hh24:mi:ss BC'), '')
AS begin_from,
COALESCE(to_char(e.begin_to, 'yyyy-mm-dd hh24:mi:ss BC'), '')
Expand All @@ -36,17 +36,21 @@ def get_types() -> list[dict[str, Any]]:
LEFT JOIN model.link l ON e.id = l.domain_id
AND l.property_code IN ('P127', 'P89')
LEFT JOIN model.entity es ON l.range_id = es.id
-- Get count
LEFT JOIN model.link l2 ON e.id = l2.range_id
AND l2.property_code IN ('P2', 'P89')
LEFT JOIN model.link l3 ON e.id = l3.type_id
"""
if with_count:
sql += """
-- Get count
LEFT JOIN model.link l2 ON e.id = l2.range_id
AND l2.property_code IN ('P2', 'P89')
LEFT JOIN model.link l3 ON e.id = l3.type_id
"""
sql += """
WHERE e.openatlas_class_name
IN ('administrative_unit', 'type', 'type_tools')
GROUP BY e.id, es.id
ORDER BY e.name;
""")
"""
g.cursor.execute(sql)
return [dict(row) for row in g.cursor.fetchall()]

@staticmethod
Expand Down
4 changes: 2 additions & 2 deletions openatlas/models/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ def move_entities(self, new_type_id: int, checkbox_values: str) -> None:
Db.remove_entity_type(self.id, delete_ids)

@staticmethod
def get_all() -> dict[int, Type]:
def get_all(with_count: bool) -> dict[int, Type]:
types = {}
for row in Db.get_types():
for row in Db.get_types(with_count):
type_ = Type(row)
types[type_.id] = type_
type_.count = row['count'] or row['count_property']
Expand Down
8 changes: 3 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,22 +408,20 @@ def test_api(self) -> None:
# ---Type Endpoints---
for rv in [
self.app.get(url_for('api_03.type_overview')),
self.app.get(
url_for('api_03.type_overview', download=True))]:
self.app.get(url_for('api_03.type_overview', download=True))]:
rv = rv.get_json()['place'][0]['children'][0]
assert bool(rv['label'] == 'Austria')

for rv in [
self.app.get(url_for('api_03.type_by_view_class')),
self.app.get(
url_for('api_03.type_by_view_class', download=True))]:
url_for('api_03.type_by_view_class', download=True))]:
rv = rv.get_json()['place'][2]['children'][0]
assert bool(rv['label'] == 'Boundary Mark')

for rv in [
self.app.get(url_for('api_03.type_tree')),
self.app.get(
url_for('api_03.type_tree', download=True))]:
self.app.get(url_for('api_03.type_tree', download=True))]:
assert bool(rv.get_json()['typeTree'])
rv = self.app.get(url_for('api_03.type_tree', count=True))
assert rv.get_json() > 0
Expand Down

0 comments on commit 1553dbd

Please sign in to comment.