# azarenkov/aitu-keeper architecture

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

- Interactive diagram: https://gitdiagram.com/azarenkov/aitu-keeper
- Repository: https://github.com/azarenkov/aitu-keeper

## Overview

##### Main Components
- Backend (API) Layer: Highlight the controllers (located in src/controllers). Note that there are specific controllers for courses, deadlines, grades, and users, which expose endpoints.
- Service Layer: Include the services (in src/services) that encapsulate business logic. Services like data_service and producer_service represent the core processing logic.
- Repository Layer: Represent the repositories (src/repositories) handling data interactions with the underlying storage (MongoDB).
- Infrastructure Layer: This consists of:
- App setup and initialization (app_setup.rs)
- External service integrations:
- Moodle Client (src/infrastructure/client/moodle_client.rs) for fetching Moodle data.
- Database connection module for MongoDB (src/infrastructure/db).
- Event producer modules (src/infrastructure/event_producer) that interact with Apache Kafka.
- Configuration: src/config.rs for configuration settings.
- External Systems: Clearly represent external tools and services:
- Moodle – the source of the raw educational data.
- MongoDB – persistent storage.
- Apache Kafka – message/event bus.
- Firebase Cloud Messaging – for notifications.

##### Relationships and Data Flow
- Incoming Requests: Clients (could be dashboards or mobile devices) send requests to the controllers.
- Data Handling: Controllers call the service layer to process business logic.
- Persistence: Services interact with repositories to read/write data to MongoDB.
- Integration: When data changes (e.g., grade updates), the service triggers:
a. An event producer in the infrastructure layer to send a message to Kafka.
b. Downstream, Kafka events are used to notify client devices via Firebase Cloud Messaging.
- External Data Fetch: A separate integration point via the Moodle client fetches the initial or updated data from Moodle.

##### Architectural Patterns
- Layered Architecture: The clear separation between controllers, services, repositories, and infrastructure.
- Integration with External Systems: Use of a modular design to isolate service integration (Moodle API, database, Kafka) helps maintain a decoupled and testable codebase.

##### Final Assembly
- Follow the flow into the repository (data persistence) and infrastructure modules (Moodle client, event producer).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client("Client"):::external

    %% API Layer Subgraph
    subgraph "API Layer"
        CC("Course Controller"):::controller
        DC("Deadline Controller"):::controller
        GC("Grade Controller"):::controller
        UC("User Controller"):::controller
    end

    %% Service Layer Subgraph
    subgraph "Service Layer"
        DS("Data Service"):::service
        PS("Producer Service"):::service
    end

    %% Repository & Infrastructure Layer Subgraph
    subgraph "Repository & Infrastructure Layer"
        DR("Data Repository"):::repository
        MC("Moodle Client"):::infrastructure
        MS("MongoDB Connection"):::infrastructure
        KC("Kafka Producer"):::infrastructure
        AS("App Setup"):::infrastructure
        CFG("Configuration"):::config
    end

    %% External Systems Subgraph
    subgraph "External Systems"
        Moodle("Moodle"):::external
        Mongo("MongoDB"):::external
        Kafka("Kafka"):::external
        FCM("Firebase Cloud Messaging"):::external
    end

    %% Connections from Client to Controllers
    Client -->|"sends_request"| CC
    Client -->|"sends_request"| DC
    Client -->|"sends_request"| GC
    Client -->|"sends_request"| UC

    %% Connections from Controllers to Service Layer
    CC -->|"calls"| DS
    DC -->|"calls"| DS
    GC -->|"calls"| DS
    UC -->|"calls"| DS

    %% Specific event-triggering connection from Grade Controller to Producer Service
    GC -->|"triggers"| PS

    %% Connections from Service Layer to Repository & Infrastructure
    DS -->|"persists_data"| DR
    DS -->|"fetches_data"| MC
    PS -->|"publishes_event"| KC

    %% Infrastructure to External Systems
    DR -->|"stores_data"| Mongo
    MC -->|"pulls_data"| Moodle
    KC -->|"sends_event"| Kafka
    Kafka -->|"notifies"| FCM

    %% App Setup Initialization
    AS -->|"initializes"| CFG

    %% Styles
    classDef controller fill:#add8e6,stroke:#000,stroke-width:1px;
    classDef service fill:#90ee90,stroke:#000,stroke-width:1px;
    classDef repository fill:#f7e7ce,stroke:#000,stroke-width:1px;
    classDef infrastructure fill:#dda0dd,stroke:#000,stroke-width:1px;
    classDef config fill:#ffe4e1,stroke:#000,stroke-width:1px;
    classDef external fill:#d3d3d3,stroke:#000,stroke-width:1px;

    %% Click Events
    click CC "https://github.com/azarenkov/aitu-keeper/blob/master/src/controllers/course_controller.rs"
    click DC "https://github.com/azarenkov/aitu-keeper/blob/master/src/controllers/deadline_controller.rs"
    click GC "https://github.com/azarenkov/aitu-keeper/blob/master/src/controllers/grade_controller.rs"
    click UC "https://github.com/azarenkov/aitu-keeper/blob/master/src/controllers/user_controller.rs"
    click DS "https://github.com/azarenkov/aitu-keeper/blob/master/src/services/data_service.rs"
    click PS "https://github.com/azarenkov/aitu-keeper/blob/master/src/services/producer_service.rs"
    click DR "https://github.com/azarenkov/aitu-keeper/blob/master/src/repositories/data_repository.rs"
    click AS "https://github.com/azarenkov/aitu-keeper/blob/master/src/infrastructure/app_setup.rs"
    click MC "https://github.com/azarenkov/aitu-keeper/blob/master/src/infrastructure/client/moodle_client.rs"
    click MS "https://github.com/azarenkov/aitu-keeper/tree/master/src/infrastructure/db"
    click KC "https://github.com/azarenkov/aitu-keeper/blob/master/src/infrastructure/event_producer/producer.rs"
    click CFG "https://github.com/azarenkov/aitu-keeper/blob/master/src/config.rs"
```
