I have this log text file:
omer| (stmt : 0) | adminT| Connection id - 0
omer| (stmt : 0) | adminT| Start Time - 2018-11-06 16:52:01
omer| (stmt : 0) | adminT| Statement create or replace table amit (x date);
omer| (stmt : 0)| adminT| Connection id - 0 - Executing - create or replace table amit (x date);
omer| (stmt : 0) | adminT| Connection id - 0
omer| (stmt : 0) | adminT| End Time - 2018-11-06 16:52:01
omer| (stmt : 0) | adminT| SQL - create or replace table amit (x date);
omer| (stmt : 0) | adminT| Success
admin| (stmt : 1) | adminT| Connection id - 0
admin| (stmt : 1) | adminT| Start Time - 2018-11-06 16:52:14
admin| (stmt : 1) | adminT| Statement create or replace table amit (x int, y int);
admin| (stmt : 1)| adminT| Connection id - 0 - Executing - create or replace table amit (x int, y int);
admin| (stmt : 1) | adminT| Connection id - 0
admin| (stmt : 1) | adminT| End Time - 2018-11-06 16:52:15
admin| (stmt : 2) | adminT| Connection id - 0
admin| (stmt : 2) | adminT| Start Time - 2018-11-06 16:52:19
admin| (stmt : 2) | adminT| Statement create table amit (x int, y int);
admin| (stmt : 2) | adminT| Connection id - 0
admin| (stmt : 2) | adminT| End Time - 2018-11-06 16:52:22
admin| (stmt : 2) | adminT| SQL - Can't create table 'public.amit' - a table with the same name already exists
admin| (stmt : 2) | adminT| Failed
now I want to know the delta between start date to end date (as can be seen in the end of the line), next I want to know if the statement is successful or not (marked by Failed or Success). and then I want to calculate the delta from start time and end time, so this is the code I implemented:
def parse_log_file(log_file):
print(len(""))
my_path = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(my_path, log_file)
max_delta = 0
with open(path, 'r') as f:
lines = f.readlines()[1:]
for line in lines:
elements = line.split('|')
# strip the lines of surrounding spaces
elements = [t.strip() for t in elements]
statement_id = elements[6]
if "Start Time" in elements[8] and statement_id in elements[6]:
start_date = get_date_parsed(elements[8])
if "End Time" in elements[8] and statement_id in elements[6]:
end_date = get_date_parsed(elements[8])
date_time_start_obj = datetime.datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')
date_time_end_obj = datetime.datetime.strptime(end_date, '%Y-%m-%d %H:%M:%S')
delta = date_time_end_obj - date_time_start_obj
if delta.seconds > max_delta:
max_delta = delta
print(max_delta)
print("hello")
def get_date_parsed(date_str):
res = date_str.split(' ')[3] + ' ' + date_str.split(' ')[4]
return res
Now I want to know if there is a way to know if the next lines contain 'Success' so the date calculation would be valid.