Skip to content

Commit b79c0c3

Browse files
committed
Added logic to generate index number for first instance of a logged param in NotificationValue table
1 parent e8006e1 commit b79c0c3

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/murfey/workflows/spa/picking.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from logging import getLogger
12
from typing import List
23

34
import numpy as np
@@ -29,6 +30,8 @@
2930
from murfey.util.db import SPAFeedbackParameters, SPARelionParameters
3031
from murfey.util.processing_params import default_spa_parameters
3132

33+
logger = getLogger("murfey.workflows.spa.picking")
34+
3235

3336
def _register_picked_particles_use_diameter(
3437
message: dict, _db: Session, demo: bool = False
@@ -355,25 +358,32 @@ def _check_notifications(message: dict, murfey_db: Session) -> None:
355358
failures = []
356359
for param in notification_parameters:
357360
if message.get(param.name) is not None:
361+
# Load instances of current parameter from database
358362
param_values = murfey_db.exec(
359363
select(NotificationValue).where(
360364
NotificationValue.notification_parameter_id == param.id
361365
)
362366
).all()
363-
param_values.sort(ley=lambda x: x.index)
367+
param_values.sort(key=lambda x: x.index)
368+
369+
# Drop oldest value if number of entries exceeds threshold
364370
param_value_to_drop = None
365371
if len(param_values) >= 25:
366372
param_value_to_drop = param_values[0]
367373
param_values = param_values[1:]
374+
375+
# Add newest value to end of list
368376
param_values.append(
369377
NotificationValue(
370378
notification_parameter_id=param.id,
371-
index=param_values[-1].index + 1,
379+
index=param_values[-1].index + 1 if len(param_values) else 0,
372380
within_bounds=param.min_value
373381
<= message[param.name]
374382
<= param.max_value,
375383
)
376384
)
385+
386+
# Trigger message if this param has consistently exceeded the set threshold
377387
if (
378388
len(param_values) >= 25
379389
and sum(p.within_bounds for p in param_values) / len(param_values)
@@ -388,9 +398,14 @@ def _check_notifications(message: dict, murfey_db: Session) -> None:
388398
else:
389399
if param.notification_active:
390400
param.notification_active = False
401+
402+
# Delete oldest value
391403
if param_value_to_drop is not None:
392404
murfey_db.delete(param_value_to_drop)
405+
406+
# Add newest value
393407
murfey_db.add(param_values[-1])
408+
394409
murfey_db.add_all(notification_parameters)
395410
murfey_db.commit()
396411
murfey_db.close()

0 commit comments

Comments
 (0)