Skip to content

Commit 14cad43

Browse files
committed
Add /nodes/statistics/user endpoint to REST API
The implementation as in aiida-core.
1 parent 366f036 commit 14cad43

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

aiida_restapi/routers/nodes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ async def get_nodes_download_formats() -> dict[str, Any]:
4343
return resources.get_all_download_formats()
4444

4545

46+
@router.get('/nodes/statistics', response_model=dict[str, Any])
47+
async def get_nodes_statistics(user: Optional[int] = None) -> dict[str, Any]:
48+
"""Get statistics for nodes endpoint"""
49+
50+
from aiida.manage import get_manager
51+
52+
backend = get_manager().get_profile_storage()
53+
return backend.query().get_creation_statistics(user_pk=user)
54+
55+
4656
@router.get('/nodes/{nodes_id}/download')
4757
@with_dbenv()
4858
async def download_node(nodes_id: int, download_format: Optional[str] = None) -> StreamingResponse:

tests/test_nodes.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,39 @@ async def test_get_download_node(array_data_node, async_client):
351351
response = await async_client.get(f'/nodes/{array_data_node.pk}/download')
352352
assert response.status_code == 422, response.json()
353353
assert 'Please specify the download format' in response.json()['detail']
354+
355+
356+
@pytest.mark.anyio
357+
async def test_get_statistics(default_nodes, async_client):
358+
"""Test get statistics for nodes."""
359+
360+
from datetime import datetime
361+
362+
default_user_reference_json = {
363+
'total': 4,
364+
'types': {
365+
'data.core.float.Float.': 1,
366+
'data.core.str.Str.': 1,
367+
'data.core.bool.Bool.': 1,
368+
'data.core.int.Int.': 1,
369+
},
370+
'ctime_by_day': {datetime.today().strftime('%Y-%m-%d'): 4},
371+
}
372+
373+
# Test without specifiying user, should use default user
374+
response = await async_client.get('/nodes/statistics')
375+
assert response.status_code == 200, response.json()
376+
assert response.json() == default_user_reference_json
377+
378+
# Test that the output is the same when we use the pk of the default user
379+
from aiida import orm
380+
381+
default_user_pk = orm.User(email='').collection.get_default().pk
382+
response = await async_client.get(f'/nodes/statistics?user={default_user_pk}')
383+
assert response.status_code == 200, response.json()
384+
assert response.json() == default_user_reference_json
385+
386+
# Test empty response for nonexisting user
387+
response = await async_client.get('/nodes/statistics?user=99999')
388+
assert response.status_code == 200, response.json()
389+
assert response.json() == {'total': 0, 'types': {}, 'ctime_by_day': {}}

0 commit comments

Comments
 (0)