mx-bluesky 1.5.2__py3-none-any.whl → 1.5.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mx_bluesky/_version.py +16 -3
- mx_bluesky/beamlines/i04/__init__.py +7 -3
- mx_bluesky/beamlines/i04/experiment_plans/i04_grid_detect_then_xray_centre_plan.py +3 -0
- mx_bluesky/beamlines/i24/serial/blueapi_config.yaml +2 -2
- mx_bluesky/common/experiment_plans/oav_grid_detection_plan.py +12 -2
- mx_bluesky/common/external_interaction/alerting/__init__.py +13 -0
- mx_bluesky/common/external_interaction/alerting/_service.py +82 -0
- mx_bluesky/common/external_interaction/alerting/log_based_service.py +57 -0
- mx_bluesky/common/external_interaction/callbacks/sample_handling/sample_handling_callback.py +28 -4
- mx_bluesky/common/external_interaction/config_server.py +151 -54
- mx_bluesky/common/parameters/constants.py +26 -8
- mx_bluesky/common/parameters/gridscan.py +1 -1
- mx_bluesky/hyperion/__main__.py +50 -178
- mx_bluesky/hyperion/baton_handler.py +125 -69
- mx_bluesky/hyperion/device_setup_plans/dcm_pitch_roll_mirror_adjuster.py +29 -24
- mx_bluesky/hyperion/experiment_plans/hyperion_flyscan_xray_centre_plan.py +4 -1
- mx_bluesky/hyperion/experiment_plans/load_centre_collect_full_plan.py +12 -4
- mx_bluesky/hyperion/experiment_plans/pin_tip_centring_plan.py +1 -1
- mx_bluesky/hyperion/experiment_plans/rotation_scan_plan.py +2 -3
- mx_bluesky/hyperion/external_interaction/agamemnon.py +128 -73
- mx_bluesky/hyperion/external_interaction/alerting/__init__.py +0 -0
- mx_bluesky/hyperion/external_interaction/alerting/constants.py +12 -0
- mx_bluesky/hyperion/external_interaction/callbacks/__main__.py +5 -0
- mx_bluesky/hyperion/external_interaction/callbacks/rotation/nexus_callback.py +2 -2
- mx_bluesky/hyperion/external_interaction/config_server.py +12 -31
- mx_bluesky/hyperion/parameters/cli.py +15 -3
- mx_bluesky/hyperion/parameters/components.py +7 -5
- mx_bluesky/hyperion/parameters/constants.py +20 -4
- mx_bluesky/hyperion/parameters/gridscan.py +22 -14
- mx_bluesky/hyperion/parameters/load_centre_collect.py +1 -14
- mx_bluesky/hyperion/parameters/robot_load.py +1 -4
- mx_bluesky/hyperion/parameters/rotation.py +1 -2
- mx_bluesky/hyperion/plan_runner.py +78 -0
- mx_bluesky/hyperion/runner.py +189 -0
- {mx_bluesky-1.5.2.dist-info → mx_bluesky-1.5.3.dist-info}/METADATA +4 -3
- {mx_bluesky-1.5.2.dist-info → mx_bluesky-1.5.3.dist-info}/RECORD +40 -33
- {mx_bluesky-1.5.2.dist-info → mx_bluesky-1.5.3.dist-info}/entry_points.txt +0 -2
- {mx_bluesky-1.5.2.dist-info → mx_bluesky-1.5.3.dist-info}/WHEEL +0 -0
- {mx_bluesky-1.5.2.dist-info → mx_bluesky-1.5.3.dist-info}/licenses/LICENSE +0 -0
- {mx_bluesky-1.5.2.dist-info → mx_bluesky-1.5.3.dist-info}/top_level.txt +0 -0
mx_bluesky/_version.py
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
# file generated by setuptools-scm
|
|
2
2
|
# don't change, don't track in version control
|
|
3
3
|
|
|
4
|
-
__all__ = [
|
|
4
|
+
__all__ = [
|
|
5
|
+
"__version__",
|
|
6
|
+
"__version_tuple__",
|
|
7
|
+
"version",
|
|
8
|
+
"version_tuple",
|
|
9
|
+
"__commit_id__",
|
|
10
|
+
"commit_id",
|
|
11
|
+
]
|
|
5
12
|
|
|
6
13
|
TYPE_CHECKING = False
|
|
7
14
|
if TYPE_CHECKING:
|
|
@@ -9,13 +16,19 @@ if TYPE_CHECKING:
|
|
|
9
16
|
from typing import Union
|
|
10
17
|
|
|
11
18
|
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
|
19
|
+
COMMIT_ID = Union[str, None]
|
|
12
20
|
else:
|
|
13
21
|
VERSION_TUPLE = object
|
|
22
|
+
COMMIT_ID = object
|
|
14
23
|
|
|
15
24
|
version: str
|
|
16
25
|
__version__: str
|
|
17
26
|
__version_tuple__: VERSION_TUPLE
|
|
18
27
|
version_tuple: VERSION_TUPLE
|
|
28
|
+
commit_id: COMMIT_ID
|
|
29
|
+
__commit_id__: COMMIT_ID
|
|
19
30
|
|
|
20
|
-
__version__ = version = '1.5.
|
|
21
|
-
__version_tuple__ = version_tuple = (1, 5,
|
|
31
|
+
__version__ = version = '1.5.3'
|
|
32
|
+
__version_tuple__ = version_tuple = (1, 5, 3)
|
|
33
|
+
|
|
34
|
+
__commit_id__ = commit_id = None
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
from mx_bluesky.beamlines.i04.experiment_plans import (
|
|
2
|
-
|
|
1
|
+
from mx_bluesky.beamlines.i04.experiment_plans.i04_grid_detect_then_xray_centre_plan import (
|
|
2
|
+
i04_grid_detect_then_xray_centre,
|
|
3
3
|
)
|
|
4
4
|
from mx_bluesky.beamlines.i04.thawing_plan import thaw, thaw_and_stream_to_redis
|
|
5
5
|
|
|
6
|
-
__all__ = [
|
|
6
|
+
__all__ = [
|
|
7
|
+
"thaw",
|
|
8
|
+
"thaw_and_stream_to_redis",
|
|
9
|
+
"i04_grid_detect_then_xray_centre",
|
|
10
|
+
]
|
|
@@ -67,6 +67,7 @@ from mx_bluesky.common.preprocessors.preprocessors import (
|
|
|
67
67
|
transmission_and_xbpm_feedback_for_collection_decorator,
|
|
68
68
|
)
|
|
69
69
|
from mx_bluesky.common.utils.context import device_composite_from_context
|
|
70
|
+
from mx_bluesky.common.utils.log import LOGGER
|
|
70
71
|
from mx_bluesky.phase1_zebra.device_setup_plans.setup_zebra import (
|
|
71
72
|
setup_zebra_for_gridscan,
|
|
72
73
|
tidy_up_zebra_after_gridscan,
|
|
@@ -184,9 +185,11 @@ def get_ready_for_oav_and_close_shutter(
|
|
|
184
185
|
):
|
|
185
186
|
yield from bps.wait(PlanGroupCheckpointConstants.GRID_READY_FOR_DC)
|
|
186
187
|
group = "get_ready_for_oav_and_close_shutter"
|
|
188
|
+
LOGGER.info("Non-udc tidy: Seting up beamline for OAV")
|
|
187
189
|
yield from setup_beamline_for_OAV(
|
|
188
190
|
smargon, backlight, aperture_scatterguard, group=group
|
|
189
191
|
)
|
|
192
|
+
LOGGER.info("Non-udc tidy: Closing detector shutter")
|
|
190
193
|
yield from bps.abs_set(
|
|
191
194
|
detector_motion.shutter,
|
|
192
195
|
0,
|
|
@@ -7,11 +7,11 @@ env:
|
|
|
7
7
|
events:
|
|
8
8
|
broadcast_status_events: false
|
|
9
9
|
api:
|
|
10
|
-
|
|
10
|
+
url: http://localhost:25565
|
|
11
11
|
cors:
|
|
12
12
|
allow_credentials: True
|
|
13
13
|
origins:
|
|
14
14
|
- "*"
|
|
15
15
|
stomp:
|
|
16
16
|
enabled: true
|
|
17
|
-
|
|
17
|
+
url: http://i24-control.diamond.ac.uk:61613
|
|
@@ -6,6 +6,7 @@ from typing import TYPE_CHECKING
|
|
|
6
6
|
import bluesky.plan_stubs as bps
|
|
7
7
|
import numpy as np
|
|
8
8
|
from blueapi.core import BlueskyContext
|
|
9
|
+
from bluesky.utils import MsgGenerator
|
|
9
10
|
from dodal.devices.oav.oav_detector import OAV
|
|
10
11
|
from dodal.devices.oav.pin_image_recognition import PinTipDetection
|
|
11
12
|
from dodal.devices.oav.pin_image_recognition.utils import NONE_VALUE
|
|
@@ -49,6 +50,16 @@ def get_min_and_max_y_of_pin(
|
|
|
49
50
|
return min_y, max_y
|
|
50
51
|
|
|
51
52
|
|
|
53
|
+
def optimum_grid_detect_angles(smargon: Smargon) -> MsgGenerator[list[float]]:
|
|
54
|
+
"""We need to match the 0 and -90 that the fast grid scan performs but the order in
|
|
55
|
+
which we do the grid detection does not matter so we do the closest angle first."""
|
|
56
|
+
current_omega = yield from bps.rd(smargon.omega)
|
|
57
|
+
if current_omega < -45:
|
|
58
|
+
return [-90, 0]
|
|
59
|
+
else:
|
|
60
|
+
return [0, -90]
|
|
61
|
+
|
|
62
|
+
|
|
52
63
|
def grid_detection_plan(
|
|
53
64
|
composite: OavGridDetectionComposite,
|
|
54
65
|
parameters: OAVParameters,
|
|
@@ -90,8 +101,7 @@ def grid_detection_plan(
|
|
|
90
101
|
|
|
91
102
|
grid_width_pixels = int(grid_width_microns / microns_per_pixel_x)
|
|
92
103
|
|
|
93
|
-
|
|
94
|
-
for angle in [0, -90]:
|
|
104
|
+
for angle in (yield from optimum_grid_detect_angles(smargon)):
|
|
95
105
|
yield from bps.mv(smargon.omega, angle)
|
|
96
106
|
# need to wait for the OAV image to update
|
|
97
107
|
# See #673 for improvements
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from mx_bluesky.common.external_interaction.alerting._service import (
|
|
2
|
+
AlertService,
|
|
3
|
+
Metadata,
|
|
4
|
+
get_alerting_service,
|
|
5
|
+
set_alerting_service,
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"AlertService",
|
|
10
|
+
"Metadata",
|
|
11
|
+
"get_alerting_service",
|
|
12
|
+
"set_alerting_service",
|
|
13
|
+
]
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
from datetime import UTC, datetime, timedelta
|
|
2
|
+
from enum import StrEnum
|
|
3
|
+
from typing import Protocol
|
|
4
|
+
from urllib.parse import quote, urlencode
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Metadata(StrEnum):
|
|
8
|
+
"""Metadata fields that can be specified by the caller when raising an alert."""
|
|
9
|
+
|
|
10
|
+
CONTAINER = "container"
|
|
11
|
+
SAMPLE_ID = "sample_id"
|
|
12
|
+
VISIT = "visit"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ExtraMetadata(StrEnum):
|
|
16
|
+
"""Additional metadata fields that are automatically appended by
|
|
17
|
+
the AlertService implementations."""
|
|
18
|
+
|
|
19
|
+
ALERT_CONTENT = "alert_content"
|
|
20
|
+
ALERT_SUMMARY = "alert_summary"
|
|
21
|
+
BEAMLINE = "beamline"
|
|
22
|
+
GRAYLOG_URL = "graylog_url"
|
|
23
|
+
ISPYB_URL = "ispyb_url"
|
|
24
|
+
PROPOSAL = "proposal"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class AlertService(Protocol):
|
|
28
|
+
"""
|
|
29
|
+
Implemented by any backend that provides the ability to dispatch alerts to some
|
|
30
|
+
service that is capable of disseminating them via any of a variety of media such
|
|
31
|
+
as email, SMS, instant messaging, etc etc.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def raise_alert(self, summary: str, content: str, metadata: dict[Metadata, str]):
|
|
35
|
+
"""
|
|
36
|
+
Raise an alert that will be forwarded to beamline support staff, which might
|
|
37
|
+
for example be used as the basis for an incident in an incident reporting system.
|
|
38
|
+
Args:
|
|
39
|
+
summary: One line summary of the alert, that might for instance be used
|
|
40
|
+
in an email subject line.
|
|
41
|
+
content: Plain text content detailing the nature of the incident.
|
|
42
|
+
metadata: A dict of strings that can be included as metadata in the alert for
|
|
43
|
+
those backends that support it. The summary and content will be included
|
|
44
|
+
by default.
|
|
45
|
+
"""
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
_alert_service: AlertService
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def get_alerting_service() -> AlertService:
|
|
53
|
+
"""Get the alert service for this instance."""
|
|
54
|
+
return _alert_service
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def set_alerting_service(service: AlertService):
|
|
58
|
+
"""Set the alert service for this instance, call when the beamline is initialised."""
|
|
59
|
+
global _alert_service
|
|
60
|
+
_alert_service = service
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def ispyb_url(sample_id: str):
|
|
64
|
+
return f"https://ispyb.diamond.ac.uk/samples/sid/{quote(sample_id)}"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def graylog_url(stream_id: str):
|
|
68
|
+
now = datetime.now(UTC)
|
|
69
|
+
from_utc = now - timedelta(minutes=5)
|
|
70
|
+
from_timestamp = from_utc.isoformat()
|
|
71
|
+
# Add 1 second for graylog timing jitter
|
|
72
|
+
to_utc = now + timedelta(seconds=1)
|
|
73
|
+
to_timestamp = to_utc.isoformat()
|
|
74
|
+
query_string = urlencode(
|
|
75
|
+
{
|
|
76
|
+
"streams": stream_id,
|
|
77
|
+
"rangetype": "absolute",
|
|
78
|
+
"from": from_timestamp,
|
|
79
|
+
"to": to_timestamp,
|
|
80
|
+
}
|
|
81
|
+
)
|
|
82
|
+
return "https://graylog.diamond.ac.uk/search?" + query_string
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import cast
|
|
3
|
+
|
|
4
|
+
from dodal.log import LOGGER
|
|
5
|
+
from dodal.utils import get_beamline_name
|
|
6
|
+
|
|
7
|
+
from mx_bluesky.common.external_interaction.alerting import Metadata
|
|
8
|
+
from mx_bluesky.common.external_interaction.alerting._service import (
|
|
9
|
+
ExtraMetadata,
|
|
10
|
+
graylog_url,
|
|
11
|
+
ispyb_url,
|
|
12
|
+
)
|
|
13
|
+
from mx_bluesky.common.external_interaction.callbacks.common.ispyb_mapping import (
|
|
14
|
+
get_proposal_and_session_from_visit_string,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class LoggingAlertService:
|
|
19
|
+
"""
|
|
20
|
+
Implement an alert service that raises alerts by generating a specially formatted
|
|
21
|
+
log message, that may be intercepted by a logging service such as graylog and
|
|
22
|
+
used to dispatch the alert.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, graylog_stream_id: str, level=logging.WARNING):
|
|
26
|
+
"""
|
|
27
|
+
Create a new instance of the service
|
|
28
|
+
Args:
|
|
29
|
+
level: The python logging level at which to generate the message
|
|
30
|
+
"""
|
|
31
|
+
super().__init__()
|
|
32
|
+
self._level = level
|
|
33
|
+
self._graylog_stream_id = graylog_stream_id
|
|
34
|
+
|
|
35
|
+
def _append_extra_metadata(self, metadata: dict[Metadata, str]) -> dict[str, str]:
|
|
36
|
+
with_extras = cast(dict, metadata.copy())
|
|
37
|
+
with_extras[ExtraMetadata.GRAYLOG_URL] = graylog_url(self._graylog_stream_id)
|
|
38
|
+
with_extras[ExtraMetadata.BEAMLINE] = get_beamline_name("")
|
|
39
|
+
if sample_id := metadata.get(Metadata.SAMPLE_ID, None):
|
|
40
|
+
with_extras[ExtraMetadata.ISPYB_URL] = ispyb_url(sample_id)
|
|
41
|
+
if visit := metadata.get(Metadata.VISIT, None):
|
|
42
|
+
proposal, _ = get_proposal_and_session_from_visit_string(visit)
|
|
43
|
+
with_extras[ExtraMetadata.PROPOSAL] = proposal
|
|
44
|
+
return with_extras
|
|
45
|
+
|
|
46
|
+
def raise_alert(self, summary: str, content: str, metadata: dict[Metadata, str]):
|
|
47
|
+
message = f"***ALERT*** summary={summary} content={content}"
|
|
48
|
+
with_extras = self._append_extra_metadata(metadata)
|
|
49
|
+
LOGGER.log(
|
|
50
|
+
self._level,
|
|
51
|
+
message,
|
|
52
|
+
extra={
|
|
53
|
+
ExtraMetadata.ALERT_SUMMARY: summary,
|
|
54
|
+
ExtraMetadata.ALERT_CONTENT: content,
|
|
55
|
+
}
|
|
56
|
+
| with_extras,
|
|
57
|
+
)
|
mx_bluesky/common/external_interaction/callbacks/sample_handling/sample_handling_callback.py
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
from dodal.utils import get_beamline_name
|
|
1
2
|
from event_model import RunStart, RunStop
|
|
2
3
|
|
|
4
|
+
from mx_bluesky.common.external_interaction.alerting import (
|
|
5
|
+
Metadata,
|
|
6
|
+
get_alerting_service,
|
|
7
|
+
)
|
|
3
8
|
from mx_bluesky.common.external_interaction.callbacks.common.plan_reactive_callback import (
|
|
4
9
|
PlanReactiveCallback,
|
|
5
10
|
)
|
|
@@ -18,30 +23,36 @@ class SampleHandlingCallback(PlanReactiveCallback):
|
|
|
18
23
|
def __init__(self, record_loaded_on_success=False):
|
|
19
24
|
super().__init__(log=ISPYB_ZOCALO_CALLBACK_LOGGER)
|
|
20
25
|
self._sample_id: int | None = None
|
|
26
|
+
self._visit: str | None = None
|
|
21
27
|
self._descriptor: str | None = None
|
|
22
28
|
self._run_id: str | None = None
|
|
29
|
+
self._container: int | None = None
|
|
23
30
|
|
|
24
31
|
# Record 'sample loaded' if document successfully stops
|
|
25
32
|
self.record_loaded_on_success = record_loaded_on_success
|
|
26
33
|
|
|
27
34
|
def activity_gated_start(self, doc: RunStart):
|
|
28
35
|
if not self._sample_id and self.active:
|
|
29
|
-
|
|
36
|
+
metadata = doc.get("metadata", {})
|
|
37
|
+
sample_id = metadata.get("sample_id")
|
|
30
38
|
self.log.info(f"Recording sample ID at run start {sample_id}")
|
|
31
39
|
self._sample_id = sample_id
|
|
40
|
+
self._visit = metadata.get("visit")
|
|
32
41
|
self._run_id = self.activity_uid
|
|
42
|
+
self._container = metadata.get("container")
|
|
33
43
|
|
|
34
44
|
def activity_gated_stop(self, doc: RunStop) -> RunStop:
|
|
35
45
|
if self._run_id == doc.get("run_start"):
|
|
36
46
|
expeye = ExpeyeInteraction()
|
|
37
47
|
if doc["exit_status"] != "success":
|
|
48
|
+
reason = doc.get("reason", "")
|
|
38
49
|
exception_type, message = SampleException.type_and_message_from_reason(
|
|
39
|
-
|
|
50
|
+
reason
|
|
40
51
|
)
|
|
41
52
|
self.log.info(
|
|
42
53
|
f"Sample handling callback intercepted exception of type {exception_type}: {message}"
|
|
43
54
|
)
|
|
44
|
-
self._record_exception(exception_type, expeye)
|
|
55
|
+
self._record_exception(exception_type, expeye, reason)
|
|
45
56
|
|
|
46
57
|
elif self.record_loaded_on_success:
|
|
47
58
|
self._record_loaded(expeye)
|
|
@@ -51,10 +62,23 @@ class SampleHandlingCallback(PlanReactiveCallback):
|
|
|
51
62
|
|
|
52
63
|
return doc
|
|
53
64
|
|
|
54
|
-
def _record_exception(
|
|
65
|
+
def _record_exception(
|
|
66
|
+
self, exception_type: str, expeye: ExpeyeInteraction, reason: str
|
|
67
|
+
):
|
|
55
68
|
assert self._sample_id, "Unable to record exception due to no sample ID"
|
|
56
69
|
sample_status = self._decode_sample_status(exception_type)
|
|
57
70
|
expeye.update_sample_status(self._sample_id, sample_status)
|
|
71
|
+
if sample_status == BLSampleStatus.ERROR_BEAMLINE:
|
|
72
|
+
beamline = get_beamline_name("")
|
|
73
|
+
get_alerting_service().raise_alert(
|
|
74
|
+
f"UDC encountered an error on {beamline}",
|
|
75
|
+
f"Hyperion encountered the following beamline error: {reason}",
|
|
76
|
+
{
|
|
77
|
+
Metadata.SAMPLE_ID: str(self._sample_id),
|
|
78
|
+
Metadata.VISIT: self._visit or "",
|
|
79
|
+
Metadata.CONTAINER: str(self._container),
|
|
80
|
+
},
|
|
81
|
+
)
|
|
58
82
|
|
|
59
83
|
def _decode_sample_status(self, exception_type: str) -> BLSampleStatus:
|
|
60
84
|
match exception_type:
|
|
@@ -1,57 +1,154 @@
|
|
|
1
|
-
|
|
2
|
-
from
|
|
1
|
+
import json
|
|
2
|
+
from dataclasses import fields
|
|
3
|
+
from enum import Enum
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Any, Generic, TypeVar
|
|
3
6
|
|
|
4
7
|
from daq_config_server.client import ConfigServer
|
|
5
|
-
from pydantic import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
8
|
+
from pydantic import TypeAdapter
|
|
9
|
+
|
|
10
|
+
from mx_bluesky.common.parameters.constants import (
|
|
11
|
+
GDA_DOMAIN_PROPERTIES_PATH,
|
|
12
|
+
FeatureSetting,
|
|
13
|
+
FeatureSettingources,
|
|
14
|
+
OavConstants,
|
|
15
|
+
)
|
|
16
|
+
from mx_bluesky.common.utils.log import LOGGER
|
|
17
|
+
|
|
18
|
+
FEATURE_FLAG_CACHE_LENGTH_S = 60 * 5
|
|
19
|
+
# Used by the config server when refreshing its cache
|
|
20
|
+
_JSON_CONFIG_PATHS = [OavConstants.OAV_CONFIG_JSON]
|
|
21
|
+
|
|
22
|
+
T = TypeVar("T", bound=FeatureSetting)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class MXConfigClient(ConfigServer, Generic[T]):
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
feature_sources: type[FeatureSettingources],
|
|
29
|
+
feature_dc: type[T],
|
|
30
|
+
url: str = "https://daq-config.diamond.ac.uk",
|
|
31
|
+
):
|
|
32
|
+
"""MX implementation of the config server client. Makes requests to the config server to retrieve config while falling back to
|
|
33
|
+
the filesystem in the case that the request failed.
|
|
34
|
+
|
|
35
|
+
See mx_bluesky/hyperion/external_interaction/config_server.py for example implementation.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
feature_sources: A StrEnum containing available features, where the string is the name of that feature toggle in a beamline's GDA
|
|
39
|
+
domain.properties.
|
|
40
|
+
|
|
41
|
+
feature_dc: A dataclass containing available features along with their default flags. This dataclass must contain the same keys
|
|
42
|
+
as the feature_sources parameter. These defaults are used when a server request fails.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
self.feature_sources = feature_sources
|
|
46
|
+
self.feature_dc: type[T] = feature_dc
|
|
47
|
+
self._cached_features: T | None = None
|
|
48
|
+
self._cached_json_config: dict[str, dict[str, Any]] = {}
|
|
49
|
+
self._time_of_last_feature_get: float = 0
|
|
50
|
+
self._verify_feature_parameters()
|
|
51
|
+
super().__init__(url)
|
|
52
|
+
|
|
53
|
+
def get_feature_flags(self) -> T:
|
|
54
|
+
"""Get feature flags by making a request to the config server. If the request fails, use the hardcoded defaults. Store results in a cache
|
|
55
|
+
which should be updated at the start of a plan using self.refresh_cache()
|
|
56
|
+
"""
|
|
57
|
+
return self._get_feature_flags()
|
|
58
|
+
|
|
59
|
+
def get_json_config(self, path_to_json: Path | str) -> dict[str, Any]:
|
|
60
|
+
"""Get the OAV config in the form of a python dictionary. Store results in a cache
|
|
61
|
+
which should be updated at the start of a plan using self.refresh_cache().
|
|
62
|
+
|
|
63
|
+
Note that this method replicates the functionality of the config server, but provides
|
|
64
|
+
a fallback incase of server. It will be removed once the config server has been run reliably
|
|
65
|
+
for several months.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
path_to_json: Absolute file path to json file
|
|
69
|
+
"""
|
|
70
|
+
return self._get_json_config(path_to_json)
|
|
71
|
+
|
|
72
|
+
def refresh_cache(self):
|
|
73
|
+
"""Refresh the client's cache. Use at the beginning of a plan"""
|
|
74
|
+
self._get_feature_flags(reset_cached_result=True)
|
|
75
|
+
for path in _JSON_CONFIG_PATHS:
|
|
76
|
+
self._get_json_config(path, reset_cached_result=True)
|
|
77
|
+
|
|
78
|
+
def _verify_feature_parameters(self):
|
|
79
|
+
sources_keys = {feature.name for feature in self.feature_sources}
|
|
80
|
+
feature_dc_keys = {key.name for key in fields(self.feature_dc)}
|
|
81
|
+
assert sources_keys == feature_dc_keys, (
|
|
82
|
+
f"MXConfig server feature_sources names do not match feature_dc keys: {sources_keys} != {feature_dc_keys}"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
def _get_feature_flags(self, reset_cached_result=False) -> T:
|
|
86
|
+
"""
|
|
87
|
+
Args:
|
|
88
|
+
reset_cached_result (bool): Force refresh the cache for this request
|
|
89
|
+
"""
|
|
90
|
+
try:
|
|
91
|
+
if reset_cached_result:
|
|
92
|
+
self._cached_features = None
|
|
93
|
+
if not self._cached_features:
|
|
94
|
+
self._cached_features = None
|
|
95
|
+
# Construct self.feature_dc by reading the domain.properties file
|
|
96
|
+
all_features = list(self.feature_sources)
|
|
97
|
+
feature_dict = {}
|
|
98
|
+
domain_properties = self.get_file_contents(
|
|
99
|
+
GDA_DOMAIN_PROPERTIES_PATH, reset_cached_result=reset_cached_result
|
|
100
|
+
).splitlines()
|
|
101
|
+
for line in domain_properties:
|
|
102
|
+
line = line.strip()
|
|
103
|
+
if not line or line.startswith("#"):
|
|
104
|
+
continue
|
|
105
|
+
line = line.split("#", 1)[0].strip() # Remove inline comments
|
|
106
|
+
if "=" in line:
|
|
107
|
+
key, value = map(str.strip, line.split("=", 1))
|
|
108
|
+
for feature in all_features:
|
|
109
|
+
assert isinstance(feature, Enum)
|
|
110
|
+
if key == feature.value:
|
|
111
|
+
feature_dict[feature.name] = value
|
|
112
|
+
self._check_missing_fields(
|
|
113
|
+
{f.name for f in fields(self.feature_dc)}, set(feature_dict.keys())
|
|
114
|
+
)
|
|
115
|
+
self._cached_features = self.feature_dc(**feature_dict)
|
|
116
|
+
return self._cached_features
|
|
117
|
+
except Exception as e:
|
|
118
|
+
LOGGER.warning(
|
|
119
|
+
f"Failed to get feature flags from config server: {e} \nUsing defaults..."
|
|
120
|
+
)
|
|
121
|
+
return self.feature_dc()
|
|
122
|
+
|
|
123
|
+
def _check_missing_fields(self, expected: set, actual: set):
|
|
124
|
+
missing = expected - actual
|
|
125
|
+
if missing:
|
|
126
|
+
LOGGER.warning(
|
|
127
|
+
f"Missing features from domain.properties: {missing}.\n Using defaults for missing features"
|
|
56
128
|
)
|
|
57
|
-
|
|
129
|
+
|
|
130
|
+
def _get_json_config(
|
|
131
|
+
self, path_to_json: Path | str, reset_cached_result=False
|
|
132
|
+
) -> dict[str, Any]:
|
|
133
|
+
"""
|
|
134
|
+
Args:
|
|
135
|
+
reset_cached_result (bool): Force refresh the cache for this request
|
|
136
|
+
"""
|
|
137
|
+
if reset_cached_result:
|
|
138
|
+
self._cached_json_config = {}
|
|
139
|
+
str_to_json = str(path_to_json)
|
|
140
|
+
if str_to_json not in self._cached_json_config:
|
|
141
|
+
try:
|
|
142
|
+
self._cached_json_config[str_to_json] = self.get_file_contents(
|
|
143
|
+
path_to_json, dict, reset_cached_result=reset_cached_result
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
except Exception as e:
|
|
147
|
+
LOGGER.warning(
|
|
148
|
+
f"Failed to get json config from config server: {e} \nReading the file directly..."
|
|
149
|
+
)
|
|
150
|
+
with open(path_to_json) as f:
|
|
151
|
+
self._cached_json_config[str_to_json] = TypeAdapter(
|
|
152
|
+
dict[str, Any]
|
|
153
|
+
).validate_python(json.loads(f.read()))
|
|
154
|
+
return self._cached_json_config[str_to_json]
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import os
|
|
2
|
-
from enum import Enum
|
|
2
|
+
from enum import Enum, StrEnum
|
|
3
3
|
|
|
4
4
|
from dodal.devices.aperturescatterguard import ApertureValue
|
|
5
5
|
from dodal.devices.detector import EIGER2_X_16M_SIZE
|
|
@@ -13,6 +13,12 @@ BEAMLINE = get_beamline_name("test")
|
|
|
13
13
|
TEST_MODE = BEAMLINE == "test"
|
|
14
14
|
ZEBRA_STATUS_TIMEOUT = 30
|
|
15
15
|
|
|
16
|
+
GDA_DOMAIN_PROPERTIES_PATH = (
|
|
17
|
+
"tests/test_data/test_domain_properties"
|
|
18
|
+
if TEST_MODE
|
|
19
|
+
else (f"/dls_sw/{BEAMLINE}/software/daq_configuration/domain/domain.properties")
|
|
20
|
+
)
|
|
21
|
+
|
|
16
22
|
|
|
17
23
|
@dataclass(frozen=True)
|
|
18
24
|
class DocDescriptorNames:
|
|
@@ -28,15 +34,18 @@ class DocDescriptorNames:
|
|
|
28
34
|
FLYSCAN_RESULTS = "flyscan_results_obtained"
|
|
29
35
|
|
|
30
36
|
|
|
37
|
+
def _get_oav_config_json_path():
|
|
38
|
+
if TEST_MODE:
|
|
39
|
+
return "tests/test_data/test_OAVCentring.json"
|
|
40
|
+
elif BEAMLINE == "i03":
|
|
41
|
+
return f"/dls_sw/{BEAMLINE}/software/daq_configuration/json/OAVCentring_hyperion.json"
|
|
42
|
+
else:
|
|
43
|
+
return f"/dls_sw/{BEAMLINE}/software/daq_configuration/json/OAVCentring.json"
|
|
44
|
+
|
|
45
|
+
|
|
31
46
|
@dataclass(frozen=True)
|
|
32
47
|
class OavConstants:
|
|
33
|
-
OAV_CONFIG_JSON = (
|
|
34
|
-
"tests/test_data/test_OAVCentring.json"
|
|
35
|
-
if TEST_MODE
|
|
36
|
-
else (
|
|
37
|
-
f"/dls_sw/{BEAMLINE}/software/daq_configuration/json/OAVCentring_hyperion.json"
|
|
38
|
-
)
|
|
39
|
-
)
|
|
48
|
+
OAV_CONFIG_JSON = _get_oav_config_json_path()
|
|
40
49
|
|
|
41
50
|
|
|
42
51
|
@dataclass(frozen=True)
|
|
@@ -150,3 +159,12 @@ class Status(Enum):
|
|
|
150
159
|
BUSY = "Busy"
|
|
151
160
|
ABORTING = "Aborting"
|
|
152
161
|
IDLE = "Idle"
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
@dataclass
|
|
165
|
+
class FeatureSetting: ... # List of features and their default values. Subclasses must also be a pydantic dataclass
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class FeatureSettingources(
|
|
169
|
+
StrEnum
|
|
170
|
+
): ... # List of features and the name of that property in domain.properties
|
|
@@ -102,7 +102,7 @@ class SpecifiedThreeDGridScan(
|
|
|
102
102
|
"""Parameters representing a so-called 3D grid scan, which consists of doing a
|
|
103
103
|
gridscan in X and Y, followed by one in X and Z."""
|
|
104
104
|
|
|
105
|
-
grid1_omega_deg: float = Field(default=GridscanParamConstants.OMEGA_1)
|
|
105
|
+
grid1_omega_deg: float = Field(default=GridscanParamConstants.OMEGA_1)
|
|
106
106
|
grid2_omega_deg: float = Field(default=GridscanParamConstants.OMEGA_2)
|
|
107
107
|
x_step_size_um: float = Field(default=GridscanParamConstants.BOX_WIDTH_UM)
|
|
108
108
|
y_step_size_um: float = Field(default=GridscanParamConstants.BOX_WIDTH_UM)
|