Skip to content

Commit

Permalink
#330 backend: only send continuous variable update if continuous vari…
Browse files Browse the repository at this point in the history
…able state changed
  • Loading branch information
Wolkenfarmer committed Sep 16, 2024
1 parent 4a741f1 commit 1ec1e72
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
26 changes: 26 additions & 0 deletions backend/dps_training_k/game/consumers/patient_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(self, *args, **kwargs):
]
self.patient_frontend_id = ""
self.currently_inspected_action = None
self.continuous_variables_hashes = {}

patient_request_map = {
self.PatientIncomingMessageTypes.ACTION_ADD: (
Expand Down Expand Up @@ -290,11 +291,36 @@ def continuous_variable_event(self, event=None):
serialized_continuous_state = ContinuousVariableSerializer(
self.get_patient_instance()
).data

continuous_variables = serialized_continuous_state["continuousVariables"]
filtered_continuous_variables = [
variable
for variable in continuous_variables
if self.has_continuous_variable_hash_changed(variable)
]
self.update_continuous_variable_hashes(filtered_continuous_variables)
serialized_continuous_state["continuousVariables"] = (
filtered_continuous_variables
)

self.send_event(
self.PatientOutgoingMessageTypes.CONTINUOUS_VARIABLE,
continuousState=serialized_continuous_state,
)

def has_continuous_variable_hash_changed(self, variable):
"""Check if the hash of the variable has changed."""
var_name = variable["name"]
var_hash = variable["hash"]
if var_name not in self.continuous_variables_hashes:
return True
return self.continuous_variables_hashes[var_name] != var_hash

def update_continuous_variable_hashes(self, variables):
"""Update the stored hashes with the new hashes."""
for variable in variables:
self.continuous_variables_hashes[variable["name"]] = variable["hash"]

def exercise_start_event(self, event=None):
super().exercise_start_event(event)
self.continuous_variable_event()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,21 @@ def continuous_variables(self):
spo2_target = _extract_spo2(future_state.vital_signs["Breathing"])

variables = ContinuousVariable.objects.all()
return [
{
"name": variable.name,
"current": int(spo2_current),
"target": int(spo2_target),
"function": self._get_applicable_function(variable),
}
for variable in variables
]

result = []
for variable in variables:
function, var_hash = self._get_applicable_function(variable)
var_hash = hash((var_hash, spo2_current, spo2_target))
result.append(
{
"name": variable.name,
"current": 100, # Placeholder value, adjust as needed
"target": 0, # Placeholder value, adjust as needed
"function": function,
"hash": var_hash,
}
)
return result

def _get_applicable_function(self, variable):
completed_action_uuids = {
Expand All @@ -92,8 +98,12 @@ def _get_applicable_function(self, variable):
}

for exception in variable.exceptions:
if _check_subset(
exception["actions"], completed_action_uuids
) and _check_subset(exception["materials"], completed_material_uuids):
return exception["function"]
return variable.function
function = exception["function"]
actions = tuple(exception["actions"])
materials = tuple(exception["materials"])

if _check_subset(actions, completed_action_uuids) and _check_subset(
materials, completed_material_uuids
):
return function, hash((function, actions, materials))
return variable.function, hash(variable.function)

0 comments on commit 1ec1e72

Please sign in to comment.