# guilogar/tfm architecture

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

- Interactive diagram: https://gitdiagram.com/guilogar/tfm
- Repository: https://github.com/guilogar/tfm

## Overview

##### Main Components
- Backend Service: This is built in Node.js with Express (as suggested by app.js and routes). It contains API endpoints, middleware, and multiple services to handle tasks such as user authentication, crop events, notifications, scheduled tasks (cron jobs), and integration with external services.
- Database: The backend connects to a PostgreSQL database (as detailed in the README and docker-compose.yml) using Sequelize ORM. It also has interactions with Cosmos DB (e.g., config-cosmos.js and related get/set event services).
- Firebase Service: Both backend and frontend include configuration for Firebase (Firebase service account for the backend and various credentials files for the frontend including Android and public configurations). This supports notifications or authentication services.
- Frontend Client: Developed with Ionic (and likely React given the “REACT_APP” environment variables and file structure in the “frontend/src” directory). It is built to run on web browsers and mobile (Android via Capacitor), offering screens for crops, events, notifications, irrigation, and more.
- Predictions Module: A separate Python-based module (using conda, SQLAlchemy, and psycopg2) that performs predictions related to irrigate data. This module is likely scheduled or triggered via npm tasks and interacts with the PostgreSQL database.

##### Relationships and Interactions
- The Frontend communicates via RESTful API calls to the Backend Service to fetch data, trigger events, and update state.
- The Backend handles business logic, interfacing with the PostgreSQL database for persistent storage via Sequelize.
- The Backend also interacts with external services such as Cosmos DB (for some specific event or data processing tasks) and Firebase (for push notifications and authentication, plus receiving Firebase tokens).
- The Predictions module runs computations (in Python) which may extract or update data in the PostgreSQL database, likely providing predictive insights on irrigate events.
- Scheduled tasks (cron jobs within the backend) periodically fetch or update events.
- Docker Compose is used to orchestrate several components (e.g., database, backend services) for local development.

##### Important Architectural Patterns & Technologies
- Separation of Concerns: The clear segmentation into frontend, backend, and predictions accentuates modularity.
- MVC-Like structure in the backend: Routes, services, middleware, and database models are clearly separated.
- Microservice-like aspects: With a distinct Python predictions module and independent deployment for mobile and web frontends, the architecture leverages loosely coupled components.
- Use of modern web technologies: Ionic with React on the frontend, Node.js/Express on the backend, and Sequelize ORM for relational data management.
- Integration with external services: Firebase for notifications and Cosmos DB for non-relational data storage.

##### Specific Elements
- “Frontend (Ionic/React)” for mobile and web client.
- “Backend (Node.js/Express)” including sub-components like API routes, middleware, and scheduled tasks.
- “PostgreSQL Database (Sequelize)” and “Cosmos DB” for external data sources.
- “Predictions Module (Python)” with libraries SQLAlchemy and psycopg2.
- “Firebase Services” for authentication and notifications.
- Data flow from Frontend → Backend (HTTP calls).
- Backend → Database (CRUD operations via Sequelize) and Backend → Cosmos DB (event syncing).
- Backend → Firebase (for token management/notifications).
- Predictions Module ↔ PostgreSQL Database.
- Different languages or stacks (e.g., blue for Node.js components, green for the database, orange for Python predictions).
- Different deployment or architectural layers.
- Notes for design patterns (like MVC in the backend or separation of concerns between predictions and main API).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Frontend Layer
    subgraph "Frontend"
        FE["Frontend Client (Ionic/React)"]:::frontend
        FE_FM["Firebase Messaging Worker"]:::firebase
        FE_Android["Android Config"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend"
        BE_App["API Server (app.js)"]:::backend
        BE_Routes["API Routes"]:::backend
        BE_Middleware["Middleware"]:::backend
        BE_CronGet["Cron Job Get Events"]:::backend
        BE_CronSet["Cron Job Set Events"]:::backend
        BE_Cosmos["Cosmos DB Integration"]:::backend
    end

    %% Database Layer
    subgraph "Database"
        DB_Main["PostgreSQL Database (Sequelize)"]:::database
        DB_Models["Database Models"]:::database
        Docker["Docker Compose (PostgreSQL)"]:::database
    end

    %% Predictions Module Layer
    subgraph "Predictions Module"
        PRED_Module["Predictions Module (Python)"]:::predictions
        PRED_Irrigate["Irrigate Predictions"]:::predictions
        PRED_Constants["Prediction Constants"]:::predictions
    end

    %% Firebase Layer
    subgraph "Firebase"
        FB_Service["Firebase Service"]:::firebase
        FB_Token["Firebase Token Model"]:::firebase
    end

    %% External Service
    subgraph "External"
        COS["Cosmos DB"]:::cosmos
    end

    %% Arrows representing interactions
    FE -->|"HTTP"| BE_App
    BE_App -->|"Uses"| BE_Routes
    BE_App -->|"Uses"| BE_Middleware
    BE_App -->|"Schedules"| BE_CronGet
    BE_App -->|"Schedules"| BE_CronSet
    BE_App -->|"Integrates"| BE_Cosmos
    BE_App -->|"CRUD"| DB_Main
    BE_Routes -->|"DBOperations"| DB_Models
    BE_CronGet -->|"PeriodicUpdates"| DB_Main
    BE_CronSet -->|"PeriodicUpdates"| DB_Main
    BE_Cosmos -->|"SyncsWith"| COS

    PRED_Module -->|"DataProcessing"| DB_Main
    PRED_Irrigate --> PRED_Module
    PRED_Constants --> PRED_Module
    DB_Main -->|"UpdatesPredicts"| PRED_Module

    FE_FM -->|"Notifications"| FB_Service
    BE_App -->|"Notifications"| FB_Service
    FB_Service -->|"TokenStorage"| FB_Token

    %% Invisible links to encourage vertical layout
    FE --> BE_App
    BE_App --> DB_Main
    DB_Main --> PRED_Module
    BE_App --> FB_Service

    %% Click Events for Backend Service components
    click BE_App "https://github.com/guilogar/tfm/blob/main/backend/app.js"
    click BE_Routes "https://github.com/guilogar/tfm/tree/main/backend/routes"
    click BE_Middleware "https://github.com/guilogar/tfm/blob/main/backend/middleware.js"
    click BE_CronGet "https://github.com/guilogar/tfm/blob/main/backend/cron-tab-get-events.js"
    click BE_CronSet "https://github.com/guilogar/tfm/blob/main/backend/cron-tab-set-events.js"
    click BE_Cosmos "https://github.com/guilogar/tfm/blob/main/backend/config-cosmos.js"

    %% Click Events for Database components
    click DB_Models "https://github.com/guilogar/tfm/tree/main/backend/database"
    click Docker "https://github.com/guilogar/tfm/blob/main/docker-compose.yml"

    %% Click Events for Firebase Service components
    click FB_Token "https://github.com/guilogar/tfm/blob/main/backend/database/models/FirebaseToken.js"
    click FE_FM "https://github.com/guilogar/tfm/blob/main/frontend/public/firebase-messaging-sw.js"

    %% Click Events for Frontend components
    click FE "https://github.com/guilogar/tfm/tree/main/frontend"
    click FE_Android "https://github.com/guilogar/tfm/tree/main/frontend/android"

    %% Click Events for Predictions Module components
    click PRED_Module "https://github.com/guilogar/tfm/tree/main/predictions"
    click PRED_Irrigate "https://github.com/guilogar/tfm/blob/main/predictions/predictions_irrigate.py"
    click PRED_Constants "https://github.com/guilogar/tfm/blob/main/predictions/constants/constants.py"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef backend fill:#87CEEB,stroke:#000,stroke-width:2px;
    classDef database fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef predictions fill:#FFA07A,stroke:#000,stroke-width:2px;
    classDef firebase fill:#FFD700,stroke:#000,stroke-width:2px;
    classDef cosmos fill:#DDA0DD,stroke:#000,stroke-width:2px;
```
