Compare commits
190 Commits
Author | SHA1 | Date |
---|---|---|
|
1912363899 | |
|
cd3c024a77 | |
|
e96c79bf2c | |
|
d6330c7bd4 | |
|
8c7a5c4047 | |
|
a31c592fab | |
|
c7b179f1b5 | |
|
77c166c024 | |
|
7680b0f440 | |
|
e70a7a9675 | |
|
24f90df13e | |
|
780f2c1723 | |
|
53b58d4116 | |
|
19a6d2cea6 | |
|
6c2bcda16f | |
|
e4d9545b02 | |
|
1f507d3074 | |
|
82771d7b0c | |
|
5ff3323f88 | |
|
c7a871c2f1 | |
|
b948283d40 | |
|
124a05dcaf | |
|
a2daf96fec | |
|
8634d53fa6 | |
|
e2bfcef573 | |
|
d64c4b8cbc | |
|
3d91366412 | |
|
c3a4e2260b | |
|
10519997ac | |
|
4d7e9e9401 | |
|
fe9f98bcc0 | |
|
70f0d66c6e | |
|
913596c61d | |
|
7cd762a5a2 | |
|
3811be18ef | |
|
a52818271c | |
|
b643f60dd7 | |
|
7cec9a9545 | |
|
a9302506b6 | |
|
b980602a03 | |
|
a4b4843036 | |
|
7e334f6de8 | |
|
770f676fb5 | |
|
6d5e3c350d | |
|
531136183b | |
|
1a3d0350a5 | |
|
25c4f05f16 | |
|
9fba8d1b81 | |
|
568ced0f20 | |
|
9cd93c9034 | |
|
83a2625987 | |
|
f591ed7162 | |
|
bb0f267941 | |
|
0720605b6a | |
|
bcb97d025c | |
|
0ff8490275 | |
|
0a8ac60ade | |
|
37527c21be | |
|
eb660f8085 | |
|
e7d780f511 | |
|
c3b9a541b7 | |
|
1a1a14a25f | |
|
c09fc789e8 | |
|
134a4f1595 | |
|
12ebd1aed5 | |
|
e5f3201b65 | |
|
c1ce4fe233 | |
|
b2eb1878a5 | |
|
a0213c1c97 | |
|
cd03ca2b36 | |
|
c48e6a79c7 | |
|
a2e1695ecc | |
|
b683bf211c | |
|
3ab48743d6 | |
|
88173eaae9 | |
|
f557d33f36 | |
|
ffc4ff4a5c | |
|
7551b34632 | |
|
5e71ec10eb | |
|
f80d360cf9 | |
|
e70b59ecfe | |
|
4d0828b6c0 | |
|
dabc4c0259 | |
|
4fd8af07c3 | |
|
4c5326a176 | |
|
3b1ccafab4 | |
|
58b5ae82d3 | |
|
562aaeef7d | |
|
5cdba6acb1 | |
|
dd0937179c | |
|
f0c6c004db | |
|
6843f0a8ac | |
|
f5fcfcc05a | |
|
e8ac74b6d1 | |
|
14fc14daee | |
|
871aae537a | |
|
2fada6c6ff | |
|
ef0991e352 | |
|
4502d6053c | |
|
a524068cf6 | |
|
964d5dff06 | |
|
a9252d1771 | |
|
a63687d516 | |
|
73dc3a292e | |
|
1e742bad41 | |
|
164008981e | |
|
dd1769b954 | |
|
b009820fb4 | |
|
9830295978 | |
|
c93a4d7455 | |
|
2f8e4f8a0a | |
|
0650c5985e | |
|
d814f1c4f0 | |
|
00f30591c4 | |
|
acfe87b91a | |
|
bc6d84dda2 | |
|
889fb2f74a | |
|
c42cd7a818 | |
|
f8bba59d66 | |
|
b8cb752a29 | |
|
09496aa2b5 | |
|
ff5dc7506d | |
|
80a11ead97 | |
|
a282c95933 | |
|
fd7cc36306 | |
|
a20ff09570 | |
|
fdc0e73088 | |
|
b15afc9f39 | |
|
2bc18ef719 | |
|
49751b53f0 | |
|
d1c177e146 | |
|
33dc210452 | |
|
282d5f644a | |
|
05e0e8bdca | |
|
2567bb8604 | |
|
4af3c656a3 | |
|
4f8cd1097b | |
|
a02211fd60 | |
|
fc814593c7 | |
|
7de1084f60 | |
|
6b78e82fe9 | |
|
4004515967 | |
|
d1229c2387 | |
|
be83514de1 | |
|
70b2ba83ba | |
|
893039bc6a | |
|
a4628dde4e | |
|
68418ea053 | |
|
6bbee7919e | |
|
8f0061ce29 | |
|
4b1398c67f | |
|
a9d2a6d9be | |
|
a35ecf2394 | |
|
3e271c7852 | |
|
d7ba14c590 | |
|
ab82e90773 | |
|
8a1244d2d0 | |
|
04f0756c7f | |
|
830e4415f5 | |
|
47b4eb3df7 | |
|
3339bf8d9c | |
|
fba6f1ead1 | |
|
20c8ba0cf8 | |
|
b486f51dd7 | |
|
787eec20ea | |
|
9e6fcf279b | |
|
4dbf734a4b | |
|
a0d0a47150 | |
|
01e9756cf2 | |
|
b2b4eb2939 | |
|
4bbbaa4af3 | |
|
7e4d5f4b13 | |
|
428172854d | |
|
2707cb37d5 | |
|
2407aeec70 | |
|
f3a0e3a671 | |
|
4590fc8708 | |
|
8b924cf450 | |
|
ea24c73a6a | |
|
cd98d33615 | |
|
9d112266ca | |
|
2b067050ff | |
|
dc683f2d1c | |
|
f60f529bd7 | |
|
7db8458201 | |
|
707f878b94 | |
|
930250352a | |
|
e27f30ba4d | |
|
28d1917038 | |
|
fc6a9c2ad1 |
21
.build.yml
21
.build.yml
|
@ -1,21 +0,0 @@
|
|||
image: archlinux
|
||||
packages:
|
||||
- python-poetry
|
||||
- postgresql
|
||||
sources:
|
||||
- https://git.sr.ht/~alanorth/dspace-statistics-api
|
||||
tasks:
|
||||
- setup: |
|
||||
id
|
||||
psql --version
|
||||
sudo su - postgres -c "initdb --locale en_US.UTF-8 -E UTF8 -D '/var/lib/postgres/data'"
|
||||
sudo systemctl start postgresql
|
||||
createuser -U postgres dspacestatistics
|
||||
psql -U postgres -c "ALTER USER dspacestatistics WITH PASSWORD 'dspacestatistics'"
|
||||
createdb -U postgres -O dspacestatistics --encoding=UNICODE dspacestatistics
|
||||
cd dspace-statistics-api
|
||||
psql -U postgres -d dspacestatistics < tests/dspacestatistics.sql
|
||||
poetry install --no-root
|
||||
- test: |
|
||||
cd dspace-statistics-api
|
||||
poetry run pytest
|
|
@ -0,0 +1,117 @@
|
|||
kind: pipeline
|
||||
type: docker
|
||||
name: python310
|
||||
|
||||
steps:
|
||||
- name: setup
|
||||
image: postgres:15-alpine
|
||||
environment:
|
||||
PGPASSWORD: postgres
|
||||
commands:
|
||||
- id
|
||||
- psql --version
|
||||
- sleep 5
|
||||
- pg_isready -h database -U postgres -d dspacestatistics
|
||||
- createuser -h database -U postgres dspacestatistics
|
||||
- psql -h database -U postgres -c "ALTER USER dspacestatistics WITH PASSWORD 'dspacestatistics'"
|
||||
- psql -h database -U postgres -d dspacestatistics < tests/dspacestatistics.sql
|
||||
|
||||
- name: test
|
||||
image: python:3.10-slim
|
||||
environment:
|
||||
PGPASSWORD: dspacestatistics
|
||||
DATABASE_HOST: database
|
||||
commands:
|
||||
- id
|
||||
- python -V
|
||||
- apt update && apt install -y gcc git libpq-dev
|
||||
- pip install -r requirements-dev.txt
|
||||
- pytest
|
||||
|
||||
services:
|
||||
- name: database
|
||||
image: postgres:15-alpine
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: dspacestatistics
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: python39
|
||||
|
||||
steps:
|
||||
- name: setup
|
||||
image: postgres:15-alpine
|
||||
environment:
|
||||
PGPASSWORD: postgres
|
||||
commands:
|
||||
- id
|
||||
- psql --version
|
||||
- sleep 5
|
||||
- pg_isready -h database -U postgres -d dspacestatistics
|
||||
- createuser -h database -U postgres dspacestatistics
|
||||
- psql -h database -U postgres -c "ALTER USER dspacestatistics WITH PASSWORD 'dspacestatistics'"
|
||||
- psql -h database -U postgres -d dspacestatistics < tests/dspacestatistics.sql
|
||||
|
||||
- name: test
|
||||
image: python:3.9-slim
|
||||
environment:
|
||||
PGPASSWORD: dspacestatistics
|
||||
DATABASE_HOST: database
|
||||
commands:
|
||||
- id
|
||||
- python -V
|
||||
- apt update && apt install -y gcc git libpq-dev
|
||||
- pip install -r requirements-dev.txt
|
||||
- pytest
|
||||
|
||||
services:
|
||||
- name: database
|
||||
image: postgres:15-alpine
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: dspacestatistics
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: python38
|
||||
|
||||
steps:
|
||||
- name: database
|
||||
image: postgres:15-alpine
|
||||
detach: true
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_PASSWORD: postgres
|
||||
POSTGRES_DB: dspacestatistics
|
||||
|
||||
- name: setup
|
||||
image: postgres:15-alpine
|
||||
environment:
|
||||
PGPASSWORD: postgres
|
||||
commands:
|
||||
- id
|
||||
- psql --version
|
||||
- sleep 5
|
||||
- pg_isready -h database -U postgres -d dspacestatistics
|
||||
- createuser -h database -U postgres dspacestatistics
|
||||
- psql -h database -U postgres -c "ALTER USER dspacestatistics WITH PASSWORD 'dspacestatistics'"
|
||||
- psql -h database -U postgres -d dspacestatistics < tests/dspacestatistics.sql
|
||||
|
||||
- name: test
|
||||
image: python:3.8-slim
|
||||
environment:
|
||||
PGPASSWORD: dspacestatistics
|
||||
DATABASE_HOST: database
|
||||
commands:
|
||||
- id
|
||||
- python -V
|
||||
- apt update && apt install -y gcc git libpq-dev
|
||||
- pip install -r requirements-dev.txt
|
||||
- pytest
|
||||
|
||||
# vim: ts=2 sw=2 et
|
|
@ -0,0 +1,58 @@
|
|||
# This workflow will install Python dependencies, run tests and lint with a single version of Python
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
|
||||
|
||||
name: Build and Test
|
||||
|
||||
on: ['push', 'pull_request']
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-22.04
|
||||
|
||||
services:
|
||||
database:
|
||||
image: postgres:15-alpine
|
||||
env:
|
||||
# password for postgres user in the Docker container
|
||||
POSTGRES_PASSWORD: postgres
|
||||
# default database to create
|
||||
POSTGRES_DB: dspacestatistics
|
||||
options: >-
|
||||
--health-cmd pg_isready
|
||||
--health-interval 10s
|
||||
--health-timeout 5s
|
||||
--health-retries 5
|
||||
ports:
|
||||
- 5432:5432
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install poetry
|
||||
run: pipx install poetry
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
cache: 'poetry'
|
||||
- run: poetry install
|
||||
- name: Lint with flake8
|
||||
run: |
|
||||
# stop the build if there are Python syntax errors or undefined names
|
||||
poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
|
||||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
|
||||
poetry run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
||||
- name: Set up PostgreSQL
|
||||
run: |
|
||||
pg_isready -U postgres -d dspacestatistics
|
||||
createuser -U postgres dspacestatistics
|
||||
psql -U postgres -c "ALTER USER dspacestatistics WITH PASSWORD 'dspacestatistics'"
|
||||
psql -U postgres -d dspacestatistics < tests/dspacestatistics.sql
|
||||
env:
|
||||
PGHOST: localhost
|
||||
PGPASSWORD: postgres
|
||||
- name: Test with pytest
|
||||
run: |
|
||||
poetry run pytest
|
||||
env:
|
||||
PGHOST: localhost
|
||||
PGPASSWORD: dspacestatistics
|
|
@ -1,2 +1,3 @@
|
|||
__pycache__
|
||||
venv
|
||||
*.egg-info
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
flake8:
|
||||
enabled: true
|
||||
config_file: .flake8
|
||||
fail_on_violations: true
|
25
.travis.yml
25
.travis.yml
|
@ -1,25 +0,0 @@
|
|||
dist: bionic
|
||||
language: python
|
||||
python:
|
||||
- "3.6"
|
||||
- "3.7"
|
||||
- "3.8"
|
||||
- "3.9"
|
||||
- "nightly"
|
||||
jobs:
|
||||
allow_failures:
|
||||
- python: "nightly"
|
||||
addons:
|
||||
postgresql: "10"
|
||||
before_script:
|
||||
- psql --version
|
||||
- createuser -U postgres dspacestatistics
|
||||
- psql -U postgres -c "ALTER USER dspacestatistics WITH PASSWORD 'dspacestatistics'"
|
||||
- createdb -U postgres -O dspacestatistics --encoding=UNICODE dspacestatistics
|
||||
- psql -U postgres -d dspacestatistics < tests/dspacestatistics.sql
|
||||
install:
|
||||
- "pip install -r requirements.txt"
|
||||
- "pip install -r requirements-dev.txt"
|
||||
script: pytest
|
||||
|
||||
# vim: ts=2 sw=2 et
|
51
CHANGELOG.md
51
CHANGELOG.md
|
@ -4,6 +4,53 @@ 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/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## Unreleased
|
||||
### Changed
|
||||
- Update recommended Python version to 3.7+
|
||||
- Use PostgreSQL 12 in CI
|
||||
|
||||
### Updated
|
||||
- Falcon 3.1.1
|
||||
|
||||
## 1.4.3 - 2022-03-26
|
||||
### Updated
|
||||
- Update dependencies with `poetry update`
|
||||
- Falcon 3.1.0, a minor change for us, but good to be using a current upstream
|
||||
version
|
||||
|
||||
## 1.4.2 - 2021-04-14
|
||||
### Updated
|
||||
- Update dependencies with `poetry update`
|
||||
- Falcon 3.0.0, a minor change for us, but good to be using a current upstream
|
||||
version
|
||||
|
||||
### Fixed
|
||||
- Bug in several of the "valid page" tests
|
||||
|
||||
### Added
|
||||
- GitHub Actions workflow to build and test the API
|
||||
|
||||
## [1.4.1] - 2021-01-14
|
||||
### Changed
|
||||
- Limit Solr query to UUIDs to avoid errors with unmigrated legacy stats (https://github.com/ilri/dspace-statistics-api/issues/12)
|
||||
|
||||
### Updated
|
||||
- Dev dependencies
|
||||
|
||||
## [1.4.0] - 2020-12-27
|
||||
### Added
|
||||
- indexer.py now indexes views and downloads for communities and collections
|
||||
- API endpoints for /communities, /community/id, /collections, and /collections/id
|
||||
- Swagger UI interface on /swagger
|
||||
- /status page which lists the API version
|
||||
|
||||
### Changed
|
||||
- Add ORDER BY to /items resource to make sure results are returned
|
||||
deterministically
|
||||
- Use `fl` parameter in indexer to return only the field we are faceting by
|
||||
- Minor refactoring of imports for PEP8 style
|
||||
- More correct calculation of `totalPages` parameter in REST API response
|
||||
|
||||
## [1.3.2] - 2020-11-18
|
||||
### Fixed
|
||||
- Minor issue with limit parameter (> 0)
|
||||
|
@ -58,7 +105,7 @@ and gunicorn 20.0.4
|
|||
- Minor syntax issues highlighted by flake8
|
||||
|
||||
## [1.1.0] - 2019-05-05
|
||||
## Updated
|
||||
### Updated
|
||||
- Falcon 2.0.0 (@alanorth)
|
||||
|
||||
## [1.0.0] - 2019-04-15
|
||||
|
@ -76,7 +123,7 @@ and gunicorn 20.0.4
|
|||
## [0.9.0] - 2019-01-22
|
||||
### Updated
|
||||
- pytest version 4.0.0
|
||||
- Fix indexing of sharded statistics cores ([#10))
|
||||
- Fix indexing of sharded statistics cores (#10)
|
||||
- Handle case of missing views/downloads gracefully
|
||||
|
||||
## [0.8.1] - 2018-11-14
|
||||
|
|
33
README.md
33
README.md
|
@ -1,10 +1,18 @@
|
|||
# DSpace Statistics API [![Build Status](https://travis-ci.org/ilri/dspace-statistics-api.svg?branch=master)](https://travis-ci.org/ilri/dspace-statistics-api) [![builds.sr.ht status](https://builds.sr.ht/~alanorth/dspace-statistics-api.svg)](https://builds.sr.ht/~alanorth/dspace-statistics-api?)
|
||||
DSpace stores item view and download events in a Solr "statistics" core. This information is available for use in the various DSpace user interfaces, but is not exposed externally via any APIs. The DSpace 4/5/6 [REST API](https://wiki.lyrasis.org/display/DSDOC5x/REST+API), for example, only exposes information about communities, collections, item metadata, and bitstreams.
|
||||
<h1 align="center">DSpace Statistics API</h1>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://ci.mjanja.ch/alanorth/dspace-statistics-api"><img alt="Build Status" src="https://ci.mjanja.ch/api/badges/alanorth/dspace-statistics-api/status.svg?ref=refs/heads/v6_x"></a>
|
||||
<a href="https://github.com/ilri/dspace-statistics-api/actions"><img alt="Build and Test" src="https://github.com/ilri/dspace-statistics-api/actions/workflows/python-app.yml/badge.svg"></a>
|
||||
<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a>
|
||||
</p>
|
||||
|
||||
DSpace stores item view and download events in a Solr "statistics" core. This information is available for use in the various DSpace user interfaces, but is not exposed externally via any APIs. The DSpace 4/5/6 [REST API](https://wiki.lyrasis.org/display/DSDOC5x/REST+API), for example, only exposes _metadata_ about communities, collections, items, and bitstreams.
|
||||
|
||||
- If your DSpace is version 4 or 5, use [dspace-statistics-api v1.1.1](https://github.com/ilri/dspace-statistics-api/releases/tag/v1.1.1)
|
||||
- If your DSpace is version 6+, use [dspace-statistics-api v1.2.0 or greater](https://github.com/ilri/dspace-statistics-api/releases/tag/v1.2.0)
|
||||
- Please make sure your statistics have been migrated from integers to UUIDs with the [solr-upgrade-statistics-6x](https://wiki.lyrasis.org/display/DSDOC6x/SOLR+Statistics+Maintenance) command
|
||||
|
||||
This project contains an indexer and a [Falcon-based](https://falcon.readthedocs.io/) web application to make the statistics available via a simple REST API. You can read more about the Solr queries used to gather the item view and download statistics on the [DSpace wiki](https://wiki.lyrasis.org/display/DSPACE/Solr).
|
||||
This project contains an indexer and a [Falcon-based](https://falcon.readthedocs.io/) web application to make the item, community, and collection statistics available via a simple REST API. You can read more about the Solr queries used to gather the item view and download statistics on the [DSpace wiki](https://wiki.lyrasis.org/display/DSPACE/Solr).
|
||||
|
||||
If you use the DSpace Statistics API please cite:
|
||||
|
||||
|
@ -12,7 +20,7 @@ If you use the DSpace Statistics API please cite:
|
|||
|
||||
## Requirements
|
||||
|
||||
- Python 3.6+
|
||||
- Python 3.8+
|
||||
- PostgreSQL version 9.5+ (due to [`UPSERT` support](https://wiki.postgresql.org/wiki/UPSERT))
|
||||
- DSpace with [Solr usage statistics enabled](https://wiki.lyrasis.org/display/DSDOC5x/SOLR+Statistics) (tested with 5.8+ and 6.3)
|
||||
|
||||
|
@ -81,14 +89,20 @@ The API exposes the following endpoints:
|
|||
|
||||
- GET `/` — return a basic API documentation page.
|
||||
- GET `/items` — return views and downloads for all items that Solr knows about¹. Accepts `limit` and `page` query parameters for pagination of results (`limit` must be an integer between 1 and 100, and `page` must be an integer greater than or equal to 0).
|
||||
- POST `/items` — return views and downloads for an arbitrary list of items. Accepts `limit`, `page`, `dateFrom`, and `dateTo` parameters².
|
||||
- POST `/items` — return views and downloads for an arbitrary list of items with an optional date range. Accepts `limit`, `page`, `dateFrom`, and `dateTo` parameters².
|
||||
- GET `/item/id` — return views and downloads for a single item (`id` must be a UUID). Returns HTTP 404 if an item id is not found.
|
||||
- GET `/communities` — return views and downloads for all communities that Solr knows about¹. Accepts `limit` and `page` query parameters for pagination of results (`limit` must be an integer between 1 and 100, and `page` must be an integer greater than or equal to 0).
|
||||
- POST `/communities` — return views and downloads for an arbitrary list of communities with an optional date range. Accepts `limit`, `page`, `dateFrom`, and `dateTo` parameters².
|
||||
- GET `/community/id` — return views and downloads for a single community (`id` must be a UUID). Returns HTTP 404 if a community id is not found.
|
||||
- GET `/collections` — return views and downloads for all collections that Solr knows about¹. Accepts `limit` and `page` query parameters for pagination of results (`limit` must be an integer between 1 and 100, and `page` must be an integer greater than or equal to 0).
|
||||
- POST `/collections` — return views and downloads for an arbitrary list of collections with an optional date range. Accepts `limit`, `page`, `dateFrom`, and `dateTo` parameters².
|
||||
- GET `/collection/id` — return views and downloads for a single collection (`id` must be a UUID). Returns HTTP 404 if an collection id is not found.
|
||||
|
||||
The item id is the *internal* UUID for an item. You can get these from the standard DSpace REST API.
|
||||
The id is the *internal* UUID for an item, community, or collection. You can get these from the standard DSpace REST API.
|
||||
|
||||
¹ We are querying the Solr statistics core, which technically only knows about items that have either views or downloads. If an item is not present here you can assume it has zero views and zero downloads, but not necessarily that it does not exist in the repository.
|
||||
¹ We are querying the Solr statistics core, which technically only knows about items, communities, or collections that have either views or downloads. If an item, community, or collection is not present here you can assume it has zero views and zero downloads, but not necessarily that it does not exist in the repository.
|
||||
|
||||
² POST requests to `/items` should be in JSON format with the following parameters:
|
||||
² POST requests to `/items`, `/communities`, and `/collections` should be in JSON format with the following parameters (substitute the "items" list for communities or collections accordingly):
|
||||
|
||||
```
|
||||
{
|
||||
|
@ -109,11 +123,10 @@ The item id is the *internal* UUID for an item. You can get these from the stand
|
|||
|
||||
- Better logging
|
||||
- Version API (or at least include a /version endpoint?)
|
||||
- Probably use /status with a version in the response
|
||||
- Use JSON in PostgreSQL
|
||||
- Add top items endpoint, perhaps `/top/items` or `/items/top`?
|
||||
- Actually we could add `/items?limit=10&sort=views`
|
||||
- Make community and collection stats available
|
||||
- Check IDs in database to see if they are deleted...
|
||||
|
||||
## License
|
||||
This work is licensed under the [GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html).
|
||||
|
|
|
@ -1,19 +1,73 @@
|
|||
import falcon
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
import json
|
||||
import math
|
||||
|
||||
import falcon
|
||||
import psycopg2.extras
|
||||
from falcon_swagger_ui import register_swaggerui_app
|
||||
|
||||
from .config import DSPACE_STATISTICS_API_URL, VERSION
|
||||
from .database import DatabaseManager
|
||||
from .items import get_downloads, get_views
|
||||
from .util import validate_items_post_parameters
|
||||
from .stats import get_downloads, get_views
|
||||
from .util import set_statistics_scope, validate_post_parameters
|
||||
|
||||
|
||||
class RootResource:
|
||||
def on_get(self, req, resp):
|
||||
resp.status = falcon.HTTP_200
|
||||
resp.content_type = "text/html"
|
||||
with open("dspace_statistics_api/docs/index.html", "r") as f:
|
||||
resp.body = f.read()
|
||||
docs_html = (
|
||||
"<!DOCTYPE html>"
|
||||
'<html lang="en-US">'
|
||||
" <head>"
|
||||
' <meta charset="UTF-8">'
|
||||
" <title>DSpace Statistics API</title>"
|
||||
" </head>"
|
||||
" <body>"
|
||||
f" <h1>DSpace Statistics API {VERSION}</h1>"
|
||||
f" <p>This site is running the <a href=\"https://github.com/ilri/dspace-statistics-api\" title=\"DSpace Statistics API project\">DSpace Statistics API</a>. For more information see the project's README.md or the interactive <a href=\"{DSPACE_STATISTICS_API_URL + '/swagger'}\">Swagger UI</a> built into this API.</p>"
|
||||
" </body>"
|
||||
"</html"
|
||||
)
|
||||
|
||||
resp.text = docs_html
|
||||
|
||||
|
||||
class AllItemsResource:
|
||||
class StatusResource:
|
||||
def on_get(self, req, resp):
|
||||
message = {"version": VERSION}
|
||||
|
||||
resp.status = falcon.HTTP_200
|
||||
resp.media = message
|
||||
|
||||
|
||||
class OpenAPIJSONResource:
|
||||
def on_get(self, req, resp):
|
||||
resp.status = falcon.HTTP_200
|
||||
resp.content_type = "text/html"
|
||||
with open("dspace_statistics_api/docs/openapi.json", "r") as f:
|
||||
# Load the openapi.json schema
|
||||
data = json.load(f)
|
||||
|
||||
# Swagger assumes your API is at the root of the current host unless
|
||||
# you configure a "servers" block in the schema. The problem is that
|
||||
# I want this to work in both development and production, so we need
|
||||
# to make this configurable.
|
||||
#
|
||||
# If the DSPACE_STATISTICS_API_URL is configured then we will add a
|
||||
# server entry to the openapi.json schema before sending it.
|
||||
if DSPACE_STATISTICS_API_URL != "":
|
||||
data["servers"] = [{"url": DSPACE_STATISTICS_API_URL}]
|
||||
|
||||
# Set the version in the schema so Swagger UI can display it
|
||||
data["info"]["version"] = VERSION
|
||||
|
||||
resp.text = json.dumps(data)
|
||||
|
||||
|
||||
class AllStatisticsResource:
|
||||
@falcon.before(set_statistics_scope)
|
||||
def on_get(self, req, resp):
|
||||
"""Handles GET requests"""
|
||||
# Return HTTPBadRequest if id parameter is not present and valid
|
||||
|
@ -25,26 +79,26 @@ class AllItemsResource:
|
|||
db.set_session(readonly=True)
|
||||
|
||||
with db.cursor() as cursor:
|
||||
# get total number of items so we can estimate the pages
|
||||
cursor.execute("SELECT COUNT(id) FROM items")
|
||||
pages = round(cursor.fetchone()[0] / limit)
|
||||
# get total number of communities/collections/items so we can estimate the pages
|
||||
cursor.execute(f"SELECT COUNT(id) FROM {req.context.statistics_scope}")
|
||||
pages = math.ceil(cursor.fetchone()[0] / limit)
|
||||
|
||||
# get statistics and use limit and offset to page through results
|
||||
cursor.execute(
|
||||
"SELECT id, views, downloads FROM items LIMIT %s OFFSET %s",
|
||||
f"SELECT id, views, downloads FROM {req.context.statistics_scope} ORDER BY id LIMIT %s OFFSET %s",
|
||||
[limit, offset],
|
||||
)
|
||||
|
||||
# create a list to hold dicts of item stats
|
||||
statistics = list()
|
||||
# create a list to hold dicts of stats
|
||||
statistics = []
|
||||
|
||||
# iterate over results and build statistics object
|
||||
for item in cursor:
|
||||
for result in cursor:
|
||||
statistics.append(
|
||||
{
|
||||
"id": str(item["id"]),
|
||||
"views": item["views"],
|
||||
"downloads": item["downloads"],
|
||||
"id": str(result["id"]),
|
||||
"views": result["views"],
|
||||
"downloads": result["downloads"],
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -57,9 +111,15 @@ class AllItemsResource:
|
|||
|
||||
resp.media = message
|
||||
|
||||
@falcon.before(validate_items_post_parameters)
|
||||
@falcon.before(set_statistics_scope)
|
||||
@falcon.before(validate_post_parameters)
|
||||
def on_post(self, req, resp):
|
||||
"""Handles POST requests"""
|
||||
"""Handles POST requests.
|
||||
|
||||
Uses two `before` hooks to set the statistics "scope" and validate the
|
||||
POST parameters. The "scope" is the type of statistics we want, which
|
||||
will be items, communities, or collections, depending on the request.
|
||||
"""
|
||||
|
||||
# Build the Solr date string, ie: [* TO *]
|
||||
if req.context.dateFrom and req.context.dateTo:
|
||||
|
@ -73,10 +133,10 @@ class AllItemsResource:
|
|||
|
||||
# Helper variables to make working with pages/items/results easier and
|
||||
# to make the code easier to understand
|
||||
number_of_items: int = len(req.context.items)
|
||||
pages: int = int(number_of_items / req.context.limit)
|
||||
first_item: int = req.context.page * req.context.limit
|
||||
last_item: int = first_item + req.context.limit
|
||||
number_of_elements: int = len(req.context.elements)
|
||||
pages: int = math.ceil(number_of_elements / req.context.limit)
|
||||
first_element: int = req.context.page * req.context.limit
|
||||
last_element: int = first_element + req.context.limit
|
||||
# Get a subset of the POSTed items based on our limit. Note that Python
|
||||
# list slicing and indexing are both zero based, but the first and last
|
||||
# items in a slice can be confusing. See this ASCII diagram:
|
||||
|
@ -87,20 +147,24 @@ class AllItemsResource:
|
|||
# Slice position: 0 1 2 3 4 5 6
|
||||
# Index position: 0 1 2 3 4 5
|
||||
#
|
||||
# So if we have a list items with 240 items:
|
||||
# So if we have a list of items with 240 items:
|
||||
#
|
||||
# 1st set: items[0:100] would give items at indexes 0 to 99
|
||||
# 2nd set: items[100:200] would give items at indexes 100 to 199
|
||||
# 3rd set: items[200:300] would give items at indexes 200 to 239
|
||||
items_subset: list = req.context.items[first_item:last_item]
|
||||
elements_subset: list = req.context.elements[first_element:last_element]
|
||||
|
||||
views: dict = get_views(solr_date_string, items_subset)
|
||||
downloads: dict = get_downloads(solr_date_string, items_subset)
|
||||
views: dict = get_views(
|
||||
solr_date_string, elements_subset, req.context.views_facet_field
|
||||
)
|
||||
downloads: dict = get_downloads(
|
||||
solr_date_string, elements_subset, req.context.downloads_facet_field
|
||||
)
|
||||
|
||||
# create a list to hold dicts of item stats
|
||||
statistics = list()
|
||||
# create a list to hold dicts of stats
|
||||
statistics = []
|
||||
|
||||
# iterate over views dict to extract views and use the item id as an
|
||||
# iterate over views dict to extract views and use the element id as an
|
||||
# index to the downloads dict to extract downloads.
|
||||
for k, v in views.items():
|
||||
statistics.append({"id": k, "views": v, "downloads": downloads[k]})
|
||||
|
@ -116,12 +180,11 @@ class AllItemsResource:
|
|||
resp.media = message
|
||||
|
||||
|
||||
class ItemResource:
|
||||
def on_get(self, req, resp, item_id):
|
||||
class SingleStatisticsResource:
|
||||
@falcon.before(set_statistics_scope)
|
||||
def on_get(self, req, resp, id_):
|
||||
"""Handles GET requests"""
|
||||
|
||||
import psycopg2.extras
|
||||
|
||||
# Adapt Python’s uuid.UUID type to PostgreSQL’s uuid
|
||||
# See: https://www.psycopg.org/docs/extras.html
|
||||
psycopg2.extras.register_uuid()
|
||||
|
@ -132,18 +195,19 @@ class ItemResource:
|
|||
with db.cursor() as cursor:
|
||||
cursor = db.cursor()
|
||||
cursor.execute(
|
||||
"SELECT views, downloads FROM items WHERE id=%s", [str(item_id)]
|
||||
f"SELECT views, downloads FROM {req.context.database} WHERE id=%s",
|
||||
[str(id_)],
|
||||
)
|
||||
if cursor.rowcount == 0:
|
||||
raise falcon.HTTPNotFound(
|
||||
title="Item not found",
|
||||
description=f'The item with id "{str(item_id)}" was not found.',
|
||||
title=f"{req.context.statistics_scope} not found",
|
||||
description=f'The {req.context.statistics_scope} with id "{str(id_)}" was not found.',
|
||||
)
|
||||
else:
|
||||
results = cursor.fetchone()
|
||||
|
||||
statistics = {
|
||||
"id": str(item_id),
|
||||
"id": str(id_),
|
||||
"views": results["views"],
|
||||
"downloads": results["downloads"],
|
||||
}
|
||||
|
@ -151,9 +215,45 @@ class ItemResource:
|
|||
resp.media = statistics
|
||||
|
||||
|
||||
api = application = falcon.API()
|
||||
api.add_route("/", RootResource())
|
||||
api.add_route("/items", AllItemsResource())
|
||||
api.add_route("/item/{item_id:uuid}", ItemResource())
|
||||
app = application = falcon.App()
|
||||
app.add_route("/", RootResource())
|
||||
app.add_route("/status", StatusResource())
|
||||
|
||||
# Item routes
|
||||
app.add_route("/items", AllStatisticsResource())
|
||||
app.add_route("/item/{id_:uuid}", SingleStatisticsResource())
|
||||
|
||||
# Community routes
|
||||
app.add_route("/communities", AllStatisticsResource())
|
||||
app.add_route("/community/{id_:uuid}", SingleStatisticsResource())
|
||||
|
||||
# Collection routes
|
||||
app.add_route("/collections", AllStatisticsResource())
|
||||
app.add_route("/collection/{id_:uuid}", SingleStatisticsResource())
|
||||
|
||||
# Route to the Swagger UI Openapp schema
|
||||
app.add_route("/docs/openapi.json", OpenAPIJSONResource())
|
||||
|
||||
# Path to host the Swagger UI. Keep in mind that Falcon will add a route for
|
||||
# this automatically when we register Swagger and the path will be relative
|
||||
# to the Falcon app like all other routes, not the absolute root.
|
||||
SWAGGERUI_PATH = "/swagger"
|
||||
|
||||
# The *absolute* path to the OpenJSON schema. This must be absolute because
|
||||
# it will be requested by the client and must resolve absolutely. Note: the
|
||||
# name of this variable is misleading because it is actually the schema URL
|
||||
# but we pass it into the register_swaggerui_app() function as the app_url
|
||||
# parameter.
|
||||
SWAGGERUI_API_URL = f"{DSPACE_STATISTICS_API_URL}/docs/openapi.json"
|
||||
|
||||
register_swaggerui_app(
|
||||
app,
|
||||
SWAGGERUI_PATH,
|
||||
SWAGGERUI_API_URL,
|
||||
config={
|
||||
"supportedSubmitMethods": ["get", "post"],
|
||||
},
|
||||
uri_prefix=DSPACE_STATISTICS_API_URL,
|
||||
)
|
||||
|
||||
# vim: set sw=4 ts=4 expandtab:
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
import os
|
||||
|
||||
# Check if Solr connection information was provided in the environment
|
||||
|
@ -9,4 +11,13 @@ DATABASE_PASS = os.environ.get("DATABASE_PASS", "dspacestatistics")
|
|||
DATABASE_HOST = os.environ.get("DATABASE_HOST", "localhost")
|
||||
DATABASE_PORT = os.environ.get("DATABASE_PORT", "5432")
|
||||
|
||||
# URL to DSpace Statistics API, which will be used as a prefix to API calls in
|
||||
# the Swagger UI. An empty string will allow this to work out of the box in a
|
||||
# local development environment, but for production it should be set to a value
|
||||
# like "/rest/statistics", assuming that the statistics API is deployed next to
|
||||
# the vanilla DSpace REST API.
|
||||
DSPACE_STATISTICS_API_URL = os.environ.get("DSPACE_STATISTICS_API_URL", "")
|
||||
|
||||
VERSION = "1.4.4-dev"
|
||||
|
||||
# vim: set sw=4 ts=4 expandtab:
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
import falcon
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>DSpace Statistics API</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>DSpace Statistics API v1.3.2</h1>
|
||||
<p>This site is running the <a href="https://github.com/ilri/dspace-statistics-api" title="DSpace Statistics API project">DSpace Statistics API</a>. The following endpoints are available:</p>
|
||||
<ul>
|
||||
<li>GET <code>/</code> — return a basic API documentation page.</li>
|
||||
<li>GET <code>/items</code> — return views and downloads for all items that Solr knows about¹. Accepts <code>limit</code> and <code>page</code> query parameters for pagination of results (<code>limit</code> must be an integer between 1 and 100, and <code>page</code> must be an integer greater than or equal to 0).</li>
|
||||
<li>POST <code>/items</code> — return views and downloads for an arbitrary list of items. Accepts <code>limit</code>, <code>page</code>, <code>dateFrom</code>, and <code>dateTo</code> parameters².</li>
|
||||
<li>GET <code>/item/id</code> — return views and downloads for a single item (<code>id</code> must be a UUID). Returns HTTP 404 if an item id is not found.</li>
|
||||
</ul>
|
||||
|
||||
<p>The item id is the <em>internal</em> uuid for an item. You can get these from the standard DSpace REST API.</p>
|
||||
|
||||
<hr/>
|
||||
|
||||
<p>¹ We are querying the Solr statistics core, which technically only knows about items that have either views or downloads. If an item is not present here you can assume it has zero views and zero downloads, but not necessarily that it does not exist in the repository.</p>
|
||||
<p>² POST requests to <code>/items</code> should be in JSON format with the following parameters:
|
||||
<pre><code>{
|
||||
"limit": 100, // optional, integer between 1 and 100, default 100
|
||||
"page": 0, // optional, integer greater than 0, default 0
|
||||
"dateFrom": "2020-01-01T00:00:00Z", // optional, default *
|
||||
"dateTo": "2020-09-09T00:00:00Z", // optional, default *
|
||||
"items": [
|
||||
"f44cf173-2344-4eb2-8f00-ee55df32c76f",
|
||||
"2324aa41-e9de-4a2b-bc36-16241464683e",
|
||||
"8542f9da-9ce1-4614-abf4-f2e3fdb4b305",
|
||||
"0fe573e7-042a-4240-a4d9-753b61233908"
|
||||
]
|
||||
}</code></pre>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,616 @@
|
|||
{
|
||||
"openapi": "3.0.3",
|
||||
"info": {
|
||||
"version": "1.4.4-dev",
|
||||
"title": "DSpace Statistics API",
|
||||
"description": "A [Falcon-based](https://falcon.readthedocs.io/) web application to make DSpace's item, community, and collection statistics available via a simple REST API. This Swagger interface is powered by [falcon-swagger-ui](https://github.com/rdidyk/falcon-swagger-ui).",
|
||||
"license": {
|
||||
"name": "GPLv3.0",
|
||||
"url": "https://www.gnu.org/licenses/gpl-3.0.en.html"
|
||||
}
|
||||
},
|
||||
"paths": {
|
||||
"/item/{item_uuid}": {
|
||||
"get": {
|
||||
"summary": "Statistics for a specific item",
|
||||
"operationId": "getItem",
|
||||
"tags": [
|
||||
"item"
|
||||
],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "item_uuid",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"description": "The UUID of the item to retrieve",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"format": "uuid",
|
||||
"example": "9596aeff-0b90-47d3-9fec-02d578920507"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Expected response to a valid request",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/SingleElementResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"404": {
|
||||
"description": "Item not found"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/items": {
|
||||
"get": {
|
||||
"summary": "Get statistics for all items",
|
||||
"operationId": "getItems",
|
||||
"tags": [
|
||||
"items"
|
||||
],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
"description": "How many items to return at once (optional)",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 1,
|
||||
"maximum": 100,
|
||||
"default": 100,
|
||||
"example": 100
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "page",
|
||||
"in": "query",
|
||||
"description": "Page of results to start on (optional)",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 0,
|
||||
"default": 0,
|
||||
"example": 0
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "A paged array of items",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/SingleElementResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad request"
|
||||
}
|
||||
}
|
||||
},
|
||||
"post": {
|
||||
"summary": "Get statistics for a list of items with an optional date range",
|
||||
"operationId": "postItems",
|
||||
"tags": [
|
||||
"items"
|
||||
],
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 1,
|
||||
"maximum": 100,
|
||||
"default": 100
|
||||
},
|
||||
"page": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 0,
|
||||
"default": 0
|
||||
},
|
||||
"dateFrom": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
},
|
||||
"dateTo": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
},
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
}
|
||||
}
|
||||
},
|
||||
"example": {
|
||||
"limit": 100,
|
||||
"page": 0,
|
||||
"dateFrom": "2020-01-01T00:00:00Z",
|
||||
"dateTo": "2020-12-31T00:00:00Z",
|
||||
"items": [
|
||||
"f44cf173-2344-4eb2-8f00-ee55df32c76f",
|
||||
"2324aa41-e9de-4a2b-bc36-16241464683e",
|
||||
"8542f9da-9ce1-4614-abf4-f2e3fdb4b305",
|
||||
"0fe573e7-042a-4240-a4d9-753b61233908"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Expected response to a valid request",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"currentPage": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"totalPages": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"statistics": {
|
||||
"$ref": "#/components/schemas/ListOfElements"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad request"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/community/{community_uuid}": {
|
||||
"get": {
|
||||
"summary": "Statistics for a specific community",
|
||||
"operationId": "getCommunity",
|
||||
"tags": [
|
||||
"community"
|
||||
],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "community_uuid",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"description": "The UUID of the community to retrieve",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"format": "uuid",
|
||||
"example": "bde7139c-d321-46bb-aef6-ae70799e5edb"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Expected response to a valid request",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/SingleElementResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"404": {
|
||||
"description": "Community not found"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/communities": {
|
||||
"get": {
|
||||
"summary": "Get statistics for all communities",
|
||||
"operationId": "getCommunities",
|
||||
"tags": [
|
||||
"communities"
|
||||
],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
"description": "How many communities to return at once (optional)",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 1,
|
||||
"maximum": 100,
|
||||
"default": 100,
|
||||
"example": 100
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "page",
|
||||
"in": "query",
|
||||
"description": "Zero-based page of results to start on (optional)",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 0,
|
||||
"default": 0,
|
||||
"example": 0
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "A paged array of communities",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/SingleElementResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad request"
|
||||
}
|
||||
}
|
||||
},
|
||||
"post": {
|
||||
"summary": "Get statistics for a list of communities with an optional date range",
|
||||
"operationId": "postCommunities",
|
||||
"tags": [
|
||||
"communities"
|
||||
],
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 1,
|
||||
"maximum": 100,
|
||||
"default": 100
|
||||
},
|
||||
"page": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 0,
|
||||
"default": 0
|
||||
},
|
||||
"dateFrom": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
},
|
||||
"dateTo": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
},
|
||||
"communities": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
}
|
||||
}
|
||||
},
|
||||
"example": {
|
||||
"limit": 100,
|
||||
"page": 0,
|
||||
"dateFrom": "2020-01-01T00:00:00Z",
|
||||
"dateTo": "2020-12-31T00:00:00Z",
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"8a8aeed1-077e-4360-bdf8-a5f3020193b1",
|
||||
"47d0498a-203c-407d-afb8-1d44bf29badc",
|
||||
"d3fe99a9-e27d-4035-9339-084c93228c82"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Expected response to a valid request",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"currentPage": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"totalPages": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"statistics": {
|
||||
"$ref": "#/components/schemas/ListOfElements"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad request"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/collection/{collection_uuid}": {
|
||||
"get": {
|
||||
"summary": "Statistics for a specific collection",
|
||||
"operationId": "getCollection",
|
||||
"tags": [
|
||||
"collection"
|
||||
],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "collection_uuid",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"description": "The UUID of the collection to retrieve",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"format": "uuid",
|
||||
"example": "49dc95d8-bf2f-4e68-b30f-41ea266c37ae"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Expected response to a valid request",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/SingleElementResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"404": {
|
||||
"description": "Collection not found"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/collections": {
|
||||
"get": {
|
||||
"summary": "Get statistics for all collections",
|
||||
"operationId": "getCollections",
|
||||
"tags": [
|
||||
"collections"
|
||||
],
|
||||
"parameters": [
|
||||
{
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
"description": "How many collections to return at once (optional)",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 1,
|
||||
"maximum": 100,
|
||||
"default": 100,
|
||||
"example": 100
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "page",
|
||||
"in": "query",
|
||||
"description": "Zero-based page of results to start on (optional)",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 0,
|
||||
"default": 0,
|
||||
"example": 0
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "A paged array of collections",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/SingleElementResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad request"
|
||||
}
|
||||
}
|
||||
},
|
||||
"post": {
|
||||
"summary": "Get statistics for a list of collections with an optional date range",
|
||||
"operationId": "postCollections",
|
||||
"tags": [
|
||||
"collections"
|
||||
],
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 1,
|
||||
"maximum": 100,
|
||||
"default": 100
|
||||
},
|
||||
"page": {
|
||||
"type": "integer",
|
||||
"format": "int32",
|
||||
"minimum": 0,
|
||||
"default": 0
|
||||
},
|
||||
"dateFrom": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
},
|
||||
"dateTo": {
|
||||
"type": "string",
|
||||
"format": "date"
|
||||
},
|
||||
"collections": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
}
|
||||
}
|
||||
},
|
||||
"example": {
|
||||
"limit": 100,
|
||||
"page": 0,
|
||||
"dateFrom": "2020-01-01T00:00:00Z",
|
||||
"dateTo": "2020-12-31T00:00:00Z",
|
||||
"collections": [
|
||||
"5eeef6cf-b91b-42d0-9549-ea61bc8a758f",
|
||||
"6aac3269-b4a9-4924-a24d-9e6ee2b410d2",
|
||||
"551698dd-cd2b-4327-948e-54b5eb6deda5",
|
||||
"39358713-bbaf-4149-a453-e2b18c09fd5d"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Expected response to a valid request",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"currentPage": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"totalPages": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"statistics": {
|
||||
"$ref": "#/components/schemas/ListOfElements"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad request"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/status": {
|
||||
"get": {
|
||||
"summary": "Get API status",
|
||||
"operationId": "getStatus",
|
||||
"tags": [
|
||||
"status"
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"version": {
|
||||
"type": "string",
|
||||
"example": "1.4.0-dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"405": {
|
||||
"description": "Method Not Allowed"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"SingleElementResponse": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"id",
|
||||
"views",
|
||||
"downloads"
|
||||
],
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string",
|
||||
"format": "uuid"
|
||||
},
|
||||
"views": {
|
||||
"type": "integer",
|
||||
"example": 450
|
||||
},
|
||||
"downloads": {
|
||||
"type": "integer",
|
||||
"example": 1337
|
||||
}
|
||||
}
|
||||
},
|
||||
"ListOfElements": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/SingleElementResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,25 +1,9 @@
|
|||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
#
|
||||
# indexer.py
|
||||
#
|
||||
# Copyright 2018 Alan Orth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
# ---
|
||||
#
|
||||
# Connects to a DSpace Solr statistics core and ingests item views and downloads
|
||||
# into a PostgreSQL database for use by other applications (like an API).
|
||||
# Connects to a DSpace Solr statistics core and ingests views and downloads for
|
||||
# communities, collections, and items into a PostgreSQL database.
|
||||
#
|
||||
# This script is written for Python 3.6+ and requires several modules that you
|
||||
# can install with pip (I recommend using a Python virtual environment):
|
||||
|
@ -28,6 +12,8 @@
|
|||
#
|
||||
# See: https://wiki.duraspace.org/display/DSPACE/Solr
|
||||
|
||||
import math
|
||||
|
||||
import psycopg2.extras
|
||||
import requests
|
||||
|
||||
|
@ -36,7 +22,7 @@ from .database import DatabaseManager
|
|||
from .util import get_statistics_shards
|
||||
|
||||
|
||||
def index_views():
|
||||
def index_views(indexType: str, facetField: str):
|
||||
# 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
|
||||
# the database. Also, stats are expensive, but we need stats.calcdistinct
|
||||
|
@ -45,15 +31,16 @@ def index_views():
|
|||
#
|
||||
# see: https://lucene.apache.org/solr/guide/6_6/the-stats-component.html
|
||||
solr_query_params = {
|
||||
"q": "type:2",
|
||||
"q": f"type:2 AND {facetField}:/.{{36}}/",
|
||||
"fq": "-isBot:true AND statistics_type:view",
|
||||
"fl": facetField,
|
||||
"facet": "true",
|
||||
"facet.field": "id",
|
||||
"facet.field": facetField,
|
||||
"facet.mincount": 1,
|
||||
"facet.limit": 1,
|
||||
"facet.offset": 0,
|
||||
"stats": "true",
|
||||
"stats.field": "id",
|
||||
"stats.field": facetField,
|
||||
"stats.calcdistinct": "true",
|
||||
"shards": shards,
|
||||
"rows": 0,
|
||||
|
@ -66,17 +53,17 @@ def index_views():
|
|||
|
||||
try:
|
||||
# get total number of distinct facets (countDistinct)
|
||||
results_totalNumFacets = res.json()["stats"]["stats_fields"]["id"][
|
||||
results_totalNumFacets = res.json()["stats"]["stats_fields"][facetField][
|
||||
"countDistinct"
|
||||
]
|
||||
except TypeError:
|
||||
print("No item views to index, exiting.")
|
||||
print(f"{indexType}: no views, exiting.")
|
||||
|
||||
exit(0)
|
||||
|
||||
# divide results into "pages" (cast to int to effectively round down)
|
||||
# divide results into "pages" and round up to next integer
|
||||
results_per_page = 100
|
||||
results_num_pages = int(results_totalNumFacets / results_per_page)
|
||||
results_num_pages = math.ceil(results_totalNumFacets / results_per_page)
|
||||
results_current_page = 0
|
||||
|
||||
with DatabaseManager() as db:
|
||||
|
@ -87,14 +74,15 @@ def index_views():
|
|||
while results_current_page <= results_num_pages:
|
||||
# "pages" are zero based, but one based is more human readable
|
||||
print(
|
||||
f"Indexing item views (page {results_current_page + 1} of {results_num_pages + 1})"
|
||||
f"{indexType}: indexing views (page {results_current_page + 1} of {results_num_pages + 1})"
|
||||
)
|
||||
|
||||
solr_query_params = {
|
||||
"q": "type:2",
|
||||
"q": f"type:2 AND {facetField}:/.{{36}}/",
|
||||
"fq": "-isBot:true AND statistics_type:view",
|
||||
"fl": facetField,
|
||||
"facet": "true",
|
||||
"facet.field": "id",
|
||||
"facet.field": facetField,
|
||||
"facet.mincount": 1,
|
||||
"facet.limit": results_per_page,
|
||||
"facet.offset": results_current_page * results_per_page,
|
||||
|
@ -108,12 +96,12 @@ def index_views():
|
|||
|
||||
# Solr returns facets as a dict of dicts (see json.nl parameter)
|
||||
views = res.json()["facet_counts"]["facet_fields"]
|
||||
# iterate over the 'id' dict and get the item ids and views
|
||||
for item_id, item_views in views["id"].items():
|
||||
data.append((item_id, item_views))
|
||||
# iterate over the facetField dict and get the ids and views
|
||||
for id_, views in views[facetField].items():
|
||||
data.append((id_, views))
|
||||
|
||||
# do a batch insert of values from the current "page" of results
|
||||
sql = "INSERT INTO items(id, views) VALUES %s ON CONFLICT(id) DO UPDATE SET views=excluded.views"
|
||||
sql = f"INSERT INTO {indexType}(id, views) VALUES %s ON CONFLICT(id) DO UPDATE SET views=excluded.views"
|
||||
psycopg2.extras.execute_values(cursor, sql, data, template="(%s, %s)")
|
||||
db.commit()
|
||||
|
||||
|
@ -123,18 +111,19 @@ def index_views():
|
|||
results_current_page += 1
|
||||
|
||||
|
||||
def index_downloads():
|
||||
def index_downloads(indexType: str, facetField: str):
|
||||
# get the total number of distinct facets for items with at least 1 download
|
||||
solr_query_params = {
|
||||
"q": "type:0",
|
||||
"q": f"type:0 AND {facetField}:/.{{36}}/",
|
||||
"fq": "-isBot:true AND statistics_type:view AND bundleName:ORIGINAL",
|
||||
"fl": facetField,
|
||||
"facet": "true",
|
||||
"facet.field": "owningItem",
|
||||
"facet.field": facetField,
|
||||
"facet.mincount": 1,
|
||||
"facet.limit": 1,
|
||||
"facet.offset": 0,
|
||||
"stats": "true",
|
||||
"stats.field": "owningItem",
|
||||
"stats.field": facetField,
|
||||
"stats.calcdistinct": "true",
|
||||
"shards": shards,
|
||||
"rows": 0,
|
||||
|
@ -147,17 +136,16 @@ def index_downloads():
|
|||
|
||||
try:
|
||||
# get total number of distinct facets (countDistinct)
|
||||
results_totalNumFacets = res.json()["stats"]["stats_fields"]["owningItem"][
|
||||
results_totalNumFacets = res.json()["stats"]["stats_fields"][facetField][
|
||||
"countDistinct"
|
||||
]
|
||||
except TypeError:
|
||||
print("No item downloads to index, exiting.")
|
||||
print(f"{indexType}: no downloads, exiting.")
|
||||
|
||||
exit(0)
|
||||
|
||||
# divide results into "pages" (cast to int to effectively round down)
|
||||
results_per_page = 100
|
||||
results_num_pages = int(results_totalNumFacets / results_per_page)
|
||||
results_num_pages = math.ceil(results_totalNumFacets / results_per_page)
|
||||
results_current_page = 0
|
||||
|
||||
with DatabaseManager() as db:
|
||||
|
@ -168,14 +156,15 @@ def index_downloads():
|
|||
while results_current_page <= results_num_pages:
|
||||
# "pages" are zero based, but one based is more human readable
|
||||
print(
|
||||
f"Indexing item downloads (page {results_current_page + 1} of {results_num_pages + 1})"
|
||||
f"{indexType}: indexing downloads (page {results_current_page + 1} of {results_num_pages + 1})"
|
||||
)
|
||||
|
||||
solr_query_params = {
|
||||
"q": "type:0",
|
||||
"q": f"type:0 AND {facetField}:/.{{36}}/",
|
||||
"fq": "-isBot:true AND statistics_type:view AND bundleName:ORIGINAL",
|
||||
"fl": facetField,
|
||||
"facet": "true",
|
||||
"facet.field": "owningItem",
|
||||
"facet.field": facetField,
|
||||
"facet.mincount": 1,
|
||||
"facet.limit": results_per_page,
|
||||
"facet.offset": results_current_page * results_per_page,
|
||||
|
@ -189,12 +178,12 @@ def index_downloads():
|
|||
|
||||
# Solr returns facets as a dict of dicts (see json.nl parameter)
|
||||
downloads = res.json()["facet_counts"]["facet_fields"]
|
||||
# iterate over the 'owningItem' dict and get the item ids and downloads
|
||||
for item_id, item_downloads in downloads["owningItem"].items():
|
||||
data.append((item_id, item_downloads))
|
||||
# iterate over the facetField dict and get the item ids and downloads
|
||||
for id_, downloads in downloads[facetField].items():
|
||||
data.append((id_, downloads))
|
||||
|
||||
# do a batch insert of values from the current "page" of results
|
||||
sql = "INSERT INTO items(id, downloads) VALUES %s ON CONFLICT(id) DO UPDATE SET downloads=excluded.downloads"
|
||||
sql = f"INSERT INTO {indexType}(id, downloads) VALUES %s ON CONFLICT(id) DO UPDATE SET downloads=excluded.downloads"
|
||||
psycopg2.extras.execute_values(cursor, sql, data, template="(%s, %s)")
|
||||
db.commit()
|
||||
|
||||
|
@ -211,13 +200,32 @@ with DatabaseManager() as db:
|
|||
"""CREATE TABLE IF NOT EXISTS items
|
||||
(id UUID PRIMARY KEY, views INT DEFAULT 0, downloads INT DEFAULT 0)"""
|
||||
)
|
||||
# create table to store community views and downloads
|
||||
cursor.execute(
|
||||
"""CREATE TABLE IF NOT EXISTS communities
|
||||
(id UUID PRIMARY KEY, views INT DEFAULT 0, downloads INT DEFAULT 0)"""
|
||||
)
|
||||
# create table to store collection views and downloads
|
||||
cursor.execute(
|
||||
"""CREATE TABLE IF NOT EXISTS collections
|
||||
(id UUID PRIMARY KEY, views INT DEFAULT 0, downloads INT DEFAULT 0)"""
|
||||
)
|
||||
|
||||
# commit the table creation before closing the database connection
|
||||
db.commit()
|
||||
|
||||
shards = get_statistics_shards()
|
||||
|
||||
index_views()
|
||||
index_downloads()
|
||||
# Index views and downloads for items, communities, and collections. Here the
|
||||
# first parameter is the type of indexing to perform, and the second parameter
|
||||
# is the field to facet by in Solr's statistics to get this information.
|
||||
|
||||
index_views("items", "id")
|
||||
index_views("communities", "owningComm")
|
||||
index_views("collections", "owningColl")
|
||||
|
||||
index_downloads("items", "owningItem")
|
||||
index_downloads("communities", "owningComm")
|
||||
index_downloads("collections", "owningColl")
|
||||
|
||||
# vim: set sw=4 ts=4 expandtab:
|
||||
|
|
|
@ -1,105 +0,0 @@
|
|||
import requests
|
||||
|
||||
from .config import SOLR_SERVER
|
||||
|
||||
|
||||
def get_views(solr_date_string: str, items: list):
|
||||
"""
|
||||
Get view statistics for a list of items from Solr.
|
||||
|
||||
:parameter solr_date_string (str): Solr date string, for example "[* TO *]"
|
||||
: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
|
||||
solr_items_string: str = " OR ".join(items).replace("-", r"\-")
|
||||
|
||||
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}",
|
||||
"facet": "true",
|
||||
"facet.field": "id",
|
||||
"facet.mincount": 1,
|
||||
"shards": shards,
|
||||
"rows": 0,
|
||||
"wt": "json",
|
||||
"json.nl": "map", # return facets as a dict instead of a flat list
|
||||
}
|
||||
|
||||
solr_url = SOLR_SERVER + "/statistics/select"
|
||||
res = requests.get(solr_url, params=solr_query_params)
|
||||
|
||||
# Create an empty dict to store views
|
||||
data = {}
|
||||
|
||||
# Solr returns facets as a dict of dicts (see the json.nl parameter)
|
||||
views = res.json()["facet_counts"]["facet_fields"]
|
||||
# iterate over the 'id' dict and get the item ids and views
|
||||
for item_id, item_views in views["id"].items():
|
||||
data[item_id] = item_views
|
||||
|
||||
# Check if any items have missing stats so we can set them to 0
|
||||
if len(data) < len(items):
|
||||
# List comprehension to get a list of item ids (keys) in the data
|
||||
data_ids = [k for k, v in data.items()]
|
||||
for item_id in items:
|
||||
if item_id not in data_ids:
|
||||
data[item_id] = 0
|
||||
continue
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def get_downloads(solr_date_string: str, items: list):
|
||||
"""
|
||||
Get download statistics for a list of items from Solr.
|
||||
|
||||
:parameter solr_date_string (str): Solr date string, for example "[* TO *]"
|
||||
: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
|
||||
solr_items_string: str = " OR ".join(items).replace("-", r"\-")
|
||||
|
||||
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}",
|
||||
"facet": "true",
|
||||
"facet.field": "owningItem",
|
||||
"facet.mincount": 1,
|
||||
"shards": shards,
|
||||
"rows": 0,
|
||||
"wt": "json",
|
||||
"json.nl": "map", # return facets as a dict instead of a flat list
|
||||
}
|
||||
|
||||
solr_url = SOLR_SERVER + "/statistics/select"
|
||||
res = requests.get(solr_url, params=solr_query_params)
|
||||
|
||||
# Create an empty dict to store downloads
|
||||
data = {}
|
||||
|
||||
# Solr returns facets as a dict of dicts (see the json.nl parameter)
|
||||
downloads = res.json()["facet_counts"]["facet_fields"]
|
||||
# Iterate over the 'owningItem' dict and get the item ids and downloads
|
||||
for item_id, item_downloads in downloads["owningItem"].items():
|
||||
data[item_id] = item_downloads
|
||||
|
||||
# Check if any items have missing stats so we can set them to 0
|
||||
if len(data) < len(items):
|
||||
# List comprehension to get a list of item ids (keys) in the data
|
||||
data_ids = [k for k, v in data.items()]
|
||||
for item_id in items:
|
||||
if item_id not in data_ids:
|
||||
data[item_id] = 0
|
||||
continue
|
||||
|
||||
return data
|
||||
|
||||
# vim: set sw=4 ts=4 expandtab:
|
|
@ -0,0 +1,126 @@
|
|||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
import requests
|
||||
|
||||
from .config import SOLR_SERVER
|
||||
from .util import get_statistics_shards
|
||||
|
||||
|
||||
def get_views(solr_date_string: str, elements: list, facetField: str):
|
||||
"""
|
||||
Get view statistics for a list of elements from Solr. Depending on the req-
|
||||
uest this could be items, communities, or collections.
|
||||
|
||||
:parameter solr_date_string (str): Solr date string, for example "[* TO *]"
|
||||
:parameter elements (list): a list of IDs
|
||||
:parameter facetField (str): Solr field to facet by, for example "id"
|
||||
:returns: A dict of IDs and views
|
||||
"""
|
||||
shards = get_statistics_shards()
|
||||
|
||||
# Join the UUIDs with "OR" and escape the hyphens for Solr
|
||||
solr_elements_string: str = " OR ".join(elements).replace("-", r"\-")
|
||||
|
||||
solr_query_params = {
|
||||
"q": f"{facetField}:({solr_elements_string})",
|
||||
"fq": f"type:2 AND -isBot:true AND statistics_type:view AND time:{solr_date_string}",
|
||||
"fl": facetField,
|
||||
"facet": "true",
|
||||
"facet.field": facetField,
|
||||
"facet.mincount": 1,
|
||||
"shards": shards,
|
||||
"rows": 0,
|
||||
"wt": "json",
|
||||
"json.nl": "map", # return facets as a dict instead of a flat list
|
||||
}
|
||||
|
||||
solr_url = SOLR_SERVER + "/statistics/select"
|
||||
res = requests.get(solr_url, params=solr_query_params)
|
||||
|
||||
# Create an empty dict to store views
|
||||
data = {}
|
||||
|
||||
# Solr returns facets as a dict of dicts (see the json.nl parameter)
|
||||
views = res.json()["facet_counts"]["facet_fields"]
|
||||
# iterate over the facetField dict and ids and views
|
||||
for id_, views in views[facetField].items():
|
||||
# For items we can rely on Solr returning facets for the *only* the ids
|
||||
# in our query, but for communities and collections, the owningComm and
|
||||
# owningColl fields are multi-value so Solr will return facets with the
|
||||
# values in our query as well as *any others* that happen to be present
|
||||
# in the field (which looks like Solr returning unrelated results until
|
||||
# you realize that the field is multi-value and this is correct).
|
||||
#
|
||||
# To work around this I make sure that each id in the returned dict are
|
||||
# present in the elements list POSTed by the user.
|
||||
if id_ in elements:
|
||||
data[id_] = views
|
||||
|
||||
# Check if any ids have missing stats so we can set them to 0
|
||||
if len(data) < len(elements):
|
||||
# List comprehension to get a list of ids (keys) in the data
|
||||
data_ids = [k for k, v in data.items()]
|
||||
for element_id in elements:
|
||||
if element_id not in data_ids:
|
||||
data[element_id] = 0
|
||||
continue
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def get_downloads(solr_date_string: str, elements: list, facetField: str):
|
||||
"""
|
||||
Get download statistics for a list of items from Solr. Depending on the req-
|
||||
uest this could be items, communities, or collections.
|
||||
|
||||
:parameter solr_date_string (str): Solr date string, for example "[* TO *]"
|
||||
:parameter elements (list): a list of IDs
|
||||
:parameter facetField (str): Solr field to facet by, for example "id"
|
||||
:returns: A dict of IDs and downloads
|
||||
"""
|
||||
shards = get_statistics_shards()
|
||||
|
||||
# Join the UUIDs with "OR" and escape the hyphens for Solr
|
||||
solr_elements_string: str = " OR ".join(elements).replace("-", r"\-")
|
||||
|
||||
solr_query_params = {
|
||||
"q": f"{facetField}:({solr_elements_string})",
|
||||
"fq": f"type:0 AND -isBot:true AND statistics_type:view AND bundleName:ORIGINAL AND time:{solr_date_string}",
|
||||
"fl": facetField,
|
||||
"facet": "true",
|
||||
"facet.field": facetField,
|
||||
"facet.mincount": 1,
|
||||
"shards": shards,
|
||||
"rows": 0,
|
||||
"wt": "json",
|
||||
"json.nl": "map", # return facets as a dict instead of a flat list
|
||||
}
|
||||
|
||||
solr_url = SOLR_SERVER + "/statistics/select"
|
||||
res = requests.get(solr_url, params=solr_query_params)
|
||||
|
||||
# Create an empty dict to store downloads
|
||||
data = {}
|
||||
|
||||
# Solr returns facets as a dict of dicts (see the json.nl parameter)
|
||||
downloads = res.json()["facet_counts"]["facet_fields"]
|
||||
# Iterate over the facetField dict and get the ids and downloads
|
||||
for id_, downloads in downloads[facetField].items():
|
||||
# Make sure that each id in the returned dict are present in the
|
||||
# elements list POSTed by the user.
|
||||
if id_ in elements:
|
||||
data[id_] = downloads
|
||||
|
||||
# Check if any elements have missing stats so we can set them to 0
|
||||
if len(data) < len(elements):
|
||||
# List comprehension to get a list of ids (keys) in the data
|
||||
data_ids = [k for k, v in data.items()]
|
||||
for element_id in elements:
|
||||
if element_id not in data_ids:
|
||||
data[element_id] = 0
|
||||
continue
|
||||
|
||||
return data
|
||||
|
||||
|
||||
# vim: set sw=4 ts=4 expandtab:
|
|
@ -1,4 +1,13 @@
|
|||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
import datetime
|
||||
import json
|
||||
import re
|
||||
|
||||
import falcon
|
||||
import requests
|
||||
|
||||
from .config import SOLR_SERVER
|
||||
|
||||
|
||||
def get_statistics_shards():
|
||||
|
@ -8,11 +17,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,8 +62,6 @@ def get_statistics_shards():
|
|||
|
||||
|
||||
def is_valid_date(date):
|
||||
import datetime
|
||||
|
||||
try:
|
||||
# Solr date format is: 2020-01-01T00:00:00Z
|
||||
# See: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
|
||||
|
@ -73,12 +75,12 @@ def is_valid_date(date):
|
|||
)
|
||||
|
||||
|
||||
def validate_items_post_parameters(req, resp, resource, params):
|
||||
"""Check the POSTed request parameters for the `/items` endpoint.
|
||||
def validate_post_parameters(req, resp, resource, params):
|
||||
"""Check the POSTed request parameters for the `/items`, `/communities` and
|
||||
`/collections` endpoints.
|
||||
|
||||
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).
|
||||
|
@ -125,14 +127,67 @@ def validate_items_post_parameters(req, resp, resource, params):
|
|||
else:
|
||||
req.context.page = 0
|
||||
|
||||
# Parse the list of items from the POST request body
|
||||
if "items" in doc:
|
||||
if isinstance(doc["items"], list) and len(doc["items"]) > 0:
|
||||
req.context.items = doc["items"]
|
||||
# Parse the list of elements from the POST request body
|
||||
if req.context.statistics_scope in doc:
|
||||
if (
|
||||
isinstance(doc[req.context.statistics_scope], list)
|
||||
and len(doc[req.context.statistics_scope]) > 0
|
||||
):
|
||||
req.context.elements = doc[req.context.statistics_scope]
|
||||
else:
|
||||
raise falcon.HTTPBadRequest(
|
||||
title="Invalid parameter",
|
||||
description='The "items" parameter is invalid. The value must be a comma-separated list of item UUIDs.',
|
||||
description=f'The "{req.context.statistics_scope}" parameter is invalid. The value must be a comma-separated list of UUIDs.',
|
||||
)
|
||||
else:
|
||||
req.context.items = list()
|
||||
req.context.elements = []
|
||||
|
||||
|
||||
def set_statistics_scope(req, resp, resource, params):
|
||||
"""Set the statistics scope (item, collection, or community) of the request
|
||||
as well as the appropriate database (for GET requests) and Solr facet fields
|
||||
(for POST requests).
|
||||
|
||||
Meant to be used as a `before` hook.
|
||||
"""
|
||||
|
||||
# Extract the scope from the request path. This is *guaranteed* to be one
|
||||
# of the following values because we only send requests matching these few
|
||||
# patterns to routes using this set_statistics_scope hook.
|
||||
#
|
||||
# Note: this regex is ordered so that "items" and "collections" match before
|
||||
# "item" and "collection".
|
||||
req.context.statistics_scope = re.findall(
|
||||
r"^/(communities|community|collections|collection|items|item)", req.path
|
||||
)[0]
|
||||
|
||||
# Set the correct database based on the statistics_scope. The database is
|
||||
# used for all GET requests where statistics are returned directly from the
|
||||
# database. In this case we can return early.
|
||||
if req.method == "GET":
|
||||
if re.findall(r"^(item|items)$", req.context.statistics_scope):
|
||||
req.context.database = "items"
|
||||
elif re.findall(r"^(community|communities)$", req.context.statistics_scope):
|
||||
req.context.database = "communities"
|
||||
elif re.findall(r"^(collection|collections)$", req.context.statistics_scope):
|
||||
req.context.database = "collections"
|
||||
|
||||
# GET requests only need the scope and the database so we can return now
|
||||
return
|
||||
|
||||
# If the current request is for a plural items, communities, or collections
|
||||
# that includes a list of element ids POSTed with the request body then we
|
||||
# need to set the Solr facet field so we can get the live results.
|
||||
if req.method == "POST":
|
||||
if req.context.statistics_scope == "items":
|
||||
req.context.views_facet_field = "id"
|
||||
req.context.downloads_facet_field = "owningItem"
|
||||
elif req.context.statistics_scope == "communities":
|
||||
req.context.views_facet_field = "owningComm"
|
||||
req.context.downloads_facet_field = "owningComm"
|
||||
elif req.context.statistics_scope == "collections":
|
||||
req.context.views_facet_field = "owningColl"
|
||||
req.context.downloads_facet_field = "owningColl"
|
||||
|
||||
|
||||
# vim: set sw=4 ts=4 expandtab:
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,25 +1,29 @@
|
|||
[tool.poetry]
|
||||
name = "dspace-statistics-api"
|
||||
version = "1.3.2"
|
||||
description = "A simple REST API to expose Solr view and download statistics for items in a DSpace repository."
|
||||
version = "1.4.4-dev"
|
||||
description = "A simple REST API to expose Solr view and download statistics for items, communities, and collections in a DSpace repository."
|
||||
authors = ["Alan Orth <aorth@mjanja.ch>"]
|
||||
license = "GPL-3.0-only"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.6"
|
||||
gunicorn = "^20.0.4"
|
||||
falcon = "^2.0.0"
|
||||
psycopg2-binary = "^2.8.6"
|
||||
python = "^3.8.1"
|
||||
gunicorn = "^21.0.0"
|
||||
falcon = "3.1.3"
|
||||
psycopg2 = "^2.9.1"
|
||||
requests = "^2.24.0"
|
||||
falcon-swagger-ui = {git = "https://github.com/alanorth/falcon-swagger-ui.git", rev="falcon3-update-swagger-ui"}
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
ipython = { version = "^7.18.1", python = "^3.7" }
|
||||
flake8 = "^3.8.4"
|
||||
pytest = "^6.1.1"
|
||||
isort = "^5.5.4"
|
||||
black = "^20.8b1"
|
||||
pytest-clarity = "^0.3.0-alpha.0"
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
black = "^23.0.0"
|
||||
fixit = "^2.1.0"
|
||||
flake8 = "^7.0.0"
|
||||
isort = "^5.9.1"
|
||||
pytest = "^7.0.0"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry>=0.12"]
|
||||
build-backend = "poetry.masonry.api"
|
||||
|
||||
[tool.isort]
|
||||
profile = "black"
|
||||
line_length=88
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
[pytest]
|
||||
addopts= -rsxX -s -v --strict
|
||||
addopts= -rsxX -s -v --strict-markers
|
||||
filterwarnings =
|
||||
error::UserWarning
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||
"extends": [
|
||||
"config:base"
|
||||
],
|
||||
"pip_requirements": {
|
||||
"enabled": false
|
||||
}
|
||||
}
|
|
@ -1,51 +1,33 @@
|
|||
appdirs==1.4.4
|
||||
appnope==0.1.0; python_version >= "3.7" and python_version < "4.0" and sys_platform == "darwin"
|
||||
atomicwrites==1.4.0; sys_platform == "win32"
|
||||
attrs==20.3.0
|
||||
backcall==0.2.0; python_version >= "3.7" and python_version < "4.0"
|
||||
black==20.8b1
|
||||
certifi==2020.11.8
|
||||
chardet==3.0.4
|
||||
click==7.1.2
|
||||
colorama==0.4.4; python_version >= "3.7" and python_version < "4.0" and sys_platform == "win32" or sys_platform == "win32"
|
||||
dataclasses==0.6; python_version < "3.7"
|
||||
decorator==4.4.2; python_version >= "3.7" and python_version < "4.0"
|
||||
falcon==2.0.0
|
||||
flake8==3.8.4
|
||||
gunicorn==20.0.4
|
||||
idna==2.10
|
||||
importlib-metadata==2.0.0; python_version < "3.8"
|
||||
iniconfig==1.1.1
|
||||
ipython==7.19.0; python_version >= "3.7" and python_version < "4.0"
|
||||
ipython-genutils==0.2.0; python_version >= "3.7" and python_version < "4.0"
|
||||
isort==5.6.4
|
||||
jedi==0.17.2; python_version >= "3.7" and python_version < "4.0"
|
||||
mccabe==0.6.1
|
||||
mypy-extensions==0.4.3
|
||||
packaging==20.4
|
||||
parso==0.7.1; python_version >= "3.7" and python_version < "4.0"
|
||||
pathspec==0.8.1
|
||||
pexpect==4.8.0; python_version >= "3.7" and python_version < "4.0" and sys_platform != "win32"
|
||||
pickleshare==0.7.5; python_version >= "3.7" and python_version < "4.0"
|
||||
pluggy==0.13.1
|
||||
prompt-toolkit==3.0.8; python_version >= "3.7" and python_version < "4.0"
|
||||
psycopg2-binary==2.8.6
|
||||
ptyprocess==0.6.0; python_version >= "3.7" and python_version < "4.0" and sys_platform != "win32"
|
||||
py==1.9.0
|
||||
pycodestyle==2.6.0
|
||||
pyflakes==2.2.0
|
||||
pygments==2.7.2; python_version >= "3.7" and python_version < "4.0"
|
||||
pyparsing==2.4.7
|
||||
pytest==6.1.2
|
||||
pytest-clarity==0.3.0a0
|
||||
regex==2020.11.13
|
||||
requests==2.25.0
|
||||
six==1.15.0
|
||||
termcolor==1.1.0
|
||||
toml==0.10.2
|
||||
traitlets==5.0.5; python_version >= "3.7" and python_version < "4.0"
|
||||
typed-ast==1.4.1
|
||||
typing-extensions==3.7.4.3
|
||||
urllib3==1.26.2
|
||||
wcwidth==0.2.5; python_version >= "3.7" and python_version < "4.0"
|
||||
zipp==3.4.0; python_version < "3.8"
|
||||
atomicwrites==1.4.1 ; python_version >= "3.8" and python_version < "4.0" and sys_platform == "win32"
|
||||
attrs==23.1.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
black==22.12.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
certifi==2023.5.7 ; python_version >= "3.8" and python_version < "4.0"
|
||||
charset-normalizer==3.1.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
click==8.1.3 ; python_version >= "3.8" and python_version < "4.0"
|
||||
colorama==0.4.6 ; python_version >= "3.8" and python_version < "4.0" and (sys_platform == "win32" or platform_system == "Windows")
|
||||
falcon-swagger-ui @ git+https://github.com/alanorth/falcon-swagger-ui.git@falcon3-update-swagger-ui ; python_version >= "3.8" and python_version < "4.0"
|
||||
falcon==3.1.1 ; python_version >= "3.8" and python_version < "4.0"
|
||||
flake8==4.0.1 ; python_version >= "3.8" and python_version < "4.0"
|
||||
gunicorn==20.1.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
idna==3.4 ; python_version >= "3.8" and python_version < "4.0"
|
||||
iniconfig==2.0.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
isort==5.12.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
jinja2==3.1.2 ; python_version >= "3.8" and python_version < "4.0"
|
||||
markupsafe==2.1.2 ; python_version >= "3.8" and python_version < "4.0"
|
||||
mccabe==0.6.1 ; python_version >= "3.8" and python_version < "4.0"
|
||||
mypy-extensions==1.0.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
packaging==23.1 ; python_version >= "3.8" and python_version < "4.0"
|
||||
pathspec==0.11.1 ; python_version >= "3.8" and python_version < "4.0"
|
||||
platformdirs==3.5.1 ; python_version >= "3.8" and python_version < "4.0"
|
||||
pluggy==1.0.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
psycopg2==2.9.6 ; python_version >= "3.8" and python_version < "4.0"
|
||||
py==1.11.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
pycodestyle==2.8.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
pyflakes==2.4.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
pytest==6.2.5 ; python_version >= "3.8" and python_version < "4.0"
|
||||
requests==2.31.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
setuptools==67.8.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
toml==0.10.2 ; python_version >= "3.8" and python_version < "4.0"
|
||||
tomli==2.0.1 ; python_version >= "3.8" and python_full_version < "3.11.0a7"
|
||||
typing-extensions==4.6.2 ; python_version >= "3.8" and python_version < "3.10"
|
||||
urllib3==2.0.2 ; python_version >= "3.8" and python_version < "4.0"
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
certifi==2020.11.8
|
||||
chardet==3.0.4
|
||||
falcon==2.0.0
|
||||
gunicorn==20.0.4
|
||||
idna==2.10
|
||||
psycopg2-binary==2.8.6
|
||||
requests==2.25.0
|
||||
urllib3==1.26.2
|
||||
certifi==2023.5.7 ; python_version >= "3.8" and python_version < "4.0"
|
||||
charset-normalizer==3.1.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
falcon-swagger-ui @ git+https://github.com/alanorth/falcon-swagger-ui.git@falcon3-update-swagger-ui ; python_version >= "3.8" and python_version < "4.0"
|
||||
falcon==3.1.1 ; python_version >= "3.8" and python_version < "4.0"
|
||||
gunicorn==20.1.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
idna==3.4 ; python_version >= "3.8" and python_version < "4.0"
|
||||
jinja2==3.1.2 ; python_version >= "3.8" and python_version < "4.0"
|
||||
markupsafe==2.1.2 ; python_version >= "3.8" and python_version < "4.0"
|
||||
psycopg2==2.9.6 ; python_version >= "3.8" and python_version < "4.0"
|
||||
requests==2.31.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
setuptools==67.8.0 ; python_version >= "3.8" and python_version < "4.0"
|
||||
urllib3==2.0.2 ; python_version >= "3.8" and python_version < "4.0"
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
[isort]
|
||||
multi_line_output=3
|
||||
include_trailing_comma=True
|
||||
force_grid_wrap=0
|
||||
use_parentheses=True
|
||||
line_length=88
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,379 @@
|
|||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from falcon import testing
|
||||
|
||||
from dspace_statistics_api.app import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
return testing.TestClient(app)
|
||||
|
||||
|
||||
def test_get_collection(client):
|
||||
"""Test requesting a single collection."""
|
||||
|
||||
response = client.simulate_get("/collection/8ea4b611-1f59-4d4e-b78d-a9921a72cfe7")
|
||||
response_doc = json.loads(response.text)
|
||||
|
||||
assert isinstance(response_doc["downloads"], int)
|
||||
assert isinstance(response_doc["id"], str)
|
||||
assert isinstance(response_doc["views"], int)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_get_missing_collection(client):
|
||||
"""Test requesting a single non-existing collection."""
|
||||
|
||||
response = client.simulate_get("/collection/508abe0a-689f-402e-885d-2f6b02e7a39c")
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_get_collections(client):
|
||||
"""Test requesting 100 collections."""
|
||||
|
||||
response = client.simulate_get("/collections", query_string="limit=100")
|
||||
response_doc = json.loads(response.text)
|
||||
|
||||
assert isinstance(response_doc["currentPage"], int)
|
||||
assert isinstance(response_doc["totalPages"], int)
|
||||
assert isinstance(response_doc["statistics"], list)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_get_collections_invalid_limit(client):
|
||||
"""Test requesting 100 collections with an invalid limit parameter."""
|
||||
|
||||
response = client.simulate_get("/collections", query_string="limit=101")
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_get_collections_invalid_page(client):
|
||||
"""Test requesting 100 collections with an invalid page parameter."""
|
||||
|
||||
response = client.simulate_get("/collections", query_string="page=-1")
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
def test_post_collections_valid_dateFrom(client):
|
||||
"""Test POSTing a request to /collections with a valid dateFrom parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00Z",
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_collections_valid_dateFrom_mocked(client):
|
||||
"""Mock test POSTing a request to /collections with a valid dateFrom parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00Z",
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 21,
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5": 0,
|
||||
}
|
||||
get_downloads_return_value = {
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 575,
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5": 899,
|
||||
}
|
||||
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
||||
):
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_downloads",
|
||||
return_value=get_downloads_return_value,
|
||||
):
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_collections_invalid_dateFrom(client):
|
||||
"""Test POSTing a request to /collections with an invalid dateFrom parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00",
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
def test_post_collections_valid_dateTo(client):
|
||||
"""Test POSTing a request to /collections with a valid dateTo parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateTo": "2020-01-01T00:00:00Z",
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_collections_valid_dateTo_mocked(client):
|
||||
"""Mock test POSTing a request to /collections with a valid dateTo parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateTo": "2020-01-01T00:00:00Z",
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 21,
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5": 0,
|
||||
}
|
||||
get_downloads_return_value = {
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 575,
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5": 899,
|
||||
}
|
||||
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
||||
):
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_downloads",
|
||||
return_value=get_downloads_return_value,
|
||||
):
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_collections_invalid_dateTo(client):
|
||||
"""Test POSTing a request to /collections with an invalid dateTo parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00",
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
def test_post_collections_valid_limit(client):
|
||||
"""Test POSTing a request to /collections with a valid limit parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"limit": 1,
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 1
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 1
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_collections_valid_limit_mocked(client):
|
||||
"""Mock test POSTing a request to /collections with a valid limit parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"limit": 1,
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 21}
|
||||
get_downloads_return_value = {"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 575}
|
||||
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
||||
):
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_downloads",
|
||||
return_value=get_downloads_return_value,
|
||||
):
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 1
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 1
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_collections_invalid_limit(client):
|
||||
"""Test POSTing a request to /collections with an invalid limit parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"limit": -1,
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
def test_post_collections_valid_page(client):
|
||||
"""Test POSTing a request to /collections with a valid page parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"page": 0,
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert response.json["totalPages"] == 1
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_collections_valid_page_mocked(client):
|
||||
"""Mock test POSTing a request to /collections with a valid page parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"page": 0,
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 21,
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5": 0,
|
||||
}
|
||||
get_downloads_return_value = {
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7": 575,
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5": 899,
|
||||
}
|
||||
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
||||
):
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_downloads",
|
||||
return_value=get_downloads_return_value,
|
||||
):
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_collections_invalid_page(client):
|
||||
"""Test POSTing a request to /collections with an invalid page parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"page": -1,
|
||||
"collections": [
|
||||
"8ea4b611-1f59-4d4e-b78d-a9921a72cfe7",
|
||||
"260548c8-fda4-4dc8-a979-03495753cdd5",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/collections", json=request_body)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
# vim: set sw=4 ts=4 expandtab:
|
|
@ -0,0 +1,379 @@
|
|||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from falcon import testing
|
||||
|
||||
from dspace_statistics_api.app import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
return testing.TestClient(app)
|
||||
|
||||
|
||||
def test_get_community(client):
|
||||
"""Test requesting a single community."""
|
||||
|
||||
response = client.simulate_get("/community/bde7139c-d321-46bb-aef6-ae70799e5edb")
|
||||
response_doc = json.loads(response.text)
|
||||
|
||||
assert isinstance(response_doc["downloads"], int)
|
||||
assert isinstance(response_doc["id"], str)
|
||||
assert isinstance(response_doc["views"], int)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_get_missing_community(client):
|
||||
"""Test requesting a single non-existing community."""
|
||||
|
||||
response = client.simulate_get("/item/dec6bfc6-efeb-4f74-8436-79fa80bb5c21")
|
||||
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_get_communities(client):
|
||||
"""Test requesting 100 communities."""
|
||||
|
||||
response = client.simulate_get("/communities", query_string="limit=100")
|
||||
response_doc = json.loads(response.text)
|
||||
|
||||
assert isinstance(response_doc["currentPage"], int)
|
||||
assert isinstance(response_doc["totalPages"], int)
|
||||
assert isinstance(response_doc["statistics"], list)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_get_communities_invalid_limit(client):
|
||||
"""Test requesting 100 communities with an invalid limit parameter."""
|
||||
|
||||
response = client.simulate_get("/communities", query_string="limit=101")
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_get_communities_invalid_page(client):
|
||||
"""Test requesting 100 communities with an invalid page parameter."""
|
||||
|
||||
response = client.simulate_get("/communities", query_string="page=-1")
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
def test_post_communities_valid_dateFrom(client):
|
||||
"""Test POSTing a request to /communities with a valid dateFrom parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00Z",
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_communities_valid_dateFrom_mocked(client):
|
||||
"""Mock test POSTing a request to /communities with a valid dateFrom parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00Z",
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb": 309,
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 0,
|
||||
}
|
||||
get_downloads_return_value = {
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb": 400,
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 290,
|
||||
}
|
||||
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
||||
):
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_downloads",
|
||||
return_value=get_downloads_return_value,
|
||||
):
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_communities_invalid_dateFrom(client):
|
||||
"""Test POSTing a request to /communities with an invalid dateFrom parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00",
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
def test_post_communities_valid_dateTo(client):
|
||||
"""Test POSTing a request to /communities with a valid dateTo parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateTo": "2020-01-01T00:00:00Z",
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_communities_valid_dateTo_mocked(client):
|
||||
"""Mock test POSTing a request to /communities with a valid dateTo parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateTo": "2020-01-01T00:00:00Z",
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb": 21,
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 0,
|
||||
}
|
||||
get_downloads_return_value = {
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb": 575,
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 899,
|
||||
}
|
||||
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
||||
):
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_downloads",
|
||||
return_value=get_downloads_return_value,
|
||||
):
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_communities_invalid_dateTo(client):
|
||||
"""Test POSTing a request to /communities with an invalid dateTo parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00",
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
def test_post_communities_valid_limit(client):
|
||||
"""Test POSTing a request to /communities with a valid limit parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"limit": 1,
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 1
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 1
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_communities_valid_limit_mocked(client):
|
||||
"""Mock test POSTing a request to /communities with a valid limit parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"limit": 1,
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {"bde7139c-d321-46bb-aef6-ae70799e5edb": 200}
|
||||
get_downloads_return_value = {"bde7139c-d321-46bb-aef6-ae70799e5edb": 309}
|
||||
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
||||
):
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_downloads",
|
||||
return_value=get_downloads_return_value,
|
||||
):
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 1
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 1
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_communities_invalid_limit(client):
|
||||
"""Test POSTing a request to /communities with an invalid limit parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"limit": -1,
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
@pytest.mark.xfail
|
||||
def test_post_communities_valid_page(client):
|
||||
"""Test POSTing a request to /communities with a valid page parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"page": 0,
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert response.json["totalPages"] == 1
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_communities_valid_page_mocked(client):
|
||||
"""Mock test POSTing a request to communities with a valid page parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"page": 0,
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb": 21,
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 0,
|
||||
}
|
||||
get_downloads_return_value = {
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb": 575,
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f": 899,
|
||||
}
|
||||
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
||||
):
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_downloads",
|
||||
return_value=get_downloads_return_value,
|
||||
):
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert isinstance(response.json["totalPages"], int)
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
assert isinstance(response.json["statistics"][1]["views"], int)
|
||||
assert isinstance(response.json["statistics"][1]["downloads"], int)
|
||||
|
||||
|
||||
def test_post_communities_invalid_page(client):
|
||||
"""Test POSTing a request to /communities with an invalid page parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"page": -1,
|
||||
"communities": [
|
||||
"bde7139c-d321-46bb-aef6-ae70799e5edb",
|
||||
"2a920a61-b08a-4642-8e5d-2639c6702b1f",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/communities", json=request_body)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
# vim: set sw=4 ts=4 expandtab:
|
|
@ -0,0 +1,50 @@
|
|||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
import pytest
|
||||
from falcon import testing
|
||||
|
||||
from dspace_statistics_api.app import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
return testing.TestClient(app)
|
||||
|
||||
|
||||
def test_get_docs(client):
|
||||
"""Test requesting the documentation at the root."""
|
||||
|
||||
response = client.simulate_get("/")
|
||||
|
||||
assert isinstance(response.content, bytes)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_get_openapi_json(client):
|
||||
"""Test requesting the OpenAPI JSON schema."""
|
||||
|
||||
response = client.simulate_get("/docs/openapi.json")
|
||||
|
||||
assert isinstance(response.content, bytes)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_get_swagger_ui(client):
|
||||
"""Test requesting the Swagger UI."""
|
||||
|
||||
response = client.simulate_get("/swagger")
|
||||
|
||||
assert isinstance(response.content, bytes)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
def test_get_status(client):
|
||||
"""Test requesting the status page."""
|
||||
|
||||
response = client.simulate_get("/status")
|
||||
|
||||
assert isinstance(response.content, bytes)
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
# vim: set sw=4 ts=4 expandtab:
|
|
@ -1,29 +1,23 @@
|
|||
from falcon import testing
|
||||
# SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
import json
|
||||
import pytest
|
||||
from unittest.mock import patch
|
||||
|
||||
from dspace_statistics_api.app import api
|
||||
import pytest
|
||||
from falcon import testing
|
||||
|
||||
from dspace_statistics_api.app import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
return testing.TestClient(api)
|
||||
|
||||
|
||||
def test_get_docs(client):
|
||||
"""Test requesting the documentation at the root."""
|
||||
|
||||
response = client.simulate_get("/")
|
||||
|
||||
assert isinstance(response.content, bytes)
|
||||
assert response.status_code == 200
|
||||
return testing.TestClient(app)
|
||||
|
||||
|
||||
def test_get_item(client):
|
||||
"""Test requesting a single item."""
|
||||
|
||||
response = client.simulate_get("/item/c3910974-c3a5-4053-9dce-104aa7bb1621")
|
||||
response = client.simulate_get("/item/fd8a46d5-1480-4e69-b187-cd3db96d8e4d")
|
||||
response_doc = json.loads(response.text)
|
||||
|
||||
assert isinstance(response_doc["downloads"], int)
|
||||
|
@ -70,13 +64,13 @@ def test_get_items_invalid_page(client):
|
|||
|
||||
@pytest.mark.xfail
|
||||
def test_post_items_valid_dateFrom(client):
|
||||
"""Test POSTing a request with a valid dateFrom parameter in the request body."""
|
||||
"""Test POSTing a request to /items with a valid dateFrom parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00Z",
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
|
@ -94,23 +88,23 @@ def test_post_items_valid_dateFrom(client):
|
|||
|
||||
|
||||
def test_post_items_valid_dateFrom_mocked(client):
|
||||
"""Mock test POSTing a request with a valid dateFrom parameter in the request body."""
|
||||
"""Mock test POSTing a request to /items with a valid dateFrom parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00Z",
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620": 21,
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313": 0,
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d": 21,
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1": 0,
|
||||
}
|
||||
get_downloads_return_value = {
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620": 575,
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313": 899,
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d": 575,
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1": 899,
|
||||
}
|
||||
|
||||
with patch(
|
||||
|
@ -134,13 +128,13 @@ def test_post_items_valid_dateFrom_mocked(client):
|
|||
|
||||
|
||||
def test_post_items_invalid_dateFrom(client):
|
||||
"""Test POSTing a request with an invalid dateFrom parameter in the request body."""
|
||||
"""Test POSTing a request to /items with an invalid dateFrom parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00",
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
|
@ -151,13 +145,13 @@ def test_post_items_invalid_dateFrom(client):
|
|||
|
||||
@pytest.mark.xfail
|
||||
def test_post_items_valid_dateTo(client):
|
||||
"""Test POSTing a request with a valid dateTo parameter in the request body."""
|
||||
"""Test POSTing a request to /items with a valid dateTo parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateTo": "2020-01-01T00:00:00Z",
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
|
@ -175,23 +169,23 @@ def test_post_items_valid_dateTo(client):
|
|||
|
||||
|
||||
def test_post_items_valid_dateTo_mocked(client):
|
||||
"""Mock test POSTing a request with a valid dateTo parameter in the request body."""
|
||||
"""Mock test POSTing a request to /items with a valid dateTo parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateTo": "2020-01-01T00:00:00Z",
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620": 21,
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313": 0,
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d": 21,
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1": 0,
|
||||
}
|
||||
get_downloads_return_value = {
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620": 575,
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313": 899,
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d": 575,
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1": 899,
|
||||
}
|
||||
|
||||
with patch(
|
||||
|
@ -215,13 +209,13 @@ def test_post_items_valid_dateTo_mocked(client):
|
|||
|
||||
|
||||
def test_post_items_invalid_dateTo(client):
|
||||
"""Test POSTing a request with an invalid dateTo parameter in the request body."""
|
||||
"""Test POSTing a request to /items with an invalid dateTo parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"dateFrom": "2020-01-01T00:00:00",
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
|
@ -232,13 +226,13 @@ def test_post_items_invalid_dateTo(client):
|
|||
|
||||
@pytest.mark.xfail
|
||||
def test_post_items_valid_limit(client):
|
||||
"""Test POSTing a request with a valid limit parameter in the request body."""
|
||||
"""Test POSTing a request to /items with a valid limit parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"limit": 1,
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
|
@ -254,18 +248,18 @@ def test_post_items_valid_limit(client):
|
|||
|
||||
|
||||
def test_post_items_valid_limit_mocked(client):
|
||||
"""Mock test POSTing a request with a valid limit parameter in the request body."""
|
||||
"""Mock test POSTing a request to /items with a valid limit parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"limit": 1,
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {"c3910974-c3a5-4053-9dce-104aa7bb1620": 21}
|
||||
get_downloads_return_value = {"c3910974-c3a5-4053-9dce-104aa7bb1620": 575}
|
||||
get_views_return_value = {"fd8a46d5-1480-4e69-b187-cd3db96d8e4d": 21}
|
||||
get_downloads_return_value = {"fd8a46d5-1480-4e69-b187-cd3db96d8e4d": 575}
|
||||
|
||||
with patch(
|
||||
"dspace_statistics_api.app.get_views", return_value=get_views_return_value
|
||||
|
@ -286,13 +280,13 @@ def test_post_items_valid_limit_mocked(client):
|
|||
|
||||
|
||||
def test_post_items_invalid_limit(client):
|
||||
"""Test POSTing a request with an invalid limit parameter in the request body."""
|
||||
"""Test POSTing a request to /items with an invalid limit parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"limit": -1,
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
|
@ -303,13 +297,13 @@ def test_post_items_invalid_limit(client):
|
|||
|
||||
@pytest.mark.xfail
|
||||
def test_post_items_valid_page(client):
|
||||
"""Test POSTing a request with a valid page parameter in the request body."""
|
||||
"""Test POSTing a request to /items with a valid page parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"page": 0,
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
|
@ -318,7 +312,7 @@ def test_post_items_valid_page(client):
|
|||
assert response.status_code == 200
|
||||
assert response.json["limit"] == 100
|
||||
assert response.json["currentPage"] == 0
|
||||
assert response.json["totalPages"] == 0
|
||||
assert response.json["totalPages"] == 1
|
||||
assert len(response.json["statistics"]) == 2
|
||||
assert isinstance(response.json["statistics"][0]["views"], int)
|
||||
assert isinstance(response.json["statistics"][0]["downloads"], int)
|
||||
|
@ -327,23 +321,23 @@ def test_post_items_valid_page(client):
|
|||
|
||||
|
||||
def test_post_items_valid_page_mocked(client):
|
||||
"""Mock test POSTing a request with a valid page parameter in the request body."""
|
||||
"""Mock test POSTing a request to /items with a valid page parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"page": 0,
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
get_views_return_value = {
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620": 21,
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313": 0,
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d": 21,
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1": 0,
|
||||
}
|
||||
get_downloads_return_value = {
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620": 575,
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313": 899,
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d": 575,
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1": 899,
|
||||
}
|
||||
|
||||
with patch(
|
||||
|
@ -367,16 +361,19 @@ def test_post_items_valid_page_mocked(client):
|
|||
|
||||
|
||||
def test_post_items_invalid_page(client):
|
||||
"""Test POSTing a request with an invalid page parameter in the request body."""
|
||||
"""Test POSTing a request to /items with an invalid page parameter in the request body."""
|
||||
|
||||
request_body = {
|
||||
"page": -1,
|
||||
"items": [
|
||||
"c3910974-c3a5-4053-9dce-104aa7bb1620",
|
||||
"887cc5f8-b5e7-4a2f-9053-49c91ab81313",
|
||||
"fd8a46d5-1480-4e69-b187-cd3db96d8e4d",
|
||||
"e53a2eab-1e31-448d-907b-3656ca4e86c1",
|
||||
],
|
||||
}
|
||||
|
||||
response = client.simulate_post("/items", json=request_body)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
# vim: set sw=4 ts=4 expandtab:
|
Loading…
Reference in New Issue