Skip to content

Commit 2536697

Browse files
author
michaelyaakoby
authored
Merge pull request #38 from SolarEdgeTech/michael/health_status
Health status summary should support providers in UNKNOWN status Solves #35
2 parents cf01e44 + cd84ea3 commit 2536697

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

pyctuator/impl/pyctuator_impl.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,23 @@ def get_health(self) -> HealthSummary:
102102
for provider in self.health_providers
103103
if provider.is_supported()
104104
}
105-
service_is_up = all(health_status.status == Status.UP for health_status in health_statuses.values())
106-
return HealthSummary(Status.UP if service_is_up else Status.DOWN, health_statuses)
105+
106+
# Health is UP if no provider is registered
107+
if not health_statuses:
108+
return HealthSummary(Status.UP, health_statuses)
109+
110+
# If there's at least one provider and any of the providers is DOWN, the service is DOWN
111+
service_is_down = any(health_status.status == Status.DOWN for health_status in health_statuses.values())
112+
if service_is_down:
113+
return HealthSummary(Status.DOWN, health_statuses)
114+
115+
# IF there's at least one provider and none of the providers is DOWN and at least one is UP, the service is UP
116+
service_is_up = any(health_status.status == Status.UP for health_status in health_statuses.values())
117+
if service_is_up:
118+
return HealthSummary(Status.UP, health_statuses)
119+
120+
# else, all providers are unknown so the service is UNKNOWN
121+
return HealthSummary(Status.UNKNOWN, health_statuses)
107122

108123
def get_metric_names(self) -> MetricNames:
109124
metric_names = []

tests/health/test_health_status.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import pytest
2+
3+
from pyctuator.health.health_provider import HealthStatus, Status, HealthDetails, HealthProvider
4+
from pyctuator.impl.pyctuator_impl import PyctuatorImpl, AppInfo, AppDetails
5+
from pyctuator.pyctuator import default_logfile_format
6+
7+
8+
class MyHealthProvider(HealthProvider):
9+
def __init__(self, name: str = "kuki") -> None:
10+
self.name = name
11+
self.status = Status.UNKNOWN
12+
13+
def down(self) -> None:
14+
self.status = Status.DOWN
15+
16+
def up(self) -> None:
17+
self.status = Status.UP
18+
19+
def is_supported(self) -> bool:
20+
return True
21+
22+
def get_health(self) -> HealthStatus:
23+
return HealthStatus(self.status, HealthDetails())
24+
25+
def get_name(self) -> str:
26+
return self.name
27+
28+
29+
@pytest.fixture
30+
def pyctuator_impl() -> PyctuatorImpl:
31+
return PyctuatorImpl(
32+
AppInfo(app=AppDetails(name="appy")),
33+
"http://appy/pyctuator",
34+
10,
35+
default_logfile_format
36+
)
37+
38+
39+
def test_health_status_single_provider(pyctuator_impl: PyctuatorImpl) -> None:
40+
health_provider = MyHealthProvider()
41+
pyctuator_impl.register_health_providers(health_provider)
42+
43+
# Test's default status is UNKNOWN
44+
assert pyctuator_impl.get_health().status == Status.UNKNOWN
45+
46+
health_provider.down()
47+
assert pyctuator_impl.get_health().status == Status.DOWN
48+
49+
health_provider.up()
50+
assert pyctuator_impl.get_health().status == Status.UP
51+
52+
53+
def test_health_status_multiple_providers(pyctuator_impl: PyctuatorImpl) -> None:
54+
health_providers = [MyHealthProvider("kuki"), MyHealthProvider("puki"), MyHealthProvider("ruki")]
55+
for health_provider in health_providers:
56+
pyctuator_impl.register_health_providers(health_provider)
57+
58+
# Test's default status is UNKNOWN - all 3 are UNKNOWN
59+
assert pyctuator_impl.get_health().status == Status.UNKNOWN
60+
61+
health_providers[0].down()
62+
assert pyctuator_impl.get_health().status == Status.DOWN
63+
64+
health_providers[0].up()
65+
assert pyctuator_impl.get_health().status == Status.UP
66+
67+
# first provider is UP, but the second is DOWN
68+
health_providers[1].down()
69+
assert pyctuator_impl.get_health().status == Status.DOWN
70+
71+
# first and second providers are UP, 3rd is UNKNOWN
72+
health_providers[1].up()
73+
assert pyctuator_impl.get_health().status == Status.UP

0 commit comments

Comments
 (0)