Skip to content

Commit

Permalink
implement get_creation_statastics for sqlite backend
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbull committed Feb 19, 2025
1 parent f4c55f5 commit 79e747d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
23 changes: 21 additions & 2 deletions src/aiida/storage/psql_dos/orm/querybuilder/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from aiida.common.exceptions import NotExistent
from aiida.orm.entities import EntityTypes
from aiida.orm.implementation.querybuilder import QUERYBUILD_LOGGER, BackendQueryBuilder, QueryDictType
from aiida.storage import psql_dos, sqlite_dos

from .joiner import JoinReturn, SqlaJoiner

Expand Down Expand Up @@ -798,8 +799,26 @@ def get_creation_statistics(self, user_pk: Optional[int] = None) -> Dict[str, An

total_query = session.query(self.Node)
types_query = session.query(self.Node.node_type.label('typestring'), sa_func.count(self.Node.id))

backend_class = self._backend.__class__
date_format = '%Y-%m-%d'

if backend_class == sqlite_dos.backend.SqliteDosStorage:
cday = sa_func.strftime(date_format, sa_func.datetime(self.Node.ctime, 'localtime'))

def date_to_str(d):
return d

elif backend_class == psql_dos.backend.PsqlDosBackend:
cday = sa_func.date_trunc('day', self.Node.ctime)

def date_to_str(d):
return d.strftime(date_format)

else:
raise NotImplementedError(f'unsupported backend: {backend_class}')

Check warning on line 819 in src/aiida/storage/psql_dos/orm/querybuilder/main.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/storage/psql_dos/orm/querybuilder/main.py#L819

Added line #L819 was not covered by tests
stat_query = session.query(
sa_func.date_trunc('day', self.Node.ctime).label('cday'),
cday.label('cday'),
sa_func.count(self.Node.id),
)

Expand All @@ -817,7 +836,7 @@ def get_creation_statistics(self, user_pk: Optional[int] = None) -> Dict[str, An
# Nodes created per day
stat = stat_query.group_by('cday').order_by('cday').all()

ctime_by_day = {_[0].strftime('%Y-%m-%d'): _[1] for _ in stat}
ctime_by_day = {date_to_str(entry[0]): entry[1] for entry in stat}
retdict['ctime_by_day'] = ctime_by_day

return retdict
Expand Down
6 changes: 0 additions & 6 deletions tests/orm/test_querybuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1581,9 +1581,6 @@ class TestManager:
def init_db(self, backend):
self.backend = backend

# This fails with sqlite with:
# sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such function: date_trunc
@pytest.mark.requires_psql
def test_statistics(self):
"""Test if the statistics query works properly.
Expand Down Expand Up @@ -1620,9 +1617,6 @@ def store_and_add(n, statistics):

assert new_db_statistics == expected_db_statistics

# This fails with sqlite with:
# sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such function: date_trunc
@pytest.mark.requires_psql
def test_statistics_default_class(self):
"""Test if the statistics query works properly.
Expand Down
3 changes: 0 additions & 3 deletions tests/restapi/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ def linearize_namespace(tree_namespace, linear_namespace=None):
return linear_namespace


# This test does not work with SQLite since it uses the `statistics` endpoint,
# which uses `date_trunc` under the hood, which is not implemented in SQLite.
@pytest.mark.usefixtures('populate_restapi_database')
@pytest.mark.requires_psql
def test_count_consistency(restapi_server, server_url):
"""Test the consistency in values between full_type_count and statistics"""
server = restapi_server()
Expand Down

0 comments on commit 79e747d

Please sign in to comment.