2018-09-18 13:03:15 +02:00
|
|
|
# Tested with Python 3.6
|
|
|
|
# See DSpace Solr docs for tips about parameters
|
|
|
|
# https://wiki.duraspace.org/display/DSPACE/Solr
|
|
|
|
|
2018-09-18 15:59:28 +02:00
|
|
|
from config import SOLR_SERVER
|
|
|
|
from config import SOLR_CORE
|
2018-09-18 13:03:15 +02:00
|
|
|
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)
|
|
|
|
|
2018-09-18 15:59:28 +02:00
|
|
|
solr = SolrClient(SOLR_SERVER)
|
2018-09-18 13:03:15 +02:00
|
|
|
|
|
|
|
# Get views
|
2018-09-18 15:59:28 +02:00
|
|
|
res = solr.query(SOLR_CORE, {
|
2018-09-20 16:34:55 +02:00
|
|
|
'q':'type:2 AND id:{0}'.format(item_id),
|
|
|
|
'fq':'isBot:false AND statistics_type:view'
|
2018-09-19 11:48:28 +02:00
|
|
|
}, rows=0)
|
2018-09-18 13:03:15 +02:00
|
|
|
|
|
|
|
views = res.get_num_found()
|
|
|
|
|
|
|
|
# Get downloads
|
2018-09-18 15:59:28 +02:00
|
|
|
res = solr.query(SOLR_CORE, {
|
2018-09-20 16:34:55 +02:00
|
|
|
'q':'type:0 AND owningItem:{0}'.format(item_id),
|
|
|
|
'fq':'isBot:false AND statistics_type:view AND bundleName:ORIGINAL'
|
2018-09-19 11:48:28 +02:00
|
|
|
}, rows=0)
|
2018-09-18 13:03:15 +02:00
|
|
|
|
|
|
|
downloads = res.get_num_found()
|
|
|
|
|
|
|
|
statistics = {
|
|
|
|
'id': item_id,
|
|
|
|
'views': views,
|
|
|
|
'downloads': downloads
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.media = statistics
|
|
|
|
|
|
|
|
api = falcon.API()
|
|
|
|
api.add_route('/item', ItemResource())
|