8000 facet: fix year by rerowep · Pull Request #3605 · rero/rero-ils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

facet: fix year #3605

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

Merged
merged 1 commit into from
Apr 2, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,6 @@ def build_agent_data(code, label, index, link):
publication['endDate'] = marc21.date['end_date']
if 'note' in marc21.date:
publication['note'] = marc21.date['note']
publication['startDate'] = marc21.date['start_date']

places = []
place = marc21.build_place()
Expand Down
12 changes: 8 additions & 4 deletions rero_ils/modules/documents/serializers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,19 @@ def extract_year(key, default):
:param: default: the default year.
:return: the year in yyyy format.
"""
return aggregations['year'][key].get('value', default)
# default could be None
if year := aggregations['year'][key].get('value'):
return float(year)
return default

# transform aggregation to send configuration
# for ng-core range widget.
# this allows you to fill in the fields on the frontend.
aggregations['year'] = {
'type': 'range',
'config': {
'min': extract_year('year_min', -9999),
'max': extract_year('year_max', 9999),
'min': extract_year('year_min', 0.0),
'max': extract_year('year_max', 9999.9),
'step': 1
}
}
Expand All @@ -162,7 +165,8 @@ def extract_acquisition_date(key, default):
'config': {
'min': extract_acquisition_date('date_min', '1900-01-01'),
'max': extract_acquisition_date(
'date_max', datetime.now().strftime('%Y-%m-%d'))
'date_max', datetime.now().strftime('%Y-%m-%d')
)
}
}

Expand Down
43 changes: 33 additions & 10 deletions rero_ils/modules/imports/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

import pickle
import traceback
from datetime import timedelta
from operator import itemgetter
from datetime import datetime, timedelta

import requests
from dojson.contrib.marc21.utils import create_record
Expand Down Expand Up @@ -175,9 +174,14 @@ def calculate_aggregations(self, record, id):
)

provision_activitys = record.get('provisionActivity', [])
year_min = int(datetime.now().strftime('%Y'))
year_max = 1400
for provision_activity in provision_activitys:
date = provision_activity.get('startDate')
8000 self.calculate_aggregations_add('year', date, id)
if date := provision_activity.get('startDate'):
self.calculate_aggregations_add('year', date, id)
int_date = int(date)
year_min = min(int_date, year_min)
year_max = max(int_date, year_max)

for agent in record.get('contribution', []):
if authorized_access_point := agent.get(
Expand All @@ -193,6 +197,8 @@ def calculate_aggregations(self, record, id):
lang = language.get('value')
self.calculate_aggregations_add('language', lang, id)

return year_min, year_max + 1

def create_aggregations(self, results):
"""Create aggregations.

Expand All @@ -205,12 +211,24 @@ def create_aggregations(self, results):
'year': {},
'language': {}
}
results['aggregations'] = {}
if year_config := results.get(
'aggregations', {}).get('year', {}).get('config'):
results['aggregations'] = {
'year': {
'config': year_config
}
}
else:
results['aggregations'] = {}
year_min = int(datetime.now().strftime('%Y'))
year_max = 1400
for data in results['hits']['hits']:
self.calculate_aggregations(
y_min, y_max = self.calculate_aggregations(
data['metadata'],
data['id']
)
year_min = min(y_min, year_min)
year_max = max(y_max, year_max)
for agg, values in self.aggregations_creation.items():
buckets = []
for key, value in values.items():
Expand All @@ -236,12 +254,17 @@ def create_aggregations(self, results):
'buckets': sub_buckets
}
buckets.append(bucket_data)
if agg == 'year':
buckets.sort(key=itemgetter('key'), reverse=True)
else:
buckets.sort(key=lambda e: (-e['doc_count'], e['key']))
buckets.sort(key=lambda e: (-e['doc_count'], e['key']))
if buckets:
results['aggregations'][agg] = {'buckets': buckets}
results['aggregations'].setdefault('year', {})
if 'config' not in results['aggregations']['year']:
results['aggregations']['year']['config'] = {
'max': year_max,
'min': year_min,
'step': 1
}
results['aggregations']['year']['type'] = 'range'
results['hits']['total']['value'] = len(results['hits']['hits'])
return results

Expand Down
21 changes: 13 additions & 8 deletions rero_ils/modules/imports/views.py
73D5
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,19 @@ def get(self, **kwargs):
max_results=size,
no_cache=no_cache
)
filter_year = flask_request.args.get('year')
if filter_year:
ids = do_import.get_ids_for_aggregation(
results=results,
aggregation='year',
key=int(filter_year)
)
results = do_import.filter_records(results, ids)
if filter_years := flask_request.args.get('year'):
values = dict(zip(['from', 'to'], filter_years.split('--')))
values.setdefault('from', 1900)
values.setdefault('to', 2555)
ids = []
for year in range(int(values['from']), int(values['to'])):
year_ids = do_import.get_ids_for_aggregation(
results=results,
aggregation='year',
key=int(year)
)
ids += year_ids
results = do_import.filter_records(results, list(set(ids)))
filter_type = flask_request.args.get('document_type')
if filter_type:
sub_filter_type = flask_request.args.get('document_subtype')
Expand Down
0