# =============================================================================
# RE Workflow - Complete Monitoring Stack
# Docker Compose for Grafana, Prometheus, Loki, and Promtail
# =============================================================================
# Usage: 
#   cd monitoring
#   docker-compose -f docker-compose.monitoring.yml up -d
# =============================================================================

version: '3.8'

services:
  # ===========================================================================
  # REDIS - In-Memory Data Store (for BullMQ queues)
  # ===========================================================================
  redis:
    image: redis:7-alpine
    container_name: re_redis
    ports:
      - "${REDIS_PORT:-6379}:6379"
    command: redis-server --requirepass ${REDIS_PASSWORD:-Redis@123}
    volumes:
      - redis_data:/data
    networks:
      - monitoring_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5

  # ===========================================================================
  # PROMETHEUS - Metrics Collection
  # ===========================================================================
  prometheus:
    image: prom/prometheus:v2.47.2
    container_name: re_prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - ./prometheus/alert.rules.yml:/etc/prometheus/alert.rules.yml:ro
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--storage.tsdb.retention.time=15d'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
      - '--web.enable-lifecycle'
    networks:
      - monitoring_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "wget", "-q", "--spider", "http://localhost:9090/-/healthy"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ===========================================================================
  # LOKI - Log Aggregation
  # ===========================================================================
  loki:
    image: grafana/loki:2.9.2
    container_name: re_loki
    ports:
      - "3100:3100"
    volumes:
      - ./loki/loki-config.yml:/etc/loki/local-config.yaml:ro
      - loki_data:/loki
    command: -config.file=/etc/loki/local-config.yaml
    networks:
      - monitoring_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3100/ready || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 5

  # ===========================================================================
  # PROMTAIL - Log Shipping Agent
  # ===========================================================================
  promtail:
    image: grafana/promtail:2.9.2
    container_name: re_promtail
    volumes:
      - ./promtail/promtail-config.yml:/etc/promtail/config.yml:ro
      - ../logs:/var/log/app:ro
      - /var/lib/docker/containers:/var/lib/docker/containers:ro
      - promtail_data:/tmp/promtail
    command: -config.file=/etc/promtail/config.yml
    depends_on:
      - loki
    networks:
      - monitoring_network
    restart: unless-stopped

  # ===========================================================================
  # GRAFANA - Visualization & Dashboards
  # ===========================================================================
  grafana:
    image: grafana/grafana:10.2.2
    container_name: re_grafana
    ports:
      - "3001:3000"  # Using 3001 to avoid conflict with React frontend (3000)
    environment:
      - GF_SECURITY_ADMIN_USER=admin
      - GF_SECURITY_ADMIN_PASSWORD=REWorkflow@2024
      - GF_USERS_ALLOW_SIGN_UP=false
      - GF_FEATURE_TOGGLES_ENABLE=publicDashboards
      - GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource,grafana-piechart-panel
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources:ro
      - ./grafana/provisioning/dashboards:/etc/grafana/provisioning/dashboards:ro
      - ./grafana/dashboards:/var/lib/grafana/dashboards:ro
    depends_on:
      - prometheus
      - loki
    networks:
      - monitoring_network
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:3000/api/health || exit 1"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ===========================================================================
  # NODE EXPORTER - Host Metrics (Optional but recommended)
  # ===========================================================================
  node-exporter:
    image: prom/node-exporter:v1.6.1
    container_name: re_node_exporter
    ports:
      - "9100:9100"
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
    command:
      - '--path.procfs=/host/proc'
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
    networks:
      - monitoring_network
    restart: unless-stopped

  # ===========================================================================
  # REDIS EXPORTER - Redis Metrics
  # ===========================================================================
  redis-exporter:
    image: oliver006/redis_exporter:v1.55.0
    container_name: re_redis_exporter
    ports:
      - "9121:9121"
    environment:
      - REDIS_ADDR=redis://redis:6379
      - REDIS_PASSWORD=Redis@123
    command:
      - '--redis.addr=redis:6379'
      - '--redis.password=Redis@123'
    networks:
      - monitoring_network
    depends_on:
      redis:
        condition: service_healthy
    restart: unless-stopped

  # ===========================================================================
  # ALERTMANAGER - Alert Notifications (Optional)
  # ===========================================================================
  alertmanager:
    image: prom/alertmanager:v0.26.0
    container_name: re_alertmanager
    ports:
      - "9093:9093"
    volumes:
      - ./alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml:ro
      - alertmanager_data:/alertmanager
    command:
      - '--config.file=/etc/alertmanager/alertmanager.yml'
      - '--storage.path=/alertmanager'
    networks:
      - monitoring_network
    restart: unless-stopped

# ===========================================================================
# NETWORKS
# ===========================================================================
networks:
  monitoring_network:
    driver: bridge
    name: re_monitoring_network

# ===========================================================================
# VOLUMES
# ===========================================================================
volumes:
  redis_data:
    name: re_redis_data
  prometheus_data:
    name: re_prometheus_data
  loki_data:
    name: re_loki_data
  promtail_data:
    name: re_promtail_data
  grafana_data:
    name: re_grafana_data
  alertmanager_data:
    name: re_alertmanager_data

