git_bsmd/LogFileEvaluation/evaluate.py

79 lines
2.6 KiB
Python

import os
import datetime
from collections import deque
# open numbered log files and evaluate the results
# the log files have been copied from the server to this directory
# the log files are named log-NSWMessageService.txt.0, log-NSWMessageService.txt.1, log-NSWMessageService.txt.2, ...
# for the first analysis I have copied 10 log files to this directory but placed them in the .gitignore file
# get the current working directory
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
outputFileNameDBH = dname + "\\" + 'output_dbh.csv'
outputFileNameHISNORD = dname + "\\" + 'output_hisnord.csv'
# open the output files
outputFileDBH = open(outputFileNameDBH, 'w')
outputFileHISNORD = open(outputFileNameHISNORD, 'w')
# write the header
outputFileDBH.write('Receive time, Duration, Message class, Attempts\n')
outputFileHISNORD.write('Receive time, Duration, message class, Attemps\n')
# open the log files
q = deque()
for i in range(1, -1, -1):
logFileName = dname + "\\" + 'log-NSWMessageService.txt.' + str(i)
logFile = open(logFileName, 'r')
# read the content line by line
while True:
line = logFile.readline()
if not line:
break
# get substring of the date
dateString = line[0:19]
# kept old code for reference, but it does nothing
if "Upload of" in line:
# add the dateString to the queue
q.appendleft(dateString)
elif "Download of" in line:
# find the corresponding dateString in the queue
# calculate the duration
# remove the dateString from the queue
lastdatestring = q.pop()
# create date from string
lastdate = datetime.datetime.strptime(lastdatestring, '%Y-%m-%d %H:%M:%S')
currentdate = datetime.datetime.strptime(dateString, '%Y-%m-%d %H:%M:%S')
# calculate the duration
duration = currentdate - lastdate
# outputFile.write(dateString + "," + str(duration.seconds) + "\n")
if "MessageTelemetry" in line:
his = line[72:81].strip()
msg_class = line[83:91].strip()
duration = line[93:98]
tries = line[100:102]
if his == 'DUDR':
outputFileHISNORD.write(dateString + "," + duration + "," + msg_class + "," + tries + "\n")
elif his == 'DBH':
outputFileDBH.write(dateString + "," + duration + "," + msg_class + "," + tries + "\n")
# close the log file
logFile.close()
outputFileDBH.close()
outputFileHISNORD.close()