diff --git a/kiwi_simple_metrics/settings/__init__.py b/kiwi_simple_metrics/settings/__init__.py new file mode 100644 index 0000000..4a2a6a0 --- /dev/null +++ b/kiwi_simple_metrics/settings/__init__.py @@ -0,0 +1,36 @@ +from pydantic_settings import BaseSettings, SettingsConfigDict + +from . import metric, misc +from .metric import MetricSettings + + +class Settings(BaseSettings): + model_config = SettingsConfigDict( + env_prefix="METRIC__", + env_nested_delimiter="__", + ) + + # time between gathering reports + interval: float = 600 + + # reporting to stdout + log: misc.LogSettings = misc.LogSettings() + + # separates metrics and values in reports + separator: str = ", " + + # metrics settings + cpu: metric.CpuMS = metric.CpuMS() + memory: metric.MemoryMS = metric.MemoryMS() + disk: metric.DiskMS = metric.DiskMS() + + # pinging webhooks + webhook: misc.WebhookSettings = misc.WebhookSettings() + + +SETTINGS = Settings() + +__all__ = [ + "MetricSettings", + "SETTINGS", +] diff --git a/kiwi_simple_metrics/settings.py b/kiwi_simple_metrics/settings/metric.py similarity index 61% rename from kiwi_simple_metrics/settings.py rename to kiwi_simple_metrics/settings/metric.py index f139f4c..695696b 100644 --- a/kiwi_simple_metrics/settings.py +++ b/kiwi_simple_metrics/settings/metric.py @@ -1,9 +1,8 @@ import math from typing import Any, Literal -from pydantic import (AnyUrl, BaseModel, DirectoryPath, Field, - FieldValidationInfo, field_validator) -from pydantic_settings import BaseSettings, SettingsConfigDict +from pydantic import (BaseModel, DirectoryPath, Field, FieldValidationInfo, + field_validator) class MetricSettings(BaseModel): @@ -80,53 +79,3 @@ class DiskMS(MetricSettings): # paths to check for disk space paths: list[DirectoryPath] = Field(default_factory=list) - - -class LogSettings(BaseModel): - # if True, prints reports to stdout - enabled: bool = False - - # how to format reports to stdout - format: str = "[{state}] {result}" - - -class WebhookSettings(BaseModel): - # webhooks to ping on success/on failure - url: AnyUrl | None = None - fail: AnyUrl | None = None - - # allow insecure/self-signed webhook targets - insecure: bool = False - - def get_url(self, failed: bool) -> AnyUrl | None: - if failed: - return self.fail - - return self.url - - -class Settings(BaseSettings): - model_config = SettingsConfigDict( - env_prefix="METRIC__", - env_nested_delimiter="__", - ) - - # time between gathering reports - interval: float = 600 - - # reporting to stdout - log: LogSettings = LogSettings() - - # separates metrics and values in reports - separator: str = ", " - - # metrics settings - cpu: CpuMS = CpuMS() - memory: MemoryMS = MemoryMS() - disk: DiskMS = DiskMS() - - # pinging webhooks - webhook: WebhookSettings = WebhookSettings() - - -SETTINGS = Settings() diff --git a/kiwi_simple_metrics/settings/misc.py b/kiwi_simple_metrics/settings/misc.py new file mode 100644 index 0000000..c137f0c --- /dev/null +++ b/kiwi_simple_metrics/settings/misc.py @@ -0,0 +1,24 @@ +from pydantic import AnyUrl, BaseModel + + +class LogSettings(BaseModel): + # if True, prints reports to stdout + enabled: bool = False + + # how to format reports to stdout + format: str = "[{state}] {result}" + + +class WebhookSettings(BaseModel): + # webhooks to ping on success/on failure + url: AnyUrl | None = None + fail: AnyUrl | None = None + + # allow insecure/self-signed webhook targets + insecure: bool = False + + def get_url(self, failed: bool) -> AnyUrl | None: + if failed: + return self.fail + + return self.url