The DAG consists of five main tasks:
get_exchange_rates
: Fetches exchange rate data from an API.create_postgres_table
: Creates or ensures the existence of the required database table.create_index_and_upsert_data
: Creates a unique index and upserts the fetched data into the database.calculate_deviations
: Analyzes the data to find maximum deviations in exchange rates.send_telegram_message
: Sends a report of the findings via Telegram.
This DAG fetches daily exchange rates, processes them, and sends a report via Telegram. It demonstrates ETL processes, data analysis, and automated reporting using Apache Airflow.
- Apache Airflow
- PostgreSQL
- Python libraries: requests, pandas, telebot
Set the following environment variables in your Docker Compose file:
EXCHANGE_RATES_API_KEY
: Your API key for the exchange rates service.TELEGRAM_BOT_TOKEN
: Your Telegram bot token.TELEGRAM_CHAT_ID
: The chat ID where the bot should send messages.
Set up a PostgreSQL connection in Airflow with the ID postgres_default
.
- Schedule: Daily at 1:00 AM
- Start Date: September 1, 2024
- Catchup: Enabled
Fetches exchange rate data from an API and saves it as a JSON file.
Creates the exchange_rates
table if it doesn't exist.
Creates a unique index on the exchange_rates
table and upserts the fetched data.
Calculates the maximum deviation in exchange rates and saves the result to a CSV file.
Reads the CSV file with maximum deviations and sends a formatted report via Telegram.
- Ensure all environment variables are set in the Docker Compose file.
- Place the DAG file in the Airflow DAGs folder.
- Start the Airflow environment.
- The DAG will run automatically according to the schedule, or you can trigger it manually from the Airflow UI.
- The DAG will populate the
exchange_rates
table in your PostgreSQL database. - A daily report will be sent to the specified Telegram chat, including:
- A formatted message with the maximum deviation details.
- A CSV file attachment with the full report.
- Ensure all required libraries are installed in your Airflow environment.
- Check Airflow logs for any error messages if the DAG fails.
- Verify that the Telegram bot has permission to send messages to the specified chat.