-
Notifications
You must be signed in to change notification settings - Fork 50
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
feat(airflow): add astro-cli ingestion #200
Merged
Merged
Changes from all commits
Commits
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
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,51 @@ | ||
import datetime | ||
import os | ||
|
||
from include.tasks import split | ||
from include.tasks.extract import astro_cli_docs | ||
from include.tasks.extract.utils.weaviate.ask_astro_weaviate_hook import AskAstroWeaviateHook | ||
|
||
from airflow.decorators import dag, task | ||
|
||
ask_astro_env = os.environ.get("`ASK_ASTRO_ENV", "dev") | ||
|
||
_WEAVIATE_CONN_ID = f"weaviate_{ask_astro_env}" | ||
WEAVIATE_CLASS = os.environ.get("WEAVIATE_CLASS", "DocsDev") | ||
ask_astro_weaviate_hook = AskAstroWeaviateHook(_WEAVIATE_CONN_ID) | ||
|
||
default_args = {"retries": 3, "retry_delay": 30} | ||
|
||
schedule_interval = "0 5 * * *" if ask_astro_env == "prod" else None | ||
|
||
|
||
@dag( | ||
schedule_interval=schedule_interval, | ||
start_date=datetime.datetime(2023, 9, 27), | ||
catchup=False, | ||
is_paused_upon_creation=True, | ||
default_args=default_args, | ||
) | ||
def ask_astro_load_astro_cli_docs(): | ||
""" | ||
This DAG performs incremental load for any new docs. Initial load via ask_astro_load_bulk imported | ||
data from a point-in-time data capture. By using the upsert logic of the weaviate_import decorator | ||
any existing documents that have been updated will be removed and re-added. | ||
""" | ||
|
||
extract_astro_cli_docs = task(astro_cli_docs.extract_astro_cli_docs)() | ||
split_md_docs = task(split.split_html).expand(dfs=[extract_astro_cli_docs]) | ||
|
||
_import_data = ( | ||
task(ask_astro_weaviate_hook.ingest_data, retries=10) | ||
.partial( | ||
class_name=WEAVIATE_CLASS, | ||
existing="upsert", | ||
doc_key="docLink", | ||
batch_params={"batch_size": 1000}, | ||
verbose=True, | ||
) | ||
.expand(dfs=[split_md_docs]) | ||
) | ||
|
||
|
||
ask_astro_load_astro_cli_docs() |
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,46 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
|
||
import pandas as pd | ||
import requests | ||
from bs4 import BeautifulSoup | ||
from weaviate.util import generate_uuid5 | ||
|
||
|
||
def extract_astro_cli_docs() -> list[pd.DataFrame]: | ||
""" | ||
This task downloads Blogs from the astro-cli documentation website and returns a list of pandas dataframes. | ||
Return type is a list in order to map to upstream dynamic tasks. | ||
|
||
The returned data includes the following fields: | ||
'docSource': 'apache/airflow/docs' | ||
'docLink': URL for the page | ||
'content': HTML content of the page | ||
'sha': A UUID from the other fields | ||
""" | ||
astronomer_base_url = "https://docs.astronomer.io" | ||
astro_cli_overview_endpoint = "/astro/cli/overview" | ||
|
||
response = requests.get(f"{astronomer_base_url}/{astro_cli_overview_endpoint}") | ||
soup = BeautifulSoup(response.text, "lxml") | ||
astro_cli_links = { | ||
f"{astronomer_base_url}{link.get('href')}" | ||
for link in soup.find_all("a") | ||
if link.get("href").startswith("/astro/cli") | ||
} | ||
|
||
df = pd.DataFrame(astro_cli_links, columns=["docLink"]) | ||
df["html_content"] = df["docLink"].apply(lambda x: requests.get(x).content) | ||
|
||
df["content"] = df["html_content"].apply(lambda x: str(BeautifulSoup(x, "html.parser").find("body"))) | ||
df["content"] = df["content"].apply(lambda x: re.sub("¶", "", x)) | ||
|
||
df["sha"] = df["content"].apply(generate_uuid5) | ||
df["docSource"] = "astronomer/docs/astro-cli" | ||
df.reset_index(drop=True, inplace=True) | ||
|
||
# column order matters for uuid generation | ||
df = df[["docSource", "sha", "content", "docLink"]] | ||
|
||
return [df] |
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.
Can you add the doc strings please?
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.
done