11import json
22import logging
33import os
4+ import time
45import rich
56import yaml
67from dataclasses import dataclass
1112from dbt_artifacts_parser .parser import parse_run_results , parse_manifest
1213from dbt .config .renderer import ProfileRenderer
1314
15+ from .tracking import set_entrypoint_name , create_end_event_json , create_start_event_json , send_event_json , is_tracking_enabled
16+ from .utils import run_as_daemon , truncate_error
1417from . import connect_to_table , diff_tables , Algorithm
1518
1619RUN_RESULTS_PATH = "/target/run_results.json"
2023LOWER_DBT_V = "1.0.0"
2124UPPER_DBT_V = "1.5.0"
2225
26+ set_entrypoint_name ("CLI-dbt" )
2327
2428@dataclass
2529class DiffVars :
@@ -190,22 +194,52 @@ def _cloud_diff(diff_vars: DiffVars) -> None:
190194 "Authorization" : f"Key { api_key } " ,
191195 "Content-Type" : "application/json" ,
192196 }
197+ if is_tracking_enabled ():
198+ event_json = create_start_event_json ({"is_cloud" : True , "datasource_id" : diff_vars .datasource_id })
199+ run_as_daemon (send_event_json , event_json )
193200
194- response = requests .request ("POST" , url , headers = headers , json = payload , timeout = 30 )
195- response .raise_for_status ()
196- data = response .json ()
197- diff_id = data ["id" ]
198- # TODO in future we should support self hosted datafold
199- diff_url = f"https://app.datafold.com/datadiffs/{ diff_id } /overview"
200- rich .print (
201- "[red]"
202- + "." .join (diff_vars .dev_path )
203- + " <> "
204- + "." .join (diff_vars .prod_path )
205- + "[/] \n Diff in progress: \n "
206- + diff_url
207- + "\n "
208- )
201+ start = time .monotonic ()
202+ error = None
203+ try :
204+ response = requests .request ("POST" , url , headers = headers , json = payload , timeout = 30 )
205+ response .raise_for_status ()
206+ data = response .json ()
207+ diff_id = data ["id" ]
208+ # TODO in future we should support self hosted datafold
209+ diff_url = f"https://app.datafold.com/datadiffs/{ diff_id } /overview"
210+ rich .print (
211+ "[red]"
212+ + "." .join (diff_vars .dev_path )
213+ + " <> "
214+ + "." .join (diff_vars .prod_path )
215+ + "[/] \n Diff in progress: \n "
216+ + diff_url
217+ + "\n "
218+ )
219+ except BaseException as ex : # Catch KeyboardInterrupt too
220+ error = ex
221+ finally :
222+ # we don't currently have much of this information
223+ # but I imagine a future iteration of this _cloud method
224+ # will poll for results
225+ if is_tracking_enabled ():
226+ err_message = truncate_error (repr (error ))
227+ event_json = create_end_event_json (
228+ is_success = error is None ,
229+ runtime_seconds = time .monotonic () - start ,
230+ data_source_1_type = "" ,
231+ data_source_2_type = "" ,
232+ table1_count = 0 ,
233+ table2_count = 0 ,
234+ diff_count = 0 ,
235+ error = err_message ,
236+ diff_id = diff_id ,
237+ is_cloud = True
238+ )
239+ send_event_json (event_json )
240+
241+ if error :
242+ raise error
209243
210244
211245class DbtParser :
@@ -230,7 +264,6 @@ def get_models(self):
230264
231265 dbt_version = parse_version (run_results_obj .metadata .dbt_version )
232266
233- # TODO 1.4 support
234267 if dbt_version < parse_version (LOWER_DBT_V ) or dbt_version >= parse_version (UPPER_DBT_V ):
235268 raise Exception (
236269 f"Found dbt: v{ dbt_version } Expected the dbt project's version to be >= { LOWER_DBT_V } and < { UPPER_DBT_V } "
0 commit comments