9
9
import json
10
10
import time
11
11
from contextlib import closing
12
+ from collections import defaultdict
12
13
13
14
import pymysql
14
15
@@ -209,6 +210,7 @@ def _collect_databases_data(self, tags):
209
210
- index_type (str): The index method used.
210
211
- columns (list): A list of column dictionaries
211
212
- column (dict): A dictionary representing a column.
213
+ - name (str): The name of the column.
212
214
- sub_part (int): The number of indexed characters if column is partially indexed.
213
215
- packed (str): How the index is packed.
214
216
- nullable (bool): Whether the column is nullable.
@@ -340,6 +342,8 @@ def _populate_with_index_data(self, table_name_to_table_index, table_list, table
340
342
self ._cursor_run (cursor , query = SQL_INDEXES .format (table_names ), params = db_name )
341
343
rows = cursor .fetchall ()
342
344
for row in rows :
345
+ print ("ALLEN!!!" )
346
+ print ("row:" , json .dumps (row , indent = 4 ))
343
347
table_name = str (row .pop ("table_name" ))
344
348
table_list [table_name_to_table_index [table_name ]].setdefault ("indexes" , [])
345
349
if "nullables" in row :
@@ -351,7 +355,6 @@ def _populate_with_index_data(self, table_name_to_table_index, table_list, table
351
355
else :
352
356
nullables_converted += "false,"
353
357
row ["nullables" ] = nullables_converted [:- 1 ]
354
- # TODO ALLEN: make this booleans rather than strings
355
358
table_list [table_name_to_table_index [table_name ]]["indexes" ].append (row )
356
359
357
360
@tracked_method (agent_check_getter = agent_check_getter , track_result_length = True )
@@ -367,7 +370,46 @@ def _populate_with_foreign_keys_data(self, table_name_to_table_index, table_list
367
370
def _populate_with_partitions_data (self , table_name_to_table_index , table_list , table_names , db_name , cursor ):
368
371
self ._cursor_run (cursor , query = SQL_PARTITION .format (table_names ), params = db_name )
369
372
rows = cursor .fetchall ()
373
+ if not rows :
374
+ return
375
+ table_partitions_dict = defaultdict (lambda : defaultdict (lambda : {
376
+ "name" : None ,
377
+ "subpartitions" : [],
378
+ "partition_ordinal_position" : None ,
379
+ "partition_method" : None ,
380
+ "partition_expression" : None ,
381
+ "partition_description" : None ,
382
+ "table_rows" : 0 ,
383
+ "data_length" : 0 ,
384
+ }))
385
+
370
386
for row in rows :
371
- table_name = str (row . pop ( "table_name" ) )
387
+ table_name = str (row [ "table_name" ] )
372
388
table_list [table_name_to_table_index [table_name ]].setdefault ("partitions" , [])
373
- table_list [table_name_to_table_index [table_name ]]["partitions" ].append (row )
389
+ partition_name = str (row ["name" ])
390
+ partition_data = table_partitions_dict [table_name ][partition_name ]
391
+
392
+ # Update partition-level info
393
+ partition_data ["name" ] = partition_name
394
+ partition_data ["partition_ordinal_position" ] = int (row ["partition_ordinal_position" ])
395
+ partition_data ["partition_method" ] = str (row ["partition_method" ])
396
+ partition_data ["partition_expression" ] = str (row ["partition_expression" ]).strip ().lower ()
397
+ partition_data ["partition_description" ] = str (row ["partition_description" ])
398
+ partition_data ["table_rows" ] += int (row ["table_rows" ])
399
+ partition_data ["data_length" ] += int (row ["data_length" ])
400
+
401
+ # Add subpartition info, if exists
402
+ if row ["subpartition_name" ]:
403
+ subpartition = {
404
+ "name" : row ["subpartition_name" ],
405
+ "subpartition_ordinal_position" : int (row ["subpartition_ordinal_position" ]),
406
+ "subpartition_method" : str (row ["subpartition_method" ]),
407
+ "subpartition_expression" : str (row ["subpartition_expression" ]).strip ().lower (),
408
+ "table_rows" : int (row ["table_rows" ]),
409
+ "data_length" : int (row ["data_length" ]),
410
+ }
411
+ partition_data ["subpartitions" ].append (subpartition )
412
+ for table_name , partitions_dict in table_partitions_dict .items ():
413
+ table_list [table_name_to_table_index [table_name ]]["partitions" ] = list (partitions_dict .values ())
414
+
415
+ print (json .dumps (partition_data , indent = 4 ))
0 commit comments