Skip to content

Commit

Permalink
merged develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nwoodward committed Aug 12, 2024
2 parents 64b6eed + 4e067ed commit ba5af87
Show file tree
Hide file tree
Showing 28 changed files with 1,953 additions and 1,407 deletions.
28 changes: 0 additions & 28 deletions Pipfile

This file was deleted.

436 changes: 0 additions & 436 deletions Pipfile.lock

This file was deleted.

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# dspace-reports

A python3-based tool to generate and email statistical reports for [DSpace](https://github.com/DSpace/DSpace) repository administrators.
A tool written in Python to generate and email statistical reports for [DSpace 7+](https://github.com/DSpace/DSpace) repository administrators.

## Requirements

- Python 3.9+
- PostgreSQL 9.6+
- DSpace 6.x repository **
- PostgreSQL 13+
- DSpace 7.x or 8.x repository **

** If your Solr index contains statistics from legacy DSpace 5.x or earlier instances, then the quality of the reports will go up significantly if you have migrated the old statistics to the new UUID identifiers in DSpace 6. See the [DSpace Documentation](https://wiki.lyrasis.org/display/DSDOC6x/SOLR+Statistics+Maintenance#SOLRStatisticsMaintenance-UpgradeLegacyDSpaceObjectIdentifiers(pre-6xstatistics)toDSpace6xUUIDIdentifiers) for more information

Expand All @@ -15,8 +15,8 @@ A python3-based tool to generate and email statistical reports for [DSpace](http
```bash
python3 -m venv venv
source venv/bin/activate
pip install pipenv
pipenv install
pip install -r requirements.txt

```

## Configuration
Expand Down
79 changes: 50 additions & 29 deletions database_manager.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import sys
"""Class for managing database functions"""

from optparse import OptionParser
import argparse
import sys

from lib.database import Database
from lib.util import Utilities


class DatabaseManager():
"""Class for managing database functions"""
repository_column_map = {
'repository_id': 'Repository ID',
'repository_name': 'Repository',
'items_last_month': 'Items added last month',
'items_academic_year': 'Items added in academic year',
'items_total': 'Total Items',
'views_last_month': 'Item views last month',
'views_academic_year': 'Item views in academic year',
'views_academic_year': 'Item views in academic year',
'views_total': 'Total item views',
'downloads_last_month': 'Item downloads last month',
'downloads_academic_year': 'Item downloads in academic year',
Expand Down Expand Up @@ -53,7 +55,7 @@ class DatabaseManager():
'downloads_total': 'Total item downloads'
}

items_column_map = {
items_column_map = {
'item_id': 'Item ID',
'collection_name': 'Collection Name',
'item_name': 'Item Title',
Expand All @@ -74,6 +76,7 @@ def __init__(self, config=None):
self.config = config

def create_tables(self, config, logger):
"""Function to create statistics tables"""
logger.info('Creating tables...')

# Create statistics tables
Expand Down Expand Up @@ -152,14 +155,17 @@ def create_tables(self, config, logger):
# Commit changes
db.commit()

logger.info('Finished creating tables.')

def drop_tables(self, config, logger):
"""Function to drop statistics tables"""
# First check that tables exist
tables_exist = self.check_tables(config, logger)
if tables_exist == False:
if tables_exist is False:
logger.info('Tables do not exist.')
return
else:
logger.info('Removing tables...')

logger.info('Dropping tables...')

# Drop statistics tables
with Database(config=config['statistics_db']) as db:
Expand All @@ -186,32 +192,39 @@ def drop_tables(self, config, logger):
# Commit changes
db.commit()

logger.info('Finished dropping tables.')

def check_tables(self, config, logger):
"""Function to check if statistics tables exist"""
logger.debug('Checking for statistics tables.')
tables_exist = False

# Check if statistics tables exist
with Database(config=config['statistics_db']) as db:
with db.cursor() as cursor:
cursor.execute("SELECT * FROM information_schema.tables WHERE table_name=%s", ('repository_stats',))
cursor.execute("SELECT * FROM information_schema.tables WHERE " +
"table_name='repository_stats'")
if bool(cursor.rowcount):
logger.debug('The repository_stats table exists.')
tables_exist = True
else:
logger.debug('The repository_stats table DOES NOT exist.')
cursor.execute("SELECT * FROM information_schema.tables WHERE table_name=%s", ('community_stats',))
cursor.execute("SELECT * FROM information_schema.tables WHERE " +
"table_name='community_stats'")
if bool(cursor.rowcount):
logger.debug('The community_stats table exists.')
tables_exist = True
else:
logger.debug('The community_stats table DOES NOT exist.')
cursor.execute("SELECT * FROM information_schema.tables WHERE table_name=%s", ('collection_stats',))
cursor.execute("SELECT * FROM information_schema.tables WHERE " +
"table_name='collection_stats'")
if bool(cursor.rowcount):
logger.debug('The collection_stats table exists.')
tables_exist = True
else:
logger.debug('The collection_stats table DOES NOT exist.')
cursor.execute("SELECT * FROM information_schema.tables WHERE table_name=%s", ('item_stats',))
cursor.execute("SELECT * FROM information_schema.tables WHERE " +
"table_name='item_stats'")
if bool(cursor.rowcount):
logger.debug('The item_stats table exists.')
tables_exist = True
Expand All @@ -224,60 +237,68 @@ def check_tables(self, config, logger):


def main():
parser = OptionParser()
"""Main function"""

parser = argparse.ArgumentParser(
prog='Database Manager',
description='Commands to manage statistics database tables')

parser.add_option("-c", "--config", dest="config_file", default="config/application.yml", help="Configuration file")
parser.add_option("-f", "--function", dest="function", help="Database function to perform. Options: create, drop, check, recreate")
parser.add_argument("-c", "--config", dest="config_file", action='store', type=str,
default="config/application.yml", help="Configuration file")
parser.add_argument("-f", "--function", dest="function", action='store', type=str,
help="Database function to perform. Options: create, drop, check," +
" recreate.")

(options, args) = parser.parse_args()
args = parser.parse_args()

# Create utilities object
utilities = Utilities()

# Check required options fields
if options.function is None:
if args.function is None:
parser.print_help()
parser.error("Must specify a function to perform.")

if options.function not in ['create', 'drop', 'check', 'recreate']:
if args.function not in ['create', 'drop', 'check', 'recreate']:
parser.print_help()
parser.error("Must specify a valid function.")

# Load config
print("Loading configuration from file: %s" %(options.config_file))
config = utilities.load_config(options.config_file)
print("Loading configuration from file: %s", args.config_file)
config = utilities.load_config(args.config_file)
if not config:
print("Unable to load configuration.")
sys.exit(0)

# Set up logging
logger = utilities.load_logger(config=config)

# Create object to manage database
manage_database = DatabaseManager(config=config)

# Perform function from command line
if options.function == 'create':
if args.function == 'create':
tables_exist = manage_database.check_tables(config, logger)
if tables_exist == True:
logger.error('Unable to create statistics tables because one or more (check logs) already exists.')
if tables_exist is True:
logger.error("Unable to create statistics tables because one or more (check logs) " +
"already exists.")
sys.exit(0)
logger.info('Creating statistics tables in the database.')
manage_database.create_tables(config, logger)
elif options.function == 'drop':
elif args.function == 'drop':
logger.info('Dropping statistics tables')
manage_database.drop_tables(config, logger)
elif options.function == 'check':
elif args.function == 'check':
logger.info('Checking for statistics tables.')
tables_exist = manage_database.check_tables(config, logger)
if tables_exist == True:
if tables_exist is True:
logger.info('One or more statistics tables exists (check logs).')
sys.exit(0)
elif options.function == 'recreate':
elif args.function == 'recreate':
logger.info('Droping and recreating statistics tables in the database.')
manage_database.drop_tables(config, logger)
manage_database.create_tables(config, logger)


if __name__ == "__main__":
main()
main()
Loading

0 comments on commit ba5af87

Please sign in to comment.