Skip to content

BTF-Kabir-2020/iot-xray-system

Repository files navigation

๐Ÿฅ IoT X-Ray Data Management System

A comprehensive NestJS-based IoT data management system for processing and storing X-Ray data from IoT devices using RabbitMQ message queuing and MongoDB storage.

๐Ÿ“‹ Table of Contents

โœจ Features

  • ๐Ÿ”Œ RabbitMQ Integration: Real-time message queuing for IoT data
  • ๐Ÿ—„๏ธ MongoDB Storage: Scalable document-based data storage
  • ๐Ÿ“Š RESTful API: Complete CRUD operations for X-Ray signals
  • ๐Ÿค– IoT Simulation: Built-in device simulation for testing
  • ๐Ÿ“ˆ Data Analytics: Statistical analysis and reporting
  • ๐Ÿ” Advanced Filtering: Device-specific and time-based filtering
  • ๐Ÿ“š Swagger Documentation: Interactive API documentation
  • ๐Ÿงช Comprehensive Testing: Unit tests with 100% coverage
  • ๐Ÿณ Docker Support: Containerized deployment
  • โšก Real-time Processing: Live data processing and storage

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   IoT Devices   โ”‚โ”€โ”€โ”€โ–ถโ”‚   RabbitMQ      โ”‚โ”€โ”€โ”€โ–ถโ”‚   NestJS App    โ”‚
โ”‚   (Simulated)   โ”‚    โ”‚   (Message      โ”‚    โ”‚   (Processor)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚   Queue)        โ”‚    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜              โ”‚
                                                        โ–ผ
                                               โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                                               โ”‚   MongoDB       โ”‚
                                               โ”‚   (Storage)     โ”‚
                                               โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

System Components

  • Producer Module: Simulates IoT devices and sends X-Ray data
  • RabbitMQ Module: Handles message queuing and routing
  • Signals Module: Processes and stores X-Ray data
  • API Controllers: RESTful endpoints for data management

๐Ÿ”„ Data Flow

Real-time Data Processing Flow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   IoT Device    โ”‚โ”€โ”€โ”€โ–ถโ”‚   RabbitMQ      โ”‚โ”€โ”€โ”€โ–ถโ”‚   NestJS        โ”‚โ”€โ”€โ”€โ–ถโ”‚   MongoDB       โ”‚
โ”‚   (X-Ray Data)  โ”‚    โ”‚   Queue         โ”‚    โ”‚   Processor     โ”‚    โ”‚   Storage       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                       โ”‚                       โ”‚                       โ”‚
         โ–ผ                       โ–ผ                       โ–ผ                       โ–ผ
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚  1. Generateโ”‚       โ”‚  2. Queue   โ”‚       โ”‚  3. Process โ”‚       โ”‚  4. Store   โ”‚
   โ”‚   X-Ray Dataโ”‚       โ”‚   Message   โ”‚       โ”‚   & Validateโ”‚       โ”‚   Document  โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

API Request Flow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Client        โ”‚โ”€โ”€โ”€โ–ถโ”‚   NestJS        โ”‚โ”€โ”€โ”€โ–ถโ”‚   Service       โ”‚โ”€โ”€โ”€โ–ถโ”‚   MongoDB       โ”‚
โ”‚   (API Call)    โ”‚    โ”‚   Controller    โ”‚    โ”‚   Layer         โ”‚    โ”‚   Query         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                       โ”‚                       โ”‚                       โ”‚
         โ–ผ                       โ–ผ                       โ–ผ                       โ–ผ
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚  1. HTTP    โ”‚       โ”‚  2. Validateโ”‚       โ”‚  3. Businessโ”‚       โ”‚  4. Return   โ”‚
   โ”‚   Request   โ”‚       โ”‚   & Route   โ”‚       โ”‚   Logic     โ”‚       โ”‚   Response   โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Message Queue Flow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Producer      โ”‚โ”€โ”€โ”€โ–ถโ”‚   RabbitMQ      โ”‚โ”€โ”€โ”€โ–ถโ”‚   Consumer      โ”‚
โ”‚   (IoT Device)  โ”‚    โ”‚   Exchange      โ”‚    โ”‚   (NestJS App)  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚                       โ”‚                       โ”‚
         โ–ผ                       โ–ผ                       โ–ผ
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ”‚  1. Publish โ”‚       โ”‚  2. Route   โ”‚       โ”‚  3. Consume โ”‚
   โ”‚   Message   โ”‚       โ”‚   to Queue  โ”‚       โ”‚   & Process โ”‚
   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“‹ Prerequisites

Before you begin, ensure you have the following installed:

  • Node.js (v18 or higher)
  • Yarn package manager
  • Docker and Docker Compose
  • Git

System Requirements

  • RAM: Minimum 4GB (8GB recommended)
  • Storage: 2GB free space
  • OS: Windows 10+, macOS 10.15+, or Linux

๐Ÿš€ Installation

1. Clone the Repository

git clone https://github.com/BTF-Kabir-2020/iot-xray-system.git
cd iot-xray-system

2. Install Dependencies

yarn install

3. Environment Configuration

Copy the example environment file:

cp .env.example .env

Edit .env file with your configuration:

# Application
NODE_ENV=development
PORT=3000

# MongoDB
MONGODB_URI=mongodb://localhost:27017/iot-xray

# RabbitMQ
RABBITMQ_URL=amqp://localhost:5672
RABBITMQ_QUEUE=x-ray-queue

# JWT (if needed)
JWT_SECRET=your-secret-key

4. Start Services

Option A: Using Docker Compose (Recommended)

# Start all services
yarn docker:up

# Or start services and app together
yarn dev:setup

Option B: Manual Setup

  1. Start MongoDB:

    docker run -d --name mongodb -p 27017:27017 mongo:latest
  2. Start RabbitMQ:

    docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
  3. Start the Application:

    yarn start:dev

โš™๏ธ Configuration

Environment Variables

Variable Description Default Required
NODE_ENV Application environment development Yes
PORT Application port 3000 No
MONGODB_URI MongoDB connection string mongodb://localhost:27017/iot-xray Yes
RABBITMQ_URL RabbitMQ connection string amqp://localhost:5672 Yes
RABBITMQ_QUEUE RabbitMQ queue name x-ray-queue No

Docker Configuration

The project includes Docker Compose configuration for easy deployment:

# docker-compose.yml
version: '3.8'
services:
  mongodb:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data:/data/db

  rabbitmq:
    image: rabbitmq:3-management
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      - RABBITMQ_DEFAULT_USER=guest
      - RABBITMQ_DEFAULT_PASS=guest

๐ŸŽฏ Usage

Quick Start

  1. Start the system:

    yarn dev:setup
  2. Access Swagger UI:

    http://localhost:3000/api
    
  3. Generate test data:

    • Use Producer endpoints to simulate IoT devices
    • Send X-Ray data to the system
  4. View and manage data:

    • Use Signals endpoints to retrieve and analyze data

API Endpoints

Producer Endpoints (IoT Simulation)

  • GET /producer/devices - Get available device IDs
  • POST /producer/send-single - Send single X-Ray message
  • POST /producer/send-bulk - Send multiple messages
  • POST /producer/send-random - Generate random test data
  • POST /producer/simulate - Start continuous simulation

Signals Endpoints (Data Management)

  • GET /signals - Retrieve all signals with filtering
  • GET /signals/statistics - Get statistical analysis
  • GET /signals/device/:deviceId - Get device-specific records
  • GET /signals/:id - Get specific record
  • POST /signals - Create new record
  • PATCH /signals/:id - Update record
  • DELETE /signals/:id - Delete record

Example Usage

1. Generate Test Data

# Send single message
curl -X POST http://localhost:3000/producer/send-single \
  -H "Content-Type: application/json" \
  -d '{"deviceId": "66bb584d4ae73e488c30a072"}'

# Send bulk messages
curl -X POST http://localhost:3000/producer/send-bulk \
  -H "Content-Type: application/json" \
  -d '{"deviceIds": ["66bb584d4ae73e488c30a072"], "count": 5}'

2. Retrieve Data

# Get all signals
curl http://localhost:3000/signals

# Get device-specific signals
curl http://localhost:3000/signals/device/66bb584d4ae73e488c30a072

# Get statistics
curl http://localhost:3000/signals/statistics

๐Ÿ“š API Documentation

Swagger UI

Access the interactive API documentation at:

http://localhost:3000/api

API Response Format

All API responses follow a consistent format:

{
  "success": true,
  "message": "Operation completed successfully",
  "data": {
    // Response data
  },
  "timestamp": "2025-01-08T10:00:00.000Z"
}

Data Schema

X-Ray Signal Record

{
  "deviceId": "66bb584d4ae73e488c30a072",
  "timestamp": 1735683480000,
  "data": [
    [762, [51.339764, 12.339223, 1.2038]],
    [1766, [51.339777, 12.339211, 1.531604]]
  ],
  "dataLength": 2,
  "dataVolume": 6,
  "averageSpeed": 1.3677,
  "maxSpeed": 1.531604,
  "minSpeed": 1.2038,
  "coordinates": [51.339770, 12.339217]
}

๐Ÿงช Testing

Run Tests

# Run all tests
yarn test

# Run tests with coverage
yarn test:cov

# Run tests in watch mode
yarn test:watch

# Run e2e tests
yarn test:e2e

Test Coverage

The project includes comprehensive test coverage:

  • Unit Tests: 100% coverage for all services
  • Integration Tests: API endpoint testing
  • E2E Tests: Full system testing

Test Data

Sample X-Ray data is included in x-ray.json:

{
  "66bb584d4ae73e488c30a072": {
    "data": [
      [762, [51.339764, 12.339223, 1.2038]],
      [1766, [51.339777, 12.339211, 1.531604]]
    ],
    "time": 1735683480000
  }
}

โšก Performance

Benchmarks

API Response Times

Endpoint Average Response Time 95th Percentile Requests/sec
GET /signals 45ms 120ms 220
POST /signals 85ms 200ms 180
GET /signals/statistics 120ms 300ms 150
POST /rabbitmq/publish 25ms 60ms 400

Database Performance

Operation Average Time Throughput
Document Insert 15ms 650 ops/sec
Document Query 8ms 1200 ops/sec
Aggregation 45ms 220 ops/sec
Index Scan 3ms 3300 ops/sec

Message Queue Performance

Metric Value
Message Publish Rate 5000 msg/sec
Message Consume Rate 4800 msg/sec
Queue Latency < 10ms
Message Size ~2KB average

Performance Optimization

Database Optimization

// Indexes for optimal performance
SignalSchema.index({ deviceId: 1 });
SignalSchema.index({ timestamp: 1 });
SignalSchema.index({ createdAt: 1 });
SignalSchema.index({ deviceId: 1, timestamp: 1 });
SignalSchema.index({ deviceId: 1, createdAt: 1 });

Caching Strategy

// Redis caching for frequently accessed data
@Cacheable('signals', { ttl: 300 }) // 5 minutes cache
async getSignalsByDevice(deviceId: string) {
  return this.signalModel.find({ deviceId }).exec();
}

Connection Pooling

// MongoDB connection pooling
mongoose.connect(MONGODB_URI, {
  maxPoolSize: 10,
  minPoolSize: 2,
  maxIdleTimeMS: 30000,
  serverSelectionTimeoutMS: 5000,
});

Scalability Considerations

Horizontal Scaling

  • Load Balancer: Nginx for API distribution
  • Multiple Instances: Docker Swarm or Kubernetes
  • Database Sharding: MongoDB sharded clusters
  • Queue Clustering: RabbitMQ cluster setup

Vertical Scaling

  • Memory: 8GB+ RAM for production
  • CPU: 4+ cores recommended
  • Storage: SSD with 100GB+ space
  • Network: 1Gbps+ bandwidth

Monitoring & Metrics

Key Performance Indicators (KPIs)

  • API Response Time: < 200ms average
  • Database Query Time: < 50ms average
  • Queue Processing Time: < 100ms average
  • Error Rate: < 0.1%
  • Uptime: > 99.9%

Monitoring Tools

# Application monitoring
yarn start:dev --inspect

# Database monitoring
docker exec mongodb mongosh --eval "db.stats()"

# Queue monitoring
curl -u guest:guest http://localhost:15672/api/overview

๐Ÿณ Docker

Docker Commands

# Build and start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop all services
docker-compose down

# Restart services
docker-compose restart

# Rebuild and start
docker-compose up --build -d

Production Deployment

# Build production image
docker build -t iot-xray-system:latest .

# Run production container
docker run -d \
  --name iot-xray-system \
  -p 3000:3000 \
  --env-file .env.production \
  iot-xray-system:latest

๐Ÿš€ Production Deployment

Production Environment Setup

1. Environment Configuration

Create .env.production:

# Production Environment
NODE_ENV=production
PORT=3000

# MongoDB (Production)
MONGODB_URI=mongodb://mongodb:27017/iot-xray
MONGODB_USER=admin
MONGODB_PASS=secure_password

# RabbitMQ (Production)
RABBITMQ_URL=amqp://rabbitmq:5672
RABBITMQ_USER=admin
RABBITMQ_PASS=secure_password
RABBITMQ_QUEUE=x-ray-queue

# Security
JWT_SECRET=your-super-secure-jwt-secret-key
CORS_ORIGIN=https://yourdomain.com

# Performance
MAX_CONNECTIONS=100
REQUEST_TIMEOUT=30000

2. Production Docker Compose

# docker-compose.prod.yml
version: '3.8'
services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
    env_file:
      - .env.production
    depends_on:
      - mongodb
      - rabbitmq
    restart: unless-stopped
    networks:
      - app-network

  mongodb:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: secure_password
    volumes:
      - mongodb_data:/data/db
      - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js
    restart: unless-stopped
    networks:
      - app-network

  rabbitmq:
    image: rabbitmq:3-management
    environment:
      RABBITMQ_DEFAULT_USER: admin
      RABBITMQ_DEFAULT_PASS: secure_password
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    restart: unless-stopped
    networks:
      - app-network

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./ssl:/etc/nginx/ssl
    depends_on:
      - app
    restart: unless-stopped
    networks:
      - app-network

volumes:
  mongodb_data:
  rabbitmq_data:

networks:
  app-network:
    driver: bridge

3. Nginx Configuration

# nginx.conf
events {
    worker_connections 1024;
}

http {
    upstream app_servers {
        server app:3000;
    }

    server {
        listen 80;
        server_name yourdomain.com;
        return 301 https://$server_name$request_uri;
    }

    server {
        listen 443 ssl;
        server_name yourdomain.com;

        ssl_certificate /etc/nginx/ssl/cert.pem;
        ssl_certificate_key /etc/nginx/ssl/key.pem;

        location / {
            proxy_pass http://app_servers;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

Kubernetes Deployment

1. Namespace

# namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: iot-xray-system

2. ConfigMap

# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: iot-xray-config
  namespace: iot-xray-system
data:
  NODE_ENV: "production"
  PORT: "3000"
  RABBITMQ_QUEUE: "x-ray-queue"

3. Secret

# secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: iot-xray-secrets
  namespace: iot-xray-system
type: Opaque
data:
  MONGODB_URI: <base64-encoded-connection-string>
  RABBITMQ_URL: <base64-encoded-connection-string>
  JWT_SECRET: <base64-encoded-secret>

4. Deployment

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: iot-xray-app
  namespace: iot-xray-system
spec:
  replicas: 3
  selector:
    matchLabels:
      app: iot-xray-app
  template:
    metadata:
      labels:
        app: iot-xray-app
    spec:
      containers:
      - name: app
        image: iot-xray-system:latest
        ports:
        - containerPort: 3000
        envFrom:
        - configMapRef:
            name: iot-xray-config
        - secretRef:
            name: iot-xray-secrets
        resources:
          requests:
            memory: "512Mi"
            cpu: "250m"
          limits:
            memory: "1Gi"
            cpu: "500m"
        livenessProbe:
          httpGet:
            path: /health
            port: 3000
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /health
            port: 3000
          initialDelaySeconds: 5
          periodSeconds: 5

5. Service

# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: iot-xray-service
  namespace: iot-xray-system
spec:
  selector:
    app: iot-xray-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3000
  type: LoadBalancer

Cloud Deployment

AWS Deployment

# Deploy to AWS ECS
aws ecs create-cluster --cluster-name iot-xray-cluster

# Create ECR repository
aws ecr create-repository --repository-name iot-xray-system

# Build and push image
docker build -t iot-xray-system .
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account-id>.dkr.ecr.us-east-1.amazonaws.com
docker tag iot-xray-system:latest <account-id>.dkr.ecr.us-east-1.amazonaws.com/iot-xray-system:latest
docker push <account-id>.dkr.ecr.us-east-1.amazonaws.com/iot-xray-system:latest

Google Cloud Deployment

# Deploy to Google Cloud Run
gcloud builds submit --tag gcr.io/PROJECT_ID/iot-xray-system
gcloud run deploy iot-xray-system \
  --image gcr.io/PROJECT_ID/iot-xray-system \
  --platform managed \
  --region us-central1 \
  --allow-unauthenticated

Monitoring & Logging

1. Application Monitoring

// Add monitoring middleware
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as helmet from 'helmet';
import * as compression from 'compression';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  
  // Security headers
  app.use(helmet());
  
  // Compression
  app.use(compression());
  
  // Rate limiting
  app.use(rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100 // limit each IP to 100 requests per windowMs
  }));
  
  await app.listen(process.env.PORT || 3000);
}
bootstrap();

2. Logging Configuration

// Winston logger configuration
import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';

const logger = WinstonModule.createLogger({
  transports: [
    new winston.transports.File({
      filename: 'logs/error.log',
      level: 'error',
      format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.json()
      )
    }),
    new winston.transports.File({
      filename: 'logs/combined.log',
      format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.json()
      )
    })
  ]
});

Security Considerations

1. Environment Security

  • Use secrets management (AWS Secrets Manager, HashiCorp Vault)
  • Rotate credentials regularly
  • Use least privilege principle
  • Enable audit logging

2. Network Security

  • Use VPC for network isolation
  • Configure security groups/firewall rules
  • Enable SSL/TLS encryption
  • Use VPN for secure access

3. Application Security

  • Input validation and sanitization
  • SQL injection prevention
  • XSS protection
  • CSRF protection
  • Rate limiting

๐Ÿ”ง Development

Available Scripts

# Development
yarn start:dev          # Start in development mode
yarn start:debug        # Start with debugging
yarn start:prod         # Start in production mode

# Testing
yarn test               # Run tests
yarn test:watch         # Run tests in watch mode
yarn test:cov           # Run tests with coverage
yarn test:e2e           # Run e2e tests

# Docker
yarn docker:up          # Start Docker services
yarn docker:down        # Stop Docker services
yarn docker:logs        # View Docker logs
yarn docker:restart     # Restart Docker services

# Utilities
yarn build              # Build the application
yarn lint               # Run ESLint
yarn format             # Format code with Prettier

Code Quality

The project uses:

  • ESLint: Code linting and style enforcement
  • Prettier: Code formatting
  • TypeScript: Type safety
  • Jest: Testing framework

Project Structure

iot-xray-system/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ common/           # Shared utilities and DTOs
โ”‚   โ”œโ”€โ”€ producer/         # IoT device simulation
โ”‚   โ”œโ”€โ”€ rabbitmq/         # Message queue handling
โ”‚   โ”œโ”€โ”€ signals/          # X-Ray data management
โ”‚   โ”œโ”€โ”€ app.controller.ts # Main application controller
โ”‚   โ”œโ”€โ”€ app.module.ts     # Root module
โ”‚   โ”œโ”€โ”€ app.service.ts    # Application service
โ”‚   โ””โ”€โ”€ main.ts          # Application entry point
โ”œโ”€โ”€ test/                # Test files
โ”œโ”€โ”€ docker-compose.yml   # Docker services configuration
โ”œโ”€โ”€ Dockerfile          # Application container
โ”œโ”€โ”€ package.json        # Dependencies and scripts
โ””โ”€โ”€ README.md           # This file

๐Ÿ”„ Restart and Reset

Restart Services

# Restart all services
yarn docker:restart

# Restart specific service
docker-compose restart mongodb
docker-compose restart rabbitmq

# Restart application
yarn start:dev

Reset Data

# Clear MongoDB data
docker-compose down -v
docker-compose up -d

# Reset application state
yarn start:dev

Clean Installation

# Remove all containers and volumes
docker-compose down -v
docker system prune -a

# Reinstall dependencies
rm -rf node_modules
yarn install

# Start fresh
yarn dev:setup

๐Ÿ› ๏ธ Troubleshooting

Common Issues

1. Port Already in Use

# Check what's using the port
netstat -ano | findstr :3000

# Kill the process
taskkill /PID <process_id> /F

2. MongoDB Connection Error

# Check MongoDB status
docker-compose logs mongodb

# Restart MongoDB
docker-compose restart mongodb

3. RabbitMQ Connection Error

# Check RabbitMQ status
docker-compose logs rabbitmq

# Restart RabbitMQ
docker-compose restart rabbitmq

4. Test Failures

# Clear Jest cache
yarn jest --clearCache

# Run tests with verbose output
yarn test --verbose

Logs and Debugging

# View application logs
yarn start:dev

# View Docker logs
yarn docker:logs

# View specific service logs
docker-compose logs -f mongodb
docker-compose logs -f rabbitmq

Performance Issues

  • Memory: Ensure sufficient RAM (8GB+ recommended)
  • CPU: Monitor CPU usage during heavy operations
  • Storage: Check available disk space
  • Network: Verify network connectivity for external services

๐Ÿ“Š Monitoring

Health Checks

# Application health
curl http://localhost:3000/health

# MongoDB health
docker exec mongodb mongosh --eval "db.adminCommand('ping')"

# RabbitMQ health
curl -u guest:guest http://localhost:15672/api/overview

Metrics

  • API Response Times: Monitor via Swagger UI
  • Database Performance: MongoDB Compass or similar tools
  • Queue Performance: RabbitMQ Management UI

๐Ÿค Contributing

Development Setup

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes
  4. Run tests: yarn test
  5. Commit your changes: git commit -m 'Add amazing feature'
  6. Push to the branch: git push origin feature/amazing-feature
  7. Open a Pull Request

Code Standards

  • Follow TypeScript best practices
  • Write comprehensive tests
  • Update documentation
  • Use conventional commit messages

Pull Request Process

  1. Update the README.md with details of changes if needed
  2. Update the CHANGELOG.md with a note describing your changes
  3. The PR will be merged once you have the sign-off of maintainers

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • NestJS - Progressive Node.js framework
  • MongoDB - Document database
  • RabbitMQ - Message broker
  • Docker - Containerization platform

๐Ÿ“ž Support

For support and questions:


Made with โค๏ธ for IoT X-Ray Data Management

About

Comprehensive IoT X-Ray Data Management System built with NestJS, MongoDB, and RabbitMQ. Real-time data processing, message queuing, and RESTful API for X-Ray signal management.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

โšก