# celiksa/analytics-ai architecture

> Architecture diagram of the GitHub repository celiksa/analytics-ai, 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/celiksa/analytics-ai
- Repository: https://github.com/celiksa/analytics-ai

## Overview

The Analytics AI project is a full-stack application designed to allow users to query PostgreSQL databases using natural language through an intelligent chat interface. The system leverages a React & TypeScript frontend along with a Python FastAPI backend that integrates with GPT-4o for query translation and result visualization.

##### Main Components
- Frontend: Represent the React-based application (located under the src directory), which manages chat UI, visualizations, and database selection.
- Backend: Show the FastAPI service (files located in the api directory) that processes API requests such as translating natural language questions to SQL, handling database connections, query processing, and generating visualizations.
- External Services: Include the LLM integration (OpenAI API) as an external dependency for query translation and analytics.

##### Their Relationships and Interactions
- Interaction between the backend and external services (OpenAI API) for natural language processing and query conversion.

##### Important Architectural Patterns and Design Principles
- Separation of concerns: Ensure a clear separation between the presentation layer (frontend UI) and business logic (backend FastAPI service).
- API Design: Illustrate the RESTful or WebSocket endpoints provided by FastAPI, as noted in the README.
- Modularization: Emphasize that the project is split into clearly defined directories (e.g., api for backend and src for frontend) to simplify development, testing, and deployment.
- Visualization handling: Note that the backend is responsible for processing query results and generating visualizations when applicable.

##### Relevant Technologies and Frameworks
- Frontend: React, TypeScript, TailwindCSS.
- Backend: Python, FastAPI, and a few supporting files such as pydantic_documentation.pdf, configuration files, and scripts (crawl.py, psql_agent.py, etc.).
- Database: PostgreSQL (with connection credentials managed via .env files).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend Layer"
        React_Frontend["React Frontend"]:::frontend
        Chat_UI_Component["Chat UI Component"]:::frontend
    end

    subgraph "Backend Layer"
        FastAPI_Backend["FastAPI Backend"]:::backend
        API_Routing_Config["API Routing & Config"]:::backend
        Query_Processing["Query Processing Module"]:::backend
        Visualization_Handler["Visualization Handler"]:::backend
        Data_Crawling["Data Crawling Script"]:::backend
    end

    PostgreSQL_DB["PostgreSQL Database"]:::database
    OpenAI_API["OpenAI API"]:::external

    %% Connections between layers
    React_Frontend -->|"HTTP/WebSocket"| FastAPI_Backend
    Chat_UI_Component --- React_Frontend

    %% Internal Backend module flow
    FastAPI_Backend -->|"routes"| API_Routing_Config
    API_Routing_Config -->|"processes"| Query_Processing
    API_Routing_Config -->|"generates"| Visualization_Handler
    API_Routing_Config -->|"initiates"| Data_Crawling

    %% Backend interactions with external systems
    FastAPI_Backend -->|"SQLqueries"| PostgreSQL_DB
    FastAPI_Backend -->|"querytranslation"| OpenAI_API

    %% Click Events
    click React_Frontend "https://github.com/celiksa/analytics-ai/tree/main/src"
    click Chat_UI_Component "https://github.com/celiksa/analytics-ai/blob/main/src/components/Chat.tsx"
    click FastAPI_Backend "https://github.com/celiksa/analytics-ai/tree/main/api"
    click API_Routing_Config "https://github.com/celiksa/analytics-ai/blob/main/api/api.py"
    click Query_Processing "https://github.com/celiksa/analytics-ai/blob/main/api/psql_agent.py"
    click Visualization_Handler "https://github.com/celiksa/analytics-ai/blob/main/api/visualization_handler.py"
    click Data_Crawling "https://github.com/celiksa/analytics-ai/blob/main/api/crawl.py"

    %% Styles
    classDef frontend fill:#ffe0b3,stroke:#bf5700,stroke-width:2px;
    classDef backend fill:#cce5ff,stroke:#336699,stroke-width:2px;
    classDef database fill:#d0f0c0,stroke:#228B22,stroke-width:2px;
    classDef external fill:#ffcccb,stroke:#cc0000,stroke-width:2px;
```
