1
0
mirror of https://github.com/ilri/dspace-statistics-api.git synced 2025-07-06 14:31:38 +02:00

Use PostgreSQL instead of SQLite

I was very surprised how easy and fast and robust SQLite was, but in
the end I realized that its UPSERT support only came in version 3.24
and both Ubuntu 16.04 and 18.04 have older versions than that! I did
manage to install libsqlite3-0 from Ubuntu 18.04 cosmic on my xenial
host, but that feels dirty.

PostgreSQL has support for UPSERT since 9.5, not to mention the same
nice LIMIT and OFFSET clauses.
This commit is contained in:
2018-09-25 00:49:47 +03:00
parent 28d61fb041
commit 8f7450f67a
4 changed files with 28 additions and 21 deletions

View File

@ -32,7 +32,7 @@
#
# Tested with Python 3.5 and 3.6.
from database import database_connection_rw
from database import database_connection
from solr import solr_connection
def index_views():
@ -52,6 +52,8 @@ def index_views():
results_num_pages = round(results_numFound / results_per_page)
results_current_page = 0
cursor = db.cursor()
while results_current_page <= results_num_pages:
print('Page {0} of {1}.'.format(results_current_page, results_num_pages))
@ -70,7 +72,7 @@ def index_views():
views = res.get_facets()
# in this case iterate over the 'id' dict and get the item ids and views
for item_id, item_views in views['id'].items():
db.execute('''INSERT INTO items(id, views) VALUES(?, ?)
cursor.execute('''INSERT INTO items(id, views) VALUES(%s, %s)
ON CONFLICT(id) DO UPDATE SET downloads=excluded.views''',
(item_id, item_views))
@ -78,6 +80,8 @@ def index_views():
results_current_page += 1
cursor.close()
def index_downloads():
print("Populating database with item downloads.")
@ -95,6 +99,8 @@ def index_downloads():
results_num_pages = round(results_numFound / results_per_page)
results_current_page = 0
cursor = db.cursor()
while results_current_page <= results_num_pages:
print('Page {0} of {1}.'.format(results_current_page, results_num_pages))
@ -113,7 +119,7 @@ def index_downloads():
downloads = res.get_facets()
# in this case iterate over the 'owningItem' dict and get the item ids and downloads
for item_id, item_downloads in downloads['owningItem'].items():
db.execute('''INSERT INTO items(id, downloads) VALUES(?, ?)
cursor.execute('''INSERT INTO items(id, downloads) VALUES(%s, %s)
ON CONFLICT(id) DO UPDATE SET downloads=excluded.downloads''',
(item_id, item_downloads))
@ -121,11 +127,14 @@ def index_downloads():
results_current_page += 1
db = database_connection_rw()
cursor.close()
db = database_connection()
solr = solr_connection()
# create table to store item views and downloads
db.execute('''CREATE TABLE IF NOT EXISTS items
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS items
(id INT PRIMARY KEY, views INT DEFAULT 0, downloads INT DEFAULT 0)''')
index_views()
index_downloads()