# nhatode/code architecture

> Architecture diagram of the GitHub repository nhatode/code, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nhatode/code
- Repository: https://github.com/nhatode/code

## Overview

##### Project Overview
- This is a Python-based notification system aimed at sending Microsoft Teams alerts when an Apache Airflow DAG fails. It also integrates with ServiceNow to enrich notifications with ticket information.
- The project is modular and configurable – it separates concerns into configuration, models representing data (Airflow failures and ServiceNow tickets), and notifiers (including a Teams notifier and a notification manager).

##### Main Components
a. Entry Point:
- main.py: The application’s main runner which orchestrates the notification process.
b. Configuration:
- config.py: Holds all the necessary settings (Teams webhook URL, message templates, and notification settings).
c. Data Models:
- airflow_failure.py: Defines how an Airflow DAG failure is represented.
- servicenow_ticket.py: Represents ServiceNow ticket information that may be attached to the failure notification.
d. Notifiers:
- teams_notifier.py: Responsible for sending alerts to Microsoft Teams.
- notification_manager.py: Likely acts as a coordinator that gathers information from the data models, applies the configuration, and then invokes the appropriate notifier.
e. External Dependencies/Services:
- Apache Airflow: The system which triggers the alerts on DAG failures.
- Microsoft Teams: The external service that receives the notifications.
- ServiceNow: Provides ticket details (or a reference) associated with each failure event.

##### Determining Relationships and Interactions
- The main.py acts as the orchestrator, loading the configuration and initializing the notification flow.
- When a DAG fails in Airflow, the failure event is captured and represented using the airflow_failure model.
- If available, additional context via ServiceNow ticket information is fetched and wrapped with the servicenow_ticket model.
- The notification_manager collects this data, applies the message template from the config, and forwards the information to the teams_notifier.
- The teams_notifier then formats and sends the alert through the configured Teams webhook.

##### Architectural Patterns and Design Principles
- Modular and Object-Oriented Design: The clear separation between models, configuration, and notifier modules shows that the system is designed to be extensible.
- Separation of Concerns: Each module handles a distinct part of the workflow (configuration reading, data modeling, notification logic).
- Configurability: The use of a centralized configuration file (config.py) that can be adjusted without modifying the core logic.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    A("Apache Airflow"):::external
    SN("ServiceNow"):::external
    MST("Microsoft Teams"):::external

    %% Internal Components
    subgraph "Internal Components"
        M("Main Application"):::internal
        C("Configuration"):::internal
        subgraph "Models"
            AF("Airflow Failure Model"):::model
            ST("ServiceNow Ticket Model"):::model
        end
        NM("Notification Manager"):::internal
        TN("Teams Notifier"):::internal
    end

    %% Data Flows
    A -->|"failureEvent"| M
    M -->|"loads"| C
    M -->|"orchestrates"| NM
    NM -->|"processesAF"| AF
    NM -->|"processesST"| ST
    NM <-->|"fetches"| SN
    NM -->|"notifies"| TN
    TN -->|"sendsAlert"| MST

    %% Click Events for Internal Components
    click M "https://github.com/nhatode/code/blob/msteams/airflow_teams_notifier/main.py"
    click C "https://github.com/nhatode/code/blob/msteams/airflow_teams_notifier/src/config.py"
    click AF "https://github.com/nhatode/code/blob/msteams/airflow_teams_notifier/src/models/airflow_failure.py"
    click ST "https://github.com/nhatode/code/blob/msteams/airflow_teams_notifier/src/models/servicenow_ticket.py"
    click NM "https://github.com/nhatode/code/blob/msteams/airflow_teams_notifier/src/notifiers/notification_manager.py"
    click TN "https://github.com/nhatode/code/blob/msteams/airflow_teams_notifier/src/notifiers/teams_notifier.py"

    %% Styles
    classDef internal fill:#cce5ff,stroke:#2A9DF4,stroke-width:2px,color:#000;
    classDef model fill:#d4edda,stroke:#28a745,stroke-width:2px,color:#000;
    classDef external fill:#f8d7da,stroke:#dc3545,stroke-width:2px,color:#000;
```
