Skip to content

Commit

Permalink
statistics: change permission at the library level
Browse files Browse the repository at this point in the history
* Adds category, indicator, frequence and library facets.
* Changes the stat config update, delete permission at the library level.
* Collects the librarian stats during the setup.

Co-Authored-by: Johnny Mariéthoz <Johnny.Mariethoz@rero.ch>
  • Loading branch information
jma committed Nov 16, 2023
1 parent 6b5633c commit fe32526
Show file tree
Hide file tree
Showing 29 changed files with 491 additions and 221 deletions.
74 changes: 37 additions & 37 deletions data/stats_cfg.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
[
{
"pid": "1",
"name": "organisation 1, no distributions",
"name": "library 1, no distributions",
"description": "Statistics configuration with no distributions",
"category": {
"type": "catalogue",
"indicator": {
"type": "number_of_documents"
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/1"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/1"
},
"frequency": "month",
"is_active": true
},
{
"pid": "2",
"name": "organisation 1, circulation, number of checkouts, time range month",
"name": "library 1, circulation, number of checkouts, time range month",
"description": "Statistics configuration with 1 distribution",
"category": {
"type": "circulation",
Expand All @@ -28,15 +28,15 @@
]
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/1"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/1"
},
"frequency": "month",
"is_active": true
},
{
"pid": "3",
"name": "organisation 1, number of checkouts, library",
"name": "library 2, number of checkouts, library",
"description": "Statistics configuration with 1 distribution",
"category": {
"type": "circulation",
Expand All @@ -47,15 +47,15 @@
]
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/1"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/2"
},
"frequency": "month",
"is_active": true
},
{
"pid": "4",
"name": "organisation 1, 2 distributions",
"name": "library 2, 2 distributions",
"description": "Statistics configuration with 2 distributions",
"category": {
"type": "circulation",
Expand All @@ -67,15 +67,15 @@
]
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/1"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/2"
},
"frequency": "month",
"is_active": true
},
{
"pid": "5",
"name": "organisation 1, 2 distributions, period 1 year",
"name": "library 3, 2 distributions, period 1 year",
"description": "Statistics configuration with 2 distributions",
"category": {
"type": "circulation",
Expand All @@ -88,15 +88,15 @@
"period": "year"
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/1"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/4"
},
"frequency": "month",
"is_active": true
},
{
"pid": "6",
"name": "organisation 1, 2 distributions, status disabled",
"name": "library 3, 2 distributions, status disabled",
"description": "Statistics configuration with 2 distributions",
"category": {
"type": "circulation",
Expand All @@ -108,31 +108,31 @@
]
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/1"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/4"
},
"frequency": "month",
"is_active": false
},
{
"pid": "7",
"name": "organisation 2, no distributions",
"name": "library 5, no distributions",
"description": "Statistics configuration with no distributions",
"category": {
"type": "catalogue",
"indicator": {
"type": "number_of_documents"
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/2"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/5"
},
"frequency": "month",
"is_active": true
},
{
"pid": "8",
"name": "organisation 2, 1 distribution",
"name": "library 5, 1 distribution",
"description": "Statistics configuration with 1 distribution",
"category": {
"type": "circulation",
Expand All @@ -143,15 +143,15 @@
]
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/2"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/5"
},
"frequency": "month",
"is_active": true
},
{
"pid": "9",
"name": "organisation 2, 1 distribution",
"name": "library 5, 1 distribution",
"description": "Statistics configuration with 1 distribution",
"category": {
"type": "circulation",
Expand All @@ -162,15 +162,15 @@
]
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/2"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/5"
},
"frequency": "month",
"is_active": true
},
{
"pid": "10",
"name": "organisation 2, 2 distributions",
"name": "library 5, 2 distributions",
"description": "Statistics configuration with 2 distributions",
"category": {
"type": "circulation",
Expand All @@ -182,15 +182,15 @@
]
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/2"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/5"
},
"frequency": "month",
"is_active": true
},
{
"pid": "11",
"name": "organisation 2, 2 distributions, period 1 year",
"name": "library 5, 2 distributions, period 1 year",
"description": "Statistics configuration with 2 distributions",
"category": {
"type": "circulation",
Expand All @@ -203,15 +203,15 @@
"period": "year"
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/2"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/5"
},
"frequency": "month",
"is_active": true
},
{
"pid": "12",
"name": "organisation 2, 2 distributions, status disabled",
"name": "library 5, 2 distributions, status disabled",
"description": "Statistics configuration with 1 distribution",
"category": {
"type": "circulation",
Expand All @@ -223,10 +223,10 @@
]
}
},
"organisation": {
"$ref": "https://bib.rero.ch/api/organisations/2"
"library": {
"$ref": "https://bib.rero.ch/api/libraries/5"
},
"frequency": "month",
"is_active": false
}
]
]
3 changes: 2 additions & 1 deletion data/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@
"pro_read_only",
"pro_catalog_manager",
"pro_circulation_manager",
"pro_user_manager"
"pro_user_manager",
"pro_statistic_manager"
],
"libraries": [
{
Expand Down
24 changes: 19 additions & 5 deletions rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,10 +839,12 @@ def _(x):
'json': 'application/json'
},
search_serializers_aliases={
'json': 'application/json'
'json': 'application/json',
'rero+json': 'application/json',
},
search_serializers={
'application/json': 'rero_ils.modules.serializers:json_v1_search'
'application/json': 'rero_ils.modules.serializers:json_v1_search',
'application/rero+json': 'rero_ils.modules.stats_cfg.serializers:json_search'
},
list_route='/stats_cfg/',
record_loaders={
Expand Down Expand Up @@ -2336,11 +2338,23 @@ def _(x):
field='category.type',
size=RERO_ILS_AGGREGATION_SIZE.get(
'stats_cfg', RERO_ILS_DEFAULT_AGGREGATION_SIZE)
),
aggs=dict(
indicator=dict(terms=dict(field='category.indicator.type', size=DOCUMENTS_AGGREGATION_SIZE))
)
)
),
frequency=dict(
terms=dict(field='frequency', size=DOCUMENTS_AGGREGATION_SIZE),

),
library=dict(terms=dict(field='library.pid', size=RERO_ILS_DEFAULT_AGGREGATION_SIZE))
),
filters={
_('category'): and_term_filter('category.type')
_('category'): and_term_filter('category.type'),
_('indicator'): and_term_filter('category.indicator.type'),
_('frequency'): and_term_filter('frequency'),
_('library'): and_term_filter('library.pid'),
_('active'): and_term_filter('is_active')
}
)
)
Expand Down Expand Up @@ -2909,7 +2923,7 @@ def _(x):
template='rero_ils/detailed_view_stats.html',
record_class='rero_ils.modules.stats.api.api:Stat',
view_imp='rero_ils.modules.stats.views.stats_view_method',
permission_factory_imp='rero_ils.permissions.admin_permission_factory',
permission_factory_imp='rero_ils.modules.stats.permissions:stats_ui_permission_factory',
)
}

Expand Down
11 changes: 11 additions & 0 deletions rero_ils/modules/libraries/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import pytz
from dateutil import parser
from dateutil.rrule import FREQNAMES, rrule
from elasticsearch_dsl import Q
from flask_babelex import gettext as _

from rero_ils.modules.api import IlsRecord, IlsRecordsIndexer, IlsRecordsSearch
from rero_ils.modules.fetchers import id_fetcher
from rero_ils.modules.locations.api import LocationsSearch
from rero_ils.modules.minters import id_minter
from rero_ils.modules.providers import Provider
from rero_ils.modules.stats_cfg.api import StatsConfigurationSearch
from rero_ils.modules.users.models import UserRole
from rero_ils.modules.utils import date_string_to_utc, \
extracted_data_from_ref, sorted_pids, strtotime
Expand Down Expand Up @@ -377,6 +379,11 @@ def get_links_to_me(self, get_pids=False):
AcqReceiptsSearch
from rero_ils.modules.patrons.api import PatronsSearch
links = {}
stat_cfg_query = StatsConfigurationSearch()\
.filter(
Q('term', library__pid=self.pid) |
Q('term', filter_by_libraries__pid=self.pid)
)
location_query = LocationsSearch() \
.filter('term', library__pid=self.pid)
patron_query = PatronsSearch() \
Expand All @@ -388,16 +395,20 @@ def get_links_to_me(self, get_pids=False):
locations = sorted_pids(location_query)
librarians = sorted_pids(patron_query)
receipts = sorted_pids(receipt_query)
stats_cfg = sorted_pids(stat_cfg_query)
else:
locations = location_query.count()
librarians = patron_query.count()
receipts = receipt_query.count()
stats_cfg = stat_cfg_query.count()
if locations:
links['locations'] = locations
if librarians:
links['patrons'] = librarians
if receipts:
links['acq_receipts'] = receipts
if stats_cfg:
links['stats_cfg'] = stats_cfg
return links

def reasons_not_to_delete(self):
Expand Down
5 changes: 4 additions & 1 deletion rero_ils/modules/stats/api/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from rero_ils.modules.libraries.api import LibrariesSearch
from rero_ils.modules.locations.api import LocationsSearch
from rero_ils.modules.patron_types.api import PatronTypesSearch
from rero_ils.modules.stats_cfg.api import StatConfiguration
from rero_ils.modules.utils import extracted_data_from_ref

from .indicators import NumberOfActivePatronsCfg, NumberOfCirculationCfg, \
Expand All @@ -42,13 +43,15 @@ def __init__(self, config):
Set variables to create report.
"""
if not isinstance(config, StatConfiguration):
config = StatConfiguration(data=config)
self.config = config
self.is_active = config.get('is_active', False)
self.indicator = config['category']['indicator']['type']
self.period = config['category']['indicator'].get('period')
self.distributions = config[
'category']['indicator'].get('distributions', [])
self.org_pid = extracted_data_from_ref(config['organisation'])
self.org_pid = config.organisation_pid
self.filter_by_libraries = []
for library in config.get('filter_by_libraries', []):
self.filter_by_libraries.append(extracted_data_from_ref(library))
Expand Down
13 changes: 10 additions & 3 deletions rero_ils/modules/stats/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

from invenio_records.extensions import RecordExtension

from rero_ils.modules.libraries.api import Library
from rero_ils.modules.patrons.api import current_librarian

from rero_ils.modules.utils import extracted_data_from_ref
from .models import StatType


Expand All @@ -41,8 +42,14 @@ def pre_dump(self, record, data, dumper=None):
:param dumper: the dumper class used to dump the record.
"""
# to filter the search list results
if org := record.get('config', {}).get('organisation'):
record['organisation'] = org
if lib := record.get('config', {}).get('library'):
if lib.get('pid'):
library_pid = Library.get_record_by_pid(lib['pid']).organisation_pid
else:
library_pid = extracted_data_from_ref(lib.get('$ref'))
record['organisation'] = {
'pid': library_pid
}

if not current_librarian:
return
Expand Down
Loading

0 comments on commit fe32526

Please sign in to comment.