@@ -187,31 +187,34 @@ def tag_and_cost_category_fields(self) -> list:
187
187
number_of_rows_scanned = 100000 # empiric value
188
188
for tag_type in ["resource_tags" , 'cost_category' ]:
189
189
cid_print (f'Scanning { tag_type } in { self .table_name } .' )
190
- res = self .athena .query (
191
- sql = f'''
192
- SELECT
193
- key,
194
- COUNT(DISTINCT value) as unique_values
195
- FROM (
196
- SELECT { tag_type }
197
- FROM "{ self .database } "."{ self .table_name } "
198
- WHERE billing_period >= DATE_FORMAT(DATE_ADD('day', -60, CURRENT_DATE), '%Y-%m')
199
- AND line_item_usage_start_date > DATE_ADD('day', -60, CURRENT_DATE)
200
- AND cardinality({ tag_type } ) > 0
201
- LIMIT { number_of_rows_scanned }
202
- ) t
203
- CROSS JOIN UNNEST({ tag_type } ) AS t(key, value)
204
- GROUP BY key
205
- ORDER BY unique_values DESC;
206
- ''' ,
207
- database = self .database ,
208
- )
209
- max_width = max (len (str (line [0 ])) for line in res )
210
- cid_print (f' <BOLD>{ tag_type :<{max_width }} | Distinct Values <END> ' )
211
- for line in res :
212
- if int (line [1 ]) > 10 :
213
- cid_print (f' <BOLD>{ line [0 ]:<{max_width }} <END> | { line [1 ]} ' )
214
- self ._tag_and_cost_category += sorted ([f"{ tag_type } ['{ line [0 ]} ']" for line in res ])
190
+ try :
191
+ res = self .athena .query (
192
+ sql = f'''
193
+ SELECT
194
+ key,
195
+ COUNT(DISTINCT value) as unique_values
196
+ FROM (
197
+ SELECT { tag_type }
198
+ FROM "{ self .database } "."{ self .table_name } "
199
+ WHERE billing_period >= DATE_FORMAT(DATE_ADD('day', -60, CURRENT_DATE), '%Y-%m')
200
+ AND line_item_usage_start_date > DATE_ADD('day', -60, CURRENT_DATE)
201
+ AND cardinality({ tag_type } ) > 0
202
+ LIMIT { number_of_rows_scanned }
203
+ ) t
204
+ CROSS JOIN UNNEST({ tag_type } ) AS t(key, value)
205
+ GROUP BY key
206
+ ORDER BY unique_values DESC;
207
+ ''' ,
208
+ database = self .database ,
209
+ )
210
+ max_width = max (len (str (line [0 ])) for line in res )
211
+ cid_print (f' <BOLD>{ tag_type :<{max_width }} | Distinct Values <END> ' )
212
+ for line in res :
213
+ if int (line [1 ]) > 10 :
214
+ cid_print (f' <BOLD>{ line [0 ]:<{max_width }} <END> | { line [1 ]} ' )
215
+ self ._tag_and_cost_category += sorted ([f"{ tag_type } ['{ line [0 ]} ']" for line in res ])
216
+ except self .athena .client .exceptions .ClientError as exc :
217
+ logger .error (f'Failed to read { tag_type } from { self .table_name } : { exc } . Will continue without.' )
215
218
return self ._tag_and_cost_category
216
219
else :
217
220
raise NotImplemented ('cur version not known' )
0 commit comments