From 36633e405a49e9061f82c2473c555ca39066e978 Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Tue, 18 Sep 2018 14:03:15 +0300 Subject: [PATCH] Initial commit Add first working version of the statistics API. --- app.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 app.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..9134290 --- /dev/null +++ b/app.py @@ -0,0 +1,44 @@ +# Tested with Python 3.6 +# See DSpace Solr docs for tips about parameters +# https://wiki.duraspace.org/display/DSPACE/Solr + +import falcon +from SolrClient import SolrClient + +solr_server = 'http://localhost:3000/solr' +solr_core = 'statistics' + +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:[* TO *] -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())