diff --git a/.dockerignore b/.dockerignore index 632cc18..a1ed966 100644 --- a/.dockerignore +++ b/.dockerignore @@ -20,4 +20,5 @@ maps/ osmps/ queue.json history.json -map_directory/ \ No newline at end of file +map_directory/ +local.env \ No newline at end of file diff --git a/.github/workflows/build_docker_full.yml b/.github/workflows/build_docker_full.yml index e4ba251..96debdb 100644 --- a/.github/workflows/build_docker_full.yml +++ b/.github/workflows/build_docker_full.yml @@ -33,4 +33,6 @@ jobs: tags: | iwatkot/maps4fs:latest iwatkot/maps4fs:${{ github.ref_name }} - file: ./Dockerfile \ No newline at end of file + file: ./Dockerfile + build-args: | + API_TOKEN=${{ secrets.API_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/build_docker_lite.yml b/.github/workflows/build_docker_lite.yml index 38b6d47..06f6cdc 100644 --- a/.github/workflows/build_docker_lite.yml +++ b/.github/workflows/build_docker_lite.yml @@ -32,4 +32,6 @@ jobs: push: true tags: | iwatkot/maps4fs:${{ github.ref_name }}_lite - file: ./Dockerfile_lite \ No newline at end of file + file: ./Dockerfile_lite + build-args: | + API_TOKEN=${{ secrets.API_TOKEN }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index d957678..1963095 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ tests/data/ osmps/ queue.json history.json -map_directory/ \ No newline at end of file +map_directory/ +local.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index c22acbd..0e9e779 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,8 @@ FROM python:3.11-slim-buster +ARG API_TOKEN +ENV API_TOKEN=${API_TOKEN} + # Dependencies for opencv and blender. RUN apt-get update && apt-get install -y \ libgl1-mesa-dev \ diff --git a/Dockerfile_lite b/Dockerfile_lite index 94e00e5..8fdc5d2 100644 --- a/Dockerfile_lite +++ b/Dockerfile_lite @@ -1,5 +1,8 @@ FROM python:3.11-slim-buster +ARG API_TOKEN +ENV API_TOKEN=${API_TOKEN} + # Dependencies for opencv. RUN apt-get update && apt-get install -y \ libgl1-mesa-dev \ diff --git a/dev/requirements.txt b/dev/requirements.txt index 85a1d5c..7a608fc 100644 --- a/dev/requirements.txt +++ b/dev/requirements.txt @@ -23,4 +23,5 @@ tqdm types-tqdm scipy streamlit-folium -schedule \ No newline at end of file +schedule +python-dotenv \ No newline at end of file diff --git a/maps4fs/generator/map.py b/maps4fs/generator/map.py index 2bb3ecf..cb30315 100644 --- a/maps4fs/generator/map.py +++ b/maps4fs/generator/map.py @@ -20,6 +20,7 @@ SplineSettings, TextureSettings, ) +from maps4fs.generator.statistics import send_advanced_settings, send_main_settings from maps4fs.logger import Logger @@ -74,6 +75,19 @@ def __init__( self.coordinates = coordinates self.map_directory = map_directory + try: + main_settings = { + "game": game.code, + "coordinates": coordinates, + "size": size, + "rotation": rotation, + "dtm_provider": dtm_provider.name(), + "custom_osm": bool(custom_osm), + } + send_main_settings(main_settings) + except Exception as e: + self.logger.error("Error sending main settings: %s", e) + log_entry = "" log_entry += f"Map instance created for Game: {game.code}. " log_entry += f"Coordinates: {coordinates}. Size: {size}. Rotation: {rotation}. " @@ -128,6 +142,11 @@ def __init__( for setting in settings: settings_json[setting.__class__.__name__] = setting.model_dump() + try: + send_advanced_settings(settings_json) + except Exception as e: + self.logger.error("Error sending advanced settings: %s", e) + save_path = os.path.join(self.map_directory, "generation_settings.json") with open(save_path, "w", encoding="utf-8") as file: diff --git a/maps4fs/generator/statistics.py b/maps4fs/generator/statistics.py new file mode 100644 index 0000000..0e06a22 --- /dev/null +++ b/maps4fs/generator/statistics.py @@ -0,0 +1,59 @@ +"""Module for sending settings to the statistics server.""" + +import os +from typing import Any + +import requests + +from maps4fs.logger import Logger + +logger = Logger() + +try: + from dotenv import load_dotenv + + load_dotenv("local.env") +except Exception: + pass + +STATS_HOST = os.getenv("STATS_HOST") +if not STATS_HOST: + print("STATS_HOST not set in environment") +API_TOKEN = os.getenv("API_TOKEN") +if not API_TOKEN: + print("API_TOKEN not set in environment") + + +def send_settings(endpoint: str, data: dict[str, Any]) -> None: + """Send settings to the statistics server. + + Arguments: + endpoint (str): The endpoint to send the settings to. + data (dict[str, Any]): The settings to send. + """ + headers = {"Authorization": f"Bearer {API_TOKEN}", "Content-Type": "application/json"} + response = requests.post(endpoint, headers=headers, json=data, timeout=10) + if response.status_code != 200: + logger.error("Failed to send settings: %s", response.text) + else: + logger.info("Settings sent successfully") + + +def send_main_settings(data: dict[str, Any]) -> None: + """Send main settings to the statistics server. + + Arguments: + data (dict[str, Any]): The main settings to send. + """ + endpoint = f"{STATS_HOST}/receive_main_settings" + send_settings(endpoint, data) + + +def send_advanced_settings(data: dict[str, Any]) -> None: + """Send advanced settings to the statistics server. + + Arguments: + data (dict[str, Any]): The advanced settings to send. + """ + endpoint = f"{STATS_HOST}/receive_advanced_settings" + send_settings(endpoint, data) diff --git a/requirements.txt b/requirements.txt index e8879dd..38d5ec4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,4 +16,5 @@ owslib tqdm scipy streamlit-folium -schedule \ No newline at end of file +schedule +python-dotenv \ No newline at end of file