From 3f74982823e2e6aa43d8e56ad3e6b46046ea5eb0 Mon Sep 17 00:00:00 2001 From: Varshika Gupta Date: Tue, 24 Aug 2021 10:52:35 +0000 Subject: [PATCH 1/3] feat: Onboard COVID19-Italy dataset --- .../_images/run_csv_transform_kub/Dockerfile | 38 ++++ .../_images/run_csv_transform_kub/Pipfile | 13 ++ .../run_csv_transform_kub/csv_transform.py | 149 +++++++++++++++ .../run_csv_transform_kub/requirements.txt | 3 + .../_terraform/covid19_italy_dataset.tf | 26 +++ .../_terraform/data_by_province_pipeline.tf | 39 ++++ .../_terraform/data_by_region_pipeline.tf | 39 ++++ .../_terraform/national_trends_pipeline.tf | 39 ++++ datasets/covid19_italy/_terraform/provider.tf | 28 +++ .../covid19_italy/_terraform/variables.tf | 23 +++ .../data_by_province/data_by_province_dag.py | 83 +++++++++ .../data_by_province/pipeline.yaml | 149 +++++++++++++++ .../data_by_region/data_by_region_dag.py | 122 +++++++++++++ .../data_by_region/pipeline.yaml | 171 ++++++++++++++++++ datasets/covid19_italy/dataset.yaml | 58 ++++++ .../national_trends/national_trends_dag.py | 117 ++++++++++++ .../national_trends/pipeline.yaml | 161 +++++++++++++++++ 17 files changed, 1258 insertions(+) create mode 100644 datasets/covid19_italy/_images/run_csv_transform_kub/Dockerfile create mode 100644 datasets/covid19_italy/_images/run_csv_transform_kub/Pipfile create mode 100644 datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py create mode 100644 datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt create mode 100644 datasets/covid19_italy/_terraform/covid19_italy_dataset.tf create mode 100644 datasets/covid19_italy/_terraform/data_by_province_pipeline.tf create mode 100644 datasets/covid19_italy/_terraform/data_by_region_pipeline.tf create mode 100644 datasets/covid19_italy/_terraform/national_trends_pipeline.tf create mode 100644 datasets/covid19_italy/_terraform/provider.tf create mode 100644 datasets/covid19_italy/_terraform/variables.tf create mode 100644 datasets/covid19_italy/data_by_province/data_by_province_dag.py create mode 100644 datasets/covid19_italy/data_by_province/pipeline.yaml create mode 100644 datasets/covid19_italy/data_by_region/data_by_region_dag.py create mode 100644 datasets/covid19_italy/data_by_region/pipeline.yaml create mode 100644 datasets/covid19_italy/dataset.yaml create mode 100644 datasets/covid19_italy/national_trends/national_trends_dag.py create mode 100644 datasets/covid19_italy/national_trends/pipeline.yaml diff --git a/datasets/covid19_italy/_images/run_csv_transform_kub/Dockerfile b/datasets/covid19_italy/_images/run_csv_transform_kub/Dockerfile new file mode 100644 index 000000000..85af90570 --- /dev/null +++ b/datasets/covid19_italy/_images/run_csv_transform_kub/Dockerfile @@ -0,0 +1,38 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# The base image for this build +# FROM gcr.io/google.com/cloudsdktool/cloud-sdk:slim +FROM python:3.8 + +# Allow statements and log messages to appear in Cloud logs +ENV PYTHONUNBUFFERED True + +# Copy the requirements file into the image +COPY requirements.txt ./ + +# Install the packages specified in the requirements file +RUN python3 -m pip install --no-cache-dir -r requirements.txt + +# The WORKDIR instruction sets the working directory for any RUN, CMD, +# ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. +# If the WORKDIR doesn’t exist, it will be created even if it’s not used in +# any subsequent Dockerfile instruction +WORKDIR /custom + +# Copy the specific data processing script/s in the image under /custom/* +COPY ./csv_transform.py . + +# Command to run the data processing script when the container is run +CMD ["python3", "csv_transform.py"] diff --git a/datasets/covid19_italy/_images/run_csv_transform_kub/Pipfile b/datasets/covid19_italy/_images/run_csv_transform_kub/Pipfile new file mode 100644 index 000000000..37f9797d3 --- /dev/null +++ b/datasets/covid19_italy/_images/run_csv_transform_kub/Pipfile @@ -0,0 +1,13 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +requests = "*" +vaex = "*" + +[dev-packages] + +[requires] +python_version = "3.9" diff --git a/datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py b/datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py new file mode 100644 index 000000000..daab397ac --- /dev/null +++ b/datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py @@ -0,0 +1,149 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# CSV transform for: covid19_italy + +import datetime +import json +import logging +import os +import pathlib +import typing + +import pandas as pd +import requests +from google.cloud import storage + + +def main( + source_url: str, + source_file: pathlib.Path, + target_file: pathlib.Path, + target_gcs_bucket: str, + target_gcs_path: str, + headers: typing.List[str], + rename_mappings: dict, + pipeline_name: str, +): + + logging.info( + "Covid-19 Italy process started at " + + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + ) + + logging.info("creating 'files' folder") + pathlib.Path("./files").mkdir(parents=True, exist_ok=True) + + logging.info(f"Downloading file {source_url}") + download_file(source_url, source_file) + + # open the input file + logging.info(f"Opening file {source_file}") + + df = pd.read_csv(str(source_file)) + + # steps in the pipeline + logging.info(f"Transformation Process Starting.. {source_file}") + + # rename the headers + logging.info(f"Transform: Renaming Headers.. {source_file}") + rename_headers(df, rename_mappings) + + # create location_geom + logging.info(f"Transform: Creating Geometry Column.. {pipeline_name}") + if pipeline_name == "data_by_province" or pipeline_name == "data_by_region": + df["location_geom"] = ( + "POINT(" + + df["longitude"].astype(str).replace("nan", "") + + " " + + df["latitude"].astype(str).replace("nan", "") + + ")" + ) + # replace blank POINT( ) valye with blank + df.location_geom = df.location_geom.replace("POINT( )", "") + + # reorder headers in output + logging.info("Transform: Reordering headers..") + df = df[headers] + + # steps in the pipeline + logging.info(f"Transformation Process complete .. {source_file}") + + # save to output file + logging.info(f"Saving to output file.. {target_file}") + + try: + # save_to_new_file(df, file_path=str(target_file)) + save_to_new_file(df, file_path=str(target_file)) + except Exception as e: + logging.error(f"Error saving output file: {e}.") + + logging.info( + f"Uploading output file to.. gs://{target_gcs_bucket}/{target_gcs_path}" + ) + upload_file_to_gcs(target_file, target_gcs_bucket, target_gcs_path) + + # log completion + logging.info( + "Covid-19 Italy process completed at " + + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + ) + + +def convert_dt_format(date_str, time_str): + # date_str, time_str + # 10/26/2014,13:12:00 + return str(datetime.datetime.strptime(date_str, "%m/%d/%Y").date()) + " " + time_str + + +def rename_headers(df, rename_mappings: dict): + df.rename(columns=rename_mappings, inplace=True) + + +def save_to_new_file(df, file_path): + df.to_csv(file_path, float_format="%.0f", index=False) + + +def download_file(source_url: str, source_file: pathlib.Path): + logging.info(f"Downloading {source_url} into {source_file}") + r = requests.get(source_url, stream=True) + if r.status_code == 200: + with open(source_file, "wb") as f: + for chunk in r: + f.write(chunk) + else: + logging.error(f"Couldn't download {source_url}: {r.text}") + + +def upload_file_to_gcs(file_path: pathlib.Path, gcs_bucket: str, gcs_path: str) -> None: + storage_client = storage.Client() + bucket = storage_client.bucket(gcs_bucket) + blob = bucket.blob(gcs_path) + blob.upload_from_filename(file_path) + + +if __name__ == "__main__": + logging.getLogger().setLevel(logging.INFO) + + # if os.environ["PIPELINE"] == 'data_by_province': + main( + source_url=os.environ["SOURCE_URL"], + source_file=pathlib.Path(os.environ["SOURCE_FILE"]).expanduser(), + target_file=pathlib.Path(os.environ["TARGET_FILE"]).expanduser(), + target_gcs_bucket=os.environ["TARGET_GCS_BUCKET"], + target_gcs_path=os.environ["TARGET_GCS_PATH"], + headers=json.loads(os.environ["CSV_HEADERS"]), + rename_mappings=json.loads(os.environ["RENAME_MAPPINGS"]), + pipeline_name=os.environ["PIPELINE_NAME"], + ) diff --git a/datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt b/datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt new file mode 100644 index 000000000..64755a48d --- /dev/null +++ b/datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt @@ -0,0 +1,3 @@ +requests +vaex +google-cloud-storage diff --git a/datasets/covid19_italy/_terraform/covid19_italy_dataset.tf b/datasets/covid19_italy/_terraform/covid19_italy_dataset.tf new file mode 100644 index 000000000..331a89fc9 --- /dev/null +++ b/datasets/covid19_italy/_terraform/covid19_italy_dataset.tf @@ -0,0 +1,26 @@ +/** + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +resource "google_bigquery_dataset" "covid19_italy" { + dataset_id = "covid19_italy" + project = var.project_id + description = "ETL Processes for Covid 19 Italy" +} + +output "bigquery_dataset-covid19_italy-dataset_id" { + value = google_bigquery_dataset.covid19_italy.dataset_id +} diff --git a/datasets/covid19_italy/_terraform/data_by_province_pipeline.tf b/datasets/covid19_italy/_terraform/data_by_province_pipeline.tf new file mode 100644 index 000000000..abc36283b --- /dev/null +++ b/datasets/covid19_italy/_terraform/data_by_province_pipeline.tf @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +resource "google_bigquery_table" "data_by_province" { + project = var.project_id + dataset_id = "covid19_italy" + table_id = "data_by_province" + + description = "covid19 Italy Data By Province" + + + + + depends_on = [ + google_bigquery_dataset.covid19_italy + ] +} + +output "bigquery_table-data_by_province-table_id" { + value = google_bigquery_table.data_by_province.table_id +} + +output "bigquery_table-data_by_province-id" { + value = google_bigquery_table.data_by_province.id +} diff --git a/datasets/covid19_italy/_terraform/data_by_region_pipeline.tf b/datasets/covid19_italy/_terraform/data_by_region_pipeline.tf new file mode 100644 index 000000000..2c6397b01 --- /dev/null +++ b/datasets/covid19_italy/_terraform/data_by_region_pipeline.tf @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +resource "google_bigquery_table" "data_by_region" { + project = var.project_id + dataset_id = "covid19_italy" + table_id = "data_by_region" + + description = "covid19 Italy Data By Region" + + + + + depends_on = [ + google_bigquery_dataset.covid19_italy + ] +} + +output "bigquery_table-data_by_region-table_id" { + value = google_bigquery_table.data_by_region.table_id +} + +output "bigquery_table-data_by_region-id" { + value = google_bigquery_table.data_by_region.id +} diff --git a/datasets/covid19_italy/_terraform/national_trends_pipeline.tf b/datasets/covid19_italy/_terraform/national_trends_pipeline.tf new file mode 100644 index 000000000..84a5632b9 --- /dev/null +++ b/datasets/covid19_italy/_terraform/national_trends_pipeline.tf @@ -0,0 +1,39 @@ +/** + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +resource "google_bigquery_table" "national_trends" { + project = var.project_id + dataset_id = "covid19_italy" + table_id = "national_trends" + + description = "covid19 Italy National Trends" + + + + + depends_on = [ + google_bigquery_dataset.covid19_italy + ] +} + +output "bigquery_table-national_trends-table_id" { + value = google_bigquery_table.national_trends.table_id +} + +output "bigquery_table-national_trends-id" { + value = google_bigquery_table.national_trends.id +} diff --git a/datasets/covid19_italy/_terraform/provider.tf b/datasets/covid19_italy/_terraform/provider.tf new file mode 100644 index 000000000..23ab87dcd --- /dev/null +++ b/datasets/covid19_italy/_terraform/provider.tf @@ -0,0 +1,28 @@ +/** + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +provider "google" { + project = var.project_id + impersonate_service_account = var.impersonating_acct + region = var.region +} + +data "google_client_openid_userinfo" "me" {} + +output "impersonating-account" { + value = data.google_client_openid_userinfo.me.email +} diff --git a/datasets/covid19_italy/_terraform/variables.tf b/datasets/covid19_italy/_terraform/variables.tf new file mode 100644 index 000000000..c3ec7c506 --- /dev/null +++ b/datasets/covid19_italy/_terraform/variables.tf @@ -0,0 +1,23 @@ +/** + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +variable "project_id" {} +variable "bucket_name_prefix" {} +variable "impersonating_acct" {} +variable "region" {} +variable "env" {} + diff --git a/datasets/covid19_italy/data_by_province/data_by_province_dag.py b/datasets/covid19_italy/data_by_province/data_by_province_dag.py new file mode 100644 index 000000000..2779eca0c --- /dev/null +++ b/datasets/covid19_italy/data_by_province/data_by_province_dag.py @@ -0,0 +1,83 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from airflow import DAG +from airflow.contrib.operators import gcs_to_bq, kubernetes_pod_operator + +default_args = { + "owner": "Google", + "depends_on_past": False, + "start_date": "2021-04-01", +} + + +with DAG( + dag_id="covid19_italy.data_by_province", + default_args=default_args, + max_active_runs=1, + schedule_interval="@daily", + catchup=False, + default_view="graph", +) as dag: + + # Run CSV transform within kubernetes pod + covid19_italy_data_by_province_transform_csv = kubernetes_pod_operator.KubernetesPodOperator( + task_id="covid19_italy_data_by_province_transform_csv", + startup_timeout_seconds=600, + name="covid19_italy_data_by_province", + namespace="default", + image_pull_policy="Always", + image="{{ var.json.covid19_italy.container_registry.run_csv_transform_kub }}", + env_vars={ + "SOURCE_URL": "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-province/dpc-covid19-ita-province.csv", + "SOURCE_FILE": "files/data.csv", + "TARGET_FILE": "files/data_output.csv", + "TARGET_GCS_BUCKET": "{{ var.json.shared.composer_bucket }}", + "TARGET_GCS_PATH": "data/covid19_italy/data_by_province/data_output.csv", + "CSV_HEADERS": '["date","country","region_code","region_name","province_code","province_name","province_abbreviation","latitude","longitude","location_geom","confirmed_cases","note"]', + "RENAME_MAPPINGS": '{"data": "date","stato": "country","codice_regione": "region_code","denominazione_regione": "region_name","lat": "latitude","long": "longitude","codice_provincia": "province_code","denominazione_provincia": "province_name","sigla_provincia": "province_abbreviation","totale_casi": "confirmed_cases","note": "note"}', + "PIPELINE_NAME": "data_by_province", + }, + resources={"request_memory": "4G", "request_cpu": "1"}, + ) + + # Task to load CSV data to a BigQuery table + load_covid19_italy_data_by_province_to_bq = ( + gcs_to_bq.GoogleCloudStorageToBigQueryOperator( + task_id="load_covid19_italy_data_by_province_to_bq", + bucket="{{ var.json.shared.composer_bucket }}", + source_objects=["data/covid19_italy/data_by_province/data_output.csv"], + source_format="CSV", + destination_project_dataset_table="covid19_italy.data_by_province", + skip_leading_rows=1, + write_disposition="WRITE_TRUNCATE", + schema_fields=[ + {"name": "date", "type": "TIMESTAMP", "mode": "NULLABLE"}, + {"name": "country", "type": "STRING", "mode": "NULLABLE"}, + {"name": "region_code", "type": "STRING", "mode": "NULLABLE"}, + {"name": "name", "type": "STRING", "mode": "NULLABLE"}, + {"name": "province_code", "type": "STRING", "mode": "NULLABLE"}, + {"name": "province_name", "type": "STRING", "mode": "NULLABLE"}, + {"name": "province_abbreviation", "type": "STRING", "mode": "NULLABLE"}, + {"name": "latitude", "type": "FLOAT", "mode": "NULLABLE"}, + {"name": "longitude", "type": "FLOAT", "mode": "NULLABLE"}, + {"name": "location_geom", "type": "GEOGRAPHY", "mode": "NULLABLE"}, + {"name": "confirmed_cases", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "note", "type": "STRING", "mode": "NULLABLE"}, + ], + ) + ) + + covid19_italy_data_by_province_transform_csv >> load_covid19_italy_data_by_province_to_bq diff --git a/datasets/covid19_italy/data_by_province/pipeline.yaml b/datasets/covid19_italy/data_by_province/pipeline.yaml new file mode 100644 index 000000000..9f1e047f9 --- /dev/null +++ b/datasets/covid19_italy/data_by_province/pipeline.yaml @@ -0,0 +1,149 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +resources: + + - type: bigquery_table + # Required Properties: + table_id: data_by_province + + # Description of the table + description: "covid19 Italy Data By Province" + +dag: + + airflow_version: 1 + + initialize: + dag_id: data_by_province + default_args: + owner: "Google" + + # When set to True, keeps a task from getting triggered if the previous schedule for the task hasn’t succeeded + depends_on_past: False + start_date: '2021-04-01' + max_active_runs: 1 + schedule_interval: "@daily" # run once a week at Sunday 12am + catchup: False + default_view: graph + + tasks: + + - operator: "KubernetesPodOperator" + + # Task description + description: "Run CSV transform within kubernetes pod" + + args: + + task_id: "covid19_italy_data_by_province_transform_csv" + startup_timeout_seconds: 600 + + + # The name of the pod in which the task will run. This will be used (plus a random suffix) to generate a pod id + name: "covid19_italy_data_by_province" + + # The namespace to run within Kubernetes. Always set its value to "default" because we follow the guideline that KubernetesPodOperator will only be used for very light workloads, i.e. use the Cloud Composer environment's resources without starving other pipelines. + namespace: "default" + + image_pull_policy: "Always" + + # Docker images will be built and pushed to GCR by default whenever the `scripts/generate_dag.py` is run. To skip building and pushing images, use the optional `--skip-builds` flag. + image: "{{ var.json.covid19_italy.container_registry.run_csv_transform_kub }}" + + # Set the environment variables you need initialized in the container. Use these as input variables for the script your container is expected to perform. + env_vars: + SOURCE_URL: https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-province/dpc-covid19-ita-province.csv + SOURCE_FILE: "files/data.csv" + TARGET_FILE: "files/data_output.csv" + TARGET_GCS_BUCKET: "{{ var.json.shared.composer_bucket }}" + TARGET_GCS_PATH: "data/covid19_italy/data_by_province/data_output.csv" + CSV_HEADERS: >- + ["date","country","region_code","region_name","province_code","province_name","province_abbreviation","latitude","longitude","location_geom","confirmed_cases","note"] + RENAME_MAPPINGS: >- + {"data": "date","stato": "country","codice_regione": "region_code","denominazione_regione": "region_name","lat": "latitude","long": "longitude","codice_provincia": "province_code","denominazione_provincia": "province_name","sigla_provincia": "province_abbreviation","totale_casi": "confirmed_cases","note": "note"} + PIPELINE_NAME: "data_by_province" + + # Set resource limits for the pod here. For resource units in Kubernetes, see https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes + resources: + request_memory: "4G" + request_cpu: "1" + + - operator: "GoogleCloudStorageToBigQueryOperator" + description: "Task to load CSV data to a BigQuery table" + + args: + task_id: "load_covid19_italy_data_by_province_to_bq" + + # The GCS bucket where the CSV file is located in. + bucket: "{{ var.json.shared.composer_bucket }}" + + # The GCS object path for the CSV file + source_objects: ["data/covid19_italy/data_by_province/data_output.csv"] + source_format: "CSV" + destination_project_dataset_table: "covid19_italy.data_by_province" + + # Use this if your CSV file contains a header row + skip_leading_rows: 1 + + # How to write data to the table: overwrite, append, or write if empty + # See https://cloud.google.com/bigquery/docs/reference/auditlogs/rest/Shared.Types/WriteDisposition + write_disposition: "WRITE_TRUNCATE" + + # The BigQuery table schema based on the CSV file. For more info, see + # https://cloud.google.com/bigquery/docs/schemas. + # Always use snake_case and lowercase for column names, and be explicit, + # i.e. specify modes for all columns. + # types: "INTEGER", "TIMESTAMP", "STRING" + schema_fields: + - name: "date" + type: "TIMESTAMP" + mode: "NULLABLE" + - name: "country" + type: "STRING" + mode: "NULLABLE" + - name: "region_code" + type: "STRING" + mode: "NULLABLE" + - name: "name" + type: "STRING" + mode: "NULLABLE" + - name: "province_code" + type: "STRING" + mode: "NULLABLE" + - name: "province_name" + type: "STRING" + mode: "NULLABLE" + - name: "province_abbreviation" + type: "STRING" + mode: "NULLABLE" + - name: "latitude" + type: "FLOAT" + mode: "NULLABLE" + - name: "longitude" + type: "FLOAT" + mode: "NULLABLE" + - name: "location_geom" + type: "GEOGRAPHY" + mode: "NULLABLE" + - name: "confirmed_cases" + type: "INTEGER" + mode: "NULLABLE" + - name: "note" + type: "STRING" + mode: "NULLABLE" + + graph_paths: + - "covid19_italy_data_by_province_transform_csv >> load_covid19_italy_data_by_province_to_bq" diff --git a/datasets/covid19_italy/data_by_region/data_by_region_dag.py b/datasets/covid19_italy/data_by_region/data_by_region_dag.py new file mode 100644 index 000000000..74cd5cc67 --- /dev/null +++ b/datasets/covid19_italy/data_by_region/data_by_region_dag.py @@ -0,0 +1,122 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from airflow import DAG +from airflow.contrib.operators import gcs_to_bq, kubernetes_pod_operator + +default_args = { + "owner": "Google", + "depends_on_past": False, + "start_date": "2021-04-01", +} + + +with DAG( + dag_id="covid19_italy.data_by_region", + default_args=default_args, + max_active_runs=1, + schedule_interval="@daily", + catchup=False, + default_view="graph", +) as dag: + + # Run CSV transform within kubernetes pod + covid19_italy_data_by_region_transform_csv = kubernetes_pod_operator.KubernetesPodOperator( + task_id="covid19_italy_data_by_region_transform_csv", + startup_timeout_seconds=600, + name="covid19_italy_data_by_region", + namespace="default", + image_pull_policy="Always", + image="{{ var.json.covid19_italy.container_registry.run_csv_transform_kub }}", + env_vars={ + "SOURCE_URL": "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni.csv", + "SOURCE_FILE": "files/data.csv", + "TARGET_FILE": "files/data_output.csv", + "TARGET_GCS_BUCKET": "{{ var.json.shared.composer_bucket }}", + "TARGET_GCS_PATH": "data/covid19_italy/data_by_region/data_output.csv", + "CSV_HEADERS": '["date","country","region_code","region_name","latitude","longitude","location_geom","hospitalized_patients_symptoms","hospitalized_patients_intensive_care","total_hospitalized_patients","home_confinement_cases","total_current_confirmed_cases","new_current_confirmed_cases","new_total_confirmed_cases","recovered","deaths","total_confirmed_cases","tests_performed","note"]', + "RENAME_MAPPINGS": '{"data": "date","stato": "country","codice_regione": "region_code","denominazione_regione": "region_name","lat": "latitude","long": "longitude","ricoverati_con_sintomi": "hospitalized_patients_symptoms","terapia_intensiva": "hospitalized_patients_intensive_care","totale_ospedalizzati": "total_hospitalized_patients","isolamento_domiciliare": "home_confinement_cases","totale_positivi": "total_current_confirmed_cases","variazione_totale_positivi": "new_current_confirmed_cases","nuovi_positivi": "new_total_confirmed_cases","note": "note","dimessi_guariti": "recovered","totale_casi": "total_confirmed_cases","tamponi": "tests_performed","deceduti": "deaths"}', + "PIPELINE_NAME": "data_by_region", + }, + resources={"limit_memory": "4G", "limit_cpu": "1"}, + ) + + # Task to load CSV data to a BigQuery table + load_covid19_italy_data_by_region_to_bq = ( + gcs_to_bq.GoogleCloudStorageToBigQueryOperator( + task_id="load_covid19_italy_data_by_region_to_bq", + bucket="{{ var.json.shared.composer_bucket }}", + source_objects=["data/covid19_italy/data_by_region/data_output.csv"], + source_format="CSV", + destination_project_dataset_table="covid19_italy.data_by_region", + skip_leading_rows=1, + write_disposition="WRITE_TRUNCATE", + schema_fields=[ + {"name": "date", "type": "TIMESTAMP", "mode": "NULLABLE"}, + {"name": "country", "type": "STRING", "mode": "NULLABLE"}, + {"name": "region_code", "type": "STRING", "mode": "NULLABLE"}, + {"name": "region_name", "type": "STRING", "mode": "NULLABLE"}, + {"name": "latitude", "type": "FLOAT", "mode": "NULLABLE"}, + {"name": "longitude", "type": "FLOAT", "mode": "NULLABLE"}, + {"name": "location_geom", "type": "GEOGRAPHY", "mode": "NULLABLE"}, + { + "name": "hospitalized_patients_symptoms", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "hospitalized_patients_intensive_care", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "total_hospitalized_patients", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "home_confinement_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "total_current_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "new_current_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "new_total_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + {"name": "recovered", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "deaths", "type": "INTEGER", "mode": "NULLABLE"}, + { + "name": "total_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + {"name": "tests_performed", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "note", "type": "STRING", "mode": "NULLABLE"}, + ], + ) + ) + + covid19_italy_data_by_region_transform_csv >> load_covid19_italy_data_by_region_to_bq diff --git a/datasets/covid19_italy/data_by_region/pipeline.yaml b/datasets/covid19_italy/data_by_region/pipeline.yaml new file mode 100644 index 000000000..14595d75f --- /dev/null +++ b/datasets/covid19_italy/data_by_region/pipeline.yaml @@ -0,0 +1,171 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +resources: + + - type: bigquery_table + # Required Properties: + table_id: data_by_region + + # Description of the table + description: "covid19 Italy Data By Region" + +dag: + +# [Required] Specify the Airflow version of the operators used by the DAG. + airflow_version: 1 + + initialize: + dag_id: data_by_region + default_args: + owner: "Google" + + # When set to True, keeps a task from getting triggered if the previous schedule for the task hasn’t succeeded + depends_on_past: False + start_date: '2021-04-01' + max_active_runs: 1 + schedule_interval: "@daily" # run once a week at Sunday 12am + catchup: False + default_view: graph + + tasks: + + - operator: "KubernetesPodOperator" + + # Task description + description: "Run CSV transform within kubernetes pod" + + args: + + task_id: "covid19_italy_data_by_region_transform_csv" + startup_timeout_seconds: 600 + + # The name of the pod in which the task will run. This will be used (plus a random suffix) to generate a pod id + name: "covid19_italy_data_by_region" + + # The namespace to run within Kubernetes. Always set its value to "default" because we follow the guideline that KubernetesPodOperator will only be used for very light workloads, i.e. use the Cloud Composer environment's resources without starving other pipelines. + namespace: "default" + + image_pull_policy: "Always" + + # Docker images will be built and pushed to GCR by default whenever the `scripts/generate_dag.py` is run. To skip building and pushing images, use the optional `--skip-builds` flag. + image: "{{ var.json.covid19_italy.container_registry.run_csv_transform_kub }}" + + # Set the environment variables you need initialized in the container. Use these as input variables for the script your container is expected to perform. + env_vars: + SOURCE_URL: https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni.csv + SOURCE_FILE: "files/data.csv" + TARGET_FILE: "files/data_output.csv" + TARGET_GCS_BUCKET: "{{ var.json.shared.composer_bucket }}" + TARGET_GCS_PATH: "data/covid19_italy/data_by_region/data_output.csv" + CSV_HEADERS: >- + ["date","country","region_code","region_name","latitude","longitude","location_geom","hospitalized_patients_symptoms","hospitalized_patients_intensive_care","total_hospitalized_patients","home_confinement_cases","total_current_confirmed_cases","new_current_confirmed_cases","new_total_confirmed_cases","recovered","deaths","total_confirmed_cases","tests_performed","note"] + RENAME_MAPPINGS: >- + {"data": "date","stato": "country","codice_regione": "region_code","denominazione_regione": "region_name","lat": "latitude","long": "longitude","ricoverati_con_sintomi": "hospitalized_patients_symptoms","terapia_intensiva": "hospitalized_patients_intensive_care","totale_ospedalizzati": "total_hospitalized_patients","isolamento_domiciliare": "home_confinement_cases","totale_positivi": "total_current_confirmed_cases","variazione_totale_positivi": "new_current_confirmed_cases","nuovi_positivi": "new_total_confirmed_cases","note": "note","dimessi_guariti": "recovered","totale_casi": "total_confirmed_cases","tamponi": "tests_performed","deceduti": "deaths"} + PIPELINE_NAME: "data_by_region" + + # Set resource limits for the pod here. For resource units in Kubernetes, see https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes + resources: + limit_memory: "4G" + limit_cpu: "1" + + - operator: "GoogleCloudStorageToBigQueryOperator" + description: "Task to load CSV data to a BigQuery table" + + args: + task_id: "load_covid19_italy_data_by_region_to_bq" + + # The GCS bucket where the CSV file is located in. + bucket: "{{ var.json.shared.composer_bucket }}" + + # The GCS object path for the CSV file + source_objects: ["data/covid19_italy/data_by_region/data_output.csv"] + source_format: "CSV" + destination_project_dataset_table: "covid19_italy.data_by_region" + + # Use this if your CSV file contains a header row + skip_leading_rows: 1 + + # How to write data to the table: overwrite, append, or write if empty + # See https://cloud.google.com/bigquery/docs/reference/auditlogs/rest/Shared.Types/WriteDisposition + write_disposition: "WRITE_TRUNCATE" + + # The BigQuery table schema based on the CSV file. For more info, see + # https://cloud.google.com/bigquery/docs/schemas. + # Always use snake_case and lowercase for column names, and be explicit, + # i.e. specify modes for all columns. + # types: "INTEGER", "TIMESTAMP", "STRING" + schema_fields: + - name: "date" + type: "TIMESTAMP" + mode: "NULLABLE" + - name: "country" + type: "STRING" + mode: "NULLABLE" + - name: "region_code" + type: "STRING" + mode: "NULLABLE" + - name: "region_name" + type: "STRING" + mode: "NULLABLE" + - name: "latitude" + type: "FLOAT" + mode: "NULLABLE" + - name: "longitude" + type: "FLOAT" + mode: "NULLABLE" + - name: "location_geom" + type: "GEOGRAPHY" + mode: "NULLABLE" + - name: "hospitalized_patients_symptoms" + type: "INTEGER" + mode: "NULLABLE" + - name: "hospitalized_patients_intensive_care" + type: "INTEGER" + mode: "NULLABLE" + - name: "total_hospitalized_patients" + type: "INTEGER" + mode: "NULLABLE" + - name: "home_confinement_cases" + type: "INTEGER" + mode: "NULLABLE" + - name: "total_current_confirmed_cases" + type: "INTEGER" + mode: "NULLABLE" + - name: "new_current_confirmed_cases" + type: "INTEGER" + mode: "NULLABLE" + - name: "new_total_confirmed_cases" + type: "INTEGER" + mode: "NULLABLE" + - name: "recovered" + type: "INTEGER" + mode: "NULLABLE" + - name: "deaths" + type: "INTEGER" + mode: "NULLABLE" + - name: "total_confirmed_cases" + type: "INTEGER" + mode: "NULLABLE" + - name: "tests_performed" + type: "INTEGER" + mode: "NULLABLE" + - name: "note" + type: "STRING" + mode: "NULLABLE" + + + graph_paths: + - "covid19_italy_data_by_region_transform_csv >> load_covid19_italy_data_by_region_to_bq" diff --git a/datasets/covid19_italy/dataset.yaml b/datasets/covid19_italy/dataset.yaml new file mode 100644 index 000000000..330feafba --- /dev/null +++ b/datasets/covid19_italy/dataset.yaml @@ -0,0 +1,58 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +dataset: + # The `dataset` block includes properties for your dataset that will be shown + # to users of your data on the Google Cloud website. + + # Must be exactly the same name as the folder name your dataset.yaml is in. + name: covid19_italy + + # A friendly, human-readable name of the dataset + friendly_name: covid19_italy + + # A short, descriptive summary of the dataset. + description: covid19_italy + + # A list of sources the dataset is derived from, using the YAML list syntax. + dataset_sources: ~ + + # A list of terms and conditions that users of the dataset should agree on, + # using the YAML list syntax. + terms_of_use: ~ + + +resources: + # A list of Google Cloud resources needed by your dataset. In principle, all + # pipelines under a dataset should be able to share these resources. + # + # The currently supported resources are shown below. Use only the resources + # you need, and delete the rest as needed by your pipeline. + # + # We will keep adding to the list below to support more Google Cloud resources + # over time. If a resource you need isn't supported, please file an issue on + # the repository. + + - type: bigquery_dataset + # Google BigQuery dataset to namespace all tables managed by this folder + # + # Required Properties: + # dataset_id + # + # Optional Properties: + # friendly_name (A user-friendly name of the dataset) + # description (A user-friendly description of the dataset) + # location (The geographic location where the dataset should reside) + dataset_id: covid19_italy + description: "ETL Processes for Covid 19 Italy" diff --git a/datasets/covid19_italy/national_trends/national_trends_dag.py b/datasets/covid19_italy/national_trends/national_trends_dag.py new file mode 100644 index 000000000..f70e39ee4 --- /dev/null +++ b/datasets/covid19_italy/national_trends/national_trends_dag.py @@ -0,0 +1,117 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from airflow import DAG +from airflow.contrib.operators import gcs_to_bq, kubernetes_pod_operator + +default_args = { + "owner": "Google", + "depends_on_past": False, + "start_date": "2021-04-01", +} + + +with DAG( + dag_id="covid19_italy.national_trends", + default_args=default_args, + max_active_runs=1, + schedule_interval="@daily", + catchup=False, + default_view="graph", +) as dag: + + # Run CSV transform within kubernetes pod + covid19_italy_national_trends_transform_csv = kubernetes_pod_operator.KubernetesPodOperator( + task_id="covid19_italy_national_trends_transform_csv", + startup_timeout_seconds=600, + name="covid19_italy_national_trends", + namespace="default", + image_pull_policy="Always", + image="{{ var.json.covid19_italy.container_registry.run_csv_transform_kub }}", + env_vars={ + "SOURCE_URL": "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-andamento-nazionale/dpc-covid19-ita-andamento-nazionale.csv", + "SOURCE_FILE": "files/data.csv", + "TARGET_FILE": "files/data_output.csv", + "TARGET_GCS_BUCKET": "{{ var.json.shared.composer_bucket }}", + "TARGET_GCS_PATH": "data/covid19_italy/national_trends/data_output.csv", + "CSV_HEADERS": '["date","country","hospitalized_patients_symptoms","hospitalized_patients_intensive_care","total_hospitalized_patients","home_confinement_cases","total_current_confirmed_cases","new_current_confirmed_cases","new_total_confirmed_cases","recovered","deaths","total_confirmed_cases","tests_performed","note"]', + "RENAME_MAPPINGS": '{"data": "date","stato": "country","ricoverati_con_sintomi": "hospitalized_patients_symptoms","terapia_intensiva": "hospitalized_patients_intensive_care","totale_ospedalizzati": "total_hospitalized_patients","isolamento_domiciliare": "home_confinement_cases","totale_positivi": "total_current_confirmed_cases","variazione_totale_positivi": "new_current_confirmed_cases","nuovi_positivi": "new_total_confirmed_cases","dimessi_guariti": "recovered","deceduti": "deaths","totale_casi": "total_confirmed_cases","tamponi": "tests_performed","note": "note"}', + "PIPELINE_NAME": "national_trends", + }, + resources={"request_memory": "4G", "request_cpu": "1"}, + ) + + # Task to load CSV data to a BigQuery table + load_covid19_italy_national_trends_to_bq = ( + gcs_to_bq.GoogleCloudStorageToBigQueryOperator( + task_id="load_covid19_italy_national_trends_to_bq", + bucket="{{ var.json.shared.composer_bucket }}", + source_objects=["data/covid19_italy/national_trends/data_output.csv"], + source_format="CSV", + destination_project_dataset_table="covid19_italy.national_trends", + skip_leading_rows=1, + write_disposition="WRITE_TRUNCATE", + schema_fields=[ + {"name": "date", "type": "TIMESTAMP", "mode": "NULLABLE"}, + {"name": "country", "type": "STRING", "mode": "NULLABLE"}, + { + "name": "hospitalized_patients_symptoms", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "hospitalized_patients_intensive_care", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "total_hospitalized_patients", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "home_confinement_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "total_current_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "new_current_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "new_total_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + {"name": "recovered", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "deaths", "type": "INTEGER", "mode": "NULLABLE"}, + { + "name": "total_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + {"name": "tests_performed", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "note", "type": "STRING", "mode": "NULLABLE"}, + ], + ) + ) + + covid19_italy_national_trends_transform_csv >> load_covid19_italy_national_trends_to_bq diff --git a/datasets/covid19_italy/national_trends/pipeline.yaml b/datasets/covid19_italy/national_trends/pipeline.yaml new file mode 100644 index 000000000..575162d54 --- /dev/null +++ b/datasets/covid19_italy/national_trends/pipeline.yaml @@ -0,0 +1,161 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +resources: + + - type: bigquery_table + # Required Properties: + table_id: national_trends + + # Description of the table + description: "covid19 Italy National Trends" + +dag: + + # [Required] Specify the Airflow version of the operators used by the DAG. + airflow_version: 1 + + initialize: + dag_id: national_trends + default_args: + owner: "Google" + + # When set to True, keeps a task from getting triggered if the previous schedule for the task hasn’t succeeded + depends_on_past: False + start_date: '2021-04-01' + max_active_runs: 1 + schedule_interval: "@daily" # run once a week at Sunday 12am + catchup: False + default_view: graph + + tasks: + + - operator: "KubernetesPodOperator" + + # Task description + description: "Run CSV transform within kubernetes pod" + + args: + + task_id: "covid19_italy_national_trends_transform_csv" + startup_timeout_seconds: 600 + + + # The name of the pod in which the task will run. This will be used (plus a random suffix) to generate a pod id + name: "covid19_italy_national_trends" + + # The namespace to run within Kubernetes. Always set its value to "default" because we follow the guideline that KubernetesPodOperator will only be used for very light workloads, i.e. use the Cloud Composer environment's resources without starving other pipelines. + namespace: "default" + + image_pull_policy: "Always" + + # Docker images will be built and pushed to GCR by default whenever the `scripts/generate_dag.py` is run. To skip building and pushing images, use the optional `--skip-builds` flag. + image: "{{ var.json.covid19_italy.container_registry.run_csv_transform_kub }}" + + # Set the environment variables you need initialized in the container. Use these as input variables for the script your container is expected to perform. + env_vars: + SOURCE_URL: https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-andamento-nazionale/dpc-covid19-ita-andamento-nazionale.csv + SOURCE_FILE: "files/data.csv" + TARGET_FILE: "files/data_output.csv" + TARGET_GCS_BUCKET: "{{ var.json.shared.composer_bucket }}" + TARGET_GCS_PATH: "data/covid19_italy/national_trends/data_output.csv" + CSV_HEADERS: >- + ["date","country","hospitalized_patients_symptoms","hospitalized_patients_intensive_care","total_hospitalized_patients","home_confinement_cases","total_current_confirmed_cases","new_current_confirmed_cases","new_total_confirmed_cases","recovered","deaths","total_confirmed_cases","tests_performed","note"] + RENAME_MAPPINGS: >- + {"data": "date","stato": "country","ricoverati_con_sintomi": "hospitalized_patients_symptoms","terapia_intensiva": "hospitalized_patients_intensive_care","totale_ospedalizzati": "total_hospitalized_patients","isolamento_domiciliare": "home_confinement_cases","totale_positivi": "total_current_confirmed_cases","variazione_totale_positivi": "new_current_confirmed_cases","nuovi_positivi": "new_total_confirmed_cases","dimessi_guariti": "recovered","deceduti": "deaths","totale_casi": "total_confirmed_cases","tamponi": "tests_performed","note": "note"} + PIPELINE_NAME: "national_trends" + + # Set resource limits for the pod here. For resource units in Kubernetes, see https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-units-in-kubernetes + resources: + request_memory: "4G" + request_cpu: "1" + + - operator: "GoogleCloudStorageToBigQueryOperator" + description: "Task to load CSV data to a BigQuery table" + + args: + task_id: "load_covid19_italy_national_trends_to_bq" + + # The GCS bucket where the CSV file is located in. + bucket: "{{ var.json.shared.composer_bucket }}" + + # The GCS object path for the CSV file + source_objects: ["data/covid19_italy/national_trends/data_output.csv"] + source_format: "CSV" + destination_project_dataset_table: "covid19_italy.national_trends" + + # Use this if your CSV file contains a header row + skip_leading_rows: 1 + + # How to write data to the table: overwrite, append, or write if empty + # See https://cloud.google.com/bigquery/docs/reference/auditlogs/rest/Shared.Types/WriteDisposition + write_disposition: "WRITE_TRUNCATE" + + # The BigQuery table schema based on the CSV file. For more info, see + # https://cloud.google.com/bigquery/docs/schemas. + # Always use snake_case and lowercase for column names, and be explicit, + # i.e. specify modes for all columns. + # types: "INTEGER", "TIMESTAMP", "STRING" + schema_fields: + - name: "date" + type: "TIMESTAMP" + mode: "NULLABLE" + - name: "country" + type: "STRING" + mode: "NULLABLE" + - name: "hospitalized_patients_symptoms" + type: "INTEGER" + mode: "NULLABLE" + - name: "hospitalized_patients_intensive_care" + type: "INTEGER" + mode: "NULLABLE" + - name: "total_hospitalized_patients" + type: "INTEGER" + mode: "NULLABLE" + - name: "home_confinement_cases" + type: "INTEGER" + mode: "NULLABLE" + - name: "total_current_confirmed_cases" + type: "INTEGER" + mode: "NULLABLE" + - name: "new_current_confirmed_cases" + type: "INTEGER" + mode: "NULLABLE" + - name: "new_total_confirmed_cases" + type: "INTEGER" + mode: "NULLABLE" + - name: "recovered" + type: "INTEGER" + mode: "NULLABLE" + - name: "deaths" + type: "INTEGER" + mode: "NULLABLE" + - name: "total_confirmed_cases" + type: "INTEGER" + mode: "NULLABLE" + - name: "tests_performed" + type: "INTEGER" + mode: "NULLABLE" + - name: "note" + type: "STRING" + mode: "NULLABLE" + + graph_paths: + - "covid19_italy_national_trends_transform_csv >> load_covid19_italy_national_trends_to_bq" + + + + + From 484da8ab016c9b909a989ed6093ceb47b9729fdd Mon Sep 17 00:00:00 2001 From: Varshika Gupta Date: Tue, 24 Aug 2021 16:04:33 +0000 Subject: [PATCH 2/3] fix: Onboard COVID19-Italy dataset --- .../run_csv_transform_kub/csv_transform.py | 16 +-- .../run_csv_transform_kub/requirements.txt | 1 - .../_terraform/data_by_province_pipeline.tf | 2 +- .../_terraform/data_by_region_pipeline.tf | 2 +- .../_terraform/national_trends_pipeline.tf | 2 +- .../data_by_province/data_by_province_dag.py | 52 ++++---- .../data_by_province/pipeline.yaml | 12 +- .../data_by_region/data_by_region_dag.py | 122 ++++++++---------- .../data_by_region/pipeline.yaml | 12 +- .../national_trends/national_trends_dag.py | 112 ++++++++-------- .../national_trends/pipeline.yaml | 17 +-- 11 files changed, 155 insertions(+), 195 deletions(-) diff --git a/datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py b/datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py index daab397ac..64f94a6c2 100644 --- a/datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py +++ b/datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py @@ -48,19 +48,15 @@ def main( logging.info(f"Downloading file {source_url}") download_file(source_url, source_file) - # open the input file logging.info(f"Opening file {source_file}") df = pd.read_csv(str(source_file)) - # steps in the pipeline logging.info(f"Transformation Process Starting.. {source_file}") - # rename the headers logging.info(f"Transform: Renaming Headers.. {source_file}") rename_headers(df, rename_mappings) - # create location_geom logging.info(f"Transform: Creating Geometry Column.. {pipeline_name}") if pipeline_name == "data_by_province" or pipeline_name == "data_by_region": df["location_geom"] = ( @@ -70,21 +66,16 @@ def main( + df["latitude"].astype(str).replace("nan", "") + ")" ) - # replace blank POINT( ) valye with blank df.location_geom = df.location_geom.replace("POINT( )", "") - # reorder headers in output logging.info("Transform: Reordering headers..") df = df[headers] - # steps in the pipeline logging.info(f"Transformation Process complete .. {source_file}") - # save to output file logging.info(f"Saving to output file.. {target_file}") try: - # save_to_new_file(df, file_path=str(target_file)) save_to_new_file(df, file_path=str(target_file)) except Exception as e: logging.error(f"Error saving output file: {e}.") @@ -101,9 +92,7 @@ def main( ) -def convert_dt_format(date_str, time_str): - # date_str, time_str - # 10/26/2014,13:12:00 +def convert_dt_format(date_str: str, time_str: str): return str(datetime.datetime.strptime(date_str, "%m/%d/%Y").date()) + " " + time_str @@ -111,7 +100,7 @@ def rename_headers(df, rename_mappings: dict): df.rename(columns=rename_mappings, inplace=True) -def save_to_new_file(df, file_path): +def save_to_new_file(df, file_path: str): df.to_csv(file_path, float_format="%.0f", index=False) @@ -136,7 +125,6 @@ def upload_file_to_gcs(file_path: pathlib.Path, gcs_bucket: str, gcs_path: str) if __name__ == "__main__": logging.getLogger().setLevel(logging.INFO) - # if os.environ["PIPELINE"] == 'data_by_province': main( source_url=os.environ["SOURCE_URL"], source_file=pathlib.Path(os.environ["SOURCE_FILE"]).expanduser(), diff --git a/datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt b/datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt index 64755a48d..cd54d905c 100644 --- a/datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt +++ b/datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt @@ -1,3 +1,2 @@ requests -vaex google-cloud-storage diff --git a/datasets/covid19_italy/_terraform/data_by_province_pipeline.tf b/datasets/covid19_italy/_terraform/data_by_province_pipeline.tf index abc36283b..f7a11bb93 100644 --- a/datasets/covid19_italy/_terraform/data_by_province_pipeline.tf +++ b/datasets/covid19_italy/_terraform/data_by_province_pipeline.tf @@ -20,7 +20,7 @@ resource "google_bigquery_table" "data_by_province" { dataset_id = "covid19_italy" table_id = "data_by_province" - description = "covid19 Italy Data By Province" + description = "COVID-19 Italy Data By Province" diff --git a/datasets/covid19_italy/_terraform/data_by_region_pipeline.tf b/datasets/covid19_italy/_terraform/data_by_region_pipeline.tf index 2c6397b01..42f52d7c6 100644 --- a/datasets/covid19_italy/_terraform/data_by_region_pipeline.tf +++ b/datasets/covid19_italy/_terraform/data_by_region_pipeline.tf @@ -20,7 +20,7 @@ resource "google_bigquery_table" "data_by_region" { dataset_id = "covid19_italy" table_id = "data_by_region" - description = "covid19 Italy Data By Region" + description = "COVID-19 Italy Data By Region" diff --git a/datasets/covid19_italy/_terraform/national_trends_pipeline.tf b/datasets/covid19_italy/_terraform/national_trends_pipeline.tf index 84a5632b9..1a9a64c7f 100644 --- a/datasets/covid19_italy/_terraform/national_trends_pipeline.tf +++ b/datasets/covid19_italy/_terraform/national_trends_pipeline.tf @@ -20,7 +20,7 @@ resource "google_bigquery_table" "national_trends" { dataset_id = "covid19_italy" table_id = "national_trends" - description = "covid19 Italy National Trends" + description = "COVID-19 Italy National Trends" diff --git a/datasets/covid19_italy/data_by_province/data_by_province_dag.py b/datasets/covid19_italy/data_by_province/data_by_province_dag.py index 2779eca0c..d664aa96f 100644 --- a/datasets/covid19_italy/data_by_province/data_by_province_dag.py +++ b/datasets/covid19_italy/data_by_province/data_by_province_dag.py @@ -33,8 +33,8 @@ ) as dag: # Run CSV transform within kubernetes pod - covid19_italy_data_by_province_transform_csv = kubernetes_pod_operator.KubernetesPodOperator( - task_id="covid19_italy_data_by_province_transform_csv", + data_by_province_transform_csv = kubernetes_pod_operator.KubernetesPodOperator( + task_id="data_by_province_transform_csv", startup_timeout_seconds=600, name="covid19_italy_data_by_province", namespace="default", @@ -54,30 +54,28 @@ ) # Task to load CSV data to a BigQuery table - load_covid19_italy_data_by_province_to_bq = ( - gcs_to_bq.GoogleCloudStorageToBigQueryOperator( - task_id="load_covid19_italy_data_by_province_to_bq", - bucket="{{ var.json.shared.composer_bucket }}", - source_objects=["data/covid19_italy/data_by_province/data_output.csv"], - source_format="CSV", - destination_project_dataset_table="covid19_italy.data_by_province", - skip_leading_rows=1, - write_disposition="WRITE_TRUNCATE", - schema_fields=[ - {"name": "date", "type": "TIMESTAMP", "mode": "NULLABLE"}, - {"name": "country", "type": "STRING", "mode": "NULLABLE"}, - {"name": "region_code", "type": "STRING", "mode": "NULLABLE"}, - {"name": "name", "type": "STRING", "mode": "NULLABLE"}, - {"name": "province_code", "type": "STRING", "mode": "NULLABLE"}, - {"name": "province_name", "type": "STRING", "mode": "NULLABLE"}, - {"name": "province_abbreviation", "type": "STRING", "mode": "NULLABLE"}, - {"name": "latitude", "type": "FLOAT", "mode": "NULLABLE"}, - {"name": "longitude", "type": "FLOAT", "mode": "NULLABLE"}, - {"name": "location_geom", "type": "GEOGRAPHY", "mode": "NULLABLE"}, - {"name": "confirmed_cases", "type": "INTEGER", "mode": "NULLABLE"}, - {"name": "note", "type": "STRING", "mode": "NULLABLE"}, - ], - ) + load_data_by_province_to_bq = gcs_to_bq.GoogleCloudStorageToBigQueryOperator( + task_id="load_data_by_province_to_bq", + bucket="{{ var.json.shared.composer_bucket }}", + source_objects=["data/covid19_italy/data_by_province/data_output.csv"], + source_format="CSV", + destination_project_dataset_table="covid19_italy.data_by_province", + skip_leading_rows=1, + write_disposition="WRITE_TRUNCATE", + schema_fields=[ + {"name": "date", "type": "TIMESTAMP", "mode": "NULLABLE"}, + {"name": "country", "type": "STRING", "mode": "NULLABLE"}, + {"name": "region_code", "type": "STRING", "mode": "NULLABLE"}, + {"name": "name", "type": "STRING", "mode": "NULLABLE"}, + {"name": "province_code", "type": "STRING", "mode": "NULLABLE"}, + {"name": "province_name", "type": "STRING", "mode": "NULLABLE"}, + {"name": "province_abbreviation", "type": "STRING", "mode": "NULLABLE"}, + {"name": "latitude", "type": "FLOAT", "mode": "NULLABLE"}, + {"name": "longitude", "type": "FLOAT", "mode": "NULLABLE"}, + {"name": "location_geom", "type": "GEOGRAPHY", "mode": "NULLABLE"}, + {"name": "confirmed_cases", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "note", "type": "STRING", "mode": "NULLABLE"}, + ], ) - covid19_italy_data_by_province_transform_csv >> load_covid19_italy_data_by_province_to_bq + data_by_province_transform_csv >> load_data_by_province_to_bq diff --git a/datasets/covid19_italy/data_by_province/pipeline.yaml b/datasets/covid19_italy/data_by_province/pipeline.yaml index 9f1e047f9..66038c811 100644 --- a/datasets/covid19_italy/data_by_province/pipeline.yaml +++ b/datasets/covid19_italy/data_by_province/pipeline.yaml @@ -20,7 +20,7 @@ resources: table_id: data_by_province # Description of the table - description: "covid19 Italy Data By Province" + description: "COVID-19 Italy Data By Province" dag: @@ -35,7 +35,7 @@ dag: depends_on_past: False start_date: '2021-04-01' max_active_runs: 1 - schedule_interval: "@daily" # run once a week at Sunday 12am + schedule_interval: "@daily" catchup: False default_view: graph @@ -48,7 +48,7 @@ dag: args: - task_id: "covid19_italy_data_by_province_transform_csv" + task_id: "data_by_province_transform_csv" startup_timeout_seconds: 600 @@ -65,7 +65,7 @@ dag: # Set the environment variables you need initialized in the container. Use these as input variables for the script your container is expected to perform. env_vars: - SOURCE_URL: https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-province/dpc-covid19-ita-province.csv + SOURCE_URL: "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-province/dpc-covid19-ita-province.csv" SOURCE_FILE: "files/data.csv" TARGET_FILE: "files/data_output.csv" TARGET_GCS_BUCKET: "{{ var.json.shared.composer_bucket }}" @@ -85,7 +85,7 @@ dag: description: "Task to load CSV data to a BigQuery table" args: - task_id: "load_covid19_italy_data_by_province_to_bq" + task_id: "load_data_by_province_to_bq" # The GCS bucket where the CSV file is located in. bucket: "{{ var.json.shared.composer_bucket }}" @@ -146,4 +146,4 @@ dag: mode: "NULLABLE" graph_paths: - - "covid19_italy_data_by_province_transform_csv >> load_covid19_italy_data_by_province_to_bq" + - "data_by_province_transform_csv >> load_data_by_province_to_bq" diff --git a/datasets/covid19_italy/data_by_region/data_by_region_dag.py b/datasets/covid19_italy/data_by_region/data_by_region_dag.py index 74cd5cc67..076c673e3 100644 --- a/datasets/covid19_italy/data_by_region/data_by_region_dag.py +++ b/datasets/covid19_italy/data_by_region/data_by_region_dag.py @@ -33,8 +33,8 @@ ) as dag: # Run CSV transform within kubernetes pod - covid19_italy_data_by_region_transform_csv = kubernetes_pod_operator.KubernetesPodOperator( - task_id="covid19_italy_data_by_region_transform_csv", + data_by_region_transform_csv = kubernetes_pod_operator.KubernetesPodOperator( + task_id="data_by_region_transform_csv", startup_timeout_seconds=600, name="covid19_italy_data_by_region", namespace="default", @@ -54,69 +54,59 @@ ) # Task to load CSV data to a BigQuery table - load_covid19_italy_data_by_region_to_bq = ( - gcs_to_bq.GoogleCloudStorageToBigQueryOperator( - task_id="load_covid19_italy_data_by_region_to_bq", - bucket="{{ var.json.shared.composer_bucket }}", - source_objects=["data/covid19_italy/data_by_region/data_output.csv"], - source_format="CSV", - destination_project_dataset_table="covid19_italy.data_by_region", - skip_leading_rows=1, - write_disposition="WRITE_TRUNCATE", - schema_fields=[ - {"name": "date", "type": "TIMESTAMP", "mode": "NULLABLE"}, - {"name": "country", "type": "STRING", "mode": "NULLABLE"}, - {"name": "region_code", "type": "STRING", "mode": "NULLABLE"}, - {"name": "region_name", "type": "STRING", "mode": "NULLABLE"}, - {"name": "latitude", "type": "FLOAT", "mode": "NULLABLE"}, - {"name": "longitude", "type": "FLOAT", "mode": "NULLABLE"}, - {"name": "location_geom", "type": "GEOGRAPHY", "mode": "NULLABLE"}, - { - "name": "hospitalized_patients_symptoms", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "hospitalized_patients_intensive_care", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "total_hospitalized_patients", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "home_confinement_cases", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "total_current_confirmed_cases", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "new_current_confirmed_cases", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "new_total_confirmed_cases", - "type": "INTEGER", - "mode": "NULLABLE", - }, - {"name": "recovered", "type": "INTEGER", "mode": "NULLABLE"}, - {"name": "deaths", "type": "INTEGER", "mode": "NULLABLE"}, - { - "name": "total_confirmed_cases", - "type": "INTEGER", - "mode": "NULLABLE", - }, - {"name": "tests_performed", "type": "INTEGER", "mode": "NULLABLE"}, - {"name": "note", "type": "STRING", "mode": "NULLABLE"}, - ], - ) + load_data_by_region_to_bq = gcs_to_bq.GoogleCloudStorageToBigQueryOperator( + task_id="load_data_by_region_to_bq", + bucket="{{ var.json.shared.composer_bucket }}", + source_objects=["data/covid19_italy/data_by_region/data_output.csv"], + source_format="CSV", + destination_project_dataset_table="covid19_italy.data_by_region", + skip_leading_rows=1, + write_disposition="WRITE_TRUNCATE", + schema_fields=[ + {"name": "date", "type": "TIMESTAMP", "mode": "NULLABLE"}, + {"name": "country", "type": "STRING", "mode": "NULLABLE"}, + {"name": "region_code", "type": "STRING", "mode": "NULLABLE"}, + {"name": "region_name", "type": "STRING", "mode": "NULLABLE"}, + {"name": "latitude", "type": "FLOAT", "mode": "NULLABLE"}, + {"name": "longitude", "type": "FLOAT", "mode": "NULLABLE"}, + {"name": "location_geom", "type": "GEOGRAPHY", "mode": "NULLABLE"}, + { + "name": "hospitalized_patients_symptoms", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "hospitalized_patients_intensive_care", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "total_hospitalized_patients", + "type": "INTEGER", + "mode": "NULLABLE", + }, + {"name": "home_confinement_cases", "type": "INTEGER", "mode": "NULLABLE"}, + { + "name": "total_current_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "new_current_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "new_total_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + {"name": "recovered", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "deaths", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "total_confirmed_cases", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "tests_performed", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "note", "type": "STRING", "mode": "NULLABLE"}, + ], ) - covid19_italy_data_by_region_transform_csv >> load_covid19_italy_data_by_region_to_bq + data_by_region_transform_csv >> load_data_by_region_to_bq diff --git a/datasets/covid19_italy/data_by_region/pipeline.yaml b/datasets/covid19_italy/data_by_region/pipeline.yaml index 14595d75f..ea7883169 100644 --- a/datasets/covid19_italy/data_by_region/pipeline.yaml +++ b/datasets/covid19_italy/data_by_region/pipeline.yaml @@ -20,7 +20,7 @@ resources: table_id: data_by_region # Description of the table - description: "covid19 Italy Data By Region" + description: "COVID-19 Italy Data By Region" dag: @@ -36,7 +36,7 @@ dag: depends_on_past: False start_date: '2021-04-01' max_active_runs: 1 - schedule_interval: "@daily" # run once a week at Sunday 12am + schedule_interval: "@daily" catchup: False default_view: graph @@ -49,7 +49,7 @@ dag: args: - task_id: "covid19_italy_data_by_region_transform_csv" + task_id: "data_by_region_transform_csv" startup_timeout_seconds: 600 # The name of the pod in which the task will run. This will be used (plus a random suffix) to generate a pod id @@ -65,7 +65,7 @@ dag: # Set the environment variables you need initialized in the container. Use these as input variables for the script your container is expected to perform. env_vars: - SOURCE_URL: https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni.csv + SOURCE_URL: "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-regioni/dpc-covid19-ita-regioni.csv" SOURCE_FILE: "files/data.csv" TARGET_FILE: "files/data_output.csv" TARGET_GCS_BUCKET: "{{ var.json.shared.composer_bucket }}" @@ -85,7 +85,7 @@ dag: description: "Task to load CSV data to a BigQuery table" args: - task_id: "load_covid19_italy_data_by_region_to_bq" + task_id: "load_data_by_region_to_bq" # The GCS bucket where the CSV file is located in. bucket: "{{ var.json.shared.composer_bucket }}" @@ -168,4 +168,4 @@ dag: graph_paths: - - "covid19_italy_data_by_region_transform_csv >> load_covid19_italy_data_by_region_to_bq" + - "data_by_region_transform_csv >> load_data_by_region_to_bq" diff --git a/datasets/covid19_italy/national_trends/national_trends_dag.py b/datasets/covid19_italy/national_trends/national_trends_dag.py index f70e39ee4..8200005f2 100644 --- a/datasets/covid19_italy/national_trends/national_trends_dag.py +++ b/datasets/covid19_italy/national_trends/national_trends_dag.py @@ -33,8 +33,8 @@ ) as dag: # Run CSV transform within kubernetes pod - covid19_italy_national_trends_transform_csv = kubernetes_pod_operator.KubernetesPodOperator( - task_id="covid19_italy_national_trends_transform_csv", + national_trends_transform_csv = kubernetes_pod_operator.KubernetesPodOperator( + task_id="national_trends_transform_csv", startup_timeout_seconds=600, name="covid19_italy_national_trends", namespace="default", @@ -54,64 +54,54 @@ ) # Task to load CSV data to a BigQuery table - load_covid19_italy_national_trends_to_bq = ( - gcs_to_bq.GoogleCloudStorageToBigQueryOperator( - task_id="load_covid19_italy_national_trends_to_bq", - bucket="{{ var.json.shared.composer_bucket }}", - source_objects=["data/covid19_italy/national_trends/data_output.csv"], - source_format="CSV", - destination_project_dataset_table="covid19_italy.national_trends", - skip_leading_rows=1, - write_disposition="WRITE_TRUNCATE", - schema_fields=[ - {"name": "date", "type": "TIMESTAMP", "mode": "NULLABLE"}, - {"name": "country", "type": "STRING", "mode": "NULLABLE"}, - { - "name": "hospitalized_patients_symptoms", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "hospitalized_patients_intensive_care", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "total_hospitalized_patients", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "home_confinement_cases", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "total_current_confirmed_cases", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "new_current_confirmed_cases", - "type": "INTEGER", - "mode": "NULLABLE", - }, - { - "name": "new_total_confirmed_cases", - "type": "INTEGER", - "mode": "NULLABLE", - }, - {"name": "recovered", "type": "INTEGER", "mode": "NULLABLE"}, - {"name": "deaths", "type": "INTEGER", "mode": "NULLABLE"}, - { - "name": "total_confirmed_cases", - "type": "INTEGER", - "mode": "NULLABLE", - }, - {"name": "tests_performed", "type": "INTEGER", "mode": "NULLABLE"}, - {"name": "note", "type": "STRING", "mode": "NULLABLE"}, - ], - ) + load_national_trends_to_bq = gcs_to_bq.GoogleCloudStorageToBigQueryOperator( + task_id="load_national_trends_to_bq", + bucket="{{ var.json.shared.composer_bucket }}", + source_objects=["data/covid19_italy/national_trends/data_output.csv"], + source_format="CSV", + destination_project_dataset_table="covid19_italy.national_trends", + skip_leading_rows=1, + write_disposition="WRITE_TRUNCATE", + schema_fields=[ + {"name": "date", "type": "TIMESTAMP", "mode": "NULLABLE"}, + {"name": "country", "type": "STRING", "mode": "NULLABLE"}, + { + "name": "hospitalized_patients_symptoms", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "hospitalized_patients_intensive_care", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "total_hospitalized_patients", + "type": "INTEGER", + "mode": "NULLABLE", + }, + {"name": "home_confinement_cases", "type": "INTEGER", "mode": "NULLABLE"}, + { + "name": "total_current_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "new_current_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + { + "name": "new_total_confirmed_cases", + "type": "INTEGER", + "mode": "NULLABLE", + }, + {"name": "recovered", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "deaths", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "total_confirmed_cases", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "tests_performed", "type": "INTEGER", "mode": "NULLABLE"}, + {"name": "note", "type": "STRING", "mode": "NULLABLE"}, + ], ) - covid19_italy_national_trends_transform_csv >> load_covid19_italy_national_trends_to_bq + national_trends_transform_csv >> load_national_trends_to_bq diff --git a/datasets/covid19_italy/national_trends/pipeline.yaml b/datasets/covid19_italy/national_trends/pipeline.yaml index 575162d54..6cfec9ab4 100644 --- a/datasets/covid19_italy/national_trends/pipeline.yaml +++ b/datasets/covid19_italy/national_trends/pipeline.yaml @@ -20,7 +20,7 @@ resources: table_id: national_trends # Description of the table - description: "covid19 Italy National Trends" + description: "COVID-19 Italy National Trends" dag: @@ -36,7 +36,7 @@ dag: depends_on_past: False start_date: '2021-04-01' max_active_runs: 1 - schedule_interval: "@daily" # run once a week at Sunday 12am + schedule_interval: "@daily" catchup: False default_view: graph @@ -49,7 +49,7 @@ dag: args: - task_id: "covid19_italy_national_trends_transform_csv" + task_id: "national_trends_transform_csv" startup_timeout_seconds: 600 @@ -66,7 +66,7 @@ dag: # Set the environment variables you need initialized in the container. Use these as input variables for the script your container is expected to perform. env_vars: - SOURCE_URL: https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-andamento-nazionale/dpc-covid19-ita-andamento-nazionale.csv + SOURCE_URL: "https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-andamento-nazionale/dpc-covid19-ita-andamento-nazionale.csv" SOURCE_FILE: "files/data.csv" TARGET_FILE: "files/data_output.csv" TARGET_GCS_BUCKET: "{{ var.json.shared.composer_bucket }}" @@ -86,7 +86,7 @@ dag: description: "Task to load CSV data to a BigQuery table" args: - task_id: "load_covid19_italy_national_trends_to_bq" + task_id: "load_national_trends_to_bq" # The GCS bucket where the CSV file is located in. bucket: "{{ var.json.shared.composer_bucket }}" @@ -153,9 +153,4 @@ dag: mode: "NULLABLE" graph_paths: - - "covid19_italy_national_trends_transform_csv >> load_covid19_italy_national_trends_to_bq" - - - - - + - "national_trends_transform_csv >> load_national_trends_to_bq" From 9768fff2aafa8100c3ec3fcc9b75da511d465f77 Mon Sep 17 00:00:00 2001 From: Varshika Gupta Date: Wed, 25 Aug 2021 17:31:03 +0000 Subject: [PATCH 3/3] fix: Onboard the COVID-19 Italy dataset --- .../_images/run_csv_transform_kub/csv_transform.py | 13 +++++-------- .../_images/run_csv_transform_kub/requirements.txt | 1 + .../_terraform/covid19_italy_dataset.tf | 2 +- datasets/covid19_italy/data_by_region/pipeline.yaml | 1 - datasets/covid19_italy/dataset.yaml | 4 ++-- .../covid19_italy/national_trends/pipeline.yaml | 1 - 6 files changed, 9 insertions(+), 13 deletions(-) diff --git a/datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py b/datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py index 64f94a6c2..5459231fe 100644 --- a/datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py +++ b/datasets/covid19_italy/_images/run_csv_transform_kub/csv_transform.py @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -# CSV transform for: covid19_italy - import datetime import json import logging @@ -35,7 +33,7 @@ def main( headers: typing.List[str], rename_mappings: dict, pipeline_name: str, -): +) -> None: logging.info( "Covid-19 Italy process started at " @@ -85,26 +83,25 @@ def main( ) upload_file_to_gcs(target_file, target_gcs_bucket, target_gcs_path) - # log completion logging.info( "Covid-19 Italy process completed at " + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) ) -def convert_dt_format(date_str: str, time_str: str): +def convert_dt_format(date_str: str, time_str: str) -> None: return str(datetime.datetime.strptime(date_str, "%m/%d/%Y").date()) + " " + time_str -def rename_headers(df, rename_mappings: dict): +def rename_headers(df: pd.DataFrame, rename_mappings: dict) -> None: df.rename(columns=rename_mappings, inplace=True) -def save_to_new_file(df, file_path: str): +def save_to_new_file(df: pd.DataFrame, file_path: str) -> None: df.to_csv(file_path, float_format="%.0f", index=False) -def download_file(source_url: str, source_file: pathlib.Path): +def download_file(source_url: str, source_file: pathlib.Path) -> None: logging.info(f"Downloading {source_url} into {source_file}") r = requests.get(source_url, stream=True) if r.status_code == 200: diff --git a/datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt b/datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt index cd54d905c..1c45cdfc3 100644 --- a/datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt +++ b/datasets/covid19_italy/_images/run_csv_transform_kub/requirements.txt @@ -1,2 +1,3 @@ requests google-cloud-storage +pandas diff --git a/datasets/covid19_italy/_terraform/covid19_italy_dataset.tf b/datasets/covid19_italy/_terraform/covid19_italy_dataset.tf index 331a89fc9..8e2ac7e6c 100644 --- a/datasets/covid19_italy/_terraform/covid19_italy_dataset.tf +++ b/datasets/covid19_italy/_terraform/covid19_italy_dataset.tf @@ -18,7 +18,7 @@ resource "google_bigquery_dataset" "covid19_italy" { dataset_id = "covid19_italy" project = var.project_id - description = "ETL Processes for Covid 19 Italy" + description = "COVID-19 Italy" } output "bigquery_dataset-covid19_italy-dataset_id" { diff --git a/datasets/covid19_italy/data_by_region/pipeline.yaml b/datasets/covid19_italy/data_by_region/pipeline.yaml index ea7883169..d18c73a8d 100644 --- a/datasets/covid19_italy/data_by_region/pipeline.yaml +++ b/datasets/covid19_italy/data_by_region/pipeline.yaml @@ -24,7 +24,6 @@ resources: dag: -# [Required] Specify the Airflow version of the operators used by the DAG. airflow_version: 1 initialize: diff --git a/datasets/covid19_italy/dataset.yaml b/datasets/covid19_italy/dataset.yaml index 330feafba..46a33a60e 100644 --- a/datasets/covid19_italy/dataset.yaml +++ b/datasets/covid19_italy/dataset.yaml @@ -23,7 +23,7 @@ dataset: friendly_name: covid19_italy # A short, descriptive summary of the dataset. - description: covid19_italy + description: COVID-19 Italy # A list of sources the dataset is derived from, using the YAML list syntax. dataset_sources: ~ @@ -55,4 +55,4 @@ resources: # description (A user-friendly description of the dataset) # location (The geographic location where the dataset should reside) dataset_id: covid19_italy - description: "ETL Processes for Covid 19 Italy" + description: "COVID-19 Italy" diff --git a/datasets/covid19_italy/national_trends/pipeline.yaml b/datasets/covid19_italy/national_trends/pipeline.yaml index 6cfec9ab4..db9d91ca0 100644 --- a/datasets/covid19_italy/national_trends/pipeline.yaml +++ b/datasets/covid19_italy/national_trends/pipeline.yaml @@ -24,7 +24,6 @@ resources: dag: - # [Required] Specify the Airflow version of the operators used by the DAG. airflow_version: 1 initialize: