Skip to content
Snippets Groups Projects
Commit 6021c964 authored by Thomas Kluyver's avatar Thomas Kluyver
Browse files

Try to simplify various bits of CalCat API use

parent 2add8f72
No related branches found
No related tags found
1 merge request!1026Feat[Jungfrau]: Inject CCVs using RESTful API
...@@ -32,10 +32,9 @@ class CCVAlreadyInjectedError(InjectionError): ...@@ -32,10 +32,9 @@ class CCVAlreadyInjectedError(InjectionError):
@dataclass @dataclass
class ParameterConditionAttribute: class ParameterConditionAttribute:
value: str value: str
parameter_id: str = "" # Default value, allowing assignment later
lower_deviation_value: float = 0 lower_deviation_value: float = 0
upper_deviation_value: float = 0 upper_deviation_value: float = 0
flg_available: str = 'true' flg_available: bool = True
description: str = '' description: str = ''
...@@ -51,13 +50,7 @@ def generate_unique_condition_name( ...@@ -51,13 +50,7 @@ def generate_unique_condition_name(
detector_type (str): detector type. detector_type (str): detector type.
pdu_name (str): Physical detector unit db name. pdu_name (str): Physical detector unit db name.
pdu_uuid (float): Physical detector unit db id. pdu_uuid (float): Physical detector unit db id.
cond_params (dict): A list of dictionary with each condition cond_params (dict): Keys DB names, values ParameterConditionAttribute
e.g. [{
"parameter_name": "Memory Cells",
"value": 352.0,
"lower-deviation": 0.0,
"upper-deviation": 0.0
}]
Returns: Returns:
str: A unique name used for the table of conditions. str: A unique name used for the table of conditions.
...@@ -242,9 +235,9 @@ def get_or_create_calibration_constant( ...@@ -242,9 +235,9 @@ def get_or_create_calibration_constant(
calibration_id=cal_id, calibration_id=cal_id,
condition_id=cond_id, condition_id=cond_id,
detector_type_id=det_type_id, detector_type_id=det_type_id,
flg_auto_approve = 'true', flg_auto_approve=True,
flg_available = 'true', flg_available=True,
description = "", description="",
) )
try: try:
cc_id = client.get( cc_id = client.get(
...@@ -269,18 +262,15 @@ def create_condition( ...@@ -269,18 +262,15 @@ def create_condition(
cond_name = generate_unique_condition_name( cond_name = generate_unique_condition_name(
detector_type, pdu_name, pdu_uuid, cond_params) detector_type, pdu_name, pdu_uuid, cond_params)
# Add the missing parameter_id value in `ParameterConditionAttribute`s.
for param, cond in cond_params.items():
cond.parameter_id = client.parameter_by_name(param)['id']
cond_params[param] = asdict(cond)
# Create condition table in database, if not available. # Create condition table in database, if not available.
cond = dict( cond = dict(
name=cond_name, name=cond_name,
parameters_conditions_attributes=list(cond_params.values()), parameters_conditions_attributes=[
flg_available = 'true', asdict(cond) | {"parameter_name": db_name}
event_at = str(datetime.today()), # TODO: Why is this needed? it is not written in swagger. for (db_name, cond) in cond_params.items()
description = '', ],
flg_available=True,
description='',
) )
resp = client.post( resp = client.post(
"conditions/set_expected_condition", {"condition": cond} "conditions/set_expected_condition", {"condition": cond}
...@@ -364,9 +354,9 @@ def inject_ccv(const_src, ccv_root, report_to=None, client=None): ...@@ -364,9 +354,9 @@ def inject_ccv(const_src, ccv_root, report_to=None, client=None):
if report_to: if report_to:
report_path = Path(report_to).absolute().with_suffix('.pdf') report_path = Path(report_to).absolute().with_suffix('.pdf')
resp = client.post("reports/set", dict( resp = client.post("reports/set", dict(
name=report_path.stem, name=report_path.name,
file_path=str(report_path), file_path=str(report_path),
flg_available='true', flg_available=True,
description="", description="",
)) ))
report_id = resp['id'] report_id = resp['id']
...@@ -375,7 +365,7 @@ def inject_ccv(const_src, ccv_root, report_to=None, client=None): ...@@ -375,7 +365,7 @@ def inject_ccv(const_src, ccv_root, report_to=None, client=None):
pdu_id = client.pdu_by_name(pdu_name)['id'] pdu_id = client.pdu_by_name(pdu_name)['id']
# Prepare CCV data and inject it to CALCAT. # Prepare CCV data and inject it to CALCAT.
start_idx = '0' start_idx = 0
ccv = dict( ccv = dict(
name=create_unique_ccv_name(start_idx), name=create_unique_ccv_name(start_idx),
file_name=const_filename, file_name=const_filename,
...@@ -389,10 +379,10 @@ def inject_ccv(const_src, ccv_root, report_to=None, client=None): ...@@ -389,10 +379,10 @@ def inject_ccv(const_src, ccv_root, report_to=None, client=None):
end_validity_at='', end_validity_at='',
begin_at=begin_at, begin_at=begin_at,
start_idx=start_idx, start_idx=start_idx,
end_idx = '0', end_idx=0,
flg_deployed = 'true', flg_deployed=True,
flg_good_quality = 'true', flg_good_quality=True,
description = '', description='',
) )
try: try:
client.post("calibration_constant_versions", ccv) client.post("calibration_constant_versions", ccv)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment