Skip to content

Commit

Permalink
Public history.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwatkot committed Feb 1, 2025
1 parent c357d96 commit e19acb2
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ maps4fs.zip
maps/
osmps/
queue.json
history.json
map_directory/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ htmlcov/
tests/data/
osmps/
queue.json
history.json
map_directory/
3 changes: 3 additions & 0 deletions webui/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
DEFAULT_LON = 20.237433441210115

QUEUE_FILE = os.path.join(WORKING_DIRECTORY, "queue.json")
HISTORY_FILE = os.path.join(WORKING_DIRECTORY, "history.json")
HISTORY_INTERVAL = 60 * 60 * 2 # 2 hours
HISTORY_LIMIT = 5
QUEUE_TIMEOUT = 180 # 3 minutes
QUEUE_INTERVAL = 10

Expand Down
11 changes: 11 additions & 0 deletions webui/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from generator.advanced_settings import AdvancedSettings
from generator.expert_settings import ExpertSettings
from generator.main_settings import MainSettings
from history import add_entry, is_over_limit
from PIL import Image
from queuing import add_to_queue, get_queue_length, remove_from_queue, wait_in_queue
from streamlit_stl import stl_from_file
Expand Down Expand Up @@ -295,6 +296,16 @@ def generate_map(self) -> None:
mp, session_name = self.read_generation_settings()

if self.public:
# TODO:
if is_over_limit(*mp.coordinates):
self.status_container.error(
"You have reached the limit of generations. Please, try again in two hours.",
icon="❌",
)
return

add_entry(*mp.coordinates)

add_to_queue(session_name)
for position in wait_in_queue(session_name):
self.status_container.info(
Expand Down
105 changes: 105 additions & 0 deletions webui/history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import json
import os
from time import time

from config import HISTORY_FILE, HISTORY_INTERVAL, HISTORY_LIMIT


def get_history(force: bool = False) -> dict[str, int]:
"""Get the history from the history file.
History file is a dictionary where the key is a lat, lot coordinates and the value
is the number of times that the generation of for those coordinates was requested.
The dictionary is also contains a key "created" with the epoch time of the creation of the file.
Arguments:
force (bool): Whether to force the creation of a new history file.
Returns:
dict[str, int]: The history.
"""

def load_history() -> dict:
with open(HISTORY_FILE, "r") as f:
return json.load(f)

if force or not os.path.isfile(HISTORY_FILE):
history = null_history()
else:
history = load_history()
created = history.get("created")
if not created or created + HISTORY_INTERVAL < int(time()):
history = null_history()

save_history(history)
return history


def save_history(history: dict) -> None:
"""Save the history dictionary to the history file."""
with open(HISTORY_FILE, "w") as f:
json.dump(history, f)


def null_history() -> dict[tuple[float, float] | str, int]:
"""Return a null history."""
return {"created": int(time())}


def add_entry(lat: float, lon: float) -> None:
"""Add an entry to the history.
Arguments:
lat (float): The latitude.
lon (float): The longitude.
"""
history = get_history()
key = get_coordinates_key(lat, lon)
if key in history:
history[key] += 1
else:
history[key] = 1

save_history(history)


def get_count(lat: float, lon: float) -> int:
"""Get the count of the number of times that the generation of the coordinates was requested.
Arguments:
lat (float): The latitude.
lon (float): The longitude.
Returns:
int: The count.
"""
history = get_history()
key = get_coordinates_key(lat, lon)
return history.get(key, 0)


def get_coordinates_key(lat: float, lon: float) -> str:
"""Get the key for the coordinates in the history.
Arguments:
lat (float): The latitude.
lon (float): The longitude.
Returns:
str: The key.
"""
lat = round(lat, 4)
lon = round(lon, 4)
return f"{lat},{lon}"


def is_over_limit(lat: float, lon: float) -> bool:
"""Check if the generation of the coordinates was requested more than the limit.
Arguments:
lat (float): The latitude.
lon (float): The longitude.
Returns:
bool: Whether the limit was reached.
"""
return get_count(lat, lon) >= HISTORY_LIMIT

0 comments on commit e19acb2

Please sign in to comment.