1
0
mirror of https://github.com/ilri/dspace-statistics-api.git synced 2024-12-22 12:42:19 +01:00

app.py: Update SQL logic to use single table

The indexer.py script was updated to use a single table because I
learned about UPSERT. This simplifies the database schema and the
Python logic, and makes it easier to page all views and downloads
at once without complicated JOIN queries.
This commit is contained in:
Alan Orth 2018-09-24 14:28:00 +03:00
parent 2cab456f16
commit 1543cacc54
Signed by: alanorth
GPG Key ID: 0FB860CC9C45B1B9

20
app.py
View File

@ -14,26 +14,14 @@ class ItemResource:
"""Handles GET requests"""
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
# 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
cursor.execute('SELECT views, downloads FROM items WHERE id={0}'.format(item_id))
results = cursor.fetchone()
cursor.close()
statistics = {
'id': item_id,
'views': views,
'downloads': downloads
'views': results['views'],
'downloads': results['downloads']
}
resp.media = statistics