2018-09-18 13:03:15 +02:00
|
|
|
import falcon
|
|
|
|
|
2019-11-27 11:31:04 +01:00
|
|
|
from .database import DatabaseManager
|
|
|
|
|
2018-11-03 22:55:23 +01:00
|
|
|
|
2018-10-31 23:19:39 +01:00
|
|
|
class RootResource:
|
|
|
|
def on_get(self, req, resp):
|
|
|
|
resp.status = falcon.HTTP_200
|
2019-11-27 11:30:06 +01:00
|
|
|
resp.content_type = "text/html"
|
|
|
|
with open("dspace_statistics_api/docs/index.html", "r") as f:
|
2018-10-31 23:19:39 +01:00
|
|
|
resp.body = f.read()
|
|
|
|
|
2018-11-03 22:55:23 +01:00
|
|
|
|
2018-09-24 15:07:26 +02:00
|
|
|
class AllItemsResource:
|
|
|
|
def on_get(self, req, resp):
|
|
|
|
"""Handles GET requests"""
|
|
|
|
# Return HTTPBadRequest if id parameter is not present and valid
|
2019-03-17 22:23:23 +01:00
|
|
|
limit = req.get_param_as_int("limit", min_value=0, max_value=100) or 100
|
|
|
|
page = req.get_param_as_int("page", min_value=0) or 0
|
2018-09-24 15:07:26 +02:00
|
|
|
offset = limit * page
|
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
with DatabaseManager() as db:
|
|
|
|
db.set_session(readonly=True)
|
2018-09-24 15:07:26 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
with db.cursor() as cursor:
|
|
|
|
# get total number of items so we can estimate the pages
|
2019-11-27 11:30:06 +01:00
|
|
|
cursor.execute("SELECT COUNT(id) FROM items")
|
2018-11-07 16:41:21 +01:00
|
|
|
pages = round(cursor.fetchone()[0] / limit)
|
2018-09-24 15:07:26 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
# get statistics, ordered by id, and use limit and offset to page through results
|
2019-11-27 11:30:06 +01:00
|
|
|
cursor.execute(
|
|
|
|
"SELECT id, views, downloads FROM items ORDER BY id ASC LIMIT {} OFFSET {}".format(
|
|
|
|
limit, offset
|
|
|
|
)
|
|
|
|
)
|
2018-09-24 15:07:26 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
# create a list to hold dicts of item stats
|
|
|
|
statistics = list()
|
2018-09-24 15:07:26 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
# iterate over results and build statistics object
|
|
|
|
for item in cursor:
|
2019-11-27 11:30:06 +01:00
|
|
|
statistics.append(
|
|
|
|
{
|
|
|
|
"id": item["id"],
|
|
|
|
"views": item["views"],
|
|
|
|
"downloads": item["downloads"],
|
|
|
|
}
|
|
|
|
)
|
2018-09-27 10:03:44 +02:00
|
|
|
|
2018-09-24 15:07:26 +02:00
|
|
|
message = {
|
2019-11-27 11:30:06 +01:00
|
|
|
"currentPage": page,
|
|
|
|
"totalPages": pages,
|
|
|
|
"limit": limit,
|
|
|
|
"statistics": statistics,
|
2018-09-24 15:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.media = message
|
|
|
|
|
2018-11-03 22:55:23 +01:00
|
|
|
|
2018-09-18 13:03:15 +02:00
|
|
|
class ItemResource:
|
2018-09-23 15:23:33 +02:00
|
|
|
def on_get(self, req, resp, item_id):
|
2018-09-18 13:03:15 +02:00
|
|
|
"""Handles GET requests"""
|
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
with DatabaseManager() as db:
|
|
|
|
db.set_session(readonly=True)
|
|
|
|
|
|
|
|
with db.cursor() as cursor:
|
|
|
|
cursor = db.cursor()
|
2019-11-27 11:30:06 +01:00
|
|
|
cursor.execute(
|
|
|
|
"SELECT views, downloads FROM items WHERE id={}".format(item_id)
|
|
|
|
)
|
2018-11-07 16:41:21 +01:00
|
|
|
if cursor.rowcount == 0:
|
|
|
|
raise falcon.HTTPNotFound(
|
2019-11-27 11:30:06 +01:00
|
|
|
title="Item not found",
|
|
|
|
description='The item with id "{}" was not found.'.format(
|
|
|
|
item_id
|
|
|
|
),
|
2018-11-07 16:41:21 +01:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
results = cursor.fetchone()
|
|
|
|
|
|
|
|
statistics = {
|
2019-11-27 11:30:06 +01:00
|
|
|
"id": item_id,
|
|
|
|
"views": results["views"],
|
|
|
|
"downloads": results["downloads"],
|
2018-11-07 16:41:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.media = statistics
|
2018-09-18 13:03:15 +02:00
|
|
|
|
2018-11-03 22:55:23 +01:00
|
|
|
|
2018-10-26 18:21:27 +02:00
|
|
|
api = application = falcon.API()
|
2019-11-27 11:30:06 +01:00
|
|
|
api.add_route("/", RootResource())
|
|
|
|
api.add_route("/items", AllItemsResource())
|
|
|
|
api.add_route("/item/{item_id:int}", ItemResource())
|
2018-09-23 10:33:26 +02:00
|
|
|
|
|
|
|
# vim: set sw=4 ts=4 expandtab:
|