Skip to content

Commit

Permalink
Performance: skip model counts where not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWatzinger committed Mar 7, 2025
1 parent 5145468 commit 7c745e5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
8 changes: 6 additions & 2 deletions openatlas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ def before_request() -> None:
g.cursor = g.db.cursor(cursor_factory=extras.DictCursor)
g.settings = Settings.get_settings()
session['language'] = get_locale()
g.cidoc_classes = CidocClass.get_all(session['language'])
g.properties = CidocProperty.get_all(session['language'])
g.cidoc_classes = CidocClass.get_all(
session['language'],
(request.path.startswith('/overview/model/cidoc_class_index')))
g.properties = CidocProperty.get_all(
session['language'],
(request.path.startswith('/overview/model/property')))
g.classes = OpenatlasClass.get_all()
with_count = False
if (request.path.startswith(('/type', '/api/type_tree/', '/admin/orphans'))
Expand Down
17 changes: 12 additions & 5 deletions openatlas/database/cidoc_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
from flask import g


def get_classes() -> list[dict[str, Any]]:
def get_classes(with_count: bool = False) -> list[dict[str, Any]]:
g.cursor.execute(
"""
SELECT c.code, c.name, comment, COUNT(e.id) AS count
f"""
SELECT
c.code,
c.name,
comment
{', COUNT(e.id) AS count' if with_count else ''}
FROM model.cidoc_class c
LEFT JOIN model.entity e ON c.code = e.cidoc_class_code
GROUP BY (c.code, c.name, c.comment);
{
'''
LEFT JOIN model.entity e ON c.code = e.cidoc_class_code
GROUP BY (c.code, c.name, c.comment)''' if with_count else ''}
;
""")
return list(g.cursor)

Expand Down
27 changes: 15 additions & 12 deletions openatlas/database/cidoc_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@
from flask import g


def get_properties() -> list[dict[str, Any]]:
def get_properties(with_count: bool = False) -> list[dict[str, Any]]:

g.cursor.execute(
"""
f"""
SELECT
p.code,
p.comment,
p.domain_class_code,
p.range_class_code,
p.name,
p.name_inverse,
COUNT(l.id) AS count
p.name_inverse
{', COUNT(l.id) AS count' if with_count else ''}
FROM model.property p
LEFT JOIN model.link l ON p.code = l.property_code
GROUP BY (
p.code,
p.comment,
p.domain_class_code,
p.range_class_code,
p.name,
p.name_inverse);
{'''
LEFT JOIN model.link l ON p.code = l.property_code
GROUP BY (
p.code,
p.comment,
p.domain_class_code,
p.range_class_code,
p.name,
p.name_inverse)''' if with_count else ''}
;
""")
return list(g.cursor)

Expand Down
9 changes: 6 additions & 3 deletions openatlas/models/cidoc_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ def __init__(self, data: dict[str, Any]) -> None:
self.name = data['name']
self.code = data['code']
self.comment = data['comment']
self.count = data['count']
self.count = data['count'] if 'count' in data else None
self.i18n: dict[str, str] = {}
self.sub: list[CidocClass] = []
self.super: list[CidocClass] = []

@staticmethod
def get_all(language: str) -> dict[str, CidocClass]:
classes = {row['code']: CidocClass(row) for row in db.get_classes()}
def get_all(
language: str,
with_count: bool = False) -> dict[str, CidocClass]:
classes = {
row['code']: CidocClass(row) for row in db.get_classes(with_count)}
for row in db.get_hierarchy():
classes[row['super_code']].sub.append(row['sub_code'])
classes[row['sub_code']].super.append(row['super_code'])
Expand Down
9 changes: 6 additions & 3 deletions openatlas/models/cidoc_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, data: dict[str, Any]) -> None:
self.comment = data['comment']
self.domain_class_code = data['domain_class_code']
self.range_class_code = data['range_class_code']
self.count = data['count']
self.count = data['count'] if 'count' in data else None
self.sub: list[int] = []
self.super: list[int] = []
self.i18n: dict[str, str] = {}
Expand Down Expand Up @@ -46,9 +46,12 @@ def find_subs(
return False

@staticmethod
def get_all(language: str) -> dict[str, CidocProperty]:
def get_all(
language: str,
with_count: bool = False) -> dict[str, CidocProperty]:
properties = {
row['code']: CidocProperty(row) for row in db.get_properties()}
row['code']:
CidocProperty(row) for row in db.get_properties(with_count)}
for row in db.get_hierarchy():
properties[row['super_code']].sub.append(row['sub_code'])
properties[row['sub_code']].super.append(row['super_code'])
Expand Down

0 comments on commit 7c745e5

Please sign in to comment.