From 9e942736b1b96926b2bedbbe47bafb35bae85695 Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Sun, 23 Sep 2018 16:47:00 +0300 Subject: [PATCH] app.py: Get item statistics from SQLite database It is much more efficient to cache view and download statistics in a database than to query Solr on demand (not to mention that it is not possible to page easily with facets in Solr). I decided to use SQLite because it is fast, native in Python 3, and doesn't require any extra steps during provisioning (assuming permissions are ok). --- app.py | 30 +++++++++++++++++------------- config.py | 2 ++ database.py | 11 +++++++++++ 3 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 database.py diff --git a/app.py b/app.py index 75b7e90..1f54588 100644 --- a/app.py +++ b/app.py @@ -3,29 +3,33 @@ # https://wiki.duraspace.org/display/DSPACE/Solr from config import SOLR_CORE +from database import database_connection import falcon from solr import solr_connection +db = database_connection() +solr = solr_connection() class ItemResource: def on_get(self, req, resp, item_id): """Handles GET requests""" - # 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) + cursor = db.cursor() + # get item views (and catch the TypeError if item doesn't have any views) + cursor.execute('SELECT views FROM itemviews WHERE id={0}'.format(item_id)) + try: + views = cursor.fetchone()['views'] + except: + views = 0 - views = res.get_num_found() + # get item downloads (and catch the TypeError if item doesn't have any downloads) + cursor.execute('SELECT downloads FROM itemdownloads WHERE id={0}'.format(item_id)) + try: + downloads = cursor.fetchone()['downloads'] + except: + downloads = 0 - # 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() + cursor.close() statistics = { 'id': item_id, diff --git a/config.py b/config.py index adbc0fe..b4f4fb4 100644 --- a/config.py +++ b/config.py @@ -4,4 +4,6 @@ import os SOLR_SERVER = os.environ.get('SOLR_SERVER', 'http://localhost:8080/solr') SOLR_CORE = os.environ.get('SOLR_CORE', 'statistics') +SQLITE_DB = os.environ.get('SQLITE_DB', 'statistics.db') + # vim: set sw=4 ts=4 expandtab: diff --git a/database.py b/database.py new file mode 100644 index 0000000..fa5ed53 --- /dev/null +++ b/database.py @@ -0,0 +1,11 @@ +from config import SQLITE_DB +import sqlite3 + +def database_connection(): + connection = sqlite3.connect(SQLITE_DB) + # allow iterating over row results by column key + connection.row_factory = sqlite3.Row + + return connection + +# vim: set sw=4 ts=4 expandtab: