Home > front end >  How to find the duration time from json logs
How to find the duration time from json logs

Time:03-30

I have below json logs which is based on the DynamoDB query

{
    "message": "Dynamo response:::, {'Attributes': {'Status': {'S': 'COMPLETED'}, 'updateDateTime': {'S': '2022-03-25 15:59:09'}, 'EndTime': {'S': '2022-03-25 15:57:09'}, 'warning': {'S': 'Some Process'}}, 'ResponseMetadata': {'RequestId': 'P0IQDDEI9KTG5QU6HR0P688MARVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Fri, 25 Mar 2022 15:57:09 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '176', 'connection': 'keep-alive', 'x-amzn-requestid': 'P0IQDDEI9KTG5QU6HR0P688MARVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '923138425'}, 'RetryAttempts': 0}}

From this we have updateDateTime and EndTime I want to print the total time taken to update i.e., updateDateTime - EndTime. Here S denotes string. I am a beginner in python

CodePudding user response:

import json
from datetime import datetime
json_resp = json.loads(S)
updateDateTime = datetime.strptime(json_resp['updateDateTime'], '%Y-%m-%d %H:%M:%S')
endTime = datetime.strptime(json_resp['EndTime'], '%Y-%m-%d %H:%M:%S')
time_taken = updateDateTime - endTime

The time_taken variable will contain a Timedelta object from which you can get seconds.

  • Related