Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adiciona relatório no nível de coleção #21

Merged
merged 11 commits into from
Jan 5, 2022
Merged
66 changes: 66 additions & 0 deletions api/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions api/db_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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': {
Expand All @@ -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']),
}
}
}
2 changes: 1 addition & 1 deletion api/initialize_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 7 additions & 0 deletions api/static/json/counter_report.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
37 changes: 37 additions & 0 deletions api/static/sql/procedures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
8 changes: 8 additions & 0 deletions api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
35 changes: 33 additions & 2 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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':
Expand All @@ -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)
Expand All @@ -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 = []

Expand Down