-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
247 lines (216 loc) · 9.14 KB
/
__init__.py
File metadata and controls
247 lines (216 loc) · 9.14 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
"""Export module for GoodData metadata.
This module provides functions for exporting GoodData workspace metadata
to SQLite databases and CSV files.
Main entry point:
export_all_metadata - Orchestrates the full export process
"""
import logging
import os
import shutil
import sqlite3
from pathlib import Path
from gooddata_export.common import ExportError
from gooddata_export.db import store_workspace_metadata
from gooddata_export.export.fetch import fetch_all_workspace_data
from gooddata_export.export.writers import (
export_dashboards,
export_dashboards_metrics,
export_dashboards_permissions,
export_filter_contexts,
export_ldm,
export_metrics,
export_plugins,
export_users_and_user_groups,
export_visualizations,
export_workspaces,
)
from gooddata_export.post_export import run_post_export_sql
from gooddata_export.process import validate_workspace_exists
logger = logging.getLogger(__name__)
def export_all_metadata(
config,
csv_dir=None,
db_path="output/db/gooddata_export.db",
export_formats=None,
run_post_export=True,
layout_json: dict | None = None,
):
"""Export all metadata to SQLite database and CSV files.
Args:
config: ExportConfig instance with GoodData credentials and options
csv_dir: Directory for CSV files (default: None, uses "output/metadata_csv" if csv in formats)
db_path: Path to SQLite database file (default: "output/db/gooddata_export.db")
export_formats: List of formats to export ("sqlite", "csv") (default: both)
run_post_export: Whether to run post-export SQL processing (default: True)
layout_json: Optional local layout JSON data. When provided, skips API fetch
and uses this data directly. Expected format:
{"analytics": {"metrics": [...], ...}, "ldm": {"datasets": [...], ...}}
Returns:
dict: Export results with db_path, csv_dir, and workspace_count
"""
if export_formats is None:
export_formats = ["sqlite", "csv"]
# Set up CSV directory
if "csv" in export_formats and csv_dir is None:
csv_dir = "output/metadata_csv"
export_dir = csv_dir if "csv" in export_formats else None
# Set up database path
db_path_obj = Path(db_path)
errors = []
# Ensure database directory exists (databases overwrite themselves, no cleanup needed)
db_path_obj.parent.mkdir(parents=True, exist_ok=True)
# Clean CSV directory completely to avoid stale data (files mix together, so we need a clean slate)
if export_dir and Path(export_dir).exists():
logger.debug("Cleaning CSV directory: %s", export_dir)
shutil.rmtree(export_dir)
# Ensure CSV directory exists if needed
if export_dir:
Path(export_dir).mkdir(parents=True, exist_ok=True)
if layout_json is not None:
# Local mode: use layout_json directly (already in layout format)
logger.info("Using local layout JSON for workspace %s", config.WORKSPACE_ID)
# Validate layout_json structure and warn about missing keys
if "analytics" not in layout_json:
logger.warning(
"layout_json missing 'analytics' key - no analytics data will be exported"
)
if "ldm" not in layout_json:
logger.warning(
"layout_json missing 'ldm' key - no LDM data will be exported"
)
analytics = layout_json.get("analytics", {})
ldm = layout_json.get("ldm", {})
all_workspace_data = [
{
"workspace_id": config.WORKSPACE_ID,
"workspace_name": f"Local Layout ({config.WORKSPACE_ID})",
"is_parent": True,
"data": {
"metrics": analytics.get("metrics") or [],
"dashboards": analytics.get("analyticalDashboards") or [],
"visualizations": analytics.get("visualizationObjects") or [],
"filter_contexts": analytics.get("filterContexts") or [],
"plugins": analytics.get("dashboardPlugins") or [],
# Not available in local mode (require separate API calls):
"child_workspaces": None,
"users_and_user_groups": None,
# LDM and analytics_model come from layout.json directly:
"ldm": {"ldm": ldm} if ldm else None,
"analytics_model": layout_json,
},
}
]
else:
# API mode: validate workspace and fetch from GoodData API
logger.debug("")
logger.debug("=" * 70)
logger.debug("FETCH PHASE")
logger.debug("=" * 70)
logger.debug("Validating workspace access...")
validate_workspace_exists(config=config)
logger.debug("Fetching data from GoodData API...")
all_workspace_data = fetch_all_workspace_data(config)
logger.debug(
"Successfully fetched data from %d workspace(s)", len(all_workspace_data)
)
for ws in all_workspace_data:
logger.debug(" - %s (%s)", ws["workspace_name"], ws["workspace_id"])
# Export functions to run sequentially (workspaces first for reference)
export_functions = [
export_workspaces,
export_metrics,
export_visualizations,
export_dashboards,
export_dashboards_metrics,
export_dashboards_permissions,
export_plugins,
export_ldm,
export_filter_contexts,
export_users_and_user_groups,
]
# Execute each export function with all workspace data
# Note: Database writes are kept sequential to avoid SQLite concurrency issues
logger.debug("")
logger.debug("=" * 70)
logger.debug("EXPORT PHASE")
logger.debug("=" * 70)
logger.debug("Processing and writing data to database...")
for export_func in export_functions:
try:
export_func(all_workspace_data, export_dir, config, db_path)
except Exception as e:
# Log full exception with traceback for debugging
logger.exception("Error in %s", export_func.__name__)
# Store truncated message for user-facing error summary
error_msg = str(e).split("\n")[0]
errors.append(f"{export_func.__name__}: {error_msg}")
if errors:
# Raise detailed error messages
workspace_id = config.WORKSPACE_ID
error_details = "\n - ".join(errors)
raise ExportError(
f"Export failed for workspace: {workspace_id}\n"
f"Errors encountered:\n - {error_details}"
)
# Run post-export processing if requested
# When child workspaces are included, filter enrichment to parent workspace only
post_export_error = None
if run_post_export:
try:
if config.INCLUDE_CHILD_WORKSPACES:
# Multi-workspace: enrich only parent workspace to avoid confusing duplicates
logger.debug("Multi-workspace mode: enriching parent workspace only")
run_post_export_sql(
db_path,
parent_workspace_id=config.WORKSPACE_ID,
config=config,
)
else:
# Single workspace: enrich all data (no filter needed)
run_post_export_sql(db_path, config=config)
except ExportError as e:
# Capture error but continue (database still usable without enrichment)
post_export_error = str(e)
else:
logger.debug("Skipping post-export processing (disabled)")
# Store workspace metadata
workspace_id = config.WORKSPACE_ID
export_mode = "local" if layout_json is not None else "api"
store_workspace_metadata(db_path, config, export_mode=export_mode)
# Vacuum database to reclaim space (especially after content exclusion or truncation)
try:
conn = sqlite3.connect(db_path)
conn.execute("VACUUM")
conn.close()
final_size = os.path.getsize(db_path) / 1024 / 1024
logger.debug("Database vacuumed (final size: %.1f MB)", final_size)
except Exception as e:
logger.warning("Could not vacuum database: %s", e)
# Create workspace-specific database copy
workspace_db = None
try:
workspace_db = db_path_obj.parent / f"{workspace_id}.db"
# Create a copy of the database with workspace_id name
shutil.copy(db_path, workspace_db)
logger.debug("Created workspace-specific database: %s", workspace_db)
except Exception as e:
logger.warning("Could not create workspace-specific database: %s", e)
# Success message
total_workspaces = len(all_workspace_data)
if total_workspaces > 1:
logger.debug(
"Successfully processed %d workspaces (%d child workspaces)",
total_workspaces,
total_workspaces - 1,
)
else:
logger.debug("Successfully processed parent workspace")
return {
"db_path": db_path,
"workspace_db_path": workspace_db,
"csv_dir": export_dir,
"workspace_count": total_workspaces,
"workspace_id": workspace_id,
"post_export_error": post_export_error,
}
__all__ = ["export_all_metadata"]