diff --git a/src/groundlight/experimental_api.py b/src/groundlight/experimental_api.py index fbe8c214..e16539bc 100644 --- a/src/groundlight/experimental_api.py +++ b/src/groundlight/experimental_api.py @@ -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. diff --git a/test/unit/test_actions.py b/test/unit/test_actions.py index b2a9fe86..0749df57 100644 --- a/test/unit/test_actions.py +++ b/test/unit/test_actions.py @@ -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", "test@example.com") + 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()}"