mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2025-05-10 15:16:02 +02:00
Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
46cfc3ffbc
|
|||
2850035a4c
|
|||
c0b550109a
|
|||
bfceffd84d
|
|||
d0552f5047
|
|||
c3a0bf7f44
|
|||
6e47e9c9ee
|
|||
cd90d618d6
|
|||
280d211d56
|
|||
806d63137f
|
|||
f7c7390e4f
|
@ -2,6 +2,7 @@ language: python
|
|||||||
python:
|
python:
|
||||||
- "3.5"
|
- "3.5"
|
||||||
- "3.6"
|
- "3.6"
|
||||||
|
- "3.7"
|
||||||
install:
|
install:
|
||||||
- pip install -r requirements.txt
|
- pip install -r requirements.txt
|
||||||
|
|
||||||
|
11
CHANGELOG.md
11
CHANGELOG.md
@ -4,10 +4,19 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [0.3.2] - 2018-09-25
|
||||||
|
## Changed
|
||||||
|
- /item/id route now returns HTTP 404 if an item is not found
|
||||||
|
|
||||||
|
## [0.3.1] - 2018-09-25
|
||||||
|
### Changed
|
||||||
|
- Force SolrClient's kazoo dependency to version 2.5.0 to work with Python 3.7
|
||||||
|
- Add Python 3.7 to Travis CI configuration
|
||||||
|
|
||||||
## [0.3.0] - 2018-09-25
|
## [0.3.0] - 2018-09-25
|
||||||
### Added
|
### Added
|
||||||
- requirements.txt for pip
|
- requirements.txt for pip
|
||||||
- Travis CI build configuration for Python 3.6 and 3.7
|
- Travis CI build configuration for Python 3.5 and 3.6
|
||||||
- Documentation on using the API
|
- Documentation on using the API
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
# DSpace Statistics API
|
# DSpace Statistics API
|
||||||
A quick and dirty REST API to expose Solr view and download statistics for items in a DSpace repository.
|
A quick and dirty REST API to expose Solr view and download statistics for items in a DSpace repository.
|
||||||
|
|
||||||
Written and tested in Python 3.5 and 3.6. SolrClient (0.2.1) does [not currently run in Python 3.7.0](https://github.com/moonlitesolutions/SolrClient/issues/79). Requires PostgreSQL version 9.5 or greater for [`UPSERT` support](https://wiki.postgresql.org/wiki/UPSERT).
|
Written and tested in Python 3.5, 3.6, and 3.7. Requires PostgreSQL version 9.5 or greater for [`UPSERT` support](https://wiki.postgresql.org/wiki/UPSERT).
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
Create a virtual environment and run it:
|
Create a virtual environment and run it:
|
||||||
|
|
||||||
$ virtualenv -p /usr/bin/python3.6 venv
|
$ python -m venv venv
|
||||||
$ . venv/bin/activate
|
$ . venv/bin/activate
|
||||||
$ pip install -r requirements.txt
|
$ pip install -r requirements.txt
|
||||||
$ gunicorn app:api
|
$ gunicorn app:api
|
||||||
@ -15,16 +15,15 @@ Create a virtual environment and run it:
|
|||||||
The API exposes the following endpoints:
|
The API exposes the following endpoints:
|
||||||
|
|
||||||
- GET `/items` — return views and downloads for all items that Solr knows about¹. Accepts `limit` and `page` query parameters for pagination of results.
|
- GET `/items` — return views and downloads for all items that Solr knows about¹. Accepts `limit` and `page` query parameters for pagination of results.
|
||||||
- GET `/item/id` — return views and downloads for a single item (*id* must be a positive integer).
|
- GET `/item/id` — return views and downloads for a single item (*id* must be a positive integer). Returns HTTP 404 if an item id is not found.
|
||||||
|
|
||||||
¹ We are querying the Solr statistics core, which technically only knows about all items that have either views or downloads.
|
¹ We are querying the Solr statistics core, which technically only knows about items that have either views or downloads.
|
||||||
|
|
||||||
## Todo
|
## Todo
|
||||||
|
|
||||||
- Add API documentation
|
- Add API documentation
|
||||||
- Close up DB connection when gunicorn shuts down gracefully
|
- Close up DB connection when gunicorn shuts down gracefully
|
||||||
- Better logging
|
- Better logging
|
||||||
- Return HTTP 404 when item_id is nonexistent
|
|
||||||
- Tests
|
- Tests
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
25
app.py
25
app.py
@ -47,17 +47,24 @@ class ItemResource:
|
|||||||
|
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
cursor.execute('SELECT views, downloads FROM items WHERE id={}'.format(item_id))
|
cursor.execute('SELECT views, downloads FROM items WHERE id={}'.format(item_id))
|
||||||
results = cursor.fetchone()
|
if cursor.rowcount == 0:
|
||||||
|
raise falcon.HTTPNotFound(
|
||||||
|
title='Item not found',
|
||||||
|
description='The item with id "{}" was not found.'.format(item_id)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
results = cursor.fetchone()
|
||||||
|
|
||||||
|
statistics = {
|
||||||
|
'id': item_id,
|
||||||
|
'views': results['views'],
|
||||||
|
'downloads': results['downloads']
|
||||||
|
}
|
||||||
|
|
||||||
|
resp.media = statistics
|
||||||
|
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
statistics = {
|
|
||||||
'id': item_id,
|
|
||||||
'views': results['views'],
|
|
||||||
'downloads': results['downloads']
|
|
||||||
}
|
|
||||||
|
|
||||||
resp.media = statistics
|
|
||||||
|
|
||||||
api = falcon.API()
|
api = falcon.API()
|
||||||
api.add_route('/items', AllItemsResource())
|
api.add_route('/items', AllItemsResource())
|
||||||
api.add_route('/item/{item_id:int}', ItemResource())
|
api.add_route('/item/{item_id:int}', ItemResource())
|
||||||
|
10
indexer.py
10
indexer.py
@ -20,17 +20,15 @@
|
|||||||
# ---
|
# ---
|
||||||
#
|
#
|
||||||
# Connects to a DSpace Solr statistics core and ingests item views and downloads
|
# Connects to a DSpace Solr statistics core and ingests item views and downloads
|
||||||
# into a Postgres database for use with other applications (an API, for example).
|
# into a PostgreSQL database for use by other applications (like an API).
|
||||||
#
|
#
|
||||||
# This script is written for Python 3 and requires several modules that you can
|
# This script is written for Python 3.5+ and requires several modules that you
|
||||||
# install with pip (I recommend setting up a Python virtual environment first):
|
# can install with pip (I recommend using a Python virtual environment):
|
||||||
#
|
#
|
||||||
# $ pip install SolrClient
|
# $ pip install SolrClient psycopg2-binary
|
||||||
#
|
#
|
||||||
# See: https://solrclient.readthedocs.io/en/latest/SolrClient.html
|
# See: https://solrclient.readthedocs.io/en/latest/SolrClient.html
|
||||||
# See: https://wiki.duraspace.org/display/DSPACE/Solr
|
# See: https://wiki.duraspace.org/display/DSPACE/Solr
|
||||||
#
|
|
||||||
# Tested with Python 3.5 and 3.6.
|
|
||||||
|
|
||||||
from database import database_connection
|
from database import database_connection
|
||||||
from solr import solr_connection
|
from solr import solr_connection
|
||||||
|
@ -3,7 +3,7 @@ chardet==3.0.4
|
|||||||
falcon==1.4.1
|
falcon==1.4.1
|
||||||
gunicorn==19.9.0
|
gunicorn==19.9.0
|
||||||
idna==2.7
|
idna==2.7
|
||||||
kazoo==2.2.1
|
kazoo==2.5.0
|
||||||
psycopg2-binary==2.7.5
|
psycopg2-binary==2.7.5
|
||||||
python-mimeparse==1.6.0
|
python-mimeparse==1.6.0
|
||||||
requests==2.19.1
|
requests==2.19.1
|
||||||
|
Reference in New Issue
Block a user