diff --git a/api/adapter.py b/api/adapter.py index f505dea..c94d0e1 100644 --- a/api/adapter.py +++ b/api/adapter.py @@ -8,6 +8,8 @@ def wrapper_mount_json_for_report(report_id, result_query, attrs): return mount_json_for_reports_tr_j1(result_query, attrs) if report_id == 'tr_j4': return mount_json_for_reports_tr_j4(result_query, attrs) + if report_id == 'cr_j1': + return mount_json_for_reports_cr_j1(result_query, attrs) def mount_json_for_reports(result_query): @@ -299,3 +301,67 @@ def mount_json_for_reports_ir_a1(result_query_reports_ir_a1, attrs): json_results['Report_Items'] = [ri for ri in report_items.values() if ri['Item']] return json_results + + +def mount_json_for_reports_cr_j1(result_query_reports_cr_j1, attrs): + json_results = { + "Report_Header": { + "Created": datetime.now().isoformat(), + "Created_By": "Scientific Electronic Library Online SUSHI API", + "Customer_ID": attrs.get('customer', ''), + "Report_ID": attrs.get('report_data', {}).report_id, + "Release": attrs.get('report_data', {}).release, + "Report_Name": attrs.get('report_data', {}).name, + "Institution_Name": attrs.get('institution_name', ''), + "Institution_ID": [{ + "Type": "ISNI", + "Value": attrs.get('institution_id', '') + }], + }, + "Report_Filters": [{ + "Name": "Begin_Date", + "Value": attrs.get('begin_date', '') + }, { + "Name": "End_Date", + "Value": attrs.get('end_date', '') + }], + "Report_Attributes": [{ + "Name": "Attributes_To_Show", + "Value": "Data_Type|Access_Method" + }], + "Exceptions": [], + "Report_Items": [] + } + + report_items = {} + + for r in result_query_reports_cr_j1: + r_collection_acronym = attrs.get('collection') + if r_collection_acronym not in report_items: + report_items[r_collection_acronym] = { + 'Title': r_collection_acronym, + 'Item_ID': [], + 'Platform': attrs.get('platform', ''), + 'Data_Type': 'Collection', + 'Section_Type': 'Journal', + 'Access_Type': 'Open Access', + 'Access_Method': 'Regular', + 'Performance': []} + + for m in ['Total_Item_Requests', 'Unique_Item_Requests']: + metric_name = m[0].lower() + m[1:].replace('_', '') + + performance_m = { + 'Period': { + 'Begin_Date': str(r.beginDate), + 'End_Date': str(r.endDate) + }, + 'Instance': { + 'Metric_Type': m, + 'Count': str(getattr(r, metric_name)) + } + } + report_items[r_collection_acronym]['Performance'].append(performance_m) + + json_results['Report_Items'] = [ri for ri in report_items.values() if ri['Title']] + return json_results diff --git a/api/db_calls.py b/api/db_calls.py index 4beaae1..b52e7b9 100644 --- a/api/db_calls.py +++ b/api/db_calls.py @@ -17,6 +17,10 @@ DB_CALL_V2_TR_J1_JOURNAL_MONTHLY = 'CALL V2_TR_J1_JOURNAL_MONTHLY("%s", "%s", "%s", "%s")' DB_CALL_V2_TR_J1_TOTALS = 'CALL V2_TR_J1_TOTALS("%s", "%s", "%s")' DB_CALL_V2_TR_J1_MONTHLY = 'CALL V2_TR_J1_MONTHLY("%s", "%s", "%s")' +DB_CALL_V2_CR_J1_TOTALS = 'CALL V2_CR_J1_TOTALS("%s", "%s", "%s", "%s")' +DB_CALL_V2_CR_J1_MONTHLY = 'CALL V2_CR_J1_MONTHLY("%s", "%s", "%s", "%s")' + + REPORT_ID_STATUS_COLUMN_DICT = { 'ir_a1': 'status_sushi_article_metric', 'tr_j1': 'status_sushi_journal_metric', @@ -67,6 +71,7 @@ }, 'global': { 'tr_j1': (DB_CALL_V2_TR_J1_TOTALS, ['begin_date', 'end_date', 'collection']), + 'cr_j1': (DB_CALL_V2_CR_J1_TOTALS, ['begin_date', 'end_date', 'collection', 'collection_extra']), } }, 'monthly': { @@ -75,6 +80,7 @@ }, 'global': { 'tr_j1': (DB_CALL_V2_TR_J1_MONTHLY, ['begin_date', 'end_date', 'collection']), + 'cr_j1': (DB_CALL_V2_CR_J1_MONTHLY, ['begin_date', 'end_date', 'collection', 'collection_extra']), } } } diff --git a/api/initialize_db.py b/api/initialize_db.py index 4398575..e2cf4b8 100644 --- a/api/initialize_db.py +++ b/api/initialize_db.py @@ -43,7 +43,7 @@ def main(argv=sys.argv): Base.metadata.create_all(engine) DBSession.configure(bind=engine) - file_counter_report = os.path.join(os.getcwd(), '/app/static/counter_report.json') + file_counter_report = os.path.join(os.getcwd(), 'app/static/counter_report.json') with transaction.manager: with open(file_counter_report) as f: application_url = settings.get('application.url') diff --git a/api/static/json/counter_report.json b/api/static/json/counter_report.json index 0b7ae81..cba4d32 100644 --- a/api/static/json/counter_report.json +++ b/api/static/json/counter_report.json @@ -14,6 +14,13 @@ "Report_Description": "Platform-level usage summarized by Metric_Type.", "Path": "/reports/pr_p1" }, + "cr_j1": { + "Report_Name": "Collection Usage", + "Report_ID": "cr_j1", + "Release": "5", + "Report_Description": "Collection-level usage summarized by Metric_Type.", + "Path": "/reports/cr_j1" + }, "dr": { "Report_Name": "Database Master Report", "Report_ID": "dr", diff --git a/api/static/sql/procedures.sql b/api/static/sql/procedures.sql index 75a9e2c..1544c4c 100644 --- a/api/static/sql/procedures.sql +++ b/api/static/sql/procedures.sql @@ -534,3 +534,40 @@ BEGIN WHERE cjc.collection = collection; END $$ DELIMITER ; + +DELIMITER $$ +CREATE PROCEDURE V2_CR_J1_TOTALS(IN beginDate date, IN endDate date, IN collection varchar(3), IN collection_extra varchar(3)) +BEGIN + SELECT + MIN(year_month_day) AS beginDate, + MAX(year_month_day) AS endDate, + SUM(total_item_requests) AS totalItemRequests, + SUM(unique_item_requests) AS uniqueItemRequests + FROM sushi_journal_metric sjm + JOIN counter_journal cj on sjm.idjournal_sjm = cj.id + WHERE + (sjm.collection in (collection , collection_extra)) AND + (year_month_day between beginDate AND endDate) AND + (cj.online_issn <> '' OR cj.print_issn <> ''); +END $$ +DELIMITER ; + +DELIMITER $$ +CREATE PROCEDURE V2_CR_J1_MONTHLY(IN beginDate date, IN endDate date, IN collection varchar(3), IN collection_extra varchar(3)) +BEGIN + SELECT + SUBSTR(sjm.year_month_day, 1, 7) AS yearMonth, + MIN(year_month_day) AS beginDate, + MAX(year_month_day) AS endDate, + SUM(total_item_requests) AS totalItemRequests, + SUM(unique_item_requests) AS uniqueItemRequests + FROM sushi_journal_metric sjm + JOIN counter_journal cj on sjm.idjournal_sjm = cj.id + WHERE + (sjm.collection in (collection, collection_extra)) AND + (year_month_day between beginDate AND endDate) AND + (online_issn <> '' OR print_issn <> '') + GROUP BY + yearMonth; +END $$ +DELIMITER ; diff --git a/api/utils.py b/api/utils.py index 5dde1c2..22f7ddd 100644 --- a/api/utils.py +++ b/api/utils.py @@ -42,3 +42,11 @@ def is_valid_date_format(date): if re.match(pattern=REGEX_DATE_FORMAT, string=date): return True return False + + +def clean_field(field): + els = [] + for c in field: + if c not in ('\"', '\'', '\n', '\t', ' '): + els.append(c) + return ''.join(els) diff --git a/api/views.py b/api/views.py index 505dc2a..bf6a440 100644 --- a/api/views.py +++ b/api/views.py @@ -15,7 +15,7 @@ from .lib.database import get_dates_not_ready from .models import DBSession from .sql_declarative import Status, Alert, Member, Report -from .utils import handle_str_date, is_valid_date_range, is_valid_issn, is_valid_date_format +from .utils import handle_str_date, is_valid_date_range, is_valid_issn, is_valid_date_format, clean_field from .values import collection_acronym_to_collection_name @@ -113,6 +113,8 @@ def reports_report_id(self): 'api': clean_field(api_version), } + _set_collection_extra(report_id, attrs) + try: report_data = DBSession.query(Report).filter_by(report_id=report_id).one() except NoResultFound or MultipleResultsFound: @@ -123,7 +125,7 @@ def reports_report_id(self): json_metrics = _wrapper_call_report(report_id, attrs) # Caso não existam dados de acesso para o período selecionado - if len(json_metrics.get('Report_Items', [])) == 0: + if is_empty_report(json_metrics.get('Report_Items', [])): return error_no_usage_available() # Obtém exceções @@ -150,6 +152,23 @@ def check_exceptions(params, begin_date, end_date, report_id, collection): return exceptions +def is_empty_report(report_items): + if len(report_items) == 0: + return True + + for item in report_items: + for performance in item.get('Performance', []): + try: + count = int(performance.get('Instance', {}).get('Count', 0)) + except ValueError: + count = 0 + + if count > 0: + return False + + return True + + def _wrapper_call_report(report_id, attrs): granularity, mode = _get_granularity_and_mode(attrs) if attrs['api'] == 'v2': @@ -164,6 +183,8 @@ def _wrapper_call_report(report_id, attrs): p_value = attrs.get(p) if p_value: params.append(p_value) + if not p_value and p == 'collection_extra': + params.append('') result_query_metrics = DBSession.execute(procedure_name % tuple(params)) return wrapper_mount_json_for_report(report_id, result_query_metrics, attrs) @@ -184,6 +205,16 @@ def _get_granularity_and_mode(attrs): return granularity, mode +def _set_collection_extra(report_id, attrs): + if report_id == 'cr_j1': + if attrs['collection'] == 'scl': + attrs.update({'collection_extra': 'nbr'}) + if attrs['collection'] == 'nbr': + attrs.update({'collection_extra': 'scl'}) + else: + attrs.update({'collection_extra': ''}) + + def _check_filters(params): invalid_filters = []