Skip to content

Commit

Permalink
Statistics
Browse files Browse the repository at this point in the history
* Statistics.

* Dockerfiles.
  • Loading branch information
iwatkot authored Feb 11, 2025
1 parent df8ce9c commit 8817c75
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ maps/
osmps/
queue.json
history.json
map_directory/
map_directory/
local.env
4 changes: 3 additions & 1 deletion .github/workflows/build_docker_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ jobs:
tags: |
iwatkot/maps4fs:latest
iwatkot/maps4fs:${{ github.ref_name }}
file: ./Dockerfile
file: ./Dockerfile
build-args: |
API_TOKEN=${{ secrets.API_TOKEN }}
4 changes: 3 additions & 1 deletion .github/workflows/build_docker_lite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ jobs:
push: true
tags: |
iwatkot/maps4fs:${{ github.ref_name }}_lite
file: ./Dockerfile_lite
file: ./Dockerfile_lite
build-args: |
API_TOKEN=${{ secrets.API_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ tests/data/
osmps/
queue.json
history.json
map_directory/
map_directory/
local.env
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile_lite
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down
3 changes: 2 additions & 1 deletion dev/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ tqdm
types-tqdm
scipy
streamlit-folium
schedule
schedule
python-dotenv
19 changes: 19 additions & 0 deletions maps4fs/generator/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
SplineSettings,
TextureSettings,
)
from maps4fs.generator.statistics import send_advanced_settings, send_main_settings
from maps4fs.logger import Logger


Expand Down Expand Up @@ -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}. "
Expand Down Expand Up @@ -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:
Expand Down
59 changes: 59 additions & 0 deletions maps4fs/generator/statistics.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ owslib
tqdm
scipy
streamlit-folium
schedule
schedule
python-dotenv

0 comments on commit 8817c75

Please sign in to comment.