1
0
mirror of https://github.com/ilri/dspace-statistics-api.git synced 2024-11-21 22:05:02 +01:00

Correct issues highlighted by Flake8

Flake8 validates code style against PEP 8 in order to encourage the
writing of idiomatic Python. For reference, I am currently ignoring
errors about line length (E501) because I feel it makes code harder
to read.

This is the invocation I am using:

    $ flake8 --ignore E501 dspace_statistics_api
This commit is contained in:
Alan Orth 2018-11-03 23:55:23 +02:00
parent 70dfcb93c5
commit cc5ce3ab98
4 changed files with 52 additions and 42 deletions

View File

@ -4,6 +4,7 @@ import falcon
db = database_connection() db = database_connection()
db.set_session(readonly=True) db.set_session(readonly=True)
class RootResource: class RootResource:
def on_get(self, req, resp): def on_get(self, req, resp):
resp.status = falcon.HTTP_200 resp.status = falcon.HTTP_200
@ -11,6 +12,7 @@ class RootResource:
with open('dspace_statistics_api/docs/index.html', 'r') as f: with open('dspace_statistics_api/docs/index.html', 'r') as f:
resp.body = f.read() resp.body = f.read()
class AllItemsResource: class AllItemsResource:
def on_get(self, req, resp): def on_get(self, req, resp):
"""Handles GET requests""" """Handles GET requests"""
@ -33,19 +35,20 @@ class AllItemsResource:
# iterate over results and build statistics object # iterate over results and build statistics object
for item in cursor: for item in cursor:
statistics.append({ 'id': item['id'], 'views': item['views'], 'downloads': item['downloads'] }) statistics.append({'id': item['id'], 'views': item['views'], 'downloads': item['downloads']})
cursor.close() cursor.close()
message = { message = {
'currentPage': page, 'currentPage': page,
'totalPages': pages, 'totalPages': pages,
'limit': limit, 'limit': limit,
'statistics': statistics 'statistics': statistics
} }
resp.media = message resp.media = message
class ItemResource: class ItemResource:
def on_get(self, req, resp, item_id): def on_get(self, req, resp, item_id):
"""Handles GET requests""" """Handles GET requests"""
@ -54,8 +57,8 @@ class ItemResource:
cursor.execute('SELECT views, downloads FROM items WHERE id={}'.format(item_id)) cursor.execute('SELECT views, downloads FROM items WHERE id={}'.format(item_id))
if cursor.rowcount == 0: if cursor.rowcount == 0:
raise falcon.HTTPNotFound( raise falcon.HTTPNotFound(
title='Item not found', title='Item not found',
description='The item with id "{}" was not found.'.format(item_id) description='The item with id "{}" was not found.'.format(item_id)
) )
else: else:
results = cursor.fetchone() results = cursor.fetchone()
@ -70,6 +73,7 @@ class ItemResource:
cursor.close() cursor.close()
api = application = falcon.API() api = application = falcon.API()
api.add_route('/', RootResource()) api.add_route('/', RootResource())
api.add_route('/items', AllItemsResource()) api.add_route('/items', AllItemsResource())

View File

@ -3,7 +3,9 @@ from .config import DATABASE_USER
from .config import DATABASE_PASS from .config import DATABASE_PASS
from .config import DATABASE_HOST from .config import DATABASE_HOST
from .config import DATABASE_PORT from .config import DATABASE_PORT
import psycopg2, psycopg2.extras import psycopg2
import psycopg2.extras
def database_connection(): def database_connection():
connection = psycopg2.connect("dbname={} user={} password={} host={} port={}".format(DATABASE_NAME, DATABASE_USER, DATABASE_PASS, DATABASE_HOST, DATABASE_PORT), cursor_factory=psycopg2.extras.DictCursor) connection = psycopg2.connect("dbname={} user={} password={} host={} port={}".format(DATABASE_NAME, DATABASE_USER, DATABASE_PASS, DATABASE_HOST, DATABASE_PORT), cursor_factory=psycopg2.extras.DictCursor)

View File

@ -34,6 +34,7 @@ import json
import psycopg2.extras import psycopg2.extras
from .solr import solr_connection from .solr import solr_connection
def index_views(): def index_views():
# get total number of distinct facets for items with a minimum of 1 view, # get total number of distinct facets for items with a minimum of 1 view,
# otherwise Solr returns all kinds of weird ids that are actually not in # otherwise Solr returns all kinds of weird ids that are actually not in
@ -42,16 +43,16 @@ def index_views():
# #
# see: https://lucene.apache.org/solr/guide/6_6/the-stats-component.html # see: https://lucene.apache.org/solr/guide/6_6/the-stats-component.html
res = solr.query('statistics', { res = solr.query('statistics', {
'q':'type:2', 'q': 'type:2',
'fq':'isBot:false AND statistics_type:view', 'fq': 'isBot:false AND statistics_type:view',
'facet':True, 'facet': True,
'facet.field':'id', 'facet.field': 'id',
'facet.mincount':1, 'facet.mincount': 1,
'facet.limit':1, 'facet.limit': 1,
'facet.offset':0, 'facet.offset': 0,
'stats':True, 'stats': True,
'stats.field':'id', 'stats.field': 'id',
'stats.calcdistinct':True 'stats.calcdistinct': True
}, rows=0) }, rows=0)
# get total number of distinct facets (countDistinct) # get total number of distinct facets (countDistinct)
@ -71,13 +72,13 @@ def index_views():
print('Indexing item views (page {} of {})'.format(results_current_page, results_num_pages)) print('Indexing item views (page {} of {})'.format(results_current_page, results_num_pages))
res = solr.query('statistics', { res = solr.query('statistics', {
'q':'type:2', 'q': 'type:2',
'fq':'isBot:false AND statistics_type:view', 'fq': 'isBot:false AND statistics_type:view',
'facet':True, 'facet': True,
'facet.field':'id', 'facet.field': 'id',
'facet.mincount':1, 'facet.mincount': 1,
'facet.limit':results_per_page, 'facet.limit': results_per_page,
'facet.offset':results_current_page * results_per_page 'facet.offset': results_current_page * results_per_page
}, rows=0) }, rows=0)
# SolrClient's get_facets() returns a dict of dicts # SolrClient's get_facets() returns a dict of dicts
@ -98,19 +99,20 @@ def index_views():
cursor.close() cursor.close()
def index_downloads(): def index_downloads():
# get the total number of distinct facets for items with at least 1 download # get the total number of distinct facets for items with at least 1 download
res = solr.query('statistics', { res = solr.query('statistics', {
'q':'type:0', 'q': 'type:0',
'fq':'isBot:false AND statistics_type:view AND bundleName:ORIGINAL', 'fq': 'isBot:false AND statistics_type:view AND bundleName:ORIGINAL',
'facet':True, 'facet': True,
'facet.field':'owningItem', 'facet.field': 'owningItem',
'facet.mincount':1, 'facet.mincount': 1,
'facet.limit':1, 'facet.limit': 1,
'facet.offset':0, 'facet.offset': 0,
'stats':True, 'stats': True,
'stats.field':'owningItem', 'stats.field': 'owningItem',
'stats.calcdistinct':True 'stats.calcdistinct': True
}, rows=0) }, rows=0)
# get total number of distinct facets (countDistinct) # get total number of distinct facets (countDistinct)
@ -130,13 +132,13 @@ def index_downloads():
print('Indexing item downloads (page {} of {})'.format(results_current_page, results_num_pages)) print('Indexing item downloads (page {} of {})'.format(results_current_page, results_num_pages))
res = solr.query('statistics', { res = solr.query('statistics', {
'q':'type:0', 'q': 'type:0',
'fq':'isBot:false AND statistics_type:view AND bundleName:ORIGINAL', 'fq': 'isBot:false AND statistics_type:view AND bundleName:ORIGINAL',
'facet':True, 'facet': True,
'facet.field':'owningItem', 'facet.field': 'owningItem',
'facet.mincount':1, 'facet.mincount': 1,
'facet.limit':results_per_page, 'facet.limit': results_per_page,
'facet.offset':results_current_page * results_per_page 'facet.offset': results_current_page * results_per_page
}, rows=0) }, rows=0)
# SolrClient's get_facets() returns a dict of dicts # SolrClient's get_facets() returns a dict of dicts
@ -157,6 +159,7 @@ def index_downloads():
cursor.close() cursor.close()
db = database_connection() db = database_connection()
solr = solr_connection() solr = solr_connection()

View File

@ -1,6 +1,7 @@
from .config import SOLR_SERVER from .config import SOLR_SERVER
from SolrClient import SolrClient from SolrClient import SolrClient
def solr_connection(): def solr_connection():
connection = SolrClient(SOLR_SERVER) connection = SolrClient(SOLR_SERVER)