99DATABASE_PARENT_ID = os .environ ['DATABASE_PARENT_ID' ]
1010DATABASE_NAME = os .environ ['DATABASE_NAME' ]
1111NOTION_TOKEN = os .environ ['NOTION_TOKEN' ]
12+ NUMERIC_ZERO_VALUE = - 1
1213
1314
1415def make_request (endpoint , querystring = '' , method = 'GET' , ** request_kwargs ):
@@ -29,6 +30,49 @@ def make_request(endpoint, querystring='', method='GET', **request_kwargs):
2930
3031 return resp .json ()
3132
33+
34+ def get_path_or_empty (parent_object , path_array , zero_value = '' ):
35+ obj = parent_object
36+ for el in path_array :
37+ if el not in obj :
38+ return zero_value
39+ obj = obj [el ]
40+
41+ return obj
42+
43+
44+ def get_num_rows (catalog_nodes , model_name ):
45+ zero_value = NUMERIC_ZERO_VALUE
46+ keys = ['num_rows' , 'row_count' ]
47+ for key in keys :
48+ num_rows = get_path_or_empty (catalog_nodes , [model_name , 'stats' , key , 'value' ], NUMERIC_ZERO_VALUE )
49+ if num_rows != NUMERIC_ZERO_VALUE :
50+ return num_rows
51+
52+ return NUMERIC_ZERO_VALUE
53+
54+
55+ def get_bytes (catalog_nodes , model_name ):
56+ zero_value = NUMERIC_ZERO_VALUE
57+ keys = ['num_bytes' , 'bytes' ]
58+ for key in keys :
59+ num_rows = get_path_or_empty (catalog_nodes , [model_name , 'stats' , key , 'value' ], NUMERIC_ZERO_VALUE )
60+ if num_rows != NUMERIC_ZERO_VALUE :
61+ return num_rows
62+
63+ return NUMERIC_ZERO_VALUE
64+
65+
66+ def get_owner (data , catalog_nodes , model_name ):
67+ # Check for an owner field explicitly named in the DBT Config
68+ # If none present, fall back to database table owner
69+ owner = get_path_or_empty (data , ['config' , 'meta' , 'owner' ], None )
70+ if owner != None :
71+ return owner
72+
73+ return get_path_or_empty (catalog_nodes , [model_name , 'metadata' , 'owner' ], '' )
74+
75+
3276def main ():
3377 model_records_to_write = sys .argv [1 :] # 'all' or list of model names
3478 print (f'Model records to write: { model_records_to_write } ' )
@@ -163,7 +207,7 @@ def main():
163207 }
164208 }
165209 ]
166- col_names_and_data = list (catalog_nodes [model_name ][ 'columns' ].items ())
210+ col_names_and_data = list (get_path_or_empty ( catalog_nodes , [model_name , 'columns' ], {}) .items ())
167211 for (col_name , col_data ) in col_names_and_data [:98 ]: # notion api limit is 100 table rows
168212 columns_table_children_obj .append (
169213 {
@@ -367,7 +411,7 @@ def main():
367411 {
368412 "text" : {
369413 "content" : str (
370- catalog_nodes [ model_name ][ 'metadata' ][ 'owner' ]
414+ get_owner ( data , catalog_nodes , model_name )
371415 )[:2000 ]
372416 }
373417 }
@@ -383,10 +427,10 @@ def main():
383427 ]
384428 },
385429 "Approx Rows" : {
386- "number" : catalog_nodes [ model_name ][ 'stats' ][ 'num_rows' ][ 'value' ]
430+ "number" : get_num_rows ( catalog_nodes , model_name )
387431 },
388432 "Approx GB" : {
389- "number" : catalog_nodes [ model_name ][ 'stats' ][ 'num_bytes' ][ 'value' ] / 1e9
433+ "number" :get_bytes ( catalog_nodes , model_name ) / 1e9
390434 },
391435 "Depends On" : {
392436 "rich_text" : [
0 commit comments