44from fastapi import FastAPI , HTTPException , Request
55from fastapi .staticfiles import StaticFiles
66from fastapi .responses import FileResponse
7+ from dotenv import load_dotenv
78
8- host = ""
9- http_path = ""
10- access_token = ""
9+ load_dotenv ()
10+
11+ host = os .getenv ("DATABRICKS_HOSTNAME" )
12+ http_path = os .getenv ("DATABRICKS_HTTP_PATH" )
13+ access_token = os .getenv ("DATABRICKS_ACCESS_TOKEN" )
14+ catalog = os .getenv ("DATABRICKS_CATALOG" )
15+ schema = os .getenv ("DATABRICKS_SCHEMA" )
1116
1217logging .basicConfig (level = logging .INFO , format = "%(asctime)s [%(levelname)s] %(message)s" )
1318logger = logging .getLogger (__name__ )
@@ -34,16 +39,19 @@ async def health_check():
3439async def get_classification ():
3540 connection = get_databricks_connection ()
3641 cursor = connection .cursor ()
37- cursor .execute ('SELECT resource_category, resource_type, total_count FROM cq_catalog.cloudquery.cloud_assets_counts' )
42+ classification_query = f'SELECT resource_category, resource_type, resource_type_label, total_count FROM { catalog } .{ schema } .cloud_assets_counts'
43+ logger .info (f'Executing classification query: { classification_query } ' )
44+ cursor .execute (classification_query )
3845 result = cursor .fetchall ()
3946 cursor .close ()
4047 connection .close ()
4148
4249 data = {}
4350 for row in result :
44- resource_category = row [0 ]
51+ resource_category = row [0 ] or "Unknown"
4552 resource_type = row [1 ]
46- total_count = row [2 ]
53+ resource_type_label = row [2 ] if row [2 ] else row [1 ]
54+ total_count = row [3 ]
4755
4856 if resource_category not in data :
4957 data [resource_category ] = {
@@ -55,7 +63,8 @@ async def get_classification():
5563 # Add the type to the category
5664 data [resource_category ]["types" ].append ({
5765 "resource_type" : resource_type ,
58- "total_count" : total_count
66+ "resource_type_label" : resource_type_label ,
67+ "total_count" : total_count ,
5968 })
6069
6170 data [resource_category ]["total_count" ] += total_count
@@ -97,6 +106,12 @@ def compose_where_clause(filter_array):
97106 conditions .append (f"`{ field } ` != '{ value } '" )
98107 elif operator == "contains" :
99108 conditions .append (f"`{ field } ` LIKE '%{ value } %'" )
109+ elif operator == "doesNotContain" :
110+ conditions .append (f"`{ field } ` NOT LIKE '%{ value } %'" )
111+ elif operator == "startsWith" :
112+ conditions .append (f"`{ field } ` LIKE '{ value } %'" )
113+ elif operator == "endsWith" :
114+ conditions .append (f"`{ field } ` LIKE '%{ value } '" )
100115 # Add more operators as needed
101116
102117 if conditions :
@@ -140,12 +155,14 @@ def compose_order_by_clause(sort_array):
140155 order_by_clause = ""
141156
142157 # Get total count first with filter
143- count_query = f'SELECT COUNT(*) as total FROM cq_catalog.cloudquery.cloud_assets { where_clause } '
158+ count_query = f'SELECT COUNT(*) as total FROM { catalog } .{ schema } .cloud_assets { where_clause } '
159+ logger .info (f'Executing count query: { count_query } ' )
144160 cursor .execute (count_query )
145161 total_count = cursor .fetchone ()[0 ]
146162
147163 # Get paginated data with filter and sorting
148- data_query = f'SELECT * FROM cq_catalog.cloudquery.cloud_assets { where_clause } { order_by_clause } LIMIT { page_size } OFFSET { offset } '
164+ data_query = f'SELECT * FROM { catalog } .{ schema } .cloud_assets { where_clause } { order_by_clause } LIMIT { page_size } OFFSET { offset } '
165+ logger .info (f'Executing data query: { data_query } ' )
149166 cursor .execute (data_query )
150167 result = cursor .fetchall ()
151168
0 commit comments