# h-rathod/ai-predictive-maintenance architecture

> Architecture diagram of the GitHub repository h-rathod/ai-predictive-maintenance, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/h-rathod/ai-predictive-maintenance
- Repository: https://github.com/h-rathod/ai-predictive-maintenance

## Overview

##### Project type and purpose
- This is a full-stack mobile application (“AI Predictive Maintenance”) composed of a React Native + Expo frontend (“SensorDashboard”) and a Node.js + Express backend (“chatbot-backend”).
- Purpose: allow users to monitor IoT sensor data, view historical charts, and pose natural-language questions via a chatbot that uses OpenAI and Supabase.

##### File structure
- Top-level folders:
- SensorDashboard/ – Expo React Native app
- chatbot-backend/ – Express server
- SensorDashboard/src:
- screens/ – UI screens (Dashboard, Graph, Chatbot, etc.)
- utils/ – theme context, Supabase client, anomaly & sensor utilities
- chatbot-backend/src:
- controllers/ – request handlers (chatController)
- services/ – OpenAI integration, Supabase data access
- config/ – environment & constants
- utils/ – seed scripts
- Key configs & build scripts: app.json, babel.config.js, package.json, .env samples.

##### README for additional insights
- Frontend uses Expo, React Navigation, Victory Native (charts), Gifted Chat, Supabase client.
- Backend uses Express, OpenAI API, Supabase client, CORS, body-parser, health-check endpoint.
- Supabase PostgreSQL holds a sensor_history table.

a. Main components:
- Mobile Client (SensorDashboard)
- REST API Server (chatbot-backend)
- Supabase Database (PostgreSQL)
- OpenAI External Service API
- (Optional) Authentication / Env. config service
b. Relationships & data flows:
1. Mobile Client ↔ Supabase – direct reads for real-time & historical sensor data (via Supabase JS client).
2. Mobile Client → REST API – chatbot queries and health check.
3. REST API → Supabase – fetch sensor data for NL queries.
4. REST API → OpenAI – forward user queries, receive AI-driven responses.
5. REST API → Mobile Client – return JSON responses to display in Gifted Chat.
c. Architectural patterns & design principles:
- Separation of concerns: UI screens vs. data-access vs. business logic
- “Service” modules in backend for external integrations (OpenAI, Supabase)
- React Context (ThemeContext) and Hooks for state & theming in frontend
- RESTful API layer for natural-language chatbot interactions
d. Key technologies & libraries:
- Frontend: Expo, React Navigation, Victory Native, React Native Paper, Gifted Chat, @env, Supabase JS
- Backend: Node.js, Express, OpenAI SDK, Supabase JS, CORS, body-parser

##### Essential elements
- Component labels and purpose

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Mobile Client Subgraph
    subgraph "SensorDashboard (Mobile Client)" 
        direction TB
        App["App.js"]:::frontend
        subgraph "Screens" 
            direction TB
            Dashboard["Dashboard.js"]:::frontend
            Graph["Graph.js"]:::frontend
            Chatbot["Chatbot.js"]:::frontend
            Others["Other Screens"]:::frontend
        end
        subgraph "Utils & Config"
            direction TB
            ThemeContext["ThemeContext.js"]:::frontend
            ThemeDefs["theme.js"]:::frontend
            SupabaseClient["supabase.js"]:::frontend
            SensorUtils["sensorUtils.js"]:::frontend
            AnomalyService["anomalyService.js"]:::frontend
            Config["config.js"]:::frontend
        end
        App -->|imports screens| Dashboard
        App --> Graph
        App --> Chatbot
    end

    %% Backend Server Subgraph
    subgraph "chatbot-backend (API Server)"
        direction TB
        ServerLoader["Server.js"]:::backend
        MainServer["server.js"]:::backend
        subgraph "Controllers"
            ChatController["chatController.js"]:::backend
        end
        subgraph "Services"
            OpenAIService["openaiService.js"]:::backend
            SupaService["supabaseService.js"]:::backend
        end
        subgraph "Config & Utils"
            EnvConfig["index.js"]:::backend
            SeedScript["seedData.js (root)"]:::backend
            SeedUtils["seedData.js (utils)"]:::backend
        end
        ServerLoader --> MainServer
        MainServer --> ChatController
        ChatController --> OpenAIService
        ChatController --> SupaService
    end

    %% Database
    SupabaseDB[(PostgreSQL DB)]:::database

    %% External Service
    OpenAICloud["OpenAI API"]:::external

    %% Data Flows
    App -->|Supabase JS: fetch sensor data| SupabaseDB
    Dashboard -->|GET /sensor-history| SupabaseDB
    Graph -->|GET /sensor-history| SupabaseDB

    App -->|HTTPS/REST: /chat, /health| ChatController
    App -->|HTTPS/REST: /health| ChatController

    ChatController -->|query data| SupabaseDB
    ChatController -->|forward query| OpenAICloud
    OpenAICloud -->|AI response| ChatController
    ChatController -->|JSON response| App

    %% Click Events
    click App "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/SensorDashboard/App.js"
    click Dashboard "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/SensorDashboard/src/screens/Dashboard.js"
    click Graph "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/SensorDashboard/src/screens/Graph.js"
    click Chatbot "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/SensorDashboard/src/screens/Chatbot.js"
    click ThemeContext "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/SensorDashboard/src/utils/ThemeContext.js"
    click ThemeDefs "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/SensorDashboard/src/utils/theme.js"
    click SupabaseClient "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/SensorDashboard/src/utils/supabase.js"
    click SensorUtils "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/SensorDashboard/src/utils/sensorUtils.js"
    click AnomalyService "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/SensorDashboard/src/utils/anomalyService.js"
    click Config "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/SensorDashboard/src/utils/config.js"
    click ServerLoader "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/chatbot-backend/Server.js"
    click MainServer "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/chatbot-backend/src/server.js"
    click ChatController "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/chatbot-backend/src/controllers/chatController.js"
    click OpenAIService "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/chatbot-backend/src/services/openaiService.js"
    click SupaService "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/chatbot-backend/src/services/supabaseService.js"
    click EnvConfig "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/chatbot-backend/src/config/index.js"
    click SeedScript "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/chatbot-backend/seedData.js"
    click SeedUtils "https://github.com/h-rathod/ai-predictive-maintenance/blob/main/chatbot-backend/src/utils/seedData.js"

    %% Styles
    classDef frontend fill:#e0f7fa,stroke:#006064,color:#006064;
    classDef backend fill:#e8f5e9,stroke:#2e7d32,color:#2e7d32;
    classDef database fill:#fff3e0,stroke:#e65100,color:#e65100;
    classDef external fill:#f3e5f5,stroke:#6a1b9a,color:#6a1b9a;
```
