1
0
mirror of https://github.com/ilri/dspace-statistics-api.git synced 2024-06-26 16:13:47 +02:00
dspace-statistics-api/app.py
Alan Orth a263996582
app.py: Fix Solr queries for item views
According to dspace-api's Constants.java, items are type 2 and they
use a unique ID field of `id` instead of `owningItem`. There is no
need to check the bundleName for item types.

Also, I decided to use the main Solr query for item IDs because the
filter query parameter (fq) stores results in the filterCache and
can be quite expensive with cores storing tens of millions of docu-
ments (we currently have 149 million docs!). It makes sense to use
the filter query parameter to reduce the result set returned by the
main Solr query.
2018-09-20 17:37:13 +03:00

45 lines
1.2 KiB
Python

# Tested with Python 3.6
# See DSpace Solr docs for tips about parameters
# https://wiki.duraspace.org/display/DSPACE/Solr
from config import SOLR_SERVER
from config import SOLR_CORE
import falcon
from SolrClient import SolrClient
class ItemResource:
def on_get(self, req, resp):
"""Handles GET requests"""
# Return HTTPBadRequest if id parameter is not present and valid
item_id = req.get_param_as_int("id", required=True, min=0)
solr = SolrClient(SOLR_SERVER)
# Get views
res = solr.query(SOLR_CORE, {
'q':'type:2 AND id:{0}'.format(item_id),
'fq':'isBot:false AND statistics_type:view'
}, rows=0)
views = res.get_num_found()
# Get downloads
res = solr.query(SOLR_CORE, {
'q':'type:0 AND owningItem:{0}'.format(item_id),
'fq':'isBot:false AND statistics_type:view AND bundleName:ORIGINAL'
}, rows=0)
downloads = res.get_num_found()
statistics = {
'id': item_id,
'views': views,
'downloads': downloads
}
resp.media = statistics
api = falcon.API()
api.add_route('/item', ItemResource())