# this script reads in the log files generated by scrapeDownstream.py and scrapeUpstream.py

import string
import time
import datetime

# declare the dictionaries for storing the data from the logs
dnStream = {'time': 0,
            'channel_id': 0,
            'snr': 0.0,
            'power': 0.0}

upStream = {'time': 0,
            'channel_id': 0,
            'power': 0.0,
            'ranging': ""}

# empty lists of dicts
dnStreams = []
upStreams = []

# averages from the logs
avg_down_snr = 0
avg_down_pwr = 0
avg_up_pwr = 0

down_log = open("/Volumes/Data/scrape/downstream_log.txt", 'r')

#skip the header
down_log.readline()

for line in down_log:
    arr = line.split(',')
    dnStream['time'] = arr[0]
    dnStream['channel_id'] = int(arr[1])
    dnStream['snr'] = float(arr[2])
    dnStream['power'] = float(arr[3])
    dnStreams.append(dnStream.copy())
    avg_down_snr += dnStream['snr']
    avg_down_pwr += dnStream['power']

down_log.close()
avg_down_pwr = float(avg_down_pwr / float(len(dnStreams)))
avg_down_snr = float(avg_down_snr / float(len(dnStreams)))

up_log = open("/Volumes/Data/scrape/upstream_log.txt", 'r')

#skip the header
up_log.readline()

for line in up_log:
    arr = line.split(',')
    upStream['time'] = arr[0]
    upStream['channel_id'] = int(arr[1])
    upStream['power'] = float(arr[2])
    upStream['ranging'] = arr[3].strip()
    upStreams.append(upStream.copy())
    avg_up_pwr += upStream['power']

    # print any entry with an unsuccessful ranging
    if upStream['ranging'] != "Success":
        print datetime.datetime.fromtimestamp(float(dnStream['time'])).strftime('%Y-%m-%d %H:%M:%S') + ": " + "Ranging on channel " + arr[1] + " with power " + arr[2] + " is " + upStream['ranging']

up_log.close()
avg_up_pwr = float(avg_up_pwr / float(len(upStreams)))

timeStart = datetime.datetime.fromtimestamp(float(dnStreams[0]['time']))
timeEnd = datetime.datetime.fromtimestamp(float(dnStreams[len(dnStreams) - 1]['time']))

print "Downstream log runs from " + timeStart.strftime('%Y-%m-%d %H:%M:%S') + " to " + timeEnd.strftime('%Y-%m-%d %H:%M:%S')

timeStart = datetime.datetime.fromtimestamp(float(upStreams[0]['time']))
timeEnd = datetime.datetime.fromtimestamp(float(upStreams[len(upStreams) - 1]['time']))

print "Upstream log runs from " + timeStart.strftime('%Y-%m-%d %H:%M:%S') + " to " + timeEnd.strftime('%Y-%m-%d %H:%M:%S') + "\n"

seq = [x['power'] for x in dnStreams]
print "Minimum downstream power is " + str(min(seq))
print "Average downstream power is " + str(avg_down_pwr)
print "Maximum downstream power is " + str(max(seq)) + "\n"

seq = [x['snr'] for x in dnStreams]
print "Minimum downstream signal to noise ratio is " + str(min(seq))
print "Average downstream signal to noise ratio is " + str(avg_down_snr)
print "Maximum downstream signal to noise ratio is " + str(max(seq)) + "\n"

seq = [x['power'] for x in upStreams]
print "Minimum upstream power is " + str(min(seq))
print "Average upstream power is " + str(avg_up_pwr)
print "Maximum upstream power is " + str(max(seq))

