refector: optimized concurrent.futures
This commit is contained in:
+9
-12
@@ -16,13 +16,12 @@ def getRegions(all_regions=False):
|
||||
response = ec2.describe_regions(AllRegions=all_regions)
|
||||
return [region['RegionName'] for region in response['Regions']]
|
||||
|
||||
def printResources(region_name: str) -> list[list[str | int]]:
|
||||
def getResources(region_name: str) -> list[list[str | int]]:
|
||||
return_data = []
|
||||
client = boto3.client('logs', region_name=region_name)
|
||||
response = client.describe_log_groups()
|
||||
for logGroup in response['logGroups']:
|
||||
if logGroup:
|
||||
return_data.append([logGroup['logGroupName'], logGroup.get('retentionInDays'), logGroup['storedBytes'], region_name])
|
||||
return_data.append([logGroup['logGroupName'], logGroup.get('retentionInDays'), logGroup['storedBytes'], region_name])
|
||||
return return_data
|
||||
|
||||
# Main function
|
||||
@@ -31,16 +30,14 @@ def main() -> None:
|
||||
wb = load_workbook('aws-inventory.xlsx')
|
||||
ws = wb.create_sheet("CloudwatchLogs")
|
||||
|
||||
final_data = []
|
||||
ws.append(["logGroupName","retentionInDays","storedBytes","region"])
|
||||
with concurrent.futures.ProcessPoolExecutor(max_workers=6) as executor:
|
||||
futures = [executor.submit(printResources, region_name=r) for r in getRegions()]
|
||||
for future in concurrent.futures.as_completed(futures):
|
||||
region_data = future.result()
|
||||
final_data.extend(region_data)
|
||||
|
||||
for row in final_data:
|
||||
ws.append(row)
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
|
||||
results = executor.map(getResources, getRegions())
|
||||
for region_rows in results:
|
||||
# append to worksheet only if resoruces are found in the region
|
||||
if region_rows:
|
||||
for row in region_rows:
|
||||
ws.append(row)
|
||||
wb.save('aws-inventory.xlsx')
|
||||
|
||||
# Call main function
|
||||
|
||||
Reference in New Issue
Block a user