mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2024-11-05 06:23:00 +01:00
Alan Orth
a87aaba812
This whole business with negative query ranges is confusing as hell and I'll definitely forget it in the future. In DSpace's Solr term- inology a "download" is a view to some bitstream that lives in the ORIGINAL bundle. This is where bitstreams that are uploaded during the item submission process go, versus generated thumbnails, etc.
45 lines
1.2 KiB
Python
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:0',
|
|
'fq':'owningItem:{0} AND isBot:false AND statistics_type:view AND -bundleName:ORIGINAL'.format(item_id)
|
|
})
|
|
|
|
views = res.get_num_found()
|
|
|
|
# Get downloads
|
|
res = solr.query(SOLR_CORE, {
|
|
'q':'type:0',
|
|
'fq':'owningItem:{0} AND isBot:false AND statistics_type:view AND bundleName:ORIGINAL'.format(item_id)
|
|
})
|
|
|
|
downloads = res.get_num_found()
|
|
|
|
statistics = {
|
|
'id': item_id,
|
|
'views': views,
|
|
'downloads': downloads
|
|
}
|
|
|
|
resp.media = statistics
|
|
|
|
api = falcon.API()
|
|
api.add_route('/item', ItemResource())
|