|
3 | 3 | from robot.api.deco import keyword |
4 | 4 | from resources.minder_restapi_lib import MinderRestApiLib |
5 | 5 | from resources.errors import ConfigurationError, APIError |
| 6 | +from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type |
6 | 7 |
|
7 | 8 |
|
8 | 9 | class EvalHistoryService: |
@@ -72,3 +73,114 @@ def history_is_empty(self): |
72 | 73 | """ |
73 | 74 | if self.history["data"]: |
74 | 75 | raise ValueError("history should be empty") |
| 76 | + |
| 77 | + @keyword |
| 78 | + def history_is_not_empty(self): |
| 79 | + """ |
| 80 | + Verifies that the evaluation history is not empty. |
| 81 | +
|
| 82 | + Raises: |
| 83 | + ValueError: If the history is empty. |
| 84 | + """ |
| 85 | + if not self.history or not self.history.get("data"): |
| 86 | + raise ValueError("history should not be empty") |
| 87 | + |
| 88 | + @keyword |
| 89 | + @retry( |
| 90 | + stop=stop_after_attempt(3), |
| 91 | + wait=wait_exponential(multiplier=1, min=1, max=10), |
| 92 | + retry=(retry_if_exception_type(APIError) | retry_if_exception_type(ValueError)), |
| 93 | + reraise=True |
| 94 | + ) |
| 95 | + def client_retrieves_non_empty_eval_history(self): |
| 96 | + """ |
| 97 | + Retrieves non-empty evaluation history from the Minder API with retries. |
| 98 | + Retries up to 3 times with exponential backoff between 1 and 10 seconds. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + dict: The JSON response from the API. |
| 102 | +
|
| 103 | + Raises: |
| 104 | + ConfigurationError: If required configuration is missing. |
| 105 | + APIError: If the API request fails after maximum retries. |
| 106 | + ValueError: If the history is empty after retrieval attempt. |
| 107 | + """ |
| 108 | + project = os.getenv("MINDER_PROJECT") |
| 109 | + if not project: |
| 110 | + raise ConfigurationError("MINDER_PROJECT environment variable is not set") |
| 111 | + |
| 112 | + provider = os.getenv("MINDER_PROVIDER") |
| 113 | + if not provider: |
| 114 | + raise ConfigurationError("MINDER_PROVIDER environment variable is not set") |
| 115 | + |
| 116 | + params = { |
| 117 | + "provider": provider, |
| 118 | + "context.project": project |
| 119 | + } |
| 120 | + |
| 121 | + try: |
| 122 | + rest_api = MinderRestApiLib() |
| 123 | + self.history = rest_api.get_request('/history', params=params) |
| 124 | + if not self.history or not self.history.get("data"): |
| 125 | + raise ValueError("Retrieved history is empty") |
| 126 | + except Exception as e: |
| 127 | + raise APIError(f"API request failed: {str(e)}") |
| 128 | + |
| 129 | + @keyword |
| 130 | + def get_first_evaluation_history_id(self): |
| 131 | + """ |
| 132 | + Retrieves the ID of the first evaluation history record. |
| 133 | +
|
| 134 | + Returns: |
| 135 | + str: The ID of the first evaluation history record. |
| 136 | +
|
| 137 | + Raises: |
| 138 | + ValueError: If the history is empty or no ID is found. |
| 139 | + """ |
| 140 | + if not self.history or not self.history.get("data"): |
| 141 | + raise ValueError("No evaluation history data available") |
| 142 | + |
| 143 | + # Assuming 'data' is a list of EvaluationHistory objects |
| 144 | + first_record = self.history["data"][0] |
| 145 | + history_id = first_record.get("id") |
| 146 | + |
| 147 | + if not history_id: |
| 148 | + raise ValueError("No ID found in the first evaluation history record") |
| 149 | + |
| 150 | + return history_id |
| 151 | + |
| 152 | + @keyword |
| 153 | + def client_retrieves_eval_history_by_id(self, history_id): |
| 154 | + """ |
| 155 | + Retrieves evaluation history by ID from the Minder API. |
| 156 | +
|
| 157 | + Args: |
| 158 | + history_id (str): The ID of the history to retrieve. |
| 159 | +
|
| 160 | + Returns: |
| 161 | + dict: The JSON response from the API. |
| 162 | +
|
| 163 | + Raises: |
| 164 | + ConfigurationError: If required environment variables are not set. |
| 165 | + APIError: If the API request fails. |
| 166 | + """ |
| 167 | + |
| 168 | + project = os.getenv("MINDER_PROJECT") |
| 169 | + if not project: |
| 170 | + raise ConfigurationError("MINDER_PROJECT environment variable is not set") |
| 171 | + |
| 172 | + provider = os.getenv("MINDER_PROVIDER") |
| 173 | + if not provider: |
| 174 | + raise ConfigurationError("MINDER_PROVIDER environment variable is not set") |
| 175 | + |
| 176 | + params = { |
| 177 | + "provider": provider, |
| 178 | + "context.project": project |
| 179 | + } |
| 180 | + |
| 181 | + try: |
| 182 | + rest_api = MinderRestApiLib() |
| 183 | + response = rest_api.get_request(f'/history/{history_id}', params=params) |
| 184 | + return response |
| 185 | + except Exception as e: |
| 186 | + raise APIError(f"API request failed: {str(e)}") |
0 commit comments