mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2024-11-25 23:58:18 +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"""
|
"""Handles GET requests"""
|
||||||
|
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
# get item views (and catch the TypeError if item doesn't have any views)
|
cursor.execute('SELECT views, downloads FROM items WHERE id={0}'.format(item_id))
|
||||||
cursor.execute('SELECT views FROM itemviews WHERE id={0}'.format(item_id))
|
results = cursor.fetchone()
|
||||||
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.close()
|
cursor.close()
|
||||||
|
|
||||||
statistics = {
|
statistics = {
|
||||||
'id': item_id,
|
'id': item_id,
|
||||||
'views': views,
|
'views': results['views'],
|
||||||
'downloads': downloads
|
'downloads': results['downloads']
|
||||||
}
|
}
|
||||||
|
|
||||||
resp.media = statistics
|
resp.media = statistics
|
||||||
|
Loading…
Reference in New Issue
Block a user