Created
April 10, 2021 10:21
-
-
Save edwardinubuntu/8acf3c2aea085732a8eccf8df46bba5a to your computer and use it in GitHub Desktop.
log to excel and update to AWS S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@action(MessageAction.FUND_FUSE_BOMBER_LOGS_TO_URL) | |
def fuse_bomber_logs_to_url(payload, msg_id): | |
app_id = payload['app_id'] | |
# Step 1. Get all bomber logs data | |
bomber_data = [] | |
for service in BomberLogService.values(): | |
json_ary = get_bomber_logs(app_id, service) | |
if json_ary: | |
bomber_data.extend(json_ary) | |
logging.debug('bomber_data {}'.format(str(bomber_data))) | |
# Step 2. Write into excel file | |
cur_env = env_detect().lower() | |
file_name = '{}-bomber-logs-{}.xlsx'.format(str(app_id), cur_env) | |
write_to_excel(file_name, {'data': bomber_data}) | |
# Step 3. Upload to AWS S3 | |
s3 = boto3.client('s3') | |
s3.upload_file(file_name, | |
'pnd-fuse-bomber-logs', | |
file_name) | |
client_method = 'get_object' | |
url = s3.generate_presigned_url( | |
client_method, | |
Params={ | |
'Bucket': 'pnd-fuse-bomber-logs', | |
'Key': file_name | |
}, | |
ExpiresIn=(60 * 60 * 24 * 365) | |
) | |
logging.info('Excel to S3 and get url: '.format(url)) | |
if not url: | |
logging.error('Update to url failed') | |
return | |
os.remove(file_name) | |
# Step 4. Save url into database | |
policy = (FusePolicy.get(FusePolicy.app_id == app_id)) | |
policy.bomber_logs_url = url | |
policy.save() | |
return bomber_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment