git_bsmd/LogFileEvaluation/evaluate.py

62 lines
1.8 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)
outputFileName = dname + "\\" + 'output.csv'
# open the output file
outputFile = open(outputFileName, 'w')
# write the header
outputFile.write('Receive time, Duration\n')
# open the log files
q = deque()
for i in range(0, 10):
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]
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")
# close the log file
logFile.close()
outputFile.close()