Git Product home page Git Product logo

apache-airflow-providers-mnb's Introduction

MNB Provider for Apache Airflow

This package contains all the necessary operators, hooks and sensors necessary to access the daily exchange rates published by MNB (Central Bank of Hungary) in Apache Airflow.

Installation

You can install it with any package manager compatible with PyPI:

pip install apache-airflow-provider-mnb

Components

Component Notes
MnbHook Implements low-level functions to interact with MNB's API
MnbExchangeRateOperator Provides access to the exchange rates published on a specific day
MnbExchangeRateSensor Senses whether the rates were already published for a specific day

Example

This following example demonstrates a typical use case:

  1. Start running at 8:00AM every day
  2. Check if the exchange rates were already published (repeat every 10 minutes)
  3. Permanently fail after 3 hours if the rates were never published (there are no rates available on the weekends and public holidays)
  4. Generate the SQL commands
  5. Run them against a PostgreSQL database
from datetime import date
import json
import pendulum
from airflow.decorators import dag, task
from airflow.providers.postgres.operators.postgres import PostgresOperator
from airflow.providers.mnb.sensors.mnb import MnbExchangeRateSensor
from airflow.providers.mnb.operators.mnb import MnbExchangeRateOperator

@dag(
    dag_id="refresh_currency_rates",
    description="Refresh currency exchange rates",
    schedule="0 8 * * *",
    start_date=pendulum.datetime(2023, 1, 16, tz="Europe/Budapest"),
    catchup=False
)
def mnb():
    is_exchange_rate_available = MnbExchangeRateSensor(
        task_id="is_exchange_rate_available",
        timeout=10800,
        poke_interval=600,
        date="{{ ds }}"
    )
    
    exchange_rates = MnbExchangeRateOperator(
        task_id="get_exchange_rates",
        date="{{ ds }}"
    )
    
    @task
    def generate_queries(exchange_rates: str):
        rates = json.loads(exchange_rates)
        queries = ""
        mnb_date = rates["date"]
        for rate in rates["rates"]:
            currency_id = rate["currency"]
            mnb_rate = rate["rate"]
            queries += f"UPDATE finance.currency SET mnb_rate = '{mnb_rate}', mnb_date = '{mnb_date}' WHERE currency_id = '{currency_id}';\n"
        return queries

    is_exchange_rate_available >> exchange_rates

    queries = generate_queries(exchange_rates.output)
    PostgresOperator(
        task_id="update_exchange_rates",
        postgres_conn_id="postgres_default",
        database="erp",
        sql=queries)

mnb()

apache-airflow-providers-mnb's People

Contributors

belidzs avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.