mirror of
https://github.com/ilri/dspace-statistics-api.git
synced 2025-05-10 15:16:02 +02:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
6bf34235d4
|
|||
e604d8ca81
|
|||
fc35b816f3
|
|||
9e6a2f7559
|
@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [0.4.0] - 2018-09-25
|
||||||
|
### Fixed
|
||||||
|
- Invalid OnCalendar syntax in dspace-statistics-indexer.timer
|
||||||
|
- Major logic error in indexer.py
|
||||||
|
|
||||||
## [0.3.2] - 2018-09-25
|
## [0.3.2] - 2018-09-25
|
||||||
## Changed
|
## Changed
|
||||||
- /item/id route now returns HTTP 404 if an item is not found
|
- /item/id route now returns HTTP 404 if an item is not found
|
||||||
|
@ -3,7 +3,7 @@ Description=DSpace Statistics Indexer
|
|||||||
|
|
||||||
[Timer]
|
[Timer]
|
||||||
# twice a day, at 6AM and 6PM
|
# twice a day, at 6AM and 6PM
|
||||||
OnCalendar=*-*-* 06:00:00,18:00:00
|
OnCalendar=*-*-* 06,18:00:00
|
||||||
# Add a random delay of 0–3600 seconds
|
# Add a random delay of 0–3600 seconds
|
||||||
RandomizedDelaySec=3600
|
RandomizedDelaySec=3600
|
||||||
Persistent=true
|
Persistent=true
|
||||||
|
59
indexer.py
59
indexer.py
@ -31,41 +31,55 @@
|
|||||||
# See: https://wiki.duraspace.org/display/DSPACE/Solr
|
# See: https://wiki.duraspace.org/display/DSPACE/Solr
|
||||||
|
|
||||||
from database import database_connection
|
from database import database_connection
|
||||||
|
import json
|
||||||
from solr import solr_connection
|
from solr import solr_connection
|
||||||
|
|
||||||
def index_views():
|
def index_views():
|
||||||
print("Populating database with item views.")
|
# 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
|
||||||
# determine the total number of items with views (aka Solr's numFound)
|
# the database. Also, stats are expensive, but we need stats.calcdistinct
|
||||||
|
# so we can get the countDistinct summary.
|
||||||
|
#
|
||||||
|
# see: https://lucene.apache.org/solr/guide/6_6/the-stats-component.html
|
||||||
res = solr.query('statistics', {
|
res = solr.query('statistics', {
|
||||||
'q':'type:2',
|
'q':'type:2',
|
||||||
'fq':'isBot:false AND statistics_type:view',
|
'fq':'isBot:false AND statistics_type:view',
|
||||||
'facet':True,
|
'facet':True,
|
||||||
'facet.field':'id',
|
'facet.field':'id',
|
||||||
|
'facet.mincount':1,
|
||||||
|
'facet.limit':1,
|
||||||
|
'facet.offset':0,
|
||||||
|
'stats':True,
|
||||||
|
'stats.field':'id',
|
||||||
|
'stats.calcdistinct':True
|
||||||
}, rows=0)
|
}, rows=0)
|
||||||
|
|
||||||
# divide results into "pages" (numFound / 100)
|
# get total number of distinct facets (countDistinct)
|
||||||
results_numFound = res.get_num_found()
|
results_totalNumFacets = json.loads(res.get_json())['stats']['stats_fields']['id']['countDistinct']
|
||||||
|
|
||||||
|
# divide results into "pages" (cast to int to effectively round down)
|
||||||
results_per_page = 100
|
results_per_page = 100
|
||||||
results_num_pages = round(results_numFound / results_per_page)
|
results_num_pages = int(results_totalNumFacets / results_per_page)
|
||||||
results_current_page = 0
|
results_current_page = 0
|
||||||
|
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
|
||||||
while results_current_page <= results_num_pages:
|
while results_current_page <= results_num_pages:
|
||||||
print('Page {} of {}.'.format(results_current_page, results_num_pages))
|
print('Indexing item views (page {} of {})'.format(results_current_page, results_num_pages))
|
||||||
|
|
||||||
res = solr.query('statistics', {
|
res = solr.query('statistics', {
|
||||||
'q':'type:2',
|
'q':'type:2',
|
||||||
'fq':'isBot:false AND statistics_type:view',
|
'fq':'isBot:false AND statistics_type:view',
|
||||||
'facet':True,
|
'facet':True,
|
||||||
'facet.field':'id',
|
'facet.field':'id',
|
||||||
|
'facet.mincount':1,
|
||||||
'facet.limit':results_per_page,
|
'facet.limit':results_per_page,
|
||||||
'facet.offset':results_current_page * results_per_page
|
'facet.offset':results_current_page * results_per_page
|
||||||
})
|
}, rows=0)
|
||||||
|
|
||||||
|
# check number of facets returned in the last query
|
||||||
|
#results_currentNumFacets = len(res.get_facets()['id'])
|
||||||
|
|
||||||
# make sure total number of results > 0
|
|
||||||
if res.get_num_found() > 0:
|
|
||||||
# SolrClient's get_facets() returns a dict of dicts
|
# SolrClient's get_facets() returns a dict of dicts
|
||||||
views = res.get_facets()
|
views = res.get_facets()
|
||||||
# in this case iterate over the 'id' dict and get the item ids and views
|
# in this case iterate over the 'id' dict and get the item ids and views
|
||||||
@ -81,38 +95,43 @@ def index_views():
|
|||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
||||||
def index_downloads():
|
def index_downloads():
|
||||||
print("Populating database with item downloads.")
|
# get the total number of distinct facets for items with at least 1 download
|
||||||
|
|
||||||
# determine the total number of items with downloads (aka Solr's numFound)
|
|
||||||
res = solr.query('statistics', {
|
res = solr.query('statistics', {
|
||||||
'q':'type:0',
|
'q':'type:0',
|
||||||
'fq':'isBot:false AND statistics_type:view AND bundleName:ORIGINAL',
|
'fq':'isBot:false AND statistics_type:view AND bundleName:ORIGINAL',
|
||||||
'facet':True,
|
'facet':True,
|
||||||
'facet.field':'owningItem',
|
'facet.field':'owningItem',
|
||||||
|
'facet.mincount':1,
|
||||||
|
'facet.limit':1,
|
||||||
|
'facet.offset':0,
|
||||||
|
'stats':True,
|
||||||
|
'stats.field':'owningItem',
|
||||||
|
'stats.calcdistinct':True
|
||||||
}, rows=0)
|
}, rows=0)
|
||||||
|
|
||||||
# divide results into "pages" (numFound / 100)
|
# get total number of distinct facets (countDistinct)
|
||||||
results_numFound = res.get_num_found()
|
results_totalNumFacets = json.loads(res.get_json())['stats']['stats_fields']['owningItem']['countDistinct']
|
||||||
|
|
||||||
|
# divide results into "pages" (cast to int to effectively round down)
|
||||||
results_per_page = 100
|
results_per_page = 100
|
||||||
results_num_pages = round(results_numFound / results_per_page)
|
results_num_pages = int(results_totalNumFacets / results_per_page)
|
||||||
results_current_page = 0
|
results_current_page = 0
|
||||||
|
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
|
||||||
while results_current_page <= results_num_pages:
|
while results_current_page <= results_num_pages:
|
||||||
print('Page {} of {}.'.format(results_current_page, results_num_pages))
|
print('Indexing item downloads (page {} of {})'.format(results_current_page, results_num_pages))
|
||||||
|
|
||||||
res = solr.query('statistics', {
|
res = solr.query('statistics', {
|
||||||
'q':'type:0',
|
'q':'type:0',
|
||||||
'fq':'isBot:false AND statistics_type:view AND bundleName:ORIGINAL',
|
'fq':'isBot:false AND statistics_type:view AND bundleName:ORIGINAL',
|
||||||
'facet':True,
|
'facet':True,
|
||||||
'facet.field':'owningItem',
|
'facet.field':'owningItem',
|
||||||
|
'facet.mincount':1,
|
||||||
'facet.limit':results_per_page,
|
'facet.limit':results_per_page,
|
||||||
'facet.offset':results_current_page * results_per_page
|
'facet.offset':results_current_page * results_per_page
|
||||||
})
|
}, rows=0)
|
||||||
|
|
||||||
# make sure total number of results > 0
|
|
||||||
if res.get_num_found() > 0:
|
|
||||||
# SolrClient's get_facets() returns a dict of dicts
|
# SolrClient's get_facets() returns a dict of dicts
|
||||||
downloads = res.get_facets()
|
downloads = res.get_facets()
|
||||||
# in this case iterate over the 'owningItem' dict and get the item ids and downloads
|
# in this case iterate over the 'owningItem' dict and get the item ids and downloads
|
||||||
|
Reference in New Issue
Block a user