# 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
    table_id: sales
    description: "Sales Dataset"
dag:
  airflow_version: 2
  initialize:
    dag_id: sales
    default_args:
      owner: "Google"
      depends_on_past: False
      start_date: '2022-10-27'
    max_active_runs: 1
    schedule_interval: "@weekly"
    catchup: False
    default_view: graph

  tasks:
    - operator: "BashOperator"
      description: "Download data"
      args:
        task_id: "bash_download"
        bash_command: |
          curl https://data.iowa.gov/api/views/m3tr-qhgy/rows.csv | gsutil cp - gs://{{ var.value.composer_bucket }}/data/iowa_liquor_sales/raw_files/data.csv

    - operator: "BashOperator"
      description: "Split data file into smaller chunks"
      args:
        task_id: "bash_split"
        bash_command: tail -n +2 /home/airflow/gcs/data/iowa_liquor_sales/raw_files/data.csv | split -d -l 4000000 - --filter='sh -c "{ head -n1 /home/airflow/gcs/data/iowa_liquor_sales/raw_files/data.csv; cat; } > $FILE"' /home/airflow/gcs/data/iowa_liquor_sales/raw_files/split_data_ ;

    - operator: "GKECreateClusterOperator"
      args:
        task_id: "create_cluster"
        project_id: "{{ var.value.gcp_project }}"
        location: "us-central1-c"
        body:
          name: pdp-iowa-liquor-sales
          initial_node_count: 1
          network: "{{ var.value.vpc_network }}"
          node_config:
            machine_type: e2-standard-16
            oauth_scopes:
              - https://www.googleapis.com/auth/devstorage.read_write
              - https://www.googleapis.com/auth/cloud-platform

    - operator: "GKEStartPodOperator"
      description: "Run CSV transform within kubernetes pod"
      args:
        task_id: "kub_transform_csv"
        startup_timeout_seconds: 1000
        name: "Sales"
        namespace: "default"
        project_id: "{{ var.value.gcp_project }}"
        location: "us-central1-c"
        cluster_name: pdp-iowa-liquor-sales
        image_pull_policy: "Always"
        image: "{{ var.json.iowa_liquor_sales.container_registry.run_csv_transform_kub }}"
        env_vars:
          SOURCE_URL: "data/iowa_liquor_sales/raw_files/"
          DOWNLOAD_LOCATION: ""
          TARGET_FILE: "data_output"
          CHUNKSIZE: "1000000"
          TARGET_GCS_BUCKET: "{{ var.value.composer_bucket }}"
          SOURCE_GCS_PATH: "data/iowa_liquor_sales/raw_files/"
          TARGET_GCS_PATH: "data/iowa_liquor_sales/transformed_files/data_output_"
          HEADERS: >-
            [
              "invoice_and_item_number",
              "date",
              "store_number",
              "store_name",
              "address",
              "city",
              "zip_code",
              "store_location",
              "county_number",
              "county",
              "category",
              "category_name",
              "vendor_number",
              "vendor_name",
              "item_number",
              "item_description",
              "pack",
              "bottle_volume_ml",
              "state_bottle_cost",
              "state_bottle_retail",
              "bottles_sold",
              "sale_dollars",
              "volume_sold_liters",
              "volume_sold_gallons"
            ]
          RENAME_MAPPINGS: >-
            {
              "Invoice/Item Number":"invoice_and_item_number",
              "Date":"date",
              "Store Number":"store_number",
              "Store Name":"store_name",
              "Address":"address",
              "City":"city",
              "Zip Code":"zip_code",
              "Store Location":"store_location",
              "County Number":"county_number",
              "County":"county",
              "Category":"category",
              "Category Name":"category_name",
              "Vendor Number":"vendor_number",
              "Vendor Name":"vendor_name",
              "Item Number":"item_number",
              "Item Description":"item_description",
              "Pack":"pack",
              "Bottle Volume (ml)":"bottle_volume_ml",
              "State Bottle Cost":"state_bottle_cost",
              "State Bottle Retail":"state_bottle_retail",
              "Bottles Sold":"bottles_sold",
              "Sale (Dollars)":"sale_dollars",
              "Volume Sold (Liters)":"volume_sold_liters",
              "Volume Sold (Gallons)":"volume_sold_gallons"
            }
        container_resources:
          memory:
            request: "16Gi"
          cpu:
            request: "1"
          ephemeral-storage:
            request: "10Gi"

    - operator: "GKEDeleteClusterOperator"
      args:
        task_id: "delete_cluster"
        project_id: "{{ var.value.gcp_project }}"
        location: "us-central1-c"
        name: pdp-iowa-liquor-sales

    - operator: "BashOperator"
      description: "Combine the split files into one"
      args:
        task_id: "bash_concatenate"
        bash_command: "touch /home/airflow/gcs/data/iowa_liquor_sales/transformed_files/final_output.csv; awk 'FNR>1' /home/airflow/gcs/data/iowa_liquor_sales/transformed_files/data_output_*.csv  > /home/airflow/gcs/data/iowa_liquor_sales/transformed_files/final_output.csv ;"

    - operator: "GoogleCloudStorageToBigQueryOperator"
      description: "Task to load CSV data to a BigQuery table"
      args:
        task_id: "load_to_bq"
        bucket: "{{ var.value.composer_bucket }}"
        source_objects: ["data/iowa_liquor_sales/transformed_files/final_output.csv"]
        source_format: "CSV"
        destination_project_dataset_table: "iowa_liquor_sales.sales"
        allow_quoted_newlines: True
        write_disposition: "WRITE_TRUNCATE"
        schema_fields:
          - name: "invoice_and_item_number"
            type: "STRING"
            mode: "NULLABLE"
            description: "Concatenated invoice and line number associated with the liquor order. This provides a unique identifier for the individual liquor products included in the store order."
          - name: "date"
            type: "DATE"
            description: "Date of order"
            mode: "NULLABLE"
          - name: "store_number"
            type: "STRING"
            description: "Unique number assigned to the store who ordered the liquor."
            mode: "NULLABLE"
          - name: "store_name"
            type: "STRING"
            description: "Name of store who ordered the liquor."
            mode: "NULLABLE"
          - name: "address"
            type: "STRING"
            description: "Address of store who ordered the liquor."
            mode: "NULLABLE"
          - name: "city"
            type: "STRING"
            description: "City where the store who ordered the liquor is located"
            mode: "NULLABLE"
          - name: "zip_code"
            type: "STRING"
            description: "Zip code where the store who ordered the liquor is located"
            mode: "NULLABLE"
          - name: "store_location"
            type: "GEOGRAPHY"
            description: "Location of store who ordered the liquor. The Address, City, State and Zip Code are geocoded to provide geographic coordinates. Accuracy of geocoding is dependent on how well the address is interpreted and the completeness of the reference data used."
            mode: "NULLABLE"
          - name: "county_number"
            type: "STRING"
            description: "Iowa county number for the county where store who ordered the liquor is located"
            mode: "NULLABLE"
          - name: "county"
            type: "STRING"
            description: "County where the store who ordered the liquor is located"
            mode: "NULLABLE"
          - name: "category"
            type: "STRING"
            description: "Category code associated with the liquor ordered"
            mode: "NULLABLE"
          - name: "category_name"
            type: "STRING"
            description: "Category of the liquor ordered."
            mode: "NULLABLE"
          - name: "vendor_number"
            type: "STRING"
            description: "The vendor number of the company for the brand of liquor ordered"
            mode: "NULLABLE"
          - name: "vendor_name"
            type: "STRING"
            description: "The vendor name of the company for the brand of liquor ordered"
            mode: "NULLABLE"
          - name: "item_number"
            type: "STRING"
            description: "Item number for the individual liquor product ordered."
            mode: "NULLABLE"
          - name: "item_description"
            type: "STRING"
            description: "Description of the individual liquor product ordered."
            mode: "NULLABLE"
          - name: "pack"
            type: "INTEGER"
            description: "The number of bottles in a case for the liquor ordered"
            mode: "NULLABLE"
          - name: "bottle_volume_ml"
            type: "INTEGER"
            description: "Volume of each liquor bottle ordered in milliliters."
            mode: "NULLABLE"
          - name: "state_bottle_cost"
            type: "FLOAT"
            description: "The amount that Alcoholic Beverages Division paid for each bottle of liquor ordered"
            mode: "NULLABLE"
          - name: "state_bottle_retail"
            type: "FLOAT"
            description: "The amount the store paid for each bottle of liquor ordered"
            mode: "NULLABLE"
          - name: "bottles_sold"
            type: "INTEGER"
            description: "The number of bottles of liquor ordered by the store"
            mode: "NULLABLE"
          - name: "sale_dollars"
            type: "FLOAT"
            description: "Total cost of liquor order (number of bottles multiplied by the state bottle retail)"
            mode: "NULLABLE"
          - name: "volume_sold_liters"
            type: "FLOAT"
            description: Total volume of liquor ordered in liters. (i.e. (Bottle Volume (ml) x Bottles Sold)/1,000)"
            mode: "NULLABLE"
          - name: "volume_sold_gallons"
            type: "FLOAT"
            description: Total volume of liquor ordered in gallons. (i.e. (Bottle Volume (ml) x Bottles Sold)/3785.411784)"
            mode: "NULLABLE"


  graph_paths:
    - "bash_download >> bash_split >> create_cluster >> kub_transform_csv >> delete_cluster >> bash_concatenate >> load_to_bq"
