Skip to content

Commit

Permalink
document: add new acquisition aggregation
Browse files Browse the repository at this point in the history
* Closes #3525.

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr committed Nov 16, 2023
1 parent 9154312 commit 40b7666
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
3 changes: 2 additions & 1 deletion rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,8 @@ def _(x):
),
status=dict(terms=dict(field='holdings.items.status', size=DOCUMENTS_AGGREGATION_SIZE)),
intendedAudience=dict(terms=dict(field='intendedAudience.value', size=DOCUMENTS_AGGREGATION_SIZE)),
year=dict(date_histogram=dict(field='provisionActivity.startDate', interval='year', format='yyyy'))
year=dict(date_histogram=dict(field='provisionActivity.startDate', interval='year', format='yyyy')),
new_acquisition=dict(filter=dict(bool=dict(filter=[]))),
),
filters={
_('online'): or_terms_filter_by_criteria({
Expand Down
13 changes: 12 additions & 1 deletion rero_ils/modules/documents/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

"""Query factories for Document REST API."""
import re
from datetime import datetime

from elasticsearch_dsl import Q
from flask import request
Expand Down Expand Up @@ -52,7 +53,17 @@ def inner(values):
# pid=2) until Jan, 1 2020

# build acquisition date range query
values = dict(zip(['from', 'to'], values.pop().split(':')))
range_values = values.pop()
if '--' in range_values:
values = dict(zip(['from', 'to'], range_values.split('--')))
if 'from' in values:
values['from'] = datetime.fromtimestamp(
float(values['from'])/1000).strftime('%Y-%m-%d')
if 'to' in values:
values['to'] = datetime.fromtimestamp(
float(values['to'])/1000).strftime('%Y-%m-%d')
else:
values = dict(zip(['from', 'to'], range_values.split(':')))
range_acquisition_dates = {'lt': values.get('to') or 'now/d'}
if values.get('from'):
range_acquisition_dates['gte'] = values.get('from')
Expand Down
8 changes: 8 additions & 0 deletions rero_ils/modules/documents/serializers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

"""RERO Document JSON serialization."""

from datetime import datetime
from flask import current_app, json, request, stream_with_context
from werkzeug.local import LocalProxy

Expand Down Expand Up @@ -124,6 +125,13 @@ def _postprocess_search_aggregations(self, aggregations: dict) -> None:
'step': 1
}

if aggregations.get('new_acquisition'):
aggregations['new_acquisition']['type'] = 'date-range'
aggregations['new_acquisition']['config'] = {
'min': '1900-01-01',
'max': datetime.now().strftime('%Y-%m-%d')
}

if aggr_org := aggregations.get('organisation', {}).get('buckets', []):
# For a "local view", we only need the facet on the location
# organisation. We can filter the organisation aggregation to keep
Expand Down
31 changes: 26 additions & 5 deletions tests/api/documents/test_documents_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,24 @@ def test_documents_newacq_filters(app, client,
):
login_user_via_session(client, system_librarian_martigny.user)

def datetime_delta(**args):
"""Apply delta on date time."""
return datetime.now() + timedelta(**args)

def datetime_milliseconds(date):
"""datetime get milliseconds."""
return round(date.timestamp() * 1000)

# compute useful date
today = datetime.today()
past = (today - timedelta(days=1)).strftime('%Y-%m-%d')
future = (today + timedelta(days=10)).strftime('%Y-%m-%d')
future_1 = (today + timedelta(days=11)).strftime('%Y-%m-%d')
today = datetime.now()
past = datetime_delta(days=-1).strftime('%Y-%m-%d')
past_timestamp = datetime_milliseconds(datetime_delta(days=-30))
future = datetime_delta(days=10).strftime('%Y-%m-%d')
future_1 = datetime_delta(days=11).strftime('%Y-%m-%d')
future_1_timestamp = datetime_milliseconds(datetime_delta(days=1))
today = today.strftime('%Y-%m-%d')

# Add a new items with acq_date
# # Add a new items with acq_date
new_acq1 = deepcopy(item_lib_martigny_data)
new_acq1['pid'] = 'itemacq1'
new_acq1['acquisition_date'] = today
Expand Down Expand Up @@ -135,6 +145,17 @@ def test_documents_newacq_filters(app, client,
data = get_json(res)
assert data['hits']['total']['value'] == 0

# check new_acquisition filters with -- separator and timestamp
# Ex: 1696111200000--1700089200000
doc_list = url_for(
'invenio_records_rest.doc_list',
view='global',
new_acquisition='{0}--{1}'.format(past_timestamp, future_1_timestamp),
)
res = client.get(doc_list, headers=rero_json_header)
data = get_json(res)
assert data['hits']['total']['value'] == 1


@mock.patch('invenio_records_rest.views.verify_record_permission',
mock.MagicMock(return_value=VerifyRecordPermissionPatch))
Expand Down

0 comments on commit 40b7666

Please sign in to comment.