Skip to content

Commit

Permalink
use pydantic settings block, extend docker compose, log routes and se…
Browse files Browse the repository at this point in the history
…ttings during startup; closes #69
  • Loading branch information
ModischFabrications committed Mar 30, 2024
1 parent ef702ed commit 6de8944
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 27 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ black = "==24.3.0"
[packages]
fastapi = "==0.109.1"
uvicorn = "==0.26.0"
pydantic-settings = "==2.2.1"

[requires]
python_version = "3.11"
18 changes: 17 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ You can expect 12 entries to be solved after ~1s with `bruteforce`and <0.1s with
weaker machines.
Multiple cores won't speed up job time, but will enable efficient solving of parallel jobs.

The thresholds that decide which jobs are solved which way are defined in constants.py.
This might be configurable by CLI later (TODO #69).
The thresholds that decide which jobs are solved which way are defined in constants.py and can be passed as env,
see [docker-compose.yml](/docker-compose.yml) for details.

## Contributing

Expand Down
19 changes: 0 additions & 19 deletions app/constants.py

This file was deleted.

4 changes: 3 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from starlette.requests import Request
from starlette.responses import HTMLResponse, PlainTextResponse

from app.constants import version, solverSettings
from app.settings import version, solverSettings
# don't mark /app as a sources root or pycharm will delete the "app." prefix
# that's needed for pytest to work correctly
from app.solver.data.Job import Job
Expand All @@ -17,6 +17,8 @@
@asynccontextmanager
async def lifespan(app: FastAPI):
print(f"Starting CutSolver {version}...")
print(f"Settings: {solverSettings.json()}")
print(f"Routes: {app.routes}")
yield
print("Shutting down CutSolver...")

Expand Down
17 changes: 17 additions & 0 deletions app/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pydantic import PositiveInt
from pydantic_settings import BaseSettings

# constant; used for git tags
version = "v1.0.1"


class SolverSettings(BaseSettings):
# Desktop with Ryzen 2700X:
# (4, 3, 2)=1260 => 0.1s, (4, 3, 3)=4200 => 0.8s, (5, 3, 3)=9240 => 8s
bruteforce_max_combinations: PositiveInt = 5000
# that is already unusable x100, but the solver takes it easily
solver_n_max: PositiveInt = 2000


# defaults can be overwritten via env
solverSettings = SolverSettings()
4 changes: 2 additions & 2 deletions app/solver/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from itertools import permutations
from time import perf_counter

from app.constants import solverSettings
from app.settings import solverSettings
from app.solver.data.Job import Job, TargetSize
from app.solver.data.Result import ResultLengths, Result, SolverType
from app.solver.utils import _get_trimming, _sorted
Expand All @@ -18,7 +18,7 @@ def solve(job: Job) -> Result:
if job.n_combinations() <= solverSettings.bruteforce_max_combinations:
lengths = _solve_bruteforce(job)
solver_type = SolverType.bruteforce
elif job.n_targets() <= solverSettings.n_max:
elif job.n_targets() <= solverSettings.solver_n_max:
lengths = _solve_FFD(job)
solver_type = SolverType.FFD
else:
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ services:
restart: unless-stopped
ports:
- "8000:80"
environment:
- BRUTEFORCE_MAX_COMBINATIONS=4000
- SOLVER_N_MAX=1500

cutsolver_frontend:
image: modischfabrications/cutsolver_frontend:latest
Expand Down
4 changes: 2 additions & 2 deletions tag_from_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from git import Repo

from app import constants
from app import settings


def compare_versions(left: str, right: str):
Expand All @@ -35,7 +35,7 @@ def process():
repo = Repo(Path("."))
assert not repo.bare

version = constants.version
version = settings.version

version_tags_only = tuple(filter(lambda tag: tag.name[0] == "v", repo.tags))
newest_tag = version_tags_only[-1]
Expand Down

0 comments on commit 6de8944

Please sign in to comment.