Skip to content

Commit

Permalink
Merge pull request #36 from rafaelpezzuto/add-report-ira4-v2
Browse files Browse the repository at this point in the history
Adiciona relatório IR_A4
  • Loading branch information
rafaelpezzuto authored Mar 24, 2022
2 parents c730944 + a603dc8 commit cb22df5
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 31 deletions.
177 changes: 177 additions & 0 deletions api/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def json_report_wrapper(report_id, result_query, params, exceptions):
if report_id == 'ir_a1':
return _json_ir_a1(result_query, params, exceptions)

if report_id == 'ir_a4':
return _json_ir_a4(result_query, params, exceptions)

if report_id == 'cr_j1':
return _json_cr_j1(result_query, params, exceptions)

Expand Down Expand Up @@ -611,6 +614,97 @@ def _json_ir_a1(result_query_reports_ir_a1, params, exceptions):
return json_results


def _json_ir_a4(result_query_reports_ir_a4, 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_ir_a4:
article_doi = getattr(r, 'articleDOI', '')
if article_doi not in report_items and article_doi != '':
report_items[article_doi] = {
'Item': article_doi,
'Publisher': r.journalPublisher,
'Publisher_ID': [],
'Platform': params.get('platform', ''),
'Authors': '',
'Publication_Date': r.articleYOP,
'Article_Version': '',
'DOI': article_doi,
'Proprietary_ID': '',
'Print_ISSN': '',
'Online_ISSN': '',
'URI': '',
'Parent_Title': r.journalTitle,
'Parent_DOI': '',
'Parent_Proprietary_ID': '',
'Parent_Print_ISSN': r.printISSN,
'Parent_Online_ISSN': r.onlineISSN,
'Parent_URI': r.journalURI,
'Parent_Data_Type': 'Journal',
'Item_ID': r.articlePID,
'Data_Type': 'Article',
'Access_Type': 'Open Access',
'Access_Method': 'Regular',
'Performance': []}

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[article_doi]['Performance'].append(performance_m)

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

return json_results


def _json_cr_j1(result_query_reports_cr_j1, params, exceptions):
json_results = {
"Report_Header": {
Expand Down Expand Up @@ -864,6 +958,9 @@ def tsv_report_wrapper(request, report_id, result_query, params, exceptions):
if report_id == 'ir_a1':
return _tsv_report_ir_a1(result_query, params, exceptions)

if report_id == 'ir_a4':
return _tsv_report_ir_a4(result_query, params, exceptions)

if report_id == 'cr_j1':
return _tsv_report_cr_j1(result_query, params, exceptions)

Expand Down Expand Up @@ -1111,6 +1208,86 @@ def _tsv_report_ir_a1(result_query, params, exceptions):
return output


def _tsv_report_ir_a4(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)}

article2values = {}
article2description = {}
yms = ['Reporting_Period_Total']

for ri in result_query:
if ri.articleDOI:
article_key = ri.articleDOI
article_description = (ri.printISSN, ri.onlineISSN, ri.journalTitle, ri.journalURI, ri.journalPublisher, ri.journalCollection, ri.articlePID, ri.articleYOP, ri.articleDOI)

article2description[article_key] = article_description

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

if article_key not in article2values:
article2values[article_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 article2values[article_key]:
article2values[article_key][year_month] = 0

article2values[article_key][year_month] = (tir, uir)

article2values[article_key]['Reporting_Period_Total'] = tuple(map(sum, zip(article2values[article_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_IR_A4_ROWS + yms)

for doi in article2values:
for j, metric_name in enumerate(['Total_Item_Requests', 'Unique_Item_Requests']):
line = [
doi,
article2description[doi][4],
'',
'SciELO SUSHI API',
'',
article2description[doi][7],
'',
article2description[doi][8],
'',
'',
'',
'',
article2description[doi][2],
'',
'',
'',
'',
article2description[doi][0],
article2description[doi][1],
article2description[doi][3],
'',
metric_name
]

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

output['rows'].append(line)

return output


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

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 ('gr_j1', 'lr_j1', 'gr_j4', 'lr_j4', 'lr_a1'):
elif report_id in ('gr_j1', 'lr_j1', 'gr_j4', 'lr_j4', 'lr_a1', 'ir_a4',):
if status_column:
not_read_dates = DBSession.query(AggrStatus).filter(and_(AggrStatus.collection == collection,
getattr(AggrStatus, status_column) == 0,
Expand Down
2 changes: 1 addition & 1 deletion api/libs/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def validate_parameters(params, expected_params_list=[]):
def validate_parameters_according_to_report(report_id, params):
validation_results = {'errors': []}

if report_id in ('ir_a1',):
if report_id in ('ir_a1', 'ir_a4',):
if 'issn' not in params and 'pid' not in params:
validation_results['errors'].append(errors.error_required_filter_missing('issn or pid'))

Expand Down
21 changes: 21 additions & 0 deletions api/models/sql_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,34 @@ class AggrStatus(Base):
collection = Column(VARCHAR(3), nullable=False, primary_key=True)
date = Column(DATE, nullable=False, primary_key=True)

status_aggr_article_journal_year_month_metric = Column(BOOLEAN, default=False)
status_aggr_article_language_year_month_metric = Column(BOOLEAN, default=False)
status_aggr_journal_language_year_month_metric = Column(BOOLEAN, default=False)
status_aggr_journal_geolocation_year_month_metric = Column(BOOLEAN, default=False)
status_aggr_journal_language_yop_year_month_metric = Column(BOOLEAN, default=False)
status_aggr_journal_geolocation_yop_year_month_metric = Column(BOOLEAN, default=False)


class AggrArticleJournalYearMonthMetric(Base):
__tablename__ = 'aggr_article_journal_year_month_metric'
__table_args__ = (UniqueConstraint('collection', 'year_month', 'article_id', 'journal_id', name='uni_col_art_jou_ymm_aajymm'),)
__table_args__ += (Index('idx_ymaj_id', 'year_month', 'article_id', 'journal_id'),)
__table_args__ += (Index('idx_yj_id', 'year_month', 'journal_id'),)
__table_args__ += (Index('idx_j_id', 'journal_id'),)

id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)

collection = Column(VARCHAR(3), nullable=False, primary_key=True)
article_id = Column(INTEGER(unsigned=True), ForeignKey('counter_article.id', name='idarticle_aajymm'))
journal_id = Column(INTEGER(unsigned=True), ForeignKey('counter_journal.id', name='idjournal_aajymm'))
year_month = Column(VARCHAR(7), nullable=False)

total_item_requests = Column(INTEGER, nullable=False)
total_item_investigations = Column(INTEGER, nullable=False)
unique_item_requests = Column(INTEGER, nullable=False)
unique_item_investigations = Column(INTEGER, nullable=False)


class AggrArticleLanguageYearMonthMetric(Base):
__tablename__ = 'aggr_article_language_year_month_metric'
__table_args__ = (UniqueConstraint('collection', 'year_month', 'article_id', 'language_id', name='uni_col_art_lan_ymm_aalymm'),)
Expand Down
8 changes: 4 additions & 4 deletions api/static/sql/procedures_v2_gr_j1.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DELIMITER $$
CREATE PROCEDURE V2_GR_J1_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), collection_extra varchar(3))
CREATE PROCEDURE V2_GR_J1_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), IN collection_extra varchar(3))
BEGIN
SELECT
ajgymm.journal_id as journalID,
Expand Down Expand Up @@ -35,7 +35,7 @@ END $$
DELIMITER ;

DELIMITER $$
CREATE PROCEDURE V2_GR_J1_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), collection_extra varchar(3))
CREATE PROCEDURE V2_GR_J1_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), IN collection_extra varchar(3))
BEGIN
SELECT
ajgymm.journal_id as journalID,
Expand Down Expand Up @@ -70,7 +70,7 @@ END $$
DELIMITER ;

DELIMITER $$
CREATE PROCEDURE V2_GR_J1_JOURNAL_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), collection_extra varchar(3))
CREATE PROCEDURE V2_GR_J1_JOURNAL_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), IN collection_extra varchar(3))
BEGIN
SELECT
ajgymm.journal_id as journalID,
Expand Down Expand Up @@ -109,7 +109,7 @@ DELIMITER ;


DELIMITER $$
CREATE PROCEDURE V2_GR_J1_JOURNAL_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), collection_extra varchar(3))
CREATE PROCEDURE V2_GR_J1_JOURNAL_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), IN collection_extra varchar(3))
BEGIN
SELECT
ajgymm.journal_id as journalID,
Expand Down
16 changes: 8 additions & 8 deletions api/static/sql/procedures_v2_gr_j4.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DELIMITER $$
CREATE PROCEDURE V2_GR_J4_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), collection_extra varchar(3))
CREATE PROCEDURE V2_GR_J4_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), IN collection_extra varchar(3))
BEGIN
SELECT
ajgyymm.journal_id as journalID,
Expand Down Expand Up @@ -38,7 +38,7 @@ END $$
DELIMITER ;

DELIMITER $$
CREATE PROCEDURE V2_GR_J4_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), collection_extra varchar(3))
CREATE PROCEDURE V2_GR_J4_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), IN collection_extra varchar(3))
BEGIN
SELECT
ajgyymm.journal_id as journalID,
Expand Down Expand Up @@ -76,7 +76,7 @@ END $$
DELIMITER ;

DELIMITER $$
CREATE PROCEDURE V2_GR_J4_JOURNAL_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), collection_extra varchar(3))
CREATE PROCEDURE V2_GR_J4_JOURNAL_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), IN collection_extra varchar(3))
BEGIN
SELECT
ajgyymm.journal_id as journalID,
Expand Down Expand Up @@ -117,7 +117,7 @@ END $$
DELIMITER ;

DELIMITER $$
CREATE PROCEDURE V2_GR_J4_JOURNAL_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), collection_extra varchar(3))
CREATE PROCEDURE V2_GR_J4_JOURNAL_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), IN collection_extra varchar(3))
BEGIN
SELECT
ajgyymm.journal_id as journalID,
Expand Down Expand Up @@ -157,7 +157,7 @@ END $$
DELIMITER ;

DELIMITER $$
CREATE PROCEDURE V2_GR_J4_YOP_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), collection_extra varchar(3), yop varchar(4))
CREATE PROCEDURE V2_GR_J4_YOP_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), IN collection_extra varchar(3), IN yop varchar(4))
BEGIN
SELECT
ajgyymm.journal_id as journalID,
Expand Down Expand Up @@ -197,7 +197,7 @@ END $$
DELIMITER ;

DELIMITER $$
CREATE PROCEDURE V2_GR_J4_YOP_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), collection_extra varchar(3), yop varchar(4))
CREATE PROCEDURE V2_GR_J4_YOP_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN collection varchar(3), IN collection_extra varchar(3), IN yop varchar(4))
BEGIN
SELECT
ajgyymm.journal_id as journalID,
Expand Down Expand Up @@ -236,7 +236,7 @@ END $$
DELIMITER ;

DELIMITER $$
CREATE PROCEDURE V2_GR_J4_JOURNAL_YOP_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), collection_extra varchar(3), yop varchar(4))
CREATE PROCEDURE V2_GR_J4_JOURNAL_YOP_MONTHLY(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), IN collection_extra varchar(3), IN yop varchar(4))
BEGIN
SELECT
ajgyymm.journal_id as journalID,
Expand Down Expand Up @@ -279,7 +279,7 @@ DELIMITER ;


DELIMITER $$
CREATE PROCEDURE V2_GR_J4_JOURNAL_YOP_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), collection_extra varchar(3), yop varchar(4))
CREATE PROCEDURE V2_GR_J4_JOURNAL_YOP_TOTALS(IN beginDate varchar(7), IN endDate varchar(7), IN issn varchar(9), IN collection varchar(3), IN collection_extra varchar(3), IN yop varchar(4))
BEGIN
SELECT
ajgyymm.journal_id as journalID,
Expand Down
Loading

0 comments on commit cb22df5

Please sign in to comment.