# harpreet012/ai-predictive-maintainence- architecture

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

- Interactive diagram: https://gitdiagram.com/harpreet012/ai-predictive-maintainence-
- Repository: https://github.com/harpreet012/ai-predictive-maintainence-

## Overview

1. **Overall shape**
   - This is a three-part system: a **React/Vite client**, a **Node/Express API server**, and a separate **Python ML service**.  
   - The main production data store is **MongoDB**.  
   - Real-time behavior is handled through **Socket.io/WebSockets** on top of the Node backend.

2. **Frontend boundary**
   - The `client/` app is a single-page industrial monitoring UI built around route-level pages such as **Dashboard, Equipment, Alerts, Analytics, Maintenance, Anomaly, DigitalTwin, DataImport, Users, Calendar**.
   - Reusable visual widgets live in `components/` and include gauges, charts, equipment cards, live graphs, a factory-floor view, chatbot, sidebar, and top bar.
   - `context/AuthContext.jsx` manages user session state; `context/SocketContext.jsx` manages live updates.
   - `services/api.js` is the HTTP client boundary to the backend.

3. **Backend API boundary**
   - The `server/` app is an **Express REST API** organized by domain routes: `auth`, `users`, `equipment`, `sensors`, `alerts`, `maintenance`, `analytics`, `anomalies`, `dataImport`, and `chatbot`.
   - `middleware/auth.js` indicates protected endpoints and role/session enforcement.
   - `server.js` is the API entrypoint; `config/db.js` handles MongoDB connectivity.

4. **Domain model in MongoDB**
   - The server persists four core operational entities: **Equipment**, **SensorReading**, **Alert**, and **MaintenanceLog**, plus **User**.
   - These models define the system’s main lifecycle: equipment registration, continuous sensor history, anomaly/alert records, and maintenance actions tied back to assets.

5. **Operational data flow**
   - Sensor data enters the backend through sensor-related routes and/or the simulator, is stored in MongoDB, then drives dashboards, alerts, analytics, and maintenance views.
   - The UI reads aggregated state from the API for historical pages and subscribes to live socket events for the “now” experience.
   - The same underlying equipment and reading records appear across monitoring, anomaly detection, and maintenance workflows.

6. **Real-time subsystem**
   - `services/dataSimulator.js` generates streaming telemetry and injects faults such as wear, overheating, or leaks.
   - The backend broadcasts updates to clients over sockets, enabling live charts, health meters, and toast-style alerting without page refresh.
   - This real-time path is separate from the standard REST path: REST serves durable records; sockets carry transient updates.

7. **ML/anomaly detection layer**
   - `server/services/mlEngine.js` is the core decision layer in the Node backend and implements:
     - z-score anomaly detection,
     - weighted health scoring,
     - exponential degradation / failure projection,
     - sigmoid failure probability,
     - equipment-type-specific rules.
   - This means prediction is not just a black-box model call; it is a rule-and-statistics engine embedded in the API flow.

8. **Python ML microservice**
   - `ml-service/` is a distinct Python/Flask-style service with `app.py`, `train.py`, `test_predictions.py`, and a saved `model.pkl`.
   - Architecturally, it acts as a separate ML boundary from the Express app, likely for experimentation, training, or inference support.
   - The repository therefore contains both an embedded Node ML engine and a standalone Python ML artifact/service.

9. **Authentication and user management**
   - Login and registration are handled by dedicated frontend pages and the backend `auth` routes.
   - `User` model plus `users` routes suggest role-based access for operators, maintenance users, or admins.
   - Protected areas such as equipment management, imports, and administration are likely gated through the auth middleware.

10. **Analytics and maintenance workflows**
   - `analytics` and `anomalies` routes provide computed summaries rather than raw sensor rows.
   - Maintenance is a first-class domain: `MaintenanceLog` stores work history, and the Maintenance page is part of the main navigation, not a side feature.
   - This makes the system a closed loop: detect issues, surface them, and track remediation.

11. **Data onboarding and environment setup**
   - `config/seed.js` seeds the database with starter equipment and 7-day history, which suggests the app is designed to boot with realistic operational data.
   - `dataImport` routes and a `DataImportPage` imply the system can ingest external datasets as well as simulator-generated telemetry.

12. **Deployment and runtime notes**
   - The stack is optimized for local development with `npm run dev` on both client and server, and the client uses Vite SPA routing support (`public/_redirects`).
   - `render.yaml` indicates deployment is expected on Render or a similar platform with separate service/process definitions.

13. **Architecture diagram takeaway**
   - Draw the system as: **Users → React SPA → Express API / Socket.io → MongoDB**, with a side path from **Data Simulator** into the same backend, and a separate **Python ML service** adjacent to the Node backend.
   - The key boundaries are: presentation, API/auth, persistence, real-time telemetry, and predictive analytics.

Main entry points: [`client/src/App.jsx`](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/client/src/App.jsx) (React SPA), [`server/server.js`](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/server.js) (Express API), [`server/routes`](https://github.com/harpreet012/ai-predictive-maintainence-/tree/main/server/routes) (Domain Routes), [`server/services/dataSimulator.js`](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/services/dataSimulator.js) (Telemetry Simulator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Client"]
  node_spa["React SPA<br/>frontend-shell<br/>[App.jsx]"]
  node_routes["Pages"]
  node_widgets["Widgets<br/>ui-components"]
  node_authctx(("Auth State<br/>client-state<br/>[AuthContext.jsx]"))
  node_sockctx(("Socket State<br/>client-state<br/>[SocketContext.jsx]"))
  node_api["API Client<br/>http-client<br/>[api.js]"]
end

subgraph group_backend["API Server"]
  node_server["Express API<br/>backend-entrypoint<br/>[server.js]"]
  node_auth(("Auth Middleware<br/>access-control<br/>[auth.js]"))
  node_db[("MongoDB<br/>database<br/>[db.js]")]
  node_domains["Domain Routes<br/>rest-domains"]
  node_models[("Data Models<br/>mongo-models")]
  node_sim["Telemetry Simulator<br/>stream-source<br/>[dataSimulator.js]"]
  node_mlengine{{"ML Engine<br/>rules-stats<br/>[mlEngine.js]"}}
  node_socket(("Socket.IO<br/>realtime-bus"))
  node_seed["Seed Data<br/>bootstrap<br/>[seed.js]"]
end

subgraph group_ml["ML Services"]
  node_pyml["Python ML Service<br/>flask-service<br/>[app.py]"]
  node_model["Model Artifact<br/>serialized-model<br/>[model.pkl]"]
end

node_users(("Users<br/>actors"))

node_users -->|"uses"| node_spa
node_spa -->|"navigates"| node_routes
node_spa -->|"renders"| node_widgets
node_spa -->|"session"| node_authctx
node_spa -->|"live state"| node_sockctx
node_spa -->|"requests"| node_api
node_api -->|"http"| node_server
node_server -->|"guards"| node_auth
node_server -->|"routes"| node_domains
node_domains -->|"reads/writes"| node_models
node_models -->|"stored in"| node_db
node_server -->|"publishes"| node_socket
node_sockctx -->|"subscribes"| node_socket
node_sim -->|"injects telemetry"| node_server
node_sim -->|"faults"| node_mlengine
node_mlengine -->|"enriches"| node_domains
node_seed -->|"bootstraps"| node_db
node_server -.->|"ml boundary"| node_pyml
node_pyml -->|"loads"| node_model

click node_spa "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/client/src/App.jsx"
click node_routes "https://github.com/harpreet012/ai-predictive-maintainence-/tree/main/client/src/pages"
click node_widgets "https://github.com/harpreet012/ai-predictive-maintainence-/tree/main/client/src/components"
click node_authctx "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/client/src/context/AuthContext.jsx"
click node_sockctx "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/client/src/context/SocketContext.jsx"
click node_api "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/client/src/services/api.js"
click node_server "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/server.js"
click node_auth "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/middleware/auth.js"
click node_db "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/config/db.js"
click node_domains "https://github.com/harpreet012/ai-predictive-maintainence-/tree/main/server/routes"
click node_models "https://github.com/harpreet012/ai-predictive-maintainence-/tree/main/server/models"
click node_sim "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/services/dataSimulator.js"
click node_mlengine "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/services/mlEngine.js"
click node_seed "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/config/seed.js"
click node_pyml "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/ml-service/app.py"
click node_model "https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/ml-service/model.pkl"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_spa,node_routes,node_widgets,node_authctx,node_sockctx,node_api toneBlue
class node_server,node_auth,node_db,node_domains,node_models,node_sim,node_mlengine,node_socket,node_seed toneAmber
class node_pyml,node_model toneMint
class node_users toneNeutral
```

## Components

### Client

React/Vite single-page monitoring UI

- [React SPA](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/client/src/App.jsx): frontend-shell · Vite-based industrial monitoring client · `client/src/App.jsx`
- [Pages](https://github.com/harpreet012/ai-predictive-maintainence-/tree/main/client/src/pages): route-pages · Dashboard, equipment, alerts, analytics, maintenance, import, users · `client/src/pages`
- [Widgets](https://github.com/harpreet012/ai-predictive-maintainence-/tree/main/client/src/components): ui-components · Charts, gauges, factory floor, chatbot, navigation · `client/src/components`
- [Auth State](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/client/src/context/AuthContext.jsx): client-state · Session and login state management · `client/src/context/AuthContext.jsx`
- [Socket State](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/client/src/context/SocketContext.jsx): client-state · Live update subscription state · `client/src/context/SocketContext.jsx`
- [API Client](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/client/src/services/api.js): http-client · Frontend HTTP boundary to backend · `client/src/services/api.js`

### API Server

Express REST API with realtime sockets and MongoDB persistence

- [Express API](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/server.js): backend-entrypoint · Main Node backend entrypoint · `server/server.js`
- [Auth Middleware](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/middleware/auth.js): access-control · Protected routes and role enforcement · `server/middleware/auth.js`
- [MongoDB](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/config/db.js): database · Primary persistent data store · `server/config/db.js`
- [Domain Routes](https://github.com/harpreet012/ai-predictive-maintainence-/tree/main/server/routes): rest-domains · Auth, users, equipment, sensors, alerts, maintenance, analytics, anomalies, import, chatbot · `server/routes`
- [Data Models](https://github.com/harpreet012/ai-predictive-maintainence-/tree/main/server/models): mongo-models · User, Equipment, SensorReading, Alert, MaintenanceLog · `server/models`
- [Telemetry Simulator](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/services/dataSimulator.js): stream-source · Generates readings and fault scenarios · `server/services/dataSimulator.js`
- [ML Engine](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/services/mlEngine.js): rules-stats · Z-score detection, health scoring, degradation and failure projection · `server/services/mlEngine.js`
- **Socket.IO**: realtime-bus · Broadcasts live telemetry and alerts
- [Seed Data](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/server/config/seed.js): bootstrap · Starter equipment and history population · `server/config/seed.js`

### ML Services

Embedded Node anomaly engine plus standalone Python service

- [Python ML Service](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/ml-service/app.py): flask-service · Standalone inference/training boundary · `ml-service/app.py`
- [Model Artifact](https://github.com/harpreet012/ai-predictive-maintainence-/blob/main/ml-service/model.pkl): serialized-model · Saved Python model for predictions · `ml-service/model.pkl`

### Other components

- **Users**: actors · Operators, maintenance staff, admins

## Connections

- Users → React SPA: uses · Operators interact through the SPA
- React SPA → Pages: navigates · Route-level pages drive the UI
- React SPA → Widgets: renders · Pages compose shared monitoring widgets
- React SPA → Auth State: session · Client auth state
- React SPA → Socket State: live state · Client subscribes to live updates
- React SPA → API Client: requests · REST calls to backend
- API Client → Express API: http · API boundary
- Express API → Auth Middleware: guards · Protects sensitive endpoints
- Express API → Domain Routes: routes · Domain REST APIs
- Domain Routes → Data Models: reads/writes · CRUD and workflow persistence
- Data Models → MongoDB: stored in · MongoDB-backed persistence
- Express API → Socket.IO: publishes · Realtime events and alerts
- Socket State → Socket.IO: subscribes · Client listens for live updates
- Telemetry Simulator → Express API: injects telemetry · Streaming sensor data enters backend
- Telemetry Simulator → ML Engine: faults · Simulator feeds anomaly logic
- ML Engine → Domain Routes: enriches · Predictive results surface through API
- Seed Data → MongoDB: bootstraps · Loads starter operational data
- Express API → Python ML Service: ml boundary · Separate Python service adjacent to Node backend
- Python ML Service → Model Artifact: loads · Uses saved model artifact
