mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2024-11-22 06:15:02 +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:
parent
2cab456f16
commit
1543cacc54
20
app.py
20
app.py
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user