Skip to content

Commit

Permalink
Sort Santa targets by score
Browse files Browse the repository at this point in the history
  • Loading branch information
np5 committed Feb 11, 2025
1 parent 3e63141 commit c0afeac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
33 changes: 33 additions & 0 deletions tests/santa/test_targets_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,39 @@ def test_search_target_todo_missing_one_vote_same_config(self):
self.assertEqual(len(targets), 1)
self.assertEqual(targets[0]["identifier"], self.file_sha256)

def test_search_target_order_by_score(self):
self._login("santa.view_target")
realm, realm_user = force_realm_user(username=self.user.username)
configuration = force_configuration(voting_realm=realm)
# vote on all targets except the binary
first_target = last_target = None
score = 1
for target in Target.objects.all():
if first_target is None:
first_target = target
last_target = target
force_ballot(target, realm_user, [(configuration, True, score)])
TargetState.objects.create(
configuration=configuration,
target=target,
flagged=False,
state=TargetState.State.UNTRUSTED,
score=score,
)
score += 1
response = self.client.get(reverse("santa:targets"), {"configuration": configuration.pk,
"order_by": "-max_score"})
self.assertEqual(response.context["targets"][0]["id"], last_target.pk)
response = self.client.get(reverse("santa:targets"), {"configuration": configuration.pk,
"order_by": "-min_score"})
self.assertEqual(response.context["targets"][0]["id"], last_target.pk)
response = self.client.get(reverse("santa:targets"), {"configuration": configuration.pk,
"order_by": "+max_score"})
self.assertEqual(response.context["targets"][0]["id"], first_target.pk)
response = self.client.get(reverse("santa:targets"), {"configuration": configuration.pk,
"order_by": "+min_score"})
self.assertEqual(response.context["targets"][0]["id"], first_target.pk)

# binary target

def test_binary_target_redirect(self):
Expand Down
14 changes: 13 additions & 1 deletion zentral/contrib/santa/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,11 @@ class TargetSearchForm(forms.Form):
choices=[('', '…'),
('-last_seen', '↓ Last seen'),
('-executed', '↓ Executed count'),
('-blocked', '↓ Blocked count')]
('-blocked', '↓ Blocked count'),
('-min_score', '↓ Min score'),
('-max_score', '↓ Max score'),
('+min_score', '↑ Min score'),
('+max_score', '↑ Max score'),]
)

@classmethod
Expand Down Expand Up @@ -771,6 +775,14 @@ def search_query(
primary_order_by = "coalesce(sum(ac.executed_count), 0) desc,"
elif order_by == "-blocked":
primary_order_by = "coalesce(sum(ac.blocked_count), 0) desc,"
elif order_by == "-min_score":
primary_order_by = "coalesce(min(ts.score), 0) desc,"
elif order_by == "-max_score":
primary_order_by = "coalesce(max(ts.score), 0) desc,"
elif order_by == "+min_score":
primary_order_by = "coalesce(min(ts.score), 0) asc,"
elif order_by == "+max_score":
primary_order_by = "coalesce(max(ts.score), 0) asc,"
else:
if order_by:
logger.error("Unknown order by value: %s", order_by)
Expand Down

0 comments on commit c0afeac

Please sign in to comment.