Skip to content

Commit

Permalink
urn: monitor count of unregistered pids by days
Browse files Browse the repository at this point in the history
A new api to count documents with unregistered pids
with an optional parameters `days`.

* Completes urn config for two organisations.

Co-Authored-by: Aly Badr <[email protected]>
  • Loading branch information
Aly Badr authored and PascalRepond committed Aug 22, 2023
1 parent a683f5c commit c530871
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 1 deletion.
7 changes: 6 additions & 1 deletion sonar/config_sonar.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,11 @@
'types': ['coar:c_db06'],
'code': 6,
'namespace': 'urn:nbn:ch:rero'
},
'usi': {
'types': ['coar:c_db06'],
'code': 1,
'namespace': 'urn:nbn:ch:rero'
}
}
},
}
26 changes: 26 additions & 0 deletions sonar/modules/documents/urn.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"""Urn API."""


from datetime import datetime, timedelta, timezone

from flask import current_app
from invenio_pidstore.errors import PIDAlreadyExists
from invenio_pidstore.models import PersistentIdentifier, PIDStatus
Expand All @@ -26,6 +28,8 @@
class Urn:
"""Urn class."""

urn_pid_type = 'urn'

@classmethod
def _calculateCheckDigit(cls, urn):
"""Return the check-digit calculated on a URN.
Expand Down Expand Up @@ -97,3 +101,25 @@ def create_urn(cls, record):
except PIDAlreadyExists:
current_app.logger.error(
f'generated urn already exist for document: {doc_pid}')

@classmethod
def get_urn_pids(cls, status=PIDStatus.NEW, days=None):
"""Get count of URN pids by status and creation date.
:param status: PID status by default N.
:param days: Number of days passed since the creation of the document.
:returns: Documents count.
"""
count = 0
query = PersistentIdentifier.query\
.filter_by(pid_type=cls.urn_pid_type)\
.filter_by(status=status)
if uuuids := [str(uuid.object_uuid) for uuid in query.all()]:
from sonar.modules.documents.api import DocumentSearch
query = DocumentSearch()\
.filter('terms', _id=uuuids)
if days:
date = datetime.now(timezone.utc) - timedelta(days=days)
query = query.filter('range', _created={'lte': date})
count = query.count()
return count
15 changes: 15 additions & 0 deletions sonar/monitoring/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from flask_security import current_user
from invenio_search import current_search_client

from sonar.modules.documents.urn import Urn
from sonar.modules.permissions import superuser_access_permission
from sonar.monitoring.api.data_integrity import DataIntegrityMonitoring
from sonar.monitoring.api.database import DatabaseMonitoring
Expand Down Expand Up @@ -110,3 +111,17 @@ def elastic_search():
return jsonify({'data': info})
except Exception as exception:
return jsonify({'error': str(exception)}), 500


@api_blueprint.route('/urn')
def unregistered_urn():
"""Count of unregistered urn pids.
:return: jsonified count information.
"""
try:
days = int(args.get('days', 0)) if (args := request.args) else 0
info = Urn.get_urn_pids(days=days)
return jsonify({'data': info})
except Exception as exception:
return jsonify({'error': str(exception)}), 500
31 changes: 31 additions & 0 deletions tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,39 @@
import pytest
from invenio_app.factory import create_api

from sonar.modules.documents.api import DocumentRecord


@pytest.fixture(scope='module')
def create_app():
"""Create test app."""
return create_api


@pytest.fixture()
def minimal_thesis_document(db, bucket_location, organisation):
"""Return a minimal thesis document."""
record = DocumentRecord.create(
{
"title": [
{
"type": "bf:Title",
"mainTitle": [
{"language": "eng", "value": "Title of the document"}
],
}
],
"documentType": "coar:c_db06",
"organisation": [
{"$ref": "https://sonar.ch/api/organisations/org"}],
"identifiedBy": [
{"type": "bf:Local", "value": "10.1186"},
],
},
dbcommit=True,
with_bucket=True,
)
record.commit()
db.session.commit()
record.reindex()
return record
18 changes: 18 additions & 0 deletions tests/api/monitoring/test_monitoring_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,21 @@ def mock_info(*args):
mock_info)
response = client.get(url_for('monitoring_api.elastic_search'))
assert response.status_code == 500
assert response.json == {'error': 'Unknown exception'}


def test_unregistered_urn(client, es_clear, organisation, superuser,
monkeypatch, minimal_thesis_document):
"""Test unregistered urn counts."""

login_user_via_session(client, email=superuser['email'])

response = client.get(
url_for('monitoring_api.unregistered_urn'))
assert response.status_code == 200
assert response.json == {'data': 1}

response = client.get(
url_for('monitoring_api.unregistered_urn', days=100))
assert response.status_code == 200
assert response.json == {'data': 0}
1 change: 1 addition & 0 deletions tests/ui/documents/test_document_urn.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def minimal_document(db, bucket_location, organisation):
)
record.commit()
db.session.commit()
record.reindex()
return record


Expand Down

0 comments on commit c530871

Please sign in to comment.