implement behaviour control by config via setup data path
This commit was merged in pull request #22.
This commit is contained in:
@@ -33,7 +33,6 @@ from delta_barth.constants import (
|
||||
DEFAULT_DB_ERR_CODE,
|
||||
DUMMY_DATA_PATH,
|
||||
FEATURES_SALES_PROGNOSIS,
|
||||
SALES_BASE_NUM_DATAPOINTS_MONTHS,
|
||||
SALES_MIN_NUM_DATAPOINTS,
|
||||
)
|
||||
from delta_barth.errors import STATUS_HANDLER, wrap_result
|
||||
@@ -433,7 +432,7 @@ def pipeline_sales_forecast(
|
||||
pipe = _process_sales(
|
||||
pipe,
|
||||
min_num_data_points=SALES_MIN_NUM_DATAPOINTS,
|
||||
base_num_data_points_months=SALES_BASE_NUM_DATAPOINTS_MONTHS,
|
||||
base_num_data_points_months=SESSION.cfg.forecast.threshold_month_data_points,
|
||||
)
|
||||
if pipe.statistics is not None:
|
||||
res = _write_sales_forecast_stats_wrapped(pipe.statistics)
|
||||
|
||||
@@ -5,6 +5,7 @@ from typing import Final
|
||||
from delta_barth.types import DualDict, HttpContentHeaders
|
||||
|
||||
# ** config
|
||||
CFG_FILENAME: Final[str] = "dopt-cfg.toml"
|
||||
|
||||
# ** lib path
|
||||
lib_path = Path(__file__).parent
|
||||
@@ -63,4 +64,6 @@ FEATURES_SALES_PROGNOSIS: Final[frozenset[str]] = frozenset(
|
||||
# ** Pipelines
|
||||
# ** Forecast
|
||||
SALES_MIN_NUM_DATAPOINTS: Final[int] = 36
|
||||
SALES_BASE_NUM_DATAPOINTS_MONTHS: Final[int] = 36
|
||||
# !! now in config
|
||||
# TODO remove later till proven stable
|
||||
# SALES_BASE_NUM_DATAPOINTS_MONTHS: Final[int] = 36
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Final
|
||||
|
||||
@@ -14,12 +15,19 @@ from delta_barth.api.common import (
|
||||
LoginResponse,
|
||||
validate_credentials,
|
||||
)
|
||||
from delta_barth.constants import API_CON_TIMEOUT, DB_ECHO
|
||||
from delta_barth.config import LazyCfgLoader
|
||||
from delta_barth.constants import (
|
||||
API_CON_TIMEOUT,
|
||||
CFG_FILENAME,
|
||||
DB_ECHO,
|
||||
LIB_PATH,
|
||||
)
|
||||
from delta_barth.errors import STATUS_HANDLER
|
||||
from delta_barth.logging import logger_session as logger
|
||||
from delta_barth.types import DelBarApiError, Status
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from delta_barth.config import Config
|
||||
from delta_barth.types import ApiCredentials, HttpContentHeaders
|
||||
|
||||
|
||||
@@ -41,6 +49,7 @@ class Session:
|
||||
base_headers: HttpContentHeaders,
|
||||
db_folder: str = "data",
|
||||
logging_folder: str = "logs",
|
||||
cfg_folder: str = "config",
|
||||
) -> None:
|
||||
self._setup: bool = False
|
||||
self._data_path: Path | None = None
|
||||
@@ -49,6 +58,10 @@ class Session:
|
||||
self._db_engine: sql.Engine | None = None
|
||||
self._logging_dir: Path | None = None
|
||||
self._logging_folder = logging_folder
|
||||
self._cfg_path: Path | None = None
|
||||
self._cfg_folder = cfg_folder
|
||||
self._cfg_loader: LazyCfgLoader | None = None
|
||||
self._cfg: Config | None = None
|
||||
self._creds: ApiCredentials | None = None
|
||||
self._base_url: str | None = None
|
||||
self._headers = base_headers
|
||||
@@ -59,6 +72,7 @@ class Session:
|
||||
# at this point: no logging configured
|
||||
assert not self._setup, "tried to setup session twice"
|
||||
self._setup_logging()
|
||||
self._setup_config()
|
||||
self._setup_db_management()
|
||||
self._setup = True
|
||||
logger.info("[SESSION] Setup procedure successful")
|
||||
@@ -68,6 +82,32 @@ class Session:
|
||||
assert self._data_path is not None, "accessed data path not set"
|
||||
return self._data_path
|
||||
|
||||
@property
|
||||
def cfg_path(self) -> Path:
|
||||
if self._cfg_path is not None and self._setup:
|
||||
return self._cfg_path
|
||||
|
||||
root = (self.data_path / self._cfg_folder).resolve()
|
||||
cfg_path = root / CFG_FILENAME
|
||||
if not root.exists():
|
||||
root.mkdir(parents=False)
|
||||
self._cfg_path = cfg_path
|
||||
return self._cfg_path
|
||||
|
||||
@property
|
||||
def cfg(self) -> Config:
|
||||
assert self._cfg is not None, "tried to access not set config from session"
|
||||
return self._cfg
|
||||
|
||||
def _setup_config(self) -> None:
|
||||
if not self.cfg_path.exists():
|
||||
src_cfg = LIB_PATH / CFG_FILENAME
|
||||
shutil.copyfile(src_cfg, self.cfg_path)
|
||||
|
||||
self._cfg_loader = LazyCfgLoader(self.cfg_path)
|
||||
self._cfg = self._cfg_loader.get()
|
||||
logger.info("[SESSION] Successfully read and setup config")
|
||||
|
||||
@property
|
||||
def db_engine(self) -> sql.Engine:
|
||||
assert self._db_engine is not None, "accessed database engine not set"
|
||||
@@ -78,10 +118,10 @@ class Session:
|
||||
if self._db_path is not None and self._setup:
|
||||
return self._db_path
|
||||
|
||||
db_root = (self.data_path / self._db_folder).resolve()
|
||||
db_path = db_root / "dopt-data.db"
|
||||
if not db_root.exists():
|
||||
db_root.mkdir(parents=False)
|
||||
root = (self.data_path / self._db_folder).resolve()
|
||||
db_path = root / "dopt-data.db"
|
||||
if not root.exists():
|
||||
root.mkdir(parents=False)
|
||||
self._db_path = db_path
|
||||
return self._db_path
|
||||
|
||||
|
||||
Reference in New Issue
Block a user