From f489f90a92f3d059f48fc932da7c007ceaad499d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Fri, 1 Sep 2023 00:26:29 +0000 Subject: [PATCH] settings module split --- kiwi_simple_metrics/settings/__init__.py | 36 ++++++++++++ .../{settings.py => settings/metric.py} | 55 +------------------ kiwi_simple_metrics/settings/misc.py | 24 ++++++++ 3 files changed, 62 insertions(+), 53 deletions(-) create mode 100644 kiwi_simple_metrics/settings/__init__.py rename kiwi_simple_metrics/{settings.py => settings/metric.py} (61%) create mode 100644 kiwi_simple_metrics/settings/misc.py 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