This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get feature flags from config service (#1444)
* Add FeatureFlags class for params * Makes use of the daq-config-server to fetch data for it
- Loading branch information
Showing
14 changed files
with
117 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import TypeVar | ||
|
||
from daq_config_server.client import ConfigServer | ||
from pydantic import BaseModel | ||
|
||
from hyperion.log import LOGGER | ||
from hyperion.parameters.constants import CONST | ||
|
||
_CONFIG_SERVER: ConfigServer | None = None | ||
T = TypeVar("T") | ||
|
||
|
||
def config_server() -> ConfigServer: | ||
global _CONFIG_SERVER | ||
if _CONFIG_SERVER is None: | ||
_CONFIG_SERVER = ConfigServer(CONST.CONFIG_SERVER_URL, LOGGER) | ||
return _CONFIG_SERVER | ||
|
||
|
||
class FeatureFlags(BaseModel): | ||
# The default value will be used as the fallback when doing a best-effort fetch | ||
# from the service | ||
use_panda_for_gridscan: bool = False | ||
use_gpu_for_gridscan: bool = False | ||
set_stub_offsets: bool = False | ||
|
||
@classmethod | ||
def _get_flags(cls): | ||
flags = config_server().best_effort_get_all_feature_flags() | ||
return {f: flags[f] for f in flags if f in cls.__fields__.keys()} | ||
|
||
@classmethod | ||
def best_effort(cls): | ||
return cls(**cls._get_flags()) | ||
|
||
def update_self_from_server(self): | ||
for flag, value in self._get_flags().items(): | ||
setattr(self, flag, value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
tests/system_tests/external_interaction/test_config_service.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from unittest.mock import MagicMock | ||
from uuid import uuid4 | ||
|
||
import numpy as np | ||
import pytest | ||
from daq_config_server.client import ConfigServer | ||
|
||
from hyperion.external_interaction.config_server import config_server | ||
|
||
|
||
@pytest.fixture | ||
def config_service(): | ||
return config_server() | ||
|
||
|
||
@pytest.mark.s03 | ||
def test_get_beamline_params(config_service: ConfigServer): | ||
resp = config_service.get_beamline_param("miniap_x_SMALL_APERTURE") | ||
assert isinstance(resp, float) | ||
assert np.isclose(resp, 2.43) | ||
|
||
|
||
@pytest.mark.s03 | ||
def test_get_feature_flag(config_service: ConfigServer): | ||
resp = config_service.get_feature_flag("set_stub_offsets") | ||
assert isinstance(resp, bool) | ||
|
||
|
||
@pytest.mark.s03 | ||
def test_get_feature_flags(config_service: ConfigServer): | ||
features = config_service.best_effort_get_all_feature_flags() | ||
assert len(features.keys()) == 3 | ||
|
||
|
||
@pytest.mark.s03 | ||
def test_nonsense_feature_flag_fails_with_normal_call(config_service: ConfigServer): | ||
with pytest.raises(AssertionError): | ||
_ = config_service.get_feature_flag(str(uuid4())) | ||
|
||
|
||
@pytest.mark.s03 | ||
def test_best_effort_gracefully_fails_with_nonsense(config_service: ConfigServer): | ||
resp = config_service.best_effort_get_feature_flag(str(uuid4())) | ||
assert resp is None | ||
|
||
|
||
@pytest.mark.s03 | ||
def test_best_effort_gracefully_fails_if_service_down(config_service: ConfigServer): | ||
log_mock = MagicMock() | ||
config_service = ConfigServer("http://not_real_address", log_mock) | ||
resp = config_service.best_effort_get_feature_flag("set_stub_offsets") | ||
assert resp is None | ||
log_mock.error.assert_called_with( | ||
"Encountered an error reading from the config service.", exc_info=True | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters