import csv
import datetime
from services import io_services , log_services
# import pdb
def tally_readings_per_second ( data ) :
time_tallies = dict ( )
for item in data :
key = item [ 0 ]
if ( key in time_tallies ) :
time_tallies [ key ] = time_tallies [ key ] + 1
else :
time_tallies [ key ] = 1
return time_tallies
def total_count_for_each_reading_per_second ( time_tallies ) :
tally_totals = dict ( )
for val in time_tallies . values ( ) :
if ( val in tally_totals ) :
tally_totals [ val ] = ( tally_totals [ val ] ) + 1
else :
tally_totals [ val ] = 1
return tally_totals
def get_rps_above ( threshold , readings ) :
times = list ( )
# print(readings.values())
for k , v in readings . items ( ) :
# print([k, v])
if ( v > = threshold ) :
times . append ( k )
return times
def tally_flickers ( selected_readings , all_readings ) :
selected = dict ( )
for time in selected_readings :
# print(f"Looking for {time}...")
selected [ time ] = all_readings [ time ]
# for k, v in all_readings.items():
return selected
def main ( ) :
raw_data_path = " data/test-data-lite.csv "
rps_save_path = " data/results/readings-per-sec.csv "
raw_data = io_services . load_raw_data ( raw_data_path )
# log_services.print_list(raw_data)
time_tallies = tally_readings_per_second ( raw_data )
# log_services.print_dictionary(time_tallies)
rps_totals = total_count_for_each_reading_per_second ( time_tallies )
# log_services.print_dictionary(rps_totals)
io_services . save_rps_totals ( rps_totals , rps_save_path )
# rsp_above_two = get_rps_above(2, time_tallies)
# flicker_tallies = tally_flickers(rsp_above_two, time_tallies)
if __name__ == " __main__ " :
main ( )