-
Notifications
You must be signed in to change notification settings - Fork 1
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
Library KPIs: items in the institutional repo #31
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
dags/common/models/library/library_cern_publication_records.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from sqlalchemy import Column, DateTime, Float, Integer, func | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class LibraryCernPublicationRecords(Base): | ||
__tablename__ = "library_cern_publication_records" | ||
|
||
year = Column(Integer, primary_key=True) | ||
publications_total_count = Column(Float) | ||
conference_proceedings_count = Column(Float) | ||
non_journal_proceedings_count = Column(Float) | ||
created_at = Column(DateTime, default=func.now()) | ||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) |
14 changes: 14 additions & 0 deletions
14
dags/common/models/library/library_new_items_in_the_institutional_repository.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from sqlalchemy import Column, DateTime, Float, Integer, func | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class LibraryNewItemsInTheInstitutionalRepository(Base): | ||
__tablename__ = "library_items_in_the_institutional_repository" | ||
|
||
year = Column(Integer, primary_key=True) | ||
inspire_arxiv_records = Column(Float) | ||
inspire_curators_records = Column(Float) | ||
created_at = Column(DateTime, default=func.now()) | ||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from sqlalchemy import Column, DateTime, Float, Integer, func | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class OAGoldenOpenAccess(Base): | ||
__tablename__ = "oa_golden_open_access" | ||
|
||
year = Column(Integer, primary_key=True) | ||
cern_read_and_publish = Column(Float) | ||
cern_individual_apcs = Column(Float) | ||
scoap3 = Column(Float) | ||
other = Column(Float) | ||
other_collective_models = Column(Float) | ||
created_at = Column(DateTime, default=func.now()) | ||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from sqlalchemy import Column, DateTime, Float, Integer, func | ||
from sqlalchemy.ext.declarative import declarative_base | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class OAOpenAccess(Base): | ||
__tablename__ = "oa_open_access" | ||
|
||
year = Column(Integer, primary_key=True) | ||
closed_access = Column(Float) | ||
bronze_open_access = Column(Float) | ||
green_open_access = Column(Float) | ||
gold_open_access = Column(Float) | ||
created_at = Column(DateTime, default=func.now()) | ||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from airflow.decorators import task | ||
from airflow.hooks.postgres_hook import PostgresHook | ||
from executor_config import kubernetes_executor_config | ||
from sqlalchemy.exc import SQLAlchemyError | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
|
||
def get_session(conn_id: str): | ||
hook = PostgresHook(postgres_conn_id=conn_id) | ||
engine = hook.get_sqlalchemy_engine() | ||
return sessionmaker(bind=engine)() | ||
|
||
|
||
def sqlalchemy_task(conn_id: str): | ||
def decorator(func): | ||
@task(executor_config=kubernetes_executor_config) | ||
def wrapper(*args, **kwargs): | ||
session = get_session(conn_id) | ||
try: | ||
result = func(*args, session=session, **kwargs) | ||
session.commit() | ||
return result | ||
except SQLAlchemyError as e: | ||
session.rollback() | ||
raise e | ||
finally: | ||
session.close() | ||
|
||
return wrapper | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
INSPIRE_ARXIV_RECORDS = ( | ||
r"037%3A%27arXiv%27+and+not+980%3Ahidden&action_search=" | ||
+ r"Search&op1=a&m1=a&p1=&f1=&c=CERN+Document+" | ||
+ r"Server&sf=&so=d&rm=&rg=10&sc=1&of=xm&wl=0" | ||
) | ||
|
||
INSPIRE_CURATORS_RECORDS = ( | ||
r"035%3A%27oai%3Ainspirehep.net%27+not+" | ||
+ r"037%3A%27arXiv%27&action_search=Search" | ||
+ r"&op1=a&m1=a&p1=&f1=&c=CERN+Document+Server&sf=&so=d&" | ||
+ r"rm=&rg=10&sc=1&of=xm&wl=0" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from functools import reduce | ||
|
||
import library.constants as constants | ||
import pendulum | ||
from airflow.decorators import dag, task | ||
from airflow.providers.http.hooks.http import HttpHook | ||
from common.models.library.library_new_items_in_the_institutional_repository import ( | ||
LibraryNewItemsInTheInstitutionalRepository, | ||
) | ||
from common.operators.sqlalchemy_operator import sqlalchemy_task | ||
from common.utils import get_total_results_count | ||
from executor_config import kubernetes_executor_config | ||
from sqlalchemy.sql import func | ||
from tenacity import retry_if_exception_type, stop_after_attempt | ||
|
||
|
||
@dag( | ||
start_date=pendulum.today("UTC").add(days=-1), | ||
schedule_interval="@monthly", | ||
params={"year": 2023}, | ||
) | ||
def library_new_items_in_the_institutional_repository(): | ||
@task(executor_config=kubernetes_executor_config, multiple_outputs=True) | ||
def generate_params(query, **kwargs): | ||
year = kwargs["params"].get("year") | ||
base_query = f"search?ln=en&p=year%3A{year}+" | ||
type_of_query = [*query][0] | ||
drjova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { | ||
"endpoint": base_query + query[type_of_query], | ||
"type_of_query": type_of_query, | ||
} | ||
|
||
@task(executor_config=kubernetes_executor_config) | ||
def fetch_count(parameters): | ||
http_hook = HttpHook(http_conn_id="cds", method="GET") | ||
response = http_hook.run_with_advanced_retry( | ||
endpoint=parameters["endpoint"], | ||
_retry_args={ | ||
"stop": stop_after_attempt(3), | ||
"retry": retry_if_exception_type(Exception), | ||
}, | ||
) | ||
count = get_total_results_count(response.text) | ||
return {parameters["type_of_query"]: count} | ||
|
||
query_list = [ | ||
{"inspire_arxiv_records": constants.INSPIRE_ARXIV_RECORDS}, | ||
{"inspire_curators_records": constants.INSPIRE_CURATORS_RECORDS}, | ||
] | ||
|
||
parameters = generate_params.expand(query=query_list) | ||
drjova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
counts = fetch_count.expand(parameters=parameters) | ||
|
||
@task(multiple_outputs=True, executor_config=kubernetes_executor_config) | ||
def join_and_add_year(counts, **kwargs): | ||
year = kwargs["params"].get("year") | ||
results = reduce(lambda a, b: {**a, **b}, counts) | ||
results["year"] = year | ||
return results | ||
|
||
results = join_and_add_year(counts) | ||
|
||
@sqlalchemy_task(conn_id="superset_qa") | ||
def populate_new_items_in_the_institutional_repository(results, session, **kwargs): | ||
record = ( | ||
session.query(LibraryNewItemsInTheInstitutionalRepository) | ||
.filter_by(year=results["year"]) | ||
.first() | ||
) | ||
if record: | ||
record.inspire_arxiv_records = results["inspire_arxiv_records"] | ||
record.inspire_curators_records = results["inspire_curators_records"] | ||
record.updated_at = func.now() | ||
else: | ||
new_record = LibraryNewItemsInTheInstitutionalRepository( | ||
year=results["year"], | ||
inspire_arxiv_records=results["inspire_arxiv_records"], | ||
inspire_curators_records=results["inspire_curators_records"], | ||
) | ||
session.add(new_record) | ||
|
||
populate_new_items_in_the_institutional_repository(results) | ||
|
||
|
||
Library_new_items_in_the_institutional_repository = ( | ||
library_new_items_in_the_institutional_repository() | ||
) |
32 changes: 32 additions & 0 deletions
32
dags/migrations/versions/101f23913167_library_kpis_new_items_in_the_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Library KPIs: New items in the institutional repository | ||
|
||
Revision ID: 101f23913167 | ||
Revises: 50d9b3ef5a3b | ||
Create Date: 2024-07-05 17:55:10.676310 | ||
|
||
""" | ||
from typing import Sequence, Union | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "101f23913167" | ||
down_revision: Union[str, None] = "50d9b3ef5a3b" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"library_items_in_the_institutional_repository", | ||
sa.Column("year", sa.Integer, primary_key=True), | ||
sa.Column("inspire_arxiv_records", sa.Integer, nullable=False), | ||
sa.Column("inspire_curators_records", sa.Integer, nullable=False), | ||
sa.Column("created_at", sa.TIMESTAMP(timezone=True), nullable=False), | ||
sa.Column("updated_at", sa.TIMESTAMP(timezone=True), nullable=False), | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table("library_items_in_the_institutional_repository") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,11 @@ | |
import pendulum | ||
from airflow.decorators import dag, task | ||
from airflow.providers.http.hooks.http import HttpHook | ||
from airflow.providers.postgres.operators.postgres import PostgresOperator | ||
from common.models.open_access.oa_golden_open_access import OAGoldenOpenAccess | ||
from common.operators.sqlalchemy_operator import sqlalchemy_task | ||
from common.utils import get_total_results_count | ||
from executor_config import kubernetes_executor_config | ||
from sqlalchemy.sql import func | ||
from tenacity import retry_if_exception_type, stop_after_attempt | ||
|
||
|
||
|
@@ -68,29 +70,30 @@ def join_and_add_year(counts, **kwargs): | |
|
||
results = join_and_add_year(counts) | ||
|
||
populate_golden_open_access = PostgresOperator( | ||
task_id="populate_golden_open_access", | ||
postgres_conn_id="superset", | ||
sql=""" | ||
INSERT INTO oa_golden_open_access (year, cern_read_and_publish, cern_individual_apcs, | ||
scoap3, other, other_collective_models, created_at, updated_at) | ||
VALUES (%(year)s, %(cern_read_and_publish)s, %(cern_individual_apcs)s, | ||
%(scoap3)s, %(other)s, %(other_collective_models)s, | ||
CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) | ||
ON CONFLICT (year) | ||
DO UPDATE SET | ||
cern_read_and_publish = EXCLUDED.cern_read_and_publish, | ||
cern_individual_apcs = EXCLUDED.cern_individual_apcs, | ||
scoap3 = EXCLUDED.scoap3, | ||
other = EXCLUDED.other, | ||
other_collective_models = EXCLUDED.other_collective_models, | ||
updated_at = CURRENT_TIMESTAMP; | ||
""", | ||
parameters=results, | ||
executor_config=kubernetes_executor_config, | ||
) | ||
@sqlalchemy_task(conn_id="superset_qa") | ||
def populate_golden_open_access(results, session, **kwargs): | ||
record = ( | ||
session.query(OAGoldenOpenAccess).filter_by(year=results["year"]).first() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will fail if there is None, we have to do |
||
) | ||
if record: | ||
record.cern_read_and_publish = results["cern_read_and_publish"] | ||
record.cern_individual_apcs = results["cern_individual_apcs"] | ||
record.scoap3 = results["scoap3"] | ||
record.other = results["other"] | ||
record.other_collective_models = results["other_collective_models"] | ||
record.updated_at = func.now() | ||
else: | ||
new_record = OAGoldenOpenAccess( | ||
year=results["year"], | ||
cern_read_and_publish=results["cern_read_and_publish"], | ||
cern_individual_apcs=results["cern_individual_apcs"], | ||
scoap3=results["scoap3"], | ||
other=results["other"], | ||
other_collective_models=results["other_collective_models"], | ||
) | ||
session.add(new_record) | ||
|
||
counts >> results >> populate_golden_open_access | ||
populate_golden_open_access(results) | ||
|
||
|
||
OA_gold_open_access_mechanisms = oa_gold_open_access_mechanisms() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto