-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtesting.py
More file actions
521 lines (423 loc) · 15.7 KB
/
testing.py
File metadata and controls
521 lines (423 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
"""FQL testing and validation utilities.
This module provides utilities to test and validate FQL expressions before
committing them as Custom Metrics. Since Fiddler does not provide a dry-run
API, these utilities use temporary metrics for real validation.
Key Functions:
- validate_metric_syntax_local(): Fast local syntax validation
- test_metric_definition(): Test FQL by creating temporary metric
- validate_and_preview_metric(): Full validation with preview
- batch_test_metrics(): Test multiple definitions efficiently
Example:
```python
from fiddler_utils.testing import (
validate_metric_syntax_local,
test_metric_definition
)
import fiddler as fdl
# Fast local pre-validation
result = validate_metric_syntax_local(
definition='sum(if(fp(), 1, 0))',
model=model
)
if result['has_errors']:
print(f"Errors: {result['errors']}")
elif result['has_warnings']:
print(f"Warnings: {result['warnings']}")
# Real testing in Fiddler (creates & deletes temp metric)
result = test_metric_definition(
model_id=model.id,
definition='sum(if(fp(), 1, 0)) / sum(1)'
)
if result['valid']:
print("✓ Metric definition works!")
else:
print(f"✗ Error: {result['error']}")
```
"""
from typing import Dict, List, Optional, Any, Set
import logging
import time
import random
import string
try:
import fiddler as fdl
except ImportError:
raise ImportError(
'fiddler-client is required. Install it with: pip install fiddler-client'
)
from . import fql
from .exceptions import FQLError
logger = logging.getLogger(__name__)
def _generate_temp_metric_name(prefix: str = "__test_") -> str:
"""Generate a unique temporary metric name.
Args:
prefix: Prefix for the temp metric name
Returns:
Unique temp metric name like '__test_1704123456_a3f2'
"""
timestamp = int(time.time())
random_suffix = ''.join(random.choices(string.ascii_lowercase + string.digits, k=4))
return f"{prefix}{timestamp}_{random_suffix}"
def validate_metric_syntax_local(
definition: str,
model: Optional[fdl.Model] = None
) -> Dict[str, Any]:
"""Perform fast local validation of FQL syntax.
This does NOT test whether the metric will actually work in Fiddler,
but catches obvious syntax errors quickly without API calls.
Checks performed:
- Quote matching (double and single quotes)
- Parentheses balance
- Column references exist in schema (if model provided)
- Function names are valid FQL functions
Args:
definition: FQL metric definition to validate
model: Optional Model object for schema validation
Returns:
Dictionary with validation results:
{
'valid': bool, # True if no errors
'has_errors': bool,
'has_warnings': bool,
'errors': List[str],
'warnings': List[str],
'metadata': Dict
}
Example:
```python
result = validate_metric_syntax_local(
definition='sum(if(fp(), "value", 0)', # Missing closing paren
model=model
)
if result['has_errors']:
print("Validation failed:")
for error in result['errors']:
print(f" - {error}")
```
"""
errors = []
warnings = []
metadata = {}
# Check 1: Basic syntax validation
is_valid, error_msg = fql.validate_fql_syntax(definition)
if not is_valid:
errors.append(f"Syntax error: {error_msg}")
# Check 2: Extract and validate columns (if model provided)
columns = fql.extract_columns(definition)
metadata['columns'] = list(columns)
metadata['column_count'] = len(columns)
if model:
from .schema import SchemaValidator
schema_valid, missing_cols = SchemaValidator.validate_fql_expression(
definition, model, strict=False
)
if not schema_valid:
errors.append(f"Missing columns in schema: {missing_cols}")
# Check 3: Extract functions
functions = fql.get_fql_functions(definition)
metadata['functions'] = list(functions)
metadata['function_count'] = len(functions)
# Check 4: Verify it's an aggregation (custom metrics must be aggregated)
is_simple = fql.is_simple_filter(definition)
metadata['is_aggregation'] = not is_simple
if is_simple:
warnings.append(
"Expression appears to be a simple filter without aggregation. "
"Custom metrics must use aggregate functions (sum, avg, count, etc.)"
)
# Check 5: Complexity warnings
if len(columns) > 10:
warnings.append(
f"High complexity: {len(columns)} columns referenced. "
"Consider simplifying."
)
if len(functions) > 5:
warnings.append(
f"Deeply nested: {len(functions)} function calls. "
"May be hard to debug if issues arise."
)
# Compile results
result = {
'valid': len(errors) == 0,
'has_errors': len(errors) > 0,
'has_warnings': len(warnings) > 0,
'errors': errors,
'warnings': warnings,
'metadata': metadata,
}
if result['valid']:
logger.debug(f"Local validation passed for expression: {definition[:100]}...")
else:
logger.warning(
f"Local validation failed with {len(errors)} errors: {errors}"
)
return result
def test_metric_definition(
model_id: str,
definition: str,
name_prefix: str = "__test_",
cleanup: bool = True,
wait_for_calculation: bool = False
) -> Dict[str, Any]:
"""Test an FQL metric definition by creating a temporary metric in Fiddler.
This is the ONLY way to truly validate FQL since many functions (tp(), fp(),
jsd(), etc.) can only be evaluated by Fiddler's backend.
Workflow:
1. Create metric with temporary name (__test_<timestamp>_<random>)
2. Fiddler validates the FQL definition
3. Optionally wait for metric to calculate
4. Delete temporary metric (if cleanup=True)
5. Return validation results
Args:
model_id: Model UUID to test against
definition: FQL metric definition to test
name_prefix: Prefix for temporary metric name (default: '__test_')
cleanup: If True, delete temp metric after testing (default: True)
wait_for_calculation: If True, wait for metric to calculate (slower)
Returns:
Dictionary with test results:
{
'valid': bool,
'error': str | None,
'temp_metric_id': str | None, # For debugging
'temp_metric_name': str,
'cleaned_up': bool,
'calculation_attempted': bool
}
Example:
```python
# Test a metric definition
result = test_metric_definition(
model_id=model.id,
definition='sum(if(fp(), 1, 0)) / sum(1)'
)
if result['valid']:
print("✓ Definition is valid!")
print(" Ready to create the real metric")
else:
print(f"✗ Definition failed: {result['error']}")
```
"""
temp_name = _generate_temp_metric_name(name_prefix)
logger.info(
f"Testing metric definition by creating temporary metric '{temp_name}'"
)
result = {
'valid': False,
'error': None,
'temp_metric_id': None,
'temp_metric_name': temp_name,
'cleaned_up': False,
'calculation_attempted': wait_for_calculation,
}
temp_metric = None
try:
# Create temporary metric
temp_metric = fdl.CustomMetric(
model_id=model_id,
name=temp_name,
description='TEMPORARY TEST METRIC - Safe to delete',
definition=definition,
)
temp_metric.create()
result['temp_metric_id'] = temp_metric.id
logger.info(f"✓ Temporary metric created successfully (ID: {temp_metric.id})")
result['valid'] = True
# Optionally wait for metric to calculate
if wait_for_calculation:
logger.info("Waiting for metric calculation...")
time.sleep(2) # Give Fiddler time to calculate
# Note: We don't actually query the metric here, just wait
except fdl.BadRequest as e:
# FQL validation error from Fiddler
error_msg = str(e)
logger.warning(f"FQL validation failed: {error_msg}")
result['error'] = error_msg
result['valid'] = False
except Exception as e:
# Other error
error_msg = f"Unexpected error: {str(e)}"
logger.error(error_msg)
result['error'] = error_msg
result['valid'] = False
finally:
# Cleanup temporary metric
if cleanup and temp_metric and result['temp_metric_id']:
try:
logger.info(f"Cleaning up temporary metric '{temp_name}'")
temp_metric.delete()
result['cleaned_up'] = True
logger.debug(f"✓ Temporary metric '{temp_name}' deleted")
except Exception as e:
logger.warning(
f"Failed to cleanup temporary metric '{temp_name}': {e}. "
"You may need to manually delete it."
)
result['cleaned_up'] = False
return result
def validate_and_preview_metric(
model_id: str,
definition: str,
skip_local_validation: bool = False
) -> Dict[str, Any]:
"""Complete validation workflow with both local and Fiddler testing.
This combines fast local validation with real Fiddler testing for
comprehensive validation.
Workflow:
1. Local syntax validation (fast, catches obvious errors)
2. If local validation passes, test in Fiddler (slower, real validation)
Args:
model_id: Model UUID to validate against
definition: FQL metric definition
skip_local_validation: If True, skip local validation and go straight
to Fiddler testing
Returns:
Dictionary with comprehensive validation results:
{
'valid': bool, # True only if both local and Fiddler tests pass
'local_validation': Dict | None,
'fiddler_test': Dict | None,
'recommendation': str
}
Example:
```python
result = validate_and_preview_metric(
model_id=model.id,
definition='sum(if(fp(), 1, 0))'
)
print(result['recommendation'])
if result['valid']:
# Safe to create real metric
metric = fdl.CustomMetric(
model_id=model.id,
name='false_positive_count',
definition='sum(if(fp(), 1, 0))'
)
metric.create()
```
"""
result = {
'valid': False,
'local_validation': None,
'fiddler_test': None,
'recommendation': '',
}
# Step 1: Local validation (if not skipped)
if not skip_local_validation:
logger.info("Step 1: Running local validation...")
model = fdl.Model.get(id_=model_id)
local_result = validate_metric_syntax_local(definition, model)
result['local_validation'] = local_result
if local_result['has_errors']:
result['recommendation'] = (
"❌ Local validation failed. Fix syntax errors before testing in Fiddler."
)
logger.warning("Local validation failed - skipping Fiddler test")
return result
if local_result['has_warnings']:
logger.info(
f"Local validation passed with {len(local_result['warnings'])} warnings"
)
else:
logger.info("Skipping local validation")
# Step 2: Test in Fiddler
logger.info("Step 2: Testing in Fiddler...")
fiddler_result = test_metric_definition(
model_id=model_id,
definition=definition,
cleanup=True
)
result['fiddler_test'] = fiddler_result
if fiddler_result['valid']:
result['valid'] = True
result['recommendation'] = (
"✅ Metric definition is valid! Safe to create the real metric."
)
logger.info("✓ Complete validation passed")
else:
result['recommendation'] = (
f"❌ Fiddler validation failed: {fiddler_result['error']}"
)
logger.warning(f"Fiddler validation failed: {fiddler_result['error']}")
return result
def batch_test_metrics(
model_id: str,
definitions: List[Dict[str, str]],
delay_between_tests: float = 0.5
) -> List[Dict[str, Any]]:
"""Test multiple metric definitions efficiently.
Args:
model_id: Model UUID to test against
definitions: List of dicts with 'name' and 'definition' keys
delay_between_tests: Delay in seconds between tests (default: 0.5)
Returns:
List of test results, one per definition
Example:
```python
definitions = [
{'name': 'FP Count', 'definition': 'sum(if(fp(), 1, 0))'},
{'name': 'FN Count', 'definition': 'sum(if(fn(), 1, 0))'},
{'name': 'Accuracy', 'definition': 'sum(if(tp() or tn(), 1, 0)) / sum(1)'},
]
results = batch_test_metrics(model.id, definitions)
valid_count = sum(1 for r in results if r['valid'])
print(f"{valid_count}/{len(results)} definitions are valid")
for r in results:
status = "✓" if r['valid'] else "✗"
print(f"{status} {r['name']}: {r.get('error', 'OK')}")
```
"""
logger.info(f"Batch testing {len(definitions)} metric definitions...")
results = []
for i, def_dict in enumerate(definitions, 1):
name = def_dict.get('name', f'Metric {i}')
definition = def_dict['definition']
logger.info(f"Testing {i}/{len(definitions)}: {name}")
result = test_metric_definition(
model_id=model_id,
definition=definition,
cleanup=True
)
result['name'] = name
results.append(result)
# Delay between tests to avoid rate limiting
if i < len(definitions) and delay_between_tests > 0:
time.sleep(delay_between_tests)
valid_count = sum(1 for r in results if r['valid'])
logger.info(
f"Batch testing complete: {valid_count}/{len(definitions)} valid"
)
return results
def cleanup_orphaned_test_metrics(model_id: str, prefix: str = "__test_") -> int:
"""Clean up any orphaned temporary test metrics.
If testing was interrupted or failed, temporary metrics may remain.
This utility removes them.
Args:
model_id: Model UUID to clean
prefix: Prefix used for temp metrics (default: '__test_')
Returns:
Number of metrics deleted
Example:
```python
deleted = cleanup_orphaned_test_metrics(model.id)
if deleted > 0:
print(f"Cleaned up {deleted} orphaned test metrics")
```
"""
logger.info(f"Searching for orphaned test metrics with prefix '{prefix}'...")
metrics = list(fdl.CustomMetric.list(model_id=model_id))
orphaned = [m for m in metrics if m.name.startswith(prefix)]
if not orphaned:
logger.info("No orphaned test metrics found")
return 0
logger.info(f"Found {len(orphaned)} orphaned test metrics - cleaning up...")
deleted_count = 0
for metric in orphaned:
try:
logger.debug(f"Deleting orphaned metric '{metric.name}' (ID: {metric.id})")
metric.delete()
deleted_count += 1
except Exception as e:
logger.warning(f"Failed to delete metric '{metric.name}': {e}")
logger.info(f"Cleanup complete: deleted {deleted_count}/{len(orphaned)} metrics")
return deleted_count