add current sample widget
CI for mx_bec / unit tests with pxi_bec (pull_request) Failing after 1m53s
CI for mx_bec / unit tests with pxiii_bec (pull_request) Failing after 1m55s
CI for mx_bec / e2e tests with pxi_bec (pull_request) Successful in 2m0s
CI for mx_bec / unit tests with pxii_bec (pull_request) Failing after 2m0s
CI for mx_bec / e2e tests with pxiii_bec (pull_request) Successful in 2m1s
CI for mx_bec / e2e tests with pxii_bec (pull_request) Successful in 2m5s
CI for mx_bec / unit tests with pxi_bec (push) Failing after 1m47s
CI for mx_bec / e2e tests with pxi_bec (push) Successful in 1m48s
CI for mx_bec / e2e tests with pxii_bec (push) Successful in 1m53s
CI for mx_bec / unit tests with pxii_bec (push) Successful in 1m38s
CI for mx_bec / unit tests with pxiii_bec (push) Failing after 1m37s
CI for mx_bec / e2e tests with pxiii_bec (push) Successful in 1m45s
CI for mx_bec / unit tests with pxi_bec (pull_request) Failing after 1m53s
CI for mx_bec / unit tests with pxiii_bec (pull_request) Failing after 1m55s
CI for mx_bec / e2e tests with pxi_bec (pull_request) Successful in 2m0s
CI for mx_bec / unit tests with pxii_bec (pull_request) Failing after 2m0s
CI for mx_bec / e2e tests with pxiii_bec (pull_request) Successful in 2m1s
CI for mx_bec / e2e tests with pxii_bec (pull_request) Successful in 2m5s
CI for mx_bec / unit tests with pxi_bec (push) Failing after 1m47s
CI for mx_bec / e2e tests with pxi_bec (push) Successful in 1m48s
CI for mx_bec / e2e tests with pxii_bec (push) Successful in 1m53s
CI for mx_bec / unit tests with pxii_bec (push) Successful in 1m38s
CI for mx_bec / unit tests with pxiii_bec (push) Failing after 1m37s
CI for mx_bec / e2e tests with pxiii_bec (push) Successful in 1m45s
This commit was merged in pull request #9.
This commit is contained in:
1 parent
403f3da8f3
commit
66c66bedb7
3 files changed
+254
No files matched your search
@@ -1,5 +1,6 @@
|
||||
from .actors_config import MxActorConfigWidget
|
||||
from .automation_permission import MxAutomationPermissionWidget
|
||||
from .current_sample import MxCurrentSampleWidget
|
||||
from .sample_queue import SampleQueueWidget
|
||||
from .sample_spreadsheet_display import SampleSpreadsheetDisplay
|
||||
from .task_panel import MxTaskPanel
|
||||
@@ -8,6 +9,7 @@ from .task_queue import MxTaskQueueWidget
|
||||
__all__ = [
|
||||
"MxActorConfigWidget",
|
||||
"MxAutomationPermissionWidget",
|
||||
"MxCurrentSampleWidget",
|
||||
"SampleQueueWidget",
|
||||
"SampleSpreadsheetDisplay",
|
||||
"MxTaskPanel",
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from threading import Thread
|
||||
|
||||
from bec_lib.logger import bec_logger
|
||||
from bec_widgets.utils.bec_connector import ConnectionConfig
|
||||
from bec_widgets.utils.bec_widget import BECWidget
|
||||
from bec_widgets.utils.error_popups import SafeSlot
|
||||
from qtpy.QtCore import Signal
|
||||
from qtpy.QtWidgets import (
|
||||
QFormLayout,
|
||||
QHeaderView,
|
||||
QLabel,
|
||||
QTableWidget,
|
||||
QTableWidgetItem,
|
||||
QVBoxLayout,
|
||||
QWidget,
|
||||
)
|
||||
|
||||
from mx_bec.endpoints import StateEndpoints
|
||||
from mx_bec.external_services.aaredaq import AareDaq, LoadedSampleInfo
|
||||
from mx_bec.messages import MXMeasurementSequenceMessage
|
||||
|
||||
logger = bec_logger.logger
|
||||
|
||||
NOT_SET = "-"
|
||||
|
||||
|
||||
class MxCurrentSampleWidget(BECWidget, QWidget):
|
||||
"""
|
||||
Widget to show the mounted sample: its aareDB record, and its puck, dewar and TELL position.
|
||||
|
||||
Whenever the AareBec service announces that a sample was mounted (`sample_mounted`), the
|
||||
record is fetched from AareDAQ with the token of the user running the GUI, which needs a
|
||||
valid Kerberos ticket, and is only given for samples the user may work on.
|
||||
"""
|
||||
|
||||
PLUGIN = True
|
||||
ICON_NAME = "science"
|
||||
USER_ACCESS = ["refresh", "show_sample"]
|
||||
|
||||
# the record is fetched on a worker thread; the outcome is shown on the GUI thread:
|
||||
# (sample id, record or None, message)
|
||||
_fetched = Signal(int, object, str)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
parent: QWidget | None = None,
|
||||
client=None,
|
||||
config: ConnectionConfig | None = None,
|
||||
gui_id: str | None = None,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
super().__init__(parent=parent, client=client, config=config, gui_id=gui_id, **kwargs)
|
||||
self._daq = AareDaq() # with the user's own token, requested on first use
|
||||
self._worker: Thread | None = None
|
||||
self._sample_id: int | None = None
|
||||
|
||||
self._id = QLabel(NOT_SET, parent=self)
|
||||
self._puck = QLabel(NOT_SET, parent=self)
|
||||
self._pgroup = QLabel(NOT_SET, parent=self)
|
||||
self._dewar = QLabel(NOT_SET, parent=self)
|
||||
self._tell_position = QLabel(NOT_SET, parent=self)
|
||||
form = QFormLayout()
|
||||
form.addRow("Sample ID", self._id)
|
||||
form.addRow("Puck", self._puck)
|
||||
form.addRow("Puck pgroup", self._pgroup)
|
||||
form.addRow("Dewar", self._dewar)
|
||||
form.addRow("TELL position", self._tell_position)
|
||||
|
||||
self._record = QTableWidget(0, 2, parent=self)
|
||||
self._record.setHorizontalHeaderLabels(["Field", "Value"])
|
||||
self._record.verticalHeader().setVisible(False)
|
||||
self._record.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
|
||||
self._record.horizontalHeader().setSectionResizeMode(
|
||||
0, QHeaderView.ResizeMode.ResizeToContents
|
||||
)
|
||||
self._record.horizontalHeader().setStretchLastSection(True)
|
||||
self._message = QLabel("No sample mounted", parent=self)
|
||||
self._message.setWordWrap(True)
|
||||
|
||||
layout = QVBoxLayout(self)
|
||||
layout.addLayout(form)
|
||||
layout.addWidget(QLabel("Sample record", parent=self))
|
||||
layout.addWidget(self._record)
|
||||
layout.addWidget(self._message)
|
||||
|
||||
self._fetched.connect(self._on_fetched)
|
||||
# released automatically by BECWidget.cleanup() (disconnect_owner)
|
||||
self.bec_dispatcher.connect_slot(self._on_sample_mounted, StateEndpoints.sample_mounted())
|
||||
self.refresh()
|
||||
|
||||
@SafeSlot()
|
||||
def refresh(self):
|
||||
"""Show the sample which was mounted last, as announced by the AareBec service."""
|
||||
self._show_mounted(self.client.connector.get(StateEndpoints.sample_mounted()))
|
||||
|
||||
@SafeSlot(dict, dict)
|
||||
def _on_sample_mounted(self, content: dict, _metadata: dict):
|
||||
self._show_mounted(MXMeasurementSequenceMessage(**content))
|
||||
|
||||
def _show_mounted(self, msg: MXMeasurementSequenceMessage | None) -> None:
|
||||
if msg is None or not msg.success:
|
||||
return # nothing (new) was mounted: keep what is shown
|
||||
self.show_sample(msg.sample_id)
|
||||
|
||||
@SafeSlot(int)
|
||||
def show_sample(self, sample_id: int):
|
||||
"""Fetch the record of the sample with this database id from AareDAQ and show it.
|
||||
Returns at once; the record, or why it could not be fetched, appears in the widget."""
|
||||
self._sample_id = sample_id
|
||||
self._id.setText(str(sample_id))
|
||||
self._message.setText(f"Fetching the record of sample {sample_id}...")
|
||||
self._worker = Thread(
|
||||
target=self._fetch, args=(sample_id,), name="mx-sample-info", daemon=True
|
||||
)
|
||||
self._worker.start()
|
||||
|
||||
def _fetch(self, sample_id: int) -> None:
|
||||
"""Runs on a worker thread: the first request also has to get a token."""
|
||||
try:
|
||||
info = self._daq.sample_info(sample_id)
|
||||
except Exception as e: # pylint: disable=broad-except
|
||||
logger.warning(f"Could not fetch the record of sample {sample_id}: {e}")
|
||||
self._fetched.emit(sample_id, None, f"Could not fetch the sample record: {e}")
|
||||
return
|
||||
self._fetched.emit(sample_id, info, "")
|
||||
|
||||
@SafeSlot(int, object, str)
|
||||
def _on_fetched(self, sample_id: int, info: LoadedSampleInfo | None, message: str):
|
||||
if sample_id != self._sample_id:
|
||||
return # another sample was mounted in the meantime
|
||||
self._message.setText(message)
|
||||
self._puck.setText(info.puck_name if info else NOT_SET)
|
||||
self._pgroup.setText(info.puck_pgroup if info else NOT_SET)
|
||||
self._dewar.setText((info.dewar_name if info else None) or NOT_SET)
|
||||
self._tell_position.setText((info.tell_position if info else None) or NOT_SET)
|
||||
record = info.sample if info else {}
|
||||
self._record.setRowCount(len(record))
|
||||
for row, (field, value) in enumerate(record.items()):
|
||||
self._record.setItem(row, 0, QTableWidgetItem(str(field)))
|
||||
self._record.setItem(row, 1, QTableWidgetItem("" if value is None else str(value)))
|
||||
|
||||
def cleanup(self):
|
||||
if self._worker is not None:
|
||||
self._worker.join(timeout=1)
|
||||
super().cleanup()
|
||||
@@ -0,0 +1,105 @@
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from mx_bec.bec_widgets.widgets.aare_bec import MxCurrentSampleWidget
|
||||
from mx_bec.endpoints import StateEndpoints
|
||||
from mx_bec.external_services.aaredaq import AareDaqError, LoadedSampleInfo
|
||||
from mx_bec.messages import MXMeasurementSequenceMessage
|
||||
|
||||
WIDGET = "mx_bec.bec_widgets.widgets.aare_bec.current_sample"
|
||||
INFO = LoadedSampleInfo(
|
||||
sample={"id": 42, "sample_name": "lyso_1", "pgroup": "p16371", "comment": None},
|
||||
puck_name="PUCK7",
|
||||
puck_pgroup="p16371",
|
||||
dewar_name="dewar_a",
|
||||
tell_position="B2",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def daq():
|
||||
with patch(f"{WIDGET}.AareDaq") as daq_cls:
|
||||
daq_cls.return_value.sample_info.return_value = INFO
|
||||
yield daq_cls.return_value
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
client = MagicMock()
|
||||
client.connector.get.return_value = None
|
||||
return client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def widget(qtbot, client, daq):
|
||||
widget = MxCurrentSampleWidget(client=client)
|
||||
qtbot.addWidget(widget)
|
||||
yield widget
|
||||
widget.cleanup()
|
||||
|
||||
|
||||
def _record(widget) -> dict[str, str]:
|
||||
table = widget._record
|
||||
return {table.item(row, 0).text(): table.item(row, 1).text() for row in range(table.rowCount())}
|
||||
|
||||
|
||||
def _mounted(sample_id: int, success: bool = True) -> dict:
|
||||
return MXMeasurementSequenceMessage(sample_id=sample_id, success=success).content
|
||||
|
||||
|
||||
def test_shows_nothing_before_a_sample_is_mounted(widget, client, daq):
|
||||
client.connector.get.assert_called_with(StateEndpoints.sample_mounted())
|
||||
assert widget._id.text() == "-" and widget._message.text() == "No sample mounted"
|
||||
daq.sample_info.assert_not_called()
|
||||
|
||||
|
||||
def test_shows_the_record_when_a_sample_is_mounted(widget, daq, qtbot):
|
||||
with qtbot.waitSignal(widget._fetched, timeout=5000):
|
||||
widget._on_sample_mounted(_mounted(42), {})
|
||||
daq.sample_info.assert_called_once_with(42)
|
||||
assert widget._id.text() == "42"
|
||||
assert (widget._puck.text(), widget._pgroup.text()) == ("PUCK7", "p16371")
|
||||
assert (widget._dewar.text(), widget._tell_position.text()) == ("dewar_a", "B2")
|
||||
assert _record(widget) == {
|
||||
"id": "42",
|
||||
"sample_name": "lyso_1",
|
||||
"pgroup": "p16371",
|
||||
"comment": "",
|
||||
}
|
||||
assert widget._message.text() == ""
|
||||
|
||||
|
||||
def test_shows_the_sample_mounted_before_the_widget_opened(qtbot, client, daq):
|
||||
client.connector.get.return_value = MXMeasurementSequenceMessage(sample_id=7, success=True)
|
||||
widget = MxCurrentSampleWidget(client=client)
|
||||
qtbot.addWidget(widget)
|
||||
qtbot.waitUntil(lambda: widget._puck.text() == "PUCK7", timeout=5000)
|
||||
daq.sample_info.assert_called_once_with(7)
|
||||
widget.cleanup()
|
||||
|
||||
|
||||
def test_a_failed_mount_keeps_what_is_shown(widget, daq, qtbot):
|
||||
with qtbot.waitSignal(widget._fetched, timeout=5000):
|
||||
widget._on_sample_mounted(_mounted(42), {})
|
||||
widget._on_sample_mounted(_mounted(43, success=False), {})
|
||||
assert widget._id.text() == "42"
|
||||
daq.sample_info.assert_called_once_with(42)
|
||||
|
||||
|
||||
def test_shows_why_the_record_could_not_be_fetched(widget, daq, qtbot):
|
||||
with qtbot.waitSignal(widget._fetched, timeout=5000):
|
||||
widget._on_sample_mounted(_mounted(42), {})
|
||||
daq.sample_info.side_effect = AareDaqError("403: not in p20000")
|
||||
with qtbot.waitSignal(widget._fetched, timeout=5000):
|
||||
widget.show_sample(43)
|
||||
assert widget._id.text() == "43"
|
||||
assert "403: not in p20000" in widget._message.text()
|
||||
# nothing of the previous sample is left
|
||||
assert widget._puck.text() == "-" and _record(widget) == {}
|
||||
|
||||
|
||||
def test_a_late_answer_for_a_previous_sample_is_ignored(widget):
|
||||
widget._sample_id = 43
|
||||
widget._on_fetched(42, INFO, "")
|
||||
assert widget._puck.text() == "-"
|
||||
Reference in new issue
Block a user