-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchart_operations.py
More file actions
376 lines (270 loc) · 12 KB
/
chart_operations.py
File metadata and controls
376 lines (270 loc) · 12 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
"""Example: Working with Fiddler charts using ChartManager.
This script demonstrates how to use ChartManager to:
1. List all charts in a project
2. Export charts for backup/migration
3. Import charts to another project/model
4. Analyze chart usage
5. Delete old charts
IMPORTANT: The chart API is unofficial and may change without notice.
"""
import fiddler as fdl
from fiddler_utils import ChartManager
# ============================================================================
# Configuration
# ============================================================================
# Source instance
SOURCE_URL = 'https://source.fiddler.ai'
SOURCE_TOKEN = 'your_source_token'
SOURCE_PROJECT = 'my_project'
SOURCE_MODEL = 'my_model'
# Target instance (can be same as source)
TARGET_URL = 'https://target.fiddler.ai'
TARGET_TOKEN = 'your_target_token'
TARGET_PROJECT = 'my_project_copy'
TARGET_MODEL = 'my_model_copy'
# ============================================================================
# Example 1: List and Analyze Charts
# ============================================================================
def list_and_analyze_charts():
"""List all charts and get analytics."""
print('=' * 70)
print('EXAMPLE 1: List and Analyze Charts')
print('=' * 70)
# Suppress verbose Fiddler client logs (recommended)
# Initialize chart manager
# IMPORTANT: ChartManager requires explicit URL and token
chart_mgr = ChartManager(url=SOURCE_URL, token=SOURCE_TOKEN)
# Also initialize fiddler client for model access
fdl.init(url=SOURCE_URL, token=SOURCE_TOKEN)
project = fdl.Project.from_name(SOURCE_PROJECT)
model = fdl.Model.from_name(SOURCE_MODEL, project_id=project.id)
# List all charts in project
all_charts = chart_mgr.list_charts(project_id=project.id)
print(f'\nFound {len(all_charts)} total charts in project')
for chart in all_charts[:5]: # Show first 5
print(f'\n Chart: {chart["title"]}')
print(f' Type: {chart.get("query_type")}')
print(f' ID: {chart.get("id")}')
# Filter charts by model
model_charts = chart_mgr.list_charts(project_id=project.id, model_id=model.id)
print(f"\n{len(model_charts)} charts reference model '{model.name}'")
# Analyze chart usage
analysis = chart_mgr.analyze_charts(project_id=project.id)
print(f'\nChart Analysis:')
print(f' Total charts: {analysis["total"]}')
print(f' By type: {analysis["by_type"]}')
print(f' Unique models: {analysis["unique_models"]}')
print('\n' + '=' * 70)
# ============================================================================
# Example 2: Export Charts for Backup
# ============================================================================
def export_charts_for_backup():
"""Export all charts from a project."""
print('\n' + '=' * 70)
print('EXAMPLE 2: Export Charts for Backup')
print('=' * 70)
chart_mgr = ChartManager(url=SOURCE_URL, token=SOURCE_TOKEN)
fdl.init(url=SOURCE_URL, token=SOURCE_TOKEN)
project = fdl.Project.from_name(SOURCE_PROJECT)
model = fdl.Model.from_name(SOURCE_MODEL, project_id=project.id)
# Export all charts for a model
exported_charts = chart_mgr.export_charts(project_id=project.id, model_id=model.id)
print(f'\nExported {len(exported_charts)} charts')
for chart in exported_charts:
print(f'\n {chart["title"]}')
print(f' Type: {chart.get("query_type")}')
print(f' Description: {chart.get("description", "N/A")[:50]}')
# Could save to file for backup
# import json
# with open('charts_backup.json', 'w') as f:
# json.dump(exported_charts, f, indent=2)
print('\n' + '=' * 70)
return exported_charts
# ============================================================================
# Example 3: Import Charts to Another Model
# ============================================================================
def import_charts_to_target(exported_charts):
"""Import charts to a target model."""
print('\n' + '=' * 70)
print('EXAMPLE 3: Import Charts to Target Model')
print('=' * 70)
# Initialize chart manager for target
chart_mgr = ChartManager(url=TARGET_URL, token=TARGET_TOKEN)
# Initialize fiddler client for target
fdl.init(url=TARGET_URL, token=TARGET_TOKEN)
target_project = fdl.Project.from_name(TARGET_PROJECT)
target_model = fdl.Model.from_name(TARGET_MODEL, project_id=target_project.id)
print(f'\nTarget: {target_model.name} in project {target_project.name}')
# Dry run first to check what would happen
print('\nRunning dry run...')
dry_result = chart_mgr.import_charts(
target_project_id=target_project.id
target_model_id=target_model.id
charts=exported_charts
dry_run=True
)
print(f' Would import: {dry_result["successful"]} charts')
print(f' Would fail: {dry_result["failed"]} charts')
if dry_result['failed'] > 0:
print(f'\n Errors:')
for title, error in dry_result['errors'][:3]:
print(f' - {title}: {error}')
# Ask for confirmation
proceed = input('\nProceed with actual import? (y/n): ').lower().strip()
if proceed == 'y':
print('\nImporting charts...')
result = chart_mgr.import_charts(
target_project_id=target_project.id
target_model_id=target_model.id
charts=exported_charts
dry_run=False
)
print(f'\nImport complete:')
print(f' ✓ Successful: {result["successful"]}')
print(f' ✗ Failed: {result["failed"]}')
if result['failed'] > 0:
print(f'\n Errors:')
for title, error in result['errors']:
print(f' - {title}: {error}')
else:
print('\nImport cancelled.')
print('\n' + '=' * 70)
# ============================================================================
# Example 4: Find and Delete Specific Charts
# ============================================================================
def find_and_delete_old_charts():
"""Find charts by pattern and optionally delete them."""
print('\n' + '=' * 70)
print('EXAMPLE 4: Find and Delete Old Charts')
print('=' * 70)
chart_mgr = ChartManager(url=SOURCE_URL, token=SOURCE_TOKEN)
fdl.init(url=SOURCE_URL, token=SOURCE_TOKEN)
project = fdl.Project.from_name(SOURCE_PROJECT)
# List all charts
all_charts = chart_mgr.list_charts(project_id=project.id)
# Find charts with specific pattern in title
pattern = 'test' # Find charts with "test" in title
matching_charts = [
c for c in all_charts if pattern.lower() in c.get('title', '').lower()
]
print(f"\nFound {len(matching_charts)} charts matching pattern '{pattern}'")
if not matching_charts:
print('No charts to delete.')
print('\n' + '=' * 70)
return
for chart in matching_charts:
print(f'\n - {chart["title"]} (ID: {chart["id"]})')
# Ask for confirmation
proceed = (
input(f'\nDelete these {len(matching_charts)} charts? (y/n): ').lower().strip()
)
if proceed == 'y':
deleted_count = 0
for chart in matching_charts:
try:
chart_mgr.delete_chart(chart['id'])
print(f' ✓ Deleted: {chart["title"]}')
deleted_count += 1
except Exception as e:
print(f' ✗ Failed to delete {chart["title"]}: {e}')
print(f'\nDeleted {deleted_count} out of {len(matching_charts)} charts')
else:
print('\nDeletion cancelled.')
print('\n' + '=' * 70)
# ============================================================================
# Example 5: Get Specific Chart and Inspect
# ============================================================================
def inspect_specific_chart():
"""Get and inspect a specific chart's configuration."""
print('\n' + '=' * 70)
print('EXAMPLE 5: Inspect Specific Chart')
print('=' * 70)
chart_mgr = ChartManager(url=SOURCE_URL, token=SOURCE_TOKEN)
fdl.init(url=SOURCE_URL, token=SOURCE_TOKEN)
project = fdl.Project.from_name(SOURCE_PROJECT)
# Get chart by title
chart_title = 'Performance Over Time' # Replace with actual chart title
try:
chart = chart_mgr.get_chart_by_title(project_id=project.id, title=chart_title)
print(f'\nChart: {chart["title"]}')
print(f' ID: {chart["id"]}')
print(f' Type: {chart.get("query_type")}')
print(f' Description: {chart.get("description", "N/A")}')
# Inspect data source
data_source = chart.get('data_source', {})
print(f'\n Data Source:')
print(f' Query Type: {data_source.get("query_type")}')
if data_source.get('query_type') == 'MONITORING':
queries = data_source.get('queries', [])
print(f' Queries: {len(queries)}')
for i, query in enumerate(queries[:3]): # Show first 3
print(f'\n Query {i + 1}:')
print(f' Metric: {query.get("metric")}')
print(f' Metric Type: {query.get("metric_type")}')
print(f' Viz Type: {query.get("viz_type")}')
if query.get('columns'):
print(f' Columns: {query.get("columns")}')
except Exception as e:
print(f"\nChart '{chart_title}' not found: {e}")
print('\n' + '=' * 70)
# ============================================================================
# Example 6: Copy Charts Between Models (Same Instance)
# ============================================================================
def copy_charts_between_models():
"""Copy charts from one model to another on the same instance."""
print('\n' + '=' * 70)
print('EXAMPLE 6: Copy Charts Between Models')
print('=' * 70)
chart_mgr = ChartManager(url=SOURCE_URL, token=SOURCE_TOKEN)
fdl.init(url=SOURCE_URL, token=SOURCE_TOKEN)
project = fdl.Project.from_name(SOURCE_PROJECT)
source_model = fdl.Model.from_name(SOURCE_MODEL, project_id=project.id)
target_model = fdl.Model.from_name(TARGET_MODEL, project_id=project.id)
print(f"Copying charts from '{source_model.name}' to '{target_model.name}'")
# Export from source model
exported = chart_mgr.export_charts(project_id=project.id, model_id=source_model.id)
print(f'\nExported {len(exported)} charts')
# Import to target model
result = chart_mgr.import_charts(
target_project_id=project.id
target_model_id=target_model.id
charts=exported
dry_run=False
)
print(f'\nCopy complete:')
print(f' ✓ Successful: {result["successful"]}')
print(f' ✗ Failed: {result["failed"]}')
print('\n' + '=' * 70)
# ============================================================================
# Main
# ============================================================================
def main():
"""Run all examples."""
# Suppress verbose logs for cleaner output (recommended)
print('\n')
print('╔' + '=' * 68 + '╗')
print('║' + ' ' * 20 + 'CHART MANAGER EXAMPLES' + ' ' * 26 + '║')
print('╚' + '=' * 68 + '╝')
# Example 1: List and analyze
list_and_analyze_charts()
# Example 2: Export for backup
exported_charts = export_charts_for_backup()
# Example 3: Import to target
# import_charts_to_target(exported_charts)
# Example 4: Find and delete
# find_and_delete_old_charts()
# Example 5: Inspect specific chart
# inspect_specific_chart()
# Example 6: Copy between models
# copy_charts_between_models()
print('\n' + '=' * 70)
print('IMPORTANT NOTES:')
print('=' * 70)
print('* Chart API is unofficial - use with caution')
print('* ChartManager requires explicit URL and token')
print('* ID resolution (baselines, metrics, segments) may fail if')
print(" assets don't exist in target with same names")
print('* Test with dry_run=True before actual imports')
print('=' * 70)
if __name__ == '__main__':
main()