version: '3'

services:
  # Dashboard für Metriken
  grafana:
    image: grafana/grafana-oss:latest
    container_name: grafana
    ports:
      - "127.0.0.1:3000:3000"
    volumes:
      - ./monitoring/grafana-data:/var/lib/grafana

  # TSDB zum Speichern der Metriken
  prometheus:
    image: prom/prometheus:v2.37.9
    container_name: prometheus
    ports:
      - 127.0.0.1:9090:9090
    command: 
      - "--config.file=/etc/prometheus/prometheus.yml"
      - '--web.enable-admin-api'  
      - '--storage.tsdb.retention.time=1y'
    volumes:
      - ./monitoring/prometheus/config:/etc/prometheus
      - ./monitoring/prometheus/data:/prometheus
    
  # Container Advisor - Docker Compose Metriken zu scrapen
  cadvisor:
    image: gcr.io/cadvisor/cadvisor:v0.47.0     
    container_name: cadvisor
    ports:
      - 8080:8080
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:rob 
      - /dev/disk/:/dev/disk:ro
    devices:
      - /dev/kmsg
    privileged: true
  
  # Exporter für Postgres Statistiken/Metriken 
  postgres_exporter:
    image: bitnami/postgres-exporter:latest
    ports:
      - 9187:9187
    environment:
      - DATA_SOURCE_NAME=postgresql://postgres:postgres@postgres:5432/postgres?sslmode=disable
    depends_on:
      - postgres

  # DB Container ongDB
  ongdb:
    image: graphfoundation/ongdb-enterprise:1.0.5
    container_name: ongdb
    environment:
      - ONGDB_AUTH=none
    volumes:
      - "./ongdb/container/data:/data"
    ports:
      - "9474:7474"
      - "9687:7687"
    healthcheck:
      test: ["CMD", "wget", "http://ongdb:7474"]
      interval: 30s
      timeout: 20s
      retries: 5
      start_period: 30s  

  # DB Container memgraph (testweise)
  memgraph:
    image: memgraph/memgraph-platform
    container_name: memgraph
    ports:
      - "8687:7687"
      - "8444:7444"
      - "3001:3000"
      - "9091:9091"
    environment: 
      - MEMGRAPH="--query-modules-directory=/usr/lib/memgraph/query_modules --monitoring-address=0.0.0.0 --monitoring-port=7444 --log-file=/var/log/memgraph/memgraph.log --log-level=TRACE "
    volumes:
      - ./memgraph/mg_lib:/var/lib/memgraph
      - ./memgraph/mg_log:/var/log/memgraph:rw
      - ./memgraph/mg_etc:/etc/memgraph
    entrypoint: ["/usr/bin/supervisord"]
    healthcheck:
      test: ["CMD", "mgconsole"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 30s  

  #mem_exporter:
  #  image: mem_exporter
  #  container_name: mem_exporter
  #  ports: 
  #    - "9115:9115"

  # Datenbank Container - Neo4j
  neo4j:
    image: neo4j:enterprise
    container_name: neo4j
    volumes:
      - ./neo4j/container_e/data:/data:rw
      - ./neo4j/container_e/logs:/logs:rw
      #- ./neo4j/container_e/import:/var/lib/neo4j/import:rw
      #- ./neo4j/container_e/conf:/var/lib/neo4j/conf:rw
    ports:
      - 7474:7474
      - 7687:7687 
      - 2004:2004
    environment:
      - NEO4J_AUTH=none 
      - NEO4J_server_metrics_prometheus_enabled=true
      - NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
      - NEO4J_server_metrics_prometheus_endpoint=0.0.0.0:2004
    healthcheck:
      test: ["CMD", "wget", "http://neo4j:7474"]
      interval: 30s
      timeout: 20s
      retries: 5
      start_period: 30s  

  # Datenbank Container - Postgres
  postgres:
    image: postgres:latest
    container_name: postgres
    #user: "sven:postgres" 
    environment:
      - PGUSER=postgres
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    healthcheck:
      test: ["CMD-SHELL", "pg_isready","-U postgres"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 10s  
    logging:
      options:
        max-size: 10m
        max-file: "3"
    ports:
      - 5432:5432
    volumes: 
      - ./sql/docker/data:/var/lib/postgresql/data:rw
      - ./sql/docker/postgres_logs:/var/log/postgresql:rw
      # copy the sql script to create tables
      # - ./sql/skript/import_cadets:/opt/import_cadets:rw

  # Admin GUI Postgres
  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: pgadmin4_container
    ports:
      - "8888:80"
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@abc.de
      PGADMIN_DEFAULT_PASSWORD: admin
    volumes:
      - ./sql/pgadmin-data:/var/lib/pgadmin

  # Infrakstruktur zum Publishen und Streamen von CDM Daten
  sub_ongdb:
    container_name: sub_ongdb
    image: lab.it.hs-hannover.de:4567/cwy-p8d-u1/ma_code/sub_ongdb
    environment:
      - abort_time_limit=999999
      - db_host=bolt://ongdb:7687 
      - mos_host=mos1
      - mos_port=1883
    healthcheck:
      test: ["CMD","ls"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 10s
    depends_on:
      ongdb:
        condition: service_healthy
        #restart: true

  mosquitto:
    container_name: mos1
    image: eclipse-mosquitto
    ports:
      - 1883:1883
      - 9001:9001
      - 8883:8883
    volumes:
      - ./streaming/mosquitto.conf:/mosquitto/config/mosquitto.conf
      - ./streaming/log/mosquitto.log:/mosquitto/log/mosquitto.log     

  sub_pg:
    container_name: sub_pg
    image: lab.it.hs-hannover.de:4567/cwy-p8d-u1/ma_code/sub_pg
    volumes:
      - ./streaming/clients/sub/postgres/:/var/lib/import/
    environment:
      - path_sql_script=/var/lib/import/
    depends_on:
      postgres:
        condition: service_healthy
        #restart: true

  sub_neo4j:
    container_name: sub_neo4j
    image: lab.it.hs-hannover.de:4567/cwy-p8d-u1/ma_code/sub_neo4j
    environment:
      - abort_time_limit=999999
      - neo4j_host=bolt://neo4j:7687 
      - mos_host=mos1
      - mos_port=1883
    healthcheck:
      test: ["CMD","ls"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 10s
    depends_on:
      neo4j:
        condition: service_healthy
        #restart: true

  sub_mem:
    container_name: sub_mem
    image: lab.it.hs-hannover.de:4567/cwy-p8d-u1/ma_code/sub_mem
    environment:
      - abort_time_limit=999999
      - mem_host=bolt://memgraph:7687 
      - mos_host=mos1
      - mos_port=1883
    depends_on:
      memgraph:
        condition: service_healthy
        #restart: true

  pub: 
    container_name: pub_cdm
    image: lab.it.hs-hannover.de:4567/cwy-p8d-u1/ma_code/pub_cdm
    environment:
      - lines_per_window=2000
      - path_to_firstK=/var/lib/import/first1k.json
      - path_data=/var/lib/import/
      - sleep_time=1
    volumes: 
      - ./streaming/clients/pub/data:/var/lib/import
    depends_on: 
      sub_neo4j:
        condition: service_healthy
      sub_ongdb: 
        condition: service_healthy
      sub_pg:
        condition: service_started    
      sub_mem:
        condition: service_started

    # infrastructure for testing
  query_pg:
    container_name: query_pg
    image: lab.it.hs-hannover.de:4567/cwy-p8d-u1/ma_code/sql_queries
    environment:
      - PGHOST=postgres
      - PGDATABASE=postgres
      - PGUSER=postgres
      - PGPASSWORD=postgres
      - PGPORT=5432
      - interval=5
      - query_file_path=/app/queries/queries.txt
    volumes:
      - ./eval/query/pg/log:/app/log
      - ./eval/query/pg/queries:/app/queries
    depends_on:
      - pub

  query_neo4j:
    container_name: query_neo4j
    image: lab.it.hs-hannover.de:4567/cwy-p8d-u1/ma_code/cypher_queries
    environment:
      - interval=5
      - db_host=neo4j 
      - db_port=7687
      - query_file_path=/app/queries/queries.txt
    volumes:
      - ./eval/query/neo4j/log:/app/log
      - ./eval/query/neo4j/queries:/app/queries
    depends_on:
      - pub

  query_ongdb:
    container_name: query_ongdb
    image: lab.it.hs-hannover.de:4567/cwy-p8d-u1/ma_code/ongdb_queries
    environment:
      - interval=5
      - db_host=ongdb 
      - db_port=7687
      - query_file_path=/app/queries/queries.txt
    volumes:
      - ./eval/query/ongdb/log:/app/log
      - ./eval/query/ongdb/queries:/app/queries
    depends_on:
      - pub
  
  query_memgraph:
    container_name: query_memgraph
    image: lab.it.hs-hannover.de:4567/cwy-p8d-u1/ma_code/cypher_queries
    environment:
      - interval=5
      - db_host=memgraph 
      - db_port=7687
      - query_file_path=/app/queries/queries.txt
    volumes:
      - ./eval/query/memgraph/log:/app/log
      - ./eval/query/memgraph/queries:/app/queries
    depends_on:
      - pub  


    

volumes:
  postgres:
  pgadmin:
  prometheus-data:
    driver: local
  neo4j:
  mg_lib:
  mg_log:
  mg_etc:

networks:
  postgres:
    external: true
