diff --git a/.mypy.ini b/.mypy.ini index a99d55d738..468b055c8b 100644 --- a/.mypy.ini +++ b/.mypy.ini @@ -26,7 +26,8 @@ modules = azul.enums, azul.es, azul.exceptions, - azul.files + azul.files, + azul.health packages = azul.openapi diff --git a/src/azul/health.py b/src/azul/health.py index e256973bd2..3c5313f9da 100644 --- a/src/azul/health.py +++ b/src/azul/health.py @@ -1,7 +1,6 @@ from collections.abc import ( Iterable, Mapping, - Set, ) from itertools import ( chain, @@ -12,7 +11,6 @@ import time from typing import ( ClassVar, - Optional, ) import attr @@ -36,6 +34,7 @@ cache, cached_property, config, + json_bool, lru_cache, require, ) @@ -65,7 +64,6 @@ ) from azul.types import ( JSON, - MutableJSON, ) log = logging.getLogger(__name__) @@ -110,14 +108,13 @@ def basic_health(self) -> Response: def health(self) -> Response: return self._make_response(self._health.as_json(Health.all_keys)) - def custom_health(self, keys: Optional[str]) -> Response: + def custom_health(self, keys: str | None) -> Response: if keys is None: body = self._health.as_json(Health.all_keys) elif isinstance(keys, str): assert keys # Chalice maps empty string to None - keys = keys.split(',') try: - body = self._health.as_json(keys) + body = self._health.as_json(keys.split(',')) except AssertionError as e: if R.caused(e): body = {'Message': 'Invalid health keys'} @@ -187,9 +184,9 @@ def lambda_name(self): return self.controller.lambda_name def as_json(self, keys: Iterable[str]) -> JSON: - keys = set(keys) + keys = frozenset(keys) if keys: - require(keys.issubset(self.all_keys)) + require(keys < self.all_keys) else: keys = self.all_keys json = {k: getattr(self, k) for k in sorted(keys)} @@ -201,13 +198,15 @@ def other_lambdas(self) -> JSON: """ Indicates whether the companion REST API responds to HTTP requests. """ - response: MutableJSON = { + response = { lambda_name: self._lambda(lambda_name) for lambda_name in config.lambda_names() if lambda_name != self.lambda_name } - response['up'] = all(v['up'] for v in response.values()) - return response + return { + 'up': all(json_bool(v['up']) for v in response.values()), + **response + } @health_property def queues(self): @@ -325,7 +324,7 @@ def as_json_fast(self) -> JSON: p for p in locals().values() if isinstance(p, health_property) ) - all_keys: ClassVar[Set[str]] = frozenset(p.key for p in all_properties) + all_keys: ClassVar[frozenset[str]] = frozenset(p.key for p in all_properties) class HealthApp(AzulChaliceApp): @@ -504,7 +503,7 @@ def fast_health(): ] } ) - def custom_health(keys: Optional[str] = None): + def custom_health(keys: str | None = None): return self.health_controller.custom_health(keys) @self.metric_alarm(metric=LambdaMetric.errors,