1515 sync_detailed as get_materialization_status ,
1616)
1717from ..models .materialize_request import MaterializeRequest
18+ from .operation_client import OperationClient , OperationProgress , MonitorOptions
1819
1920logger = logging .getLogger (__name__ )
2021
@@ -27,6 +28,7 @@ class MaterializationOptions:
2728 rebuild : bool = False
2829 force : bool = False
2930 on_progress : Optional [Callable [[str ], None ]] = None
31+ timeout : Optional [int ] = 600 # 10 minute default timeout
3032
3133
3234@dataclass
@@ -66,6 +68,14 @@ def __init__(self, config: Dict[str, Any]):
6668 self .base_url = config ["base_url" ]
6769 self .headers = config .get ("headers" , {})
6870 self .token = config .get ("token" )
71+ self ._operation_client = None
72+
73+ @property
74+ def operation_client (self ) -> OperationClient :
75+ """Get or create the operation client for SSE monitoring."""
76+ if self ._operation_client is None :
77+ self ._operation_client = OperationClient (self .config )
78+ return self ._operation_client
6979
7080 def materialize (
7181 self ,
@@ -75,13 +85,13 @@ def materialize(
7585 """
7686 Materialize graph from DuckDB staging tables.
7787
78- Rebuilds the complete graph database from the current state of DuckDB
79- staging tables. Automatically discovers all tables, materializes them in
80- the correct order (nodes before relationships), and clears the staleness flag .
88+ Submits a materialization job to Dagster and monitors progress via SSE.
89+ The operation runs asynchronously on the server but this method waits
90+ for completion and returns the final result .
8191
8292 Args:
8393 graph_id: Graph database identifier
84- options: Materialization options (ignore_errors, rebuild, force)
94+ options: Materialization options (ignore_errors, rebuild, force, timeout )
8595
8696 Returns:
8797 MaterializationResult with detailed execution information
@@ -96,7 +106,7 @@ def materialize(
96106
97107 try :
98108 if options .on_progress :
99- options .on_progress ("Starting graph materialization..." )
109+ options .on_progress ("Submitting materialization job ..." )
100110
101111 request = MaterializeRequest (
102112 ignore_errors = options .ignore_errors ,
@@ -125,6 +135,7 @@ def materialize(
125135
126136 response = materialize_graph (** kwargs )
127137
138+ # Handle non-200 status codes
128139 if response .status_code != 200 or not response .parsed :
129140 error_msg = f"Materialization failed: { response .status_code } "
130141 if hasattr (response , "content" ):
@@ -148,25 +159,68 @@ def materialize(
148159 error = error_msg ,
149160 )
150161
162+ # Get the operation_id from the queued response
151163 result_data = response .parsed
164+ operation_id = result_data .operation_id
152165
153166 if options .on_progress :
154- options .on_progress (
155- f"✅ Materialization complete: { len (result_data .tables_materialized )} tables, "
156- f"{ result_data .total_rows :,} rows in { result_data .execution_time_ms :.2f} ms"
157- )
158-
159- return MaterializationResult (
160- status = result_data .status ,
161- was_stale = result_data .was_stale ,
162- stale_reason = result_data .stale_reason ,
163- tables_materialized = result_data .tables_materialized ,
164- total_rows = result_data .total_rows ,
165- execution_time_ms = result_data .execution_time_ms ,
166- message = result_data .message ,
167- success = True ,
167+ options .on_progress (f"Materialization queued (operation: { operation_id } )" )
168+
169+ # Monitor the operation via SSE until completion
170+ def on_sse_progress (progress : OperationProgress ):
171+ if options .on_progress :
172+ msg = progress .message
173+ if progress .percentage is not None :
174+ msg += f" ({ progress .percentage :.0f} %)"
175+ options .on_progress (msg )
176+
177+ monitor_options = MonitorOptions (
178+ on_progress = on_sse_progress ,
179+ timeout = options .timeout ,
168180 )
169181
182+ op_result = self .operation_client .monitor_operation (operation_id , monitor_options )
183+
184+ # Convert operation result to materialization result
185+ if op_result .status .value == "completed" :
186+ # Extract details from SSE completion event result
187+ sse_result = op_result .result or {}
188+
189+ if options .on_progress :
190+ tables = sse_result .get ("tables_materialized" , [])
191+ rows = sse_result .get ("total_rows" , 0 )
192+ time_ms = sse_result .get ("execution_time_ms" , 0 )
193+ options .on_progress (
194+ f"✅ Materialization complete: { len (tables )} tables, "
195+ f"{ rows :,} rows in { time_ms :.2f} ms"
196+ )
197+
198+ return MaterializationResult (
199+ status = "success" ,
200+ was_stale = sse_result .get ("was_stale" , False ),
201+ stale_reason = sse_result .get ("stale_reason" ),
202+ tables_materialized = sse_result .get ("tables_materialized" , []),
203+ total_rows = sse_result .get ("total_rows" , 0 ),
204+ execution_time_ms = sse_result .get (
205+ "execution_time_ms" , op_result .execution_time_ms or 0
206+ ),
207+ message = sse_result .get ("message" , "Graph materialized successfully" ),
208+ success = True ,
209+ )
210+ else :
211+ # Operation failed or was cancelled
212+ return MaterializationResult (
213+ status = op_result .status .value ,
214+ was_stale = False ,
215+ stale_reason = None ,
216+ tables_materialized = [],
217+ total_rows = 0 ,
218+ execution_time_ms = op_result .execution_time_ms or 0 ,
219+ message = op_result .error or f"Operation { op_result .status .value } " ,
220+ success = False ,
221+ error = op_result .error ,
222+ )
223+
170224 except Exception as e :
171225 logger .error (f"Materialization failed: { e } " )
172226 return MaterializationResult (
0 commit comments