#!/usr/bin/env python3 import json import datetime import re import pprint def average(lst: list) -> float: return sum(lst) / len(lst) def listtodict(inlist: list) -> dict: it = iter(inlist) res_dct = dict(zip(it, it)) return res_dct def readfile() -> list: lines = [] with open("log.txt", 'r') as inputFile: line = inputFile.readline() while line: try: linedict = json.loads(line) lines.append(linedict) except json.JSONDecodeError: continue finally: line = inputFile.readline() return lines def readconfigdates() -> dict: dates = {} with open("dates.txt", 'r') as inputFile: line = inputFile.readline().rstrip() currline = 0 while line: dateformatted = datetime.datetime.strptime( line, '%Y-%m-%d %H:%M:%S') dates[currline] = dateformatted line = inputFile.readline().rstrip() currline += 1 return dates def procesfile(file: list, start: datetime, end: datetime) -> dict: dictofsecs = {} for line in file: try: currdate = line['ts'].split('.')[0].replace('T', ' ') dateformatted = datetime.datetime.strptime( currdate, '%Y-%m-%d %H:%M:%S') if start < dateformatted < end: message = line['msg'] messagelist = re.split(' |=', message) messagedict = listtodict(messagelist) messagedict['ts'] = dateformatted if 'ObservedStableValue' in messagedict: if messagedict['ts'] not in dictofsecs: dictofsecs[messagedict['ts']] = [] dictofsecs[messagedict['ts']].append( float(messagedict['ObservedStableValue'])) else: dictofsecs[messagedict['ts']].append( float(messagedict['ObservedStableValue'])) except Exception as exception: print(exception) return dictofsecs def averagepersec(dictoftimes: dict, shouldprint: bool = False) -> dict: dictofsecs = {} for key, value in dictoftimes.items(): dictofsecs[key] = average(value) if shouldprint: print(dictofsecs[key]) return dictofsecs if __name__ == "__main__": filelines = readfile() config = readconfigdates() fitdates = procesfile(filelines, config[0], config[1]) averageofdict = averagepersec(fitdates, True)