# Note: this is an overrides file for ./docker/docker-compose.community.yml:
# It's used for setting special environment and configurations for running integration tests.
# This file also includes services that only exists in Hive's Cloud version, and are not available in the self-hosted version.
# Please refer to TESTING.md for more information.

version: '3.8'
name: 'hive-tests'

networks:
  stack:
    name: hive

services:
  local_cdn:
    image: node:22.12.0-alpine3.20
    working_dir: /app
    command: ['node', 'index.js']
    networks:
      - 'stack'
    healthcheck:
      test: ['CMD', 'wget', '--spider', '-q', 'localhost:3004/_readiness']
      interval: 5s
      timeout: 5s
      retries: 6
      start_period: 5s
    ports:
      - 3004:3004
    volumes:
      - '${PWD}/packages/services/cdn-worker/dist/index.nodejs.js:/app/index.js'
    environment:
      NODE_ENV: production
      PORT: 3004
      LOG_LEVEL: debug
      CDN_AUTH_PRIVATE_KEY: 1e1064ef9cda8bf38936b77317e90dc3
      S3_ENDPOINT: 'http://s3:9000'
      S3_ACCESS_KEY_ID: minioadmin
      S3_SECRET_ACCESS_KEY: minioadmin
      S3_BUCKET_NAME: artifacts

  local_broker:
    image: node:22.12.0-alpine3.20
    working_dir: /app
    command: ['node', 'broker.js']
    networks:
      - 'stack'
    healthcheck:
      test: ['CMD', 'wget', '--spider', '-q', 'localhost:3013/_readiness']
      interval: 5s
      timeout: 5s
      retries: 6
      start_period: 5s
    ports:
      - 3013:3013
    volumes:
      - '${PWD}/packages/services/broker-worker/dist/index.nodejs.js:/app/broker.js'
    environment:
      NODE_ENV: production
      PORT: 3013
      LOG_LEVEL: debug
      CF_BROKER_SIGNATURE: secretSignature

  mock_server:
    image: mockserver/mockserver:5.15.0
    # healthcheck - no time to implement it
    # The image does not contain curl or wget.
    networks:
      - 'stack'
    ports:
      - 3042:3042
    environment:
      MOCKSERVER_LOG_LEVEL: DEBUG
      MOCKSERVER_SERVER_PORT: 3042

  composition_federation_2:
    image: '${DOCKER_REGISTRY}composition-federation-2${DOCKER_TAG}'
    networks:
      - 'stack'
    healthcheck:
      test: ['CMD', 'wget', '--spider', '-q', 'localhost:3069/_readiness']
      interval: 5s
      timeout: 5s
      retries: 6
      start_period: 5s
    ports:
      - 3069:3069
    environment:
      NODE_ENV: production
      PORT: 3069
      LOG_LEVEL: debug
      SECRET: '${EXTERNAL_COMPOSITION_SECRET}'

  external_composition:
    image: node:22.12.0-alpine3.20
    working_dir: /app
    command: ['node', 'example.mjs']
    networks:
      - 'stack'
    healthcheck:
      test: ['CMD', 'wget', '--spider', '-q', 'localhost:3012/_readiness']
      interval: 5s
      timeout: 5s
      retries: 6
      start_period: 5s
    ports:
      - 3012:3012
    volumes:
      - '${PWD}/packages/libraries/external-composition/dist/example.js:/app/example.mjs'
    environment:
      NODE_ENV: production
      PORT: 3012
      LOG_LEVEL: debug
      SECRET: '${EXTERNAL_COMPOSITION_SECRET}'

  usage-estimator:
    image: '${DOCKER_REGISTRY}usage-estimator${DOCKER_TAG}'
    networks:
      - 'stack'
    ports:
      - 3008:3008
    depends_on:
      clickhouse:
        condition: service_healthy
      migrations:
        condition: service_completed_successfully
    environment:
      NODE_ENV: production
      LOG_LEVEL: debug
      CLICKHOUSE_PROTOCOL: 'http'
      CLICKHOUSE_HOST: 'clickhouse'
      CLICKHOUSE_PORT: '8123'
      CLICKHOUSE_USERNAME: '${CLICKHOUSE_USER}'
      CLICKHOUSE_PASSWORD: '${CLICKHOUSE_PASSWORD}'
      PORT: 3008
      FF_CLICKHOUSE_V2_TABLES: ${FF_CLICKHOUSE_V2_TABLES:-}

  rate-limit:
    image: '${DOCKER_REGISTRY}rate-limit${DOCKER_TAG}'
    networks:
      - 'stack'
    ports:
      - 3009:3009
    depends_on:
      clickhouse:
        condition: service_healthy
      migrations:
        condition: service_completed_successfully
      usage-estimator:
        condition: service_healthy
      emails:
        condition: service_healthy
    environment:
      NODE_ENV: production
      LOG_LEVEL: debug
      LIMIT_CACHE_UPDATE_INTERVAL_MS: '${LIMIT_CACHE_UPDATE_INTERVAL_MS}'
      POSTGRES_HOST: db
      POSTGRES_PORT: 5432
      POSTGRES_DB: '${POSTGRES_DB}'
      POSTGRES_USER: '${POSTGRES_USER}'
      POSTGRES_PASSWORD: '${POSTGRES_PASSWORD}'
      USAGE_ESTIMATOR_ENDPOINT: http://usage-estimator:3008
      EMAILS_ENDPOINT: http://emails:3011
      PORT: 3009

  # Overrides only to `docker-compose.community.yaml` from now on:
  server:
    environment:
      CDN_CF: '1'
      CDN_CF_BASE_PATH: http://local_cdn:3004
      CDN_CF_ACCOUNT_ID: 103df45224310d669213971ce28b5b70
      CDN_CF_AUTH_TOKEN: 85e20c26c03759603c0f45884824a1c3
      CDN_CF_NAMESPACE_ID: 33b1e3bbb4a4707d05ea0307cbb55c79
      CDN_CF_BASE_URL: http://localhost:3004
      CDN_AUTH_PRIVATE_KEY: 1e1064ef9cda8bf38936b77317e90dc3
      CDN_API: '1'
      CDN_API_BASE_URL: 'http://localhost:3001'
      REQUEST_BROKER: '1'
      REQUEST_BROKER_SIGNATURE: 'secretSignature'
      REQUEST_BROKER_ENDPOINT: 'http://local_broker:3013'
      GITHUB_APP_ID: 123123
      GITHUB_APP_PRIVATE_KEY: 5f938d51a065476c4dc1b04aeba13afb
      FEEDBACK_SLACK_TOKEN: ''
      FEEDBACK_SLACK_CHANNEL: '#hive'
      USAGE_ESTIMATOR_ENDPOINT: '${USAGE_ESTIMATOR_ENDPOINT}'
      RATE_LIMIT_ENDPOINT: '${RATE_LIMIT_ENDPOINT}'
      EMAIL_PROVIDER: '${EMAIL_PROVIDER}'
      LOG_LEVEL: debug
      # Auth
      WEB_APP_URL: '${HIVE_APP_BASE_URL}'
      AUTH_ORGANIZATION_OIDC: '1'
      AUTH_REQUIRE_EMAIL_VERIFICATION: '0'
      SUPERTOKENS_CONNECTION_URI: http://supertokens:3567
      SUPERTOKENS_API_KEY: '${SUPERTOKENS_API_KEY}'
      GRAPHQL_PUBLIC_ORIGIN: http://localhost:8082

  broker:
    image: redpandadata/redpanda:latest
    hostname: broker
    networks:
      - 'stack'
    command:
      - redpanda
      - start
      - --smp
      - '1'
      - --set redpanda.empty_seed_starts_cluster=false
      - --seeds "redpanda-1:33145"
      - --kafka-addr
      - PLAINTEXT://0.0.0.0:29092,OUTSIDE://0.0.0.0:9092
      - --advertise-kafka-addr
      - PLAINTEXT://broker:29092,OUTSIDE://localhost:9092
      - --pandaproxy-addr
      - PLAINTEXT://0.0.0.0:28082,OUTSIDE://0.0.0.0:8082
      - --advertise-pandaproxy-addr
      - PLAINTEXT://broker:28082,OUTSIDE://localhost:8082
      - --advertise-rpc-addr redpanda-1:33145
    mem_limit: 300m
    mem_reservation: 100m
    healthcheck:
      test: 'curl -f http://localhost:9644/public_metrics'
      interval: 3s
      timeout: 3s
      retries: 6
      start_period: 5s

  db:
    ports:
      - '5432:5432'
  clickhouse:
    ports:
      - '8123:8123'
    volumes:
      - ./.hive/clickhouse/logs:/var/log/clickhouse-server
      - ./.hive/clickhouse/db:/var/lib/clickhouse
      - ./configs/clickhouse:/etc/clickhouse-server/conf.d

  supertokens:
    ports:
      - '3567:3567'

  usage:
    environment:
      RATE_LIMIT_ENDPOINT: '${RATE_LIMIT_ENDPOINT}'
      LOG_LEVEL: debug
    depends_on:
      broker:
        condition: service_started # Redpand is ready when it starts
      tokens:
        condition: service_healthy

  usage-ingestor:
    environment:
      CLICKHOUSE_ASYNC_INSERT_BUSY_TIMEOUT_MS: '${CLICKHOUSE_ASYNC_INSERT_BUSY_TIMEOUT_MS}'
      CLICKHOUSE_ASYNC_INSERT_MAX_DATA_SIZE: '${CLICKHOUSE_ASYNC_INSERT_MAX_DATA_SIZE}'
      LOG_LEVEL: debug
    depends_on:
      broker:
        condition: service_started # Redpand is ready when it starts
      tokens:
        condition: service_healthy

  emails:
    environment:
      EMAIL_PROVIDER: '${EMAIL_PROVIDER}'
      LOG_LEVEL: debug
    ports:
      - '3011:3011'

  schema:
    environment:
      LOG_LEVEL: debug
      REQUEST_BROKER: '1'
      REQUEST_BROKER_SIGNATURE: 'secretSignature'
      REQUEST_BROKER_ENDPOINT: 'http://local_broker:3013'
    ports:
      - '3002:3002'

  #
  # Awkwardly, we need to override some services for different reasons
  #

  # It's not part of integration tests
  app:
    image: node:22.12.0-alpine3.20
    command: ['npx', 'http-server']

  # Redpand is used for integration tests, instead of Kafka. Zookeeper is no longer needed
  zookeeper:
    image: node:22.12.0-alpine3.20
    command: ['npx', 'http-server']
