2021-03-22 12:42:42 +01:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-only
|
2018-09-23 15:47:48 +02:00
|
|
|
#
|
2018-09-24 08:18:50 +02:00
|
|
|
# indexer.py
|
|
|
|
#
|
2020-12-18 09:57:22 +01:00
|
|
|
# Connects to a DSpace Solr statistics core and ingests views and downloads for
|
|
|
|
# communities, collections, and items into a PostgreSQL database.
|
2018-09-24 08:18:50 +02:00
|
|
|
#
|
2020-09-25 12:35:05 +02:00
|
|
|
# This script is written for Python 3.6+ and requires several modules that you
|
2018-09-25 11:23:31 +02:00
|
|
|
# can install with pip (I recommend using a Python virtual environment):
|
2018-09-24 08:18:50 +02:00
|
|
|
#
|
2020-09-24 10:30:31 +02:00
|
|
|
# $ pip install psycopg2-binary
|
2018-09-24 08:18:50 +02:00
|
|
|
#
|
|
|
|
# See: https://wiki.duraspace.org/display/DSPACE/Solr
|
2018-09-23 15:47:48 +02:00
|
|
|
|
2020-12-27 11:22:07 +01:00
|
|
|
import math
|
|
|
|
|
2019-11-27 11:31:04 +01:00
|
|
|
import psycopg2.extras
|
2019-01-22 07:39:36 +01:00
|
|
|
import requests
|
2018-09-23 15:47:48 +02:00
|
|
|
|
2019-11-27 11:31:04 +01:00
|
|
|
from .config import SOLR_SERVER
|
|
|
|
from .database import DatabaseManager
|
2020-09-24 11:03:12 +02:00
|
|
|
from .util import get_statistics_shards
|
2019-01-22 07:39:36 +01:00
|
|
|
|
|
|
|
|
2020-12-18 09:57:22 +01:00
|
|
|
def index_views(indexType: str, facetField: str):
|
2018-09-26 01:41:10 +02:00
|
|
|
# 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
|
2020-09-25 12:25:34 +02:00
|
|
|
# so we can get the countDistinct summary to calculate how many pages of
|
|
|
|
# results we have.
|
2018-09-26 01:41:10 +02:00
|
|
|
#
|
|
|
|
# see: https://lucene.apache.org/solr/guide/6_6/the-stats-component.html
|
2019-04-15 09:19:50 +02:00
|
|
|
solr_query_params = {
|
2021-01-05 11:30:27 +01:00
|
|
|
"q": f"type:2 AND {facetField}:/.{{36}}/",
|
2020-11-17 16:40:08 +01:00
|
|
|
"fq": "-isBot:true AND statistics_type:view",
|
2020-12-18 09:57:22 +01:00
|
|
|
"fl": facetField,
|
2019-11-27 11:30:06 +01:00
|
|
|
"facet": "true",
|
2020-12-18 09:57:22 +01:00
|
|
|
"facet.field": facetField,
|
2019-11-27 11:30:06 +01:00
|
|
|
"facet.mincount": 1,
|
|
|
|
"facet.limit": 1,
|
|
|
|
"facet.offset": 0,
|
|
|
|
"stats": "true",
|
2020-12-18 09:57:22 +01:00
|
|
|
"stats.field": facetField,
|
2019-11-27 11:30:06 +01:00
|
|
|
"stats.calcdistinct": "true",
|
|
|
|
"shards": shards,
|
|
|
|
"rows": 0,
|
|
|
|
"wt": "json",
|
2019-04-15 09:19:50 +02:00
|
|
|
}
|
|
|
|
|
2019-11-27 11:30:06 +01:00
|
|
|
solr_url = SOLR_SERVER + "/statistics/select"
|
2019-04-15 09:19:50 +02:00
|
|
|
|
|
|
|
res = requests.get(solr_url, params=solr_query_params)
|
2018-09-23 15:47:48 +02:00
|
|
|
|
2019-01-22 08:00:22 +01:00
|
|
|
try:
|
|
|
|
# get total number of distinct facets (countDistinct)
|
2020-12-18 09:57:22 +01:00
|
|
|
results_totalNumFacets = res.json()["stats"]["stats_fields"][facetField][
|
2019-11-27 11:30:06 +01:00
|
|
|
"countDistinct"
|
|
|
|
]
|
2019-01-22 08:00:22 +01:00
|
|
|
except TypeError:
|
2020-12-18 09:57:22 +01:00
|
|
|
print(f"{indexType}: no views, exiting.")
|
2019-01-22 08:00:22 +01:00
|
|
|
|
|
|
|
exit(0)
|
2018-09-26 01:41:10 +02:00
|
|
|
|
2020-12-27 11:22:07 +01:00
|
|
|
# divide results into "pages" and round up to next integer
|
2018-09-23 15:47:48 +02:00
|
|
|
results_per_page = 100
|
2020-12-27 11:22:07 +01:00
|
|
|
results_num_pages = math.ceil(results_totalNumFacets / results_per_page)
|
2018-09-23 15:47:48 +02:00
|
|
|
results_current_page = 0
|
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
with DatabaseManager() as db:
|
|
|
|
with db.cursor() as cursor:
|
|
|
|
# create an empty list to store values for batch insertion
|
|
|
|
data = []
|
2018-09-24 23:49:47 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
while results_current_page <= results_num_pages:
|
2019-04-15 09:25:54 +02:00
|
|
|
# "pages" are zero based, but one based is more human readable
|
2019-11-27 11:30:06 +01:00
|
|
|
print(
|
2020-12-18 09:57:22 +01:00
|
|
|
f"{indexType}: indexing views (page {results_current_page + 1} of {results_num_pages + 1})"
|
2019-11-27 11:30:06 +01:00
|
|
|
)
|
2018-09-26 22:10:29 +02:00
|
|
|
|
2019-04-15 09:19:50 +02:00
|
|
|
solr_query_params = {
|
2021-01-05 11:30:27 +01:00
|
|
|
"q": f"type:2 AND {facetField}:/.{{36}}/",
|
2020-11-17 16:40:08 +01:00
|
|
|
"fq": "-isBot:true AND statistics_type:view",
|
2020-12-18 09:57:22 +01:00
|
|
|
"fl": facetField,
|
2019-11-27 11:30:06 +01:00
|
|
|
"facet": "true",
|
2020-12-18 09:57:22 +01:00
|
|
|
"facet.field": facetField,
|
2019-11-27 11:30:06 +01:00
|
|
|
"facet.mincount": 1,
|
|
|
|
"facet.limit": results_per_page,
|
|
|
|
"facet.offset": results_current_page * results_per_page,
|
|
|
|
"shards": shards,
|
|
|
|
"rows": 0,
|
|
|
|
"wt": "json",
|
|
|
|
"json.nl": "map", # return facets as a dict instead of a flat list
|
2019-04-15 09:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res = requests.get(solr_url, params=solr_query_params)
|
|
|
|
|
|
|
|
# Solr returns facets as a dict of dicts (see json.nl parameter)
|
2019-11-27 11:30:06 +01:00
|
|
|
views = res.json()["facet_counts"]["facet_fields"]
|
2020-12-18 09:57:22 +01:00
|
|
|
# iterate over the facetField dict and get the ids and views
|
|
|
|
for id_, views in views[facetField].items():
|
|
|
|
data.append((id_, views))
|
2018-09-26 01:41:10 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
# do a batch insert of values from the current "page" of results
|
2020-12-18 09:57:22 +01:00
|
|
|
sql = f"INSERT INTO {indexType}(id, views) VALUES %s ON CONFLICT(id) DO UPDATE SET views=excluded.views"
|
2019-11-27 11:30:06 +01:00
|
|
|
psycopg2.extras.execute_values(cursor, sql, data, template="(%s, %s)")
|
2018-11-07 16:41:21 +01:00
|
|
|
db.commit()
|
2018-09-23 15:47:48 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
# clear all items from the list so we can populate it with the next batch
|
|
|
|
data.clear()
|
2018-09-23 15:47:48 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
results_current_page += 1
|
2018-09-24 23:49:47 +02:00
|
|
|
|
2018-11-03 22:55:23 +01:00
|
|
|
|
2020-12-18 09:57:22 +01:00
|
|
|
def index_downloads(indexType: str, facetField: str):
|
2018-09-26 01:41:10 +02:00
|
|
|
# get the total number of distinct facets for items with at least 1 download
|
2019-11-27 11:12:05 +01:00
|
|
|
solr_query_params = {
|
2021-01-05 11:30:27 +01:00
|
|
|
"q": f"type:0 AND {facetField}:/.{{36}}/",
|
2020-11-17 16:40:08 +01:00
|
|
|
"fq": "-isBot:true AND statistics_type:view AND bundleName:ORIGINAL",
|
2020-12-18 09:57:22 +01:00
|
|
|
"fl": facetField,
|
2019-11-27 11:30:06 +01:00
|
|
|
"facet": "true",
|
2020-12-18 09:57:22 +01:00
|
|
|
"facet.field": facetField,
|
2019-11-27 11:30:06 +01:00
|
|
|
"facet.mincount": 1,
|
|
|
|
"facet.limit": 1,
|
|
|
|
"facet.offset": 0,
|
|
|
|
"stats": "true",
|
2020-12-18 09:57:22 +01:00
|
|
|
"stats.field": facetField,
|
2019-11-27 11:30:06 +01:00
|
|
|
"stats.calcdistinct": "true",
|
|
|
|
"shards": shards,
|
|
|
|
"rows": 0,
|
|
|
|
"wt": "json",
|
2019-04-15 09:19:50 +02:00
|
|
|
}
|
|
|
|
|
2019-11-27 11:30:06 +01:00
|
|
|
solr_url = SOLR_SERVER + "/statistics/select"
|
2019-04-15 09:19:50 +02:00
|
|
|
|
|
|
|
res = requests.get(solr_url, params=solr_query_params)
|
2018-09-23 15:47:48 +02:00
|
|
|
|
2019-01-22 08:00:22 +01:00
|
|
|
try:
|
|
|
|
# get total number of distinct facets (countDistinct)
|
2020-12-18 09:57:22 +01:00
|
|
|
results_totalNumFacets = res.json()["stats"]["stats_fields"][facetField][
|
2019-11-27 11:30:06 +01:00
|
|
|
"countDistinct"
|
|
|
|
]
|
2019-01-22 08:00:22 +01:00
|
|
|
except TypeError:
|
2020-12-18 09:57:22 +01:00
|
|
|
print(f"{indexType}: no downloads, exiting.")
|
2019-01-22 08:00:22 +01:00
|
|
|
|
|
|
|
exit(0)
|
2018-09-26 01:41:10 +02:00
|
|
|
|
2018-09-23 15:47:48 +02:00
|
|
|
results_per_page = 100
|
2020-12-27 11:22:07 +01:00
|
|
|
results_num_pages = math.ceil(results_totalNumFacets / results_per_page)
|
2018-09-23 15:47:48 +02:00
|
|
|
results_current_page = 0
|
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
with DatabaseManager() as db:
|
|
|
|
with db.cursor() as cursor:
|
|
|
|
# create an empty list to store values for batch insertion
|
|
|
|
data = []
|
2018-09-23 15:47:48 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
while results_current_page <= results_num_pages:
|
2019-04-15 09:25:54 +02:00
|
|
|
# "pages" are zero based, but one based is more human readable
|
2019-11-27 11:30:06 +01:00
|
|
|
print(
|
2020-12-18 09:57:22 +01:00
|
|
|
f"{indexType}: indexing downloads (page {results_current_page + 1} of {results_num_pages + 1})"
|
2019-11-27 11:30:06 +01:00
|
|
|
)
|
2018-09-26 01:41:10 +02:00
|
|
|
|
2019-04-15 09:19:50 +02:00
|
|
|
solr_query_params = {
|
2021-01-05 11:30:27 +01:00
|
|
|
"q": f"type:0 AND {facetField}:/.{{36}}/",
|
2020-11-17 16:40:08 +01:00
|
|
|
"fq": "-isBot:true AND statistics_type:view AND bundleName:ORIGINAL",
|
2020-12-18 09:57:22 +01:00
|
|
|
"fl": facetField,
|
2019-11-27 11:30:06 +01:00
|
|
|
"facet": "true",
|
2020-12-18 09:57:22 +01:00
|
|
|
"facet.field": facetField,
|
2019-11-27 11:30:06 +01:00
|
|
|
"facet.mincount": 1,
|
|
|
|
"facet.limit": results_per_page,
|
|
|
|
"facet.offset": results_current_page * results_per_page,
|
|
|
|
"shards": shards,
|
|
|
|
"rows": 0,
|
|
|
|
"wt": "json",
|
|
|
|
"json.nl": "map", # return facets as a dict instead of a flat list
|
2019-04-15 09:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res = requests.get(solr_url, params=solr_query_params)
|
|
|
|
|
|
|
|
# Solr returns facets as a dict of dicts (see json.nl parameter)
|
2019-11-27 11:30:06 +01:00
|
|
|
downloads = res.json()["facet_counts"]["facet_fields"]
|
2020-12-18 09:57:22 +01:00
|
|
|
# iterate over the facetField dict and get the item ids and downloads
|
|
|
|
for id_, downloads in downloads[facetField].items():
|
|
|
|
data.append((id_, downloads))
|
2018-09-23 15:47:48 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
# do a batch insert of values from the current "page" of results
|
2020-12-18 09:57:22 +01:00
|
|
|
sql = f"INSERT INTO {indexType}(id, downloads) VALUES %s ON CONFLICT(id) DO UPDATE SET downloads=excluded.downloads"
|
2019-11-27 11:30:06 +01:00
|
|
|
psycopg2.extras.execute_values(cursor, sql, data, template="(%s, %s)")
|
2018-11-07 16:41:21 +01:00
|
|
|
db.commit()
|
2018-09-26 22:10:29 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
# clear all items from the list so we can populate it with the next batch
|
|
|
|
data.clear()
|
2018-09-23 15:47:48 +02:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
results_current_page += 1
|
2018-09-24 23:49:47 +02:00
|
|
|
|
2018-11-03 22:55:23 +01:00
|
|
|
|
2018-11-07 16:41:21 +01:00
|
|
|
with DatabaseManager() as db:
|
|
|
|
with db.cursor() as cursor:
|
|
|
|
# create table to store item views and downloads
|
2019-11-27 11:30:06 +01:00
|
|
|
cursor.execute(
|
|
|
|
"""CREATE TABLE IF NOT EXISTS items
|
2020-03-01 14:42:38 +01:00
|
|
|
(id UUID PRIMARY KEY, views INT DEFAULT 0, downloads INT DEFAULT 0)"""
|
2019-11-27 11:30:06 +01:00
|
|
|
)
|
2020-12-18 09:57:22 +01:00
|
|
|
# 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)"""
|
|
|
|
)
|
2018-11-07 16:41:21 +01:00
|
|
|
|
|
|
|
# commit the table creation before closing the database connection
|
|
|
|
db.commit()
|
|
|
|
|
2019-01-22 07:39:36 +01:00
|
|
|
shards = get_statistics_shards()
|
|
|
|
|
2020-12-18 09:57:22 +01:00
|
|
|
# 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")
|
2018-09-23 15:47:48 +02:00
|
|
|
|
|
|
|
# vim: set sw=4 ts=4 expandtab:
|