Compare commits

...

4 Commits

Author SHA1 Message Date
Alan Orth 787eec20ea
CHANGELOG.md: Add note about imports
continuous-integration/drone/push Build is passing Details
2020-12-18 22:52:14 +02:00
Alan Orth 9e6fcf279b
dspace_statistics_api/items.py: Format with black 2020-12-18 22:45:39 +02:00
Alan Orth 4dbf734a4b
Move all imports to top of file
A few months ago I had an issue setting up mocking because I was
trying to be clever importing these libraries only when I needed
them rather than at the global scope. Someone pointed out to me
that if the imports are at the top of the file Falcon will load
them once when the WSGI server starts, whereas if they are in the
on_get() or on_post() they will load for every request! Also, it
seems that PEP8 recommends keeping imports at the top of the file
anyways, so I will just do that.

Imports sorted with isort.

See: https://www.python.org/dev/peps/pep-0008/#imports
2020-12-18 22:42:06 +02:00
Alan Orth a0d0a47150
items.py: Add fl paramter to Solr queries
I forgot to add the fl parameter here as well.
2020-12-18 16:12:34 +02:00
4 changed files with 13 additions and 11 deletions

View File

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add ORDER BY to /items resource to make sure results are returned
deterministically
- Use `fl` parameter in indexer to return only the id field
- Minor refactoring of imports for PEP8 style
## [1.3.2] - 2020-11-18
### Fixed

View File

@ -1,4 +1,5 @@
import falcon
import psycopg2.extras
from .database import DatabaseManager
from .items import get_downloads, get_views
@ -120,8 +121,6 @@ class ItemResource:
def on_get(self, req, resp, item_id):
"""Handles GET requests"""
import psycopg2.extras
# Adapt Pythons uuid.UUID type to PostgreSQLs uuid
# See: https://www.psycopg.org/docs/extras.html
psycopg2.extras.register_uuid()

View File

@ -1,6 +1,7 @@
import requests
from .config import SOLR_SERVER
from .util import get_statistics_shards
def get_views(solr_date_string: str, items: list):
@ -11,7 +12,6 @@ def get_views(solr_date_string: str, items: list):
:parameter items (list): a list of item IDs
:returns: A dict of item IDs and views
"""
from .util import get_statistics_shards
shards = get_statistics_shards()
# Join the UUIDs with "OR" and escape the hyphens for Solr
@ -20,6 +20,7 @@ def get_views(solr_date_string: str, items: list):
solr_query_params = {
"q": f"id:({solr_items_string})",
"fq": f"type:2 AND isBot:false AND statistics_type:view AND time:{solr_date_string}",
"fl": "id",
"facet": "true",
"facet.field": "id",
"facet.mincount": 1,
@ -61,7 +62,6 @@ def get_downloads(solr_date_string: str, items: list):
:parameter items (list): a list of item IDs
:returns: A dict of item IDs and downloads
"""
from .util import get_statistics_shards
shards = get_statistics_shards()
# Join the UUIDs with "OR" and escape the hyphens for Solr
@ -70,6 +70,7 @@ def get_downloads(solr_date_string: str, items: list):
solr_query_params = {
"q": f"owningItem:({solr_items_string})",
"fq": f"type:0 AND isBot:false AND statistics_type:view AND bundleName:ORIGINAL AND time:{solr_date_string}",
"fl": "owningItem",
"facet": "true",
"facet.field": "owningItem",
"facet.mincount": 1,
@ -102,4 +103,5 @@ def get_downloads(solr_date_string: str, items: list):
return data
# vim: set sw=4 ts=4 expandtab:

View File

@ -1,4 +1,11 @@
import datetime
import json
import re
import falcon
import requests
from .config import SOLR_SERVER
def get_statistics_shards():
@ -8,11 +15,6 @@ def get_statistics_shards():
Returns:
str:A list of Solr statistics shards separated by commas.
"""
import re
import requests
from .config import SOLR_SERVER
# Initialize an empty list for statistics core years
statistics_core_years = []
@ -58,7 +60,6 @@ def get_statistics_shards():
def is_valid_date(date):
import datetime
try:
# Solr date format is: 2020-01-01T00:00:00Z
@ -78,7 +79,6 @@ def validate_items_post_parameters(req, resp, resource, params):
Meant to be used as a `before` hook.
"""
import json
# Only attempt to read the POSTed request if its length is not 0 (or
# rather, in the Python sense, if length is not a False-y value).