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

Inf 420/use api for new release date comparison #124

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
SnapshotRelease,
SnapshotTelescope,
)
from observatory.platform.utils.release_utils import get_new_release_dates, get_datasets


class CrossrefFundrefRelease(SnapshotRelease):
Expand Down Expand Up @@ -292,24 +293,15 @@ def get_release_info(self, **kwargs) -> bool:
prev_execution_date = pendulum.instance(kwargs["prev_execution_date"])
execution_date = pendulum.instance(kwargs["execution_date"])
releases_list = list_releases(prev_execution_date, execution_date)
release_dates = [pendulum.parse(release["date"]) for release in releases_list]
api_datasets = get_datasets(workflow_id=self.workflow_id)
new_releases = get_new_release_dates(dataset_id=api_datasets[0].id, releases=release_dates)
ymd = lambda x: pendulum.parse(x["date"]).strftime("%Y%m%d")
releases_list_out = list(filter(lambda x: ymd(x) in new_releases, releases_list))

logging.info(f"Releases between prev ({prev_execution_date}) and current ({execution_date}) execution date:")
logging.info(releases_list)

# Check if the BigQuery table for each release already exists and only process release if the table
# doesn't exist
releases_list_out = []
for release in releases_list:
release_date = pendulum.parse(release["date"])
table_id = bigquery_sharded_table_id(CrossrefFundrefTelescope.DAG_ID, release_date)
logging.info("Checking if bigquery table already exists:")
if bigquery_table_exists(project_id, self.dataset_id, table_id):
logging.info(
f"Skipping as table exists for {release['url']}: " f"{project_id}.{self.dataset_id}.{table_id}"
)
else:
logging.info(f"Table does not exist yet, processing {release['url']} in this workflow")
releases_list_out.append(release)

# If releases_list_out contains items then the DAG will continue (return True) otherwise it will
# stop (return False)
continue_dag = len(releases_list_out) > 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
SnapshotRelease,
SnapshotTelescope,
)
from observatory.platform.utils.release_utils import get_new_release_dates, get_datasets


class OpenCitationsRelease(SnapshotRelease):
Expand Down Expand Up @@ -186,24 +187,6 @@ def _list_releases(

return releases

def _process_release(self, release: Dict[str, str]) -> bool:
"""Indicates whether we should process this release. If there are no files, or if the BigQuery table exists, we will not process this release.

:param release: Release to consider.
:return: Whether to process the release.
"""

if len(release["files"]) == 0:
return False

project_id = Variable.get(AirflowVars.PROJECT_ID)
table_id = bigquery_sharded_table_id(self.dag_id, pendulum.parse(release["date"]))

if bigquery_table_exists(project_id, self.dataset_id, table_id):
return False

return True

def get_release_info(self, **kwargs):
"""Calculate which releases require processing, and push the info to an XCom.

Expand All @@ -216,7 +199,11 @@ def get_release_info(self, **kwargs):
start_date = kwargs["execution_date"]
end_date = kwargs["next_execution_date"].subtract(microseconds=1)
releases = self._list_releases(start_date=start_date, end_date=end_date)
filtered_releases = list(filter(self._process_release, releases))
release_dates = [pendulum.parse(release["date"]) for release in releases]
api_datasets = get_datasets(workflow_id=self.workflow_id)
new_releases = get_new_release_dates(dataset_id=api_datasets[0].id, releases=release_dates)
ymd = lambda x: pendulum.parse(x["date"]).strftime("%Y%m%d")
filtered_releases = list(filter(lambda x: ymd(x) in new_releases, releases))

continue_dag = len(filtered_releases) > 0
if continue_dag:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,72 +143,49 @@ def test_list_releases_skip(self, m_get_response):
releases = telescope._list_releases(start_date=start_date, end_date=end_date)
self.assertEqual(len(releases), 0)

@patch("academic_observatory_workflows.workflows.open_citations_telescope.bigquery_table_exists")
@patch("academic_observatory_workflows.workflows.open_citations_telescope.bigquery_sharded_table_id")
@patch("academic_observatory_workflows.workflows.open_citations_telescope.Variable.get")
def test_process_release_no_files(self, m_get, m_bq_table_id, m_bq_table_exists):
m_get.return_value = "project_id"
m_bq_table_id.return_value = "1"
m_bq_table_exists.return_value = False
telescope = OpenCitationsTelescope()
@patch("academic_observatory_workflows.workflows.open_citations_telescope.OpenCitationsTelescope._list_releases")
def test_get_release_info_continue(self, m_list_releases):
releases = [
{"files": [], "date": "20210101"},
{"files": [1], "date": "20210101"},
{"files": [2], "date": "20210101"},
{"date": "20200101", "files": "file1.txt"},
{"date": "20200102", "files": "file2.txt"},
{"date": "20200103", "files": "file3.txt"},
]
m_list_releases.return_value = releases
env = ObservatoryEnvironment(self.project_id, self.data_location, api_host=self.host, api_port=self.port)

filtered_releases = list(filter(telescope._process_release, releases))
self.assertEqual(len(filtered_releases), 2)

@patch("academic_observatory_workflows.workflows.open_citations_telescope.bigquery_table_exists")
@patch("academic_observatory_workflows.workflows.open_citations_telescope.bigquery_sharded_table_id")
@patch("academic_observatory_workflows.workflows.open_citations_telescope.Variable.get")
def test_process_release_table_exists(self, m_get, m_bq_table_id, m_bq_table_exists):
m_get.return_value = "project_id"
m_bq_table_id.return_value = "1"
m_bq_table_exists.side_effect = [False, True, False]

telescope = OpenCitationsTelescope()
releases = [
{"files": [0], "date": "20210101"},
{"files": [1], "date": "20210101"},
{"files": [2], "date": "20210101"},
]
with env.create():
self.setup_connections(env)
self.setup_api()

filtered_releases = list(filter(telescope._process_release, releases))
self.assertEqual(len(filtered_releases), 2)
telescope = OpenCitationsTelescope(workflow_id=1)
execution_date = pendulum.datetime(2021, 1, 1)
next_execution_date = pendulum.datetime(2021, 1, 8)
ti = MagicMock()
continue_dag = telescope.get_release_info(
execution_date=execution_date, next_execution_date=next_execution_date, ti=ti
)
self.assertTrue(continue_dag)
self.assertEqual(len(ti.method_calls), 1)

@patch("academic_observatory_workflows.workflows.open_citations_telescope.OpenCitationsTelescope._process_release")
@patch("academic_observatory_workflows.workflows.open_citations_telescope.OpenCitationsTelescope._list_releases")
def test_get_release_info_continue(self, m_list_releases, m_process_release):
m_list_releases.return_value = [1, 2, 3]
m_process_release.return_value = True
def test_get_release_info_skip(self, m_list_releases):
m_list_releases.return_value = []

telescope = OpenCitationsTelescope()
execution_date = pendulum.datetime(2021, 1, 1)
next_execution_date = pendulum.datetime(2021, 1, 8)
ti = MagicMock()
continue_dag = telescope.get_release_info(
execution_date=execution_date, next_execution_date=next_execution_date, ti=ti
)
self.assertTrue(continue_dag)
self.assertEqual(len(ti.method_calls), 1)
env = ObservatoryEnvironment(self.project_id, self.data_location, api_host=self.host, api_port=self.port)

@patch("academic_observatory_workflows.workflows.open_citations_telescope.OpenCitationsTelescope._process_release")
@patch("academic_observatory_workflows.workflows.open_citations_telescope.OpenCitationsTelescope._list_releases")
def test_get_release_info_skip(self, m_list_releases, m_process_release):
m_list_releases.return_value = []
m_process_release.return_value = True
with env.create():
self.setup_connections(env)
self.setup_api()

telescope = OpenCitationsTelescope()
execution_date = pendulum.datetime(2021, 1, 1)
next_execution_date = pendulum.datetime(2021, 1, 8)
ti = MagicMock()
continue_dag = telescope.get_release_info(
execution_date=execution_date, next_execution_date=next_execution_date, ti=ti
)
self.assertFalse(continue_dag)
self.assertEqual(len(ti.method_calls), 0)
telescope = OpenCitationsTelescope(workflow_id=1)
execution_date = pendulum.datetime(2021, 1, 1)
next_execution_date = pendulum.datetime(2021, 1, 8)
ti = MagicMock()
continue_dag = telescope.get_release_info(
execution_date=execution_date, next_execution_date=next_execution_date, ti=ti
)
self.assertFalse(continue_dag)
self.assertEqual(len(ti.method_calls), 0)

def create_templates(self, *, host, port):
# list open citation releases
Expand Down