# Rotate every container's stdout logs (Docker keeps them forever by default).
x-logging: &logging
  driver: json-file
  options:
    max-size: "50m"
    max-file: "3"

services:

  app:
    image: ghcr.io/mlab-sh/tprm.mlab.sh:latest
    logging: *logging
    ports:
      - "${APP_PORT:-8080}:8080"
    depends_on:
      mysql:
        condition: service_healthy
      clickhouse:
        condition: service_healthy
    env_file: .env
    environment:
      - DB_HOST=mysql
      - CH_HOST=clickhouse
    volumes:
      - uploads:/app_mlab_sh/uploads
      - logs:/app_mlab_sh/log
    restart: unless-stopped

  mysql:
    image: mysql:8
    logging: *logging
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_DATABASE: mlabtprm
      MYSQL_USER: ${DB_USERNAME}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 5s
      timeout: 5s
      retries: 10
    restart: unless-stopped

  clickhouse:
    # Pinned to the version nativeclick (Cargo.toml) targets; bump both together.
    # Each version bump renames changed system.*_log tables to *_log_N: run
    # clickhouse-cleanup-system-logs.sh after upgrading (see the update docs).
    image: clickhouse/clickhouse-server:26.9
    logging: *logging
    environment:
      CLICKHOUSE_USER: ${CH_USERNAME}
      CLICKHOUSE_PASSWORD: ${CH_PASSWORD}
      CLICKHOUSE_DB: mlabtprm
      CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: 1
    configs:
      - source: clickhouse_system_logs
        target: /etc/clickhouse-server/config.d/zz-system-logs.xml
    volumes:
      - clickhouse_data:/var/lib/clickhouse
    healthcheck:
      test: ["CMD", "clickhouse-client", "--query", "SELECT 1"]
      interval: 5s
      timeout: 5s
      retries: 10
    restart: unless-stopped

  # Optional document storage (contracts and annexes) on S3. Any S3-compatible
  # service works; this runs Silo next to the app. Uncomment it and silo_data
  # below, set S3_ENDPOINT=http://silo:9000, S3_BUCKET, S3_ACCESS_KEY_ID and
  # S3_SECRET_ACCESS_KEY in .env, then create the bucket once:
  #   docker compose exec silo sh -c 'mc alias set local http://127.0.0.1:9000 "$MINIO_ROOT_USER" "$MINIO_ROOT_PASSWORD" && mc mb local/tprm-docs'
  # silo:
  #   image: docker.io/pgsty/silo
  #   logging: *logging
  #   command: server /data
  #   environment:
  #     - MINIO_ROOT_USER=${S3_ACCESS_KEY_ID}
  #     - MINIO_ROOT_PASSWORD=${S3_SECRET_ACCESS_KEY}
  #   volumes:
  #     - silo_data:/data
  #   restart: unless-stopped

# Inline so this file stays the whole bundle (no extra file to download); mounted read-only.
# Without it ClickHouse fills the disk with its own system.*_log tables (no TTL) and
# trace-level log files. Business data (database mlabtprm) is not affected.
configs:
  clickhouse_system_logs:
    content: |
      <clickhouse>
        <!-- Warnings only, to the console (rotated by Docker): no log files in the container. -->
        <logger>
          <level>warning</level>
          <console>1</console>
          <log remove="1"/>
          <errorlog remove="1"/>
        </logger>

        <!-- High-volume system logs with no TTL: off. -->
        <text_log remove="1"/>
        <trace_log remove="1"/>
        <metric_log remove="1"/>
        <asynchronous_metric_log remove="1"/>
        <part_log remove="1"/>
        <query_metric_log remove="1"/>
        <processors_profile_log remove="1"/>
        <opentelemetry_span_log remove="1"/>
        <session_log remove="1"/>
        <background_schedule_pool_log remove="1"/>

        <!-- Kept for troubleshooting, with a short TTL. -->
        <query_log><ttl>event_date + INTERVAL 3 DAY DELETE</ttl></query_log>
        <query_thread_log><ttl>event_date + INTERVAL 3 DAY DELETE</ttl></query_thread_log>
        <query_views_log><ttl>event_date + INTERVAL 3 DAY DELETE</ttl></query_views_log>
        <error_log><ttl>event_date + INTERVAL 7 DAY DELETE</ttl></error_log>
        <crash_log><ttl>event_date + INTERVAL 7 DAY DELETE</ttl></crash_log>

        <!-- Stop writing with 5 GiB left, before the host disk is full. -->
        <storage_configuration>
          <disks>
            <default>
              <keep_free_space_bytes>5368709120</keep_free_space_bytes>
            </default>
          </disks>
        </storage_configuration>
      </clickhouse>

volumes:
  mysql_data:
  clickhouse_data:
  uploads:
  logs:
  # silo_data:
