Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QueryBuilder] Implement get_creation_statistics for SQLite backend #6763

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -798,8 +798,27 @@

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

dialect = session.bind.dialect.name
date_format = '%Y-%m-%d'

if dialect == 'sqlite':
cday = sa_func.strftime(date_format, sa_func.datetime(self.Node.ctime, 'localtime'))

def date_to_str(d):
return d

elif dialect == 'postgresql':
cday = sa_func.date_trunc('day', self.Node.ctime)

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

else:
raise NotImplementedError(f'unsupported dialect: {dialect}')

Check warning on line 818 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#L818

Added line #L818 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 @@
# 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