Skip to content

Commit

Permalink
Merge pull request #26 from rafaelpezzuto/add-report-gr-j1
Browse files Browse the repository at this point in the history
Add report gr j1
  • Loading branch information
rafaelpezzuto authored Feb 1, 2022
2 parents a4b0dcd + a7d5e17 commit 5b65186
Show file tree
Hide file tree
Showing 7 changed files with 595 additions and 9 deletions.
159 changes: 159 additions & 0 deletions api/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def json_report_wrapper(report_id, result_query, params, exceptions):
if report_id == 'lr_j1':
return _json_lr_j1(result_query, params, exceptions)

if report_id == 'gr_j1':
return _json_gr_j1(result_query, params, exceptions)


def mount_json_for_reports(result_query):
json_results = []
Expand Down Expand Up @@ -471,6 +474,95 @@ def _json_cr_j1(result_query_reports_cr_j1, params, exceptions):
return json_results


def _json_gr_j1(result_query_reports_gr_j1, params, exceptions):
begin_date, ed_discard = cleaner.get_start_and_last_days(params.get('begin_date', ''))
bd_discard, end_date = cleaner.get_start_and_last_days(params.get('end_date', ''))

json_results = {
"Report_Header": {
"Created": datetime.now().isoformat(),
"Created_By": "Scientific Electronic Library Online SUSHI API",
"Customer_ID": params.get('customer', ''),
"Report_ID": params.get('report_db_params', {}).report_id,
"Release": params.get('report_db_params', {}).release,
"Report_Name": params.get('report_db_params', {}).name,
"Institution_Name": params.get('institution_name', ''),
"Institution_ID": [{
"Type": "ISNI",
"Value": params.get('institution_id', '')
}],
},
"Report_Filters": [{
"Name": "Begin_Date",
"Value": str(begin_date),
}, {
"Name": "End_Date",
"Value": str(end_date),
}],
"Report_Attributes": [{
"Name": "Attributes_To_Show",
"Value": "Data_Type|Access_Method"
}],
"Exceptions": exceptions,
"Report_Items": []
}

report_items = {}

for r in result_query_reports_gr_j1:
key = (r.journalID, r.countryCode)
if key not in report_items:
report_items[key] = {
'Title': r.title,
'Item_ID': [],
'Platform': params.get('platform', ''),
'Publisher': r.publisherName,
'Publisher_ID': [],
'Access_Country_Code_': r.countryCode,
'Data_Type': 'Journal',
'Section_Type': 'Article',
'Access_Type': 'Open Access',
'Access_Method': 'Regular',
'Performance': []}

if r.printISSN:
report_items[key]['Item_ID'].append({
"Type": 'Print_ISSN',
"Value": r.printISSN
})

if r.onlineISSN:
report_items[key]['Item_ID'].append({
"Type": 'Online_ISSN',
"Value": r.onlineISSN
})

if params['granularity'] == 'monthly':
begin_date, end_date = cleaner.get_start_and_last_days(r.yearMonth)

elif params['granularity'] == 'totals':
begin_date, ed_discard = cleaner.get_start_and_last_days(r.beginDate)
bd_discard, end_date = cleaner.get_start_and_last_days(r.endDate)

for m in ['Total_Item_Requests', 'Unique_Item_Requests']:
metric_name = m[0].lower() + m[1:].replace('_', '')

performance_m = {
'Period': {
'Begin_Date': str(begin_date),
'End_Date': str(end_date)
},
'Instance': {
'Metric_Type': m,
'Count': str(getattr(r, metric_name))
}
}
report_items[key]['Performance'].append(performance_m)

json_results['Report_Items'] = [ri for ri in report_items.values() if ri['Title']]
return json_results


def tsv_report_wrapper(request, report_id, result_query, params, exceptions):
filename = '_'.join(['report', report_id]) + '.tsv'
request.response.content_disposition = 'attachment;filename=' + filename
Expand All @@ -490,6 +582,9 @@ def tsv_report_wrapper(request, report_id, result_query, params, exceptions):
if report_id == 'lr_j1':
return _tsv_report_lr_j1(result_query, params, exceptions)

if report_id == 'gr_j1':
return _tsv_report_gr_j1(result_query, params, exceptions)


def _tsv_header(params, exceptions, data_type='Journal'):
headers = {}
Expand Down Expand Up @@ -802,3 +897,67 @@ def _tsv_report_lr_j1(result_query, params, exceptions):
output['rows'].append(line)

return output


def _tsv_report_gr_j1(result_query, params, exceptions):
begin_date, bd_discard = cleaner.get_start_and_last_days(params['begin_date'])
ed_discard, end_date = cleaner.get_start_and_last_days(params['end_date'])

params['begin_date'] = begin_date
params['end_date'] = end_date

result = {'headers': _tsv_header(params, exceptions)}

journal2values = {}
yms = ['Reporting_Period_Total']

for ri in result_query:
journal_key = (ri.journalID, ri.title, ri.publisherName, ri.printISSN, ri.onlineISSN, ri.uri, ri.countryCode)

tir = getattr(ri, 'totalItemRequests')
uir = getattr(ri, 'uniqueItemRequests')

if journal_key not in journal2values:
journal2values[journal_key] = {'Reporting_Period_Total': (0, 0)}

if params['granularity'] == 'monthly':
year_month = cleaner.handle_str_date(ri.yearMonth, str_format=False).strftime('%b-%Y')
if year_month not in yms:
yms.append(year_month)

if year_month not in journal2values[journal_key]:
journal2values[journal_key][year_month] = 0

journal2values[journal_key][year_month] = (tir, uir)

journal2values[journal_key]['Reporting_Period_Total'] = tuple(map(sum, zip(journal2values[journal_key]['Reporting_Period_Total'], (tir, uir))))

output = {'rows': []}
for k in values.TSV_REPORT_DEFAULT_HEADERS:
output['rows'].append([k, result['headers'][k]])

output['rows'].append(values.TSV_REPORT_GR_J1_ROWS + yms)

for i in journal2values:
for j, metric_name in enumerate(['Total_Item_Requests', 'Unique_Item_Requests']):
line = [
i[1],
i[2],
'',
'SciELO SUSHI API',
'',
'',
i[3],
i[4],
i[5],
i[6],
metric_name
]

for ym in yms:
ym_v = str(journal2values[i].get(ym, (0, 0))[j])
line.append(ym_v)

output['rows'].append(line)

return output
4 changes: 2 additions & 2 deletions api/initialize_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
setup_logging,
)

from models.sql_declarative import Base, Report, Status, Alert
from api.models.sql_declarative import Base, Report, Status, Alert


def usage(argv):
Expand Down 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(), 'api/static/counter_report.json')
file_counter_report = os.path.join(os.getcwd(), 'api/static/json/counter_report.json')
with transaction.manager:
with open(file_counter_report) as f:
application_url = settings.get('application.url')
Expand Down
2 changes: 1 addition & 1 deletion api/libs/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_dates_not_ready(begin_date, end_date, collection, report_id):
else:
return []

elif report_id in ('lr_j1',):
elif report_id in ('gr_j1', 'lr_j1',):
if status_column:
not_read_dates = DBSession.query(AggrStatus).filter(and_(AggrStatus.collection == collection,
getattr(AggrStatus, status_column) == 0,
Expand Down
Loading

0 comments on commit 5b65186

Please sign in to comment.