|
| 1 | +"""Module containing convenience functions for the tests API.""" |
| 2 | + |
| 3 | +from typing import Optional, List |
| 4 | +from openlayer import Openlayer |
| 5 | + |
| 6 | + |
| 7 | +def copy_tests( |
| 8 | + client: Openlayer, |
| 9 | + origin_project_id: str, |
| 10 | + target_project_id: str, |
| 11 | + verbose: bool = False, |
| 12 | + test_ids: Optional[List[str]] = None, |
| 13 | +) -> None: |
| 14 | + """Copy tests from one project to another. |
| 15 | +
|
| 16 | + Args: |
| 17 | + client (Openlayer): The Openlayer client. |
| 18 | + origin_project_id (str): The ID of the origin project (where the tests |
| 19 | + are). |
| 20 | + target_project_id (str): The ID of the target project (where the tests |
| 21 | + will be copied to). |
| 22 | + verbose (bool): Whether to print verbose output. |
| 23 | + test_ids (List[str]): The IDs of the tests to copy. If not provided, all |
| 24 | + tests will be copied. |
| 25 | + """ |
| 26 | + tests = client.projects.tests.list(project_id=origin_project_id) |
| 27 | + |
| 28 | + if test_ids is None and verbose: |
| 29 | + print("Copying all tests from the origin project to the target project.") |
| 30 | + else: |
| 31 | + print( |
| 32 | + "Copying the following tests from the origin project to" |
| 33 | + f" the target project: {test_ids}" |
| 34 | + ) |
| 35 | + |
| 36 | + for test in tests.items: |
| 37 | + if test.id in test_ids: |
| 38 | + thresholds = _parse_thresholds(test.thresholds) |
| 39 | + client.projects.tests.create( |
| 40 | + project_id=target_project_id, |
| 41 | + name=test.name, |
| 42 | + description=test.description, |
| 43 | + type=test.type, |
| 44 | + subtype=test.subtype, |
| 45 | + thresholds=thresholds, |
| 46 | + uses_production_data=test.uses_production_data, |
| 47 | + evaluation_window=test.evaluation_window, |
| 48 | + delay_window=test.delay_window, |
| 49 | + uses_training_dataset=test.uses_training_dataset, |
| 50 | + uses_validation_dataset=test.uses_validation_dataset, |
| 51 | + uses_ml_model=test.uses_ml_model, |
| 52 | + ) |
| 53 | + if verbose: |
| 54 | + print( |
| 55 | + f"Copied test '{test.id}' - '{test.name}' from the" |
| 56 | + " origin project to the target project." |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def _parse_thresholds(thresholds: List[dict]) -> List[dict]: |
| 61 | + """Parse the thresholds from the test to the format required by the create |
| 62 | + test endpoint.""" |
| 63 | + thresholds = [] |
| 64 | + for threshold in thresholds: |
| 65 | + current_threshold = { |
| 66 | + "insightName": threshold.insight_name, |
| 67 | + "measurement": threshold.measurement, |
| 68 | + "operator": threshold.operator, |
| 69 | + "value": threshold.value, |
| 70 | + } |
| 71 | + |
| 72 | + if threshold.get("insightParameters"): |
| 73 | + current_threshold["insightParameters"] = threshold["insightParameters"] |
| 74 | + thresholds.append(current_threshold) |
| 75 | + |
| 76 | + return thresholds |
0 commit comments