Skip to content

Commit e230356

Browse files
committed
Alt method for signal priority/swapping
1 parent 80b110d commit e230356

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

colocus/api/views.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def annotate_prioritized_signals(queryset, analysis_type_priority=None):
8484
Annotated queryset with 'no_signal_swap' boolean field
8585
"""
8686
if analysis_type_priority:
87-
order_list = analysis_type_priority.split(",")
87+
# Only first two analysis types are used to designate slots 1/2; the rest are ignored
88+
order_list = analysis_type_priority.split(",")[0:2]
8889

8990
# Create CASE statements for order1 and order2
9091
order1_whens = [
@@ -110,13 +111,19 @@ def annotate_prioritized_signals(queryset, analysis_type_priority=None):
110111
)
111112
)
112113

113-
# Now determine which signal to use as primary (first signal shown)
114-
# Logic:
115-
# - If both None: use signal1
116-
# - If only one has a value:
117-
# - If that value is 0: use that signal
118-
# - If that value is 1: use the other signal
119-
# - If both have values: use the one with lower index
114+
if len(order_list) == 0:
115+
pass # nothing to do in this case
116+
elif len(order_list) == 1:
117+
queryset = queryset.filter(
118+
Q(order1=0) | Q(order2=0)
119+
)
120+
elif len(order_list) == 2:
121+
queryset = queryset.filter(
122+
((Q(order1=0) & Q(order2=1)) | (Q(order1=1) & Q(order2=0)))
123+
)
124+
else:
125+
# Raise exception
126+
raise drf_exceptions.ValidationError('analysis_type_priority should contain <=2 analysis types')
120127

121128
queryset = queryset.annotate(
122129
no_signal_swap=Case(
@@ -145,9 +152,11 @@ def annotate_prioritized_signals(queryset, analysis_type_priority=None):
145152
When(
146153
Q(order1__isnull=False) & Q(order2__isnull=False),
147154
then=Case(
148-
When(order1__lt=F('order2'), then=Value(True)),
149-
When(order1__gt=F('order2'), then=Value(False)),
150-
When(order1=F('order2'), then=Value(True)),
155+
When(order1=0, then=Value(True)),
156+
When(order1=1, then=Value(False)),
157+
When(order2=0, then=Value(False)),
158+
When(order2=1, then=Value(True)),
159+
default=Value(True),
151160
output_field=BooleanField()
152161
)
153162
),

0 commit comments

Comments
 (0)