Skip to content

Commit

Permalink
fetch rules by detector
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-groundlight committed Dec 12, 2024
1 parent f5b5ceb commit b6b22b6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/groundlight/experimental_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ def get_rule(self, action_id: int) -> Rule:
"""
return Rule.model_validate(self.actions_api.get_rule(action_id).to_dict())

def list_detector_rules(self, detector: Union[str, Detector]) -> List[Rule]:
"""
Gets all rules associated with the given detector.
**Example usage**::
gl = ExperimentalApi()
# Get all rules for a specific detector
rules = gl.list_detector_rules(det_mydetectorid)
for rule in rules:
print(f"Rule {rule.id}: {rule.name}")
:param detector: the detector or detector_id to get the rules for
:return: a list of Rule objects associated with the given detector
"""
return [Rule.model_validate(rule.to_dict()) for rule in self.actions_api.list_detector_rules(detector.id)]

def delete_rule(self, action_id: int) -> None:
"""
Deletes the rule with the given id.
Expand Down
11 changes: 9 additions & 2 deletions test/unit/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ def test_get_all_actions(gl_experimental: ExperimentalApi):
assert rules.count > num_test_rules
assert len(rules.results) == gl_experimental.ITEMS_PER_PAGE

def test_delete_actions():
def test_delete_actions(gl_experimental: ExperimentalApi):
name = f"Test {datetime.utcnow()}"
num_test_rules = 13 # needs to be larger than the default page size
# TODO: get actions by detector
det = gl_experimental.get_or_create_detector(name, "test_query")
for i in range(num_test_rules):
_ = gl_experimental.create_rule(det, f"test_rule_{i}", "EMAIL", "[email protected]")
rules = gl_experimental.list_detector_rules(det.id)
assert rules.count == num_test_rules
gl_experimental.delete_all_rules(det.id)
rules = gl_experimental.list_detector_rules(det.id)
assert rules.count == 0

def test_create_action_with_human_review(gl_experimental: ExperimentalApi):
name = f"Test {datetime.utcnow()}"
Expand Down

0 comments on commit b6b22b6

Please sign in to comment.