Snake case

merge-requests/30/head
Bob Mottram 2022-01-02 13:18:11 +00:00
parent b5ef362237
commit 0d4088705f
2 changed files with 91 additions and 83 deletions

View File

@ -15,46 +15,48 @@ from utils import get_config_param
from utils import save_json from utils import save_json
def fitness_performance(startTime, fitnessState: {}, def fitness_performance(startTime, fitness_state: {},
fitnessId: str, watch_point: str, debug: bool) -> None: fitness_id: str, watch_point: str,
debug: bool) -> None:
"""Log a performance watchpoint """Log a performance watchpoint
""" """
if 'performance' not in fitnessState: if 'performance' not in fitness_state:
fitnessState['performance'] = {} fitness_state['performance'] = {}
if fitnessId not in fitnessState['performance']: if fitness_id not in fitness_state['performance']:
fitnessState['performance'][fitnessId] = {} fitness_state['performance'][fitness_id] = {}
if watch_point not in fitnessState['performance'][fitnessId]: if watch_point not in fitness_state['performance'][fitness_id]:
fitnessState['performance'][fitnessId][watch_point] = { fitness_state['performance'][fitness_id][watch_point] = {
"total": float(0), "total": float(0),
"ctr": int(0) "ctr": int(0)
} }
time_diff = float(time.time() - startTime) time_diff = float(time.time() - startTime)
fitnessState['performance'][fitnessId][watch_point]['total'] += time_diff fitness_state['performance'][fitness_id][watch_point]['total'] += time_diff
fitnessState['performance'][fitnessId][watch_point]['ctr'] += 1 fitness_state['performance'][fitness_id][watch_point]['ctr'] += 1
if fitnessState['performance'][fitnessId][watch_point]['ctr'] >= 1024: if fitness_state['performance'][fitness_id][watch_point]['ctr'] >= 1024:
fitnessState['performance'][fitnessId][watch_point]['total'] /= 2 fitness_state['performance'][fitness_id][watch_point]['total'] /= 2
fitnessState['performance'][fitnessId][watch_point]['ctr'] = \ fitness_state['performance'][fitness_id][watch_point]['ctr'] = \
int(fitnessState['performance'][fitnessId][watch_point]['ctr'] / 2) int(fitness_state['performance'][fitness_id][watch_point]['ctr'] /
2)
if debug: if debug:
ctr = fitnessState['performance'][fitnessId][watch_point]['ctr'] ctr = fitness_state['performance'][fitness_id][watch_point]['ctr']
total = fitnessState['performance'][fitnessId][watch_point]['total'] total = fitness_state['performance'][fitness_id][watch_point]['total']
print('FITNESS: performance/' + fitnessId + '/' + print('FITNESS: performance/' + fitness_id + '/' +
watch_point + '/' + str(total * 1000 / ctr)) watch_point + '/' + str(total * 1000 / ctr))
def sorted_watch_points(fitness: {}, fitnessId: str) -> []: def sorted_watch_points(fitness: {}, fitness_id: str) -> []:
"""Returns a sorted list of watchpoints """Returns a sorted list of watchpoints
times are in mS times are in mS
""" """
if not fitness.get('performance'): if not fitness.get('performance'):
return [] return []
if not fitness['performance'].get(fitnessId): if not fitness['performance'].get(fitness_id):
return [] return []
result = [] result = []
for watch_point, item in fitness['performance'][fitnessId].items(): for watch_point, item in fitness['performance'][fitness_id].items():
if not item.get('total'): if not item.get('total'):
continue continue
average_time = item['total'] * 1000 / item['ctr'] average_time = item['total'] * 1000 / item['ctr']
@ -64,11 +66,11 @@ def sorted_watch_points(fitness: {}, fitnessId: str) -> []:
return result return result
def html_watch_points_graph(base_dir: str, fitness: {}, fitnessId: str, def html_watch_points_graph(base_dir: str, fitness: {}, fitness_id: str,
maxEntries: int) -> str: max_entries: int) -> str:
"""Returns the html for a graph of watchpoints """Returns the html for a graph of watchpoints
""" """
watch_points_list = sorted_watch_points(fitness, fitnessId) watch_points_list = sorted_watch_points(fitness, fitness_id)
css_filename = base_dir + '/epicyon-graph.css' css_filename = base_dir + '/epicyon-graph.css'
if os.path.isfile(base_dir + '/graph.css'): if os.path.isfile(base_dir + '/graph.css'):
@ -80,7 +82,7 @@ def html_watch_points_graph(base_dir: str, fitness: {}, fitnessId: str,
html_header_with_external_style(css_filename, instance_title, None) html_header_with_external_style(css_filename, instance_title, None)
html_str += \ html_str += \
'<table class="graph">\n' + \ '<table class="graph">\n' + \
'<caption>Watchpoints for ' + fitnessId + '</caption>\n' + \ '<caption>Watchpoints for ' + fitness_id + '</caption>\n' + \
'<thead>\n' + \ '<thead>\n' + \
' <tr>\n' + \ ' <tr>\n' + \
' <th scope="col">Item</th>\n' + \ ' <th scope="col">Item</th>\n' + \
@ -101,17 +103,17 @@ def html_watch_points_graph(base_dir: str, fitness: {}, fitnessId: str,
for watch_point in watch_points_list: for watch_point in watch_points_list:
name = watch_point.split(' ', 1)[1] name = watch_point.split(' ', 1)[1]
average_time = float(watch_point.split(' ')[0]) average_time = float(watch_point.split(' ')[0])
heightPercent = int(average_time * 100 / max_average_time) height_percent = int(average_time * 100 / max_average_time)
timeMS = int(average_time) time_ms = int(average_time)
if heightPercent == 0: if height_percent == 0:
continue continue
html_str += \ html_str += \
'<tr style="height:' + str(heightPercent) + '%">\n' + \ '<tr style="height:' + str(height_percent) + '%">\n' + \
' <th scope="row">' + name + '</th>\n' + \ ' <th scope="row">' + name + '</th>\n' + \
' <td><span>' + str(timeMS) + '</span></td>\n' + \ ' <td><span>' + str(time_ms) + '</span></td>\n' + \
'</tr>\n' '</tr>\n'
ctr += 1 ctr += 1
if ctr >= maxEntries: if ctr >= max_entries:
break break
html_str += '</tbody></table>\n' + html_footer() html_str += '</tbody></table>\n' + html_footer()

View File

@ -11,6 +11,8 @@ import os
def _dir_acct(base_dir: str, nickname: str, domain: str) -> str: def _dir_acct(base_dir: str, nickname: str, domain: str) -> str:
"""Returns the directory of an account
"""
return base_dir + '/accounts/' + nickname + '@' + domain return base_dir + '/accounts/' + nickname + '@' + domain
@ -27,124 +29,128 @@ def _port_domain_remove(domain: str) -> str:
def receiving_calendar_events(base_dir: str, nickname: str, domain: str, def receiving_calendar_events(base_dir: str, nickname: str, domain: str,
followingNickname: str, following_nickname: str,
followingDomain: str) -> bool: following_domain: str) -> bool:
"""Returns true if receiving calendar events from the given """Returns true if receiving calendar events from the given
account from following.txt account from following.txt
""" """
if followingNickname == nickname and followingDomain == domain: if following_nickname == nickname and following_domain == domain:
# reminder post # reminder post
return True return True
calendarFilename = \ calendar_filename = \
_dir_acct(base_dir, nickname, domain) + '/followingCalendar.txt' _dir_acct(base_dir, nickname, domain) + '/followingCalendar.txt'
handle = followingNickname + '@' + followingDomain handle = following_nickname + '@' + following_domain
if not os.path.isfile(calendarFilename): if not os.path.isfile(calendar_filename):
followingFilename = \ following_filename = \
_dir_acct(base_dir, nickname, domain) + '/following.txt' _dir_acct(base_dir, nickname, domain) + '/following.txt'
if not os.path.isfile(followingFilename): if not os.path.isfile(following_filename):
return False return False
# create a new calendar file from the following file # create a new calendar file from the following file
followingHandles = None following_handles = None
try: try:
with open(followingFilename, 'r') as followingFile: with open(following_filename, 'r') as following_file:
followingHandles = followingFile.read() following_handles = following_file.read()
except OSError: except OSError:
print('EX: receiving_calendar_events ' + followingFilename) print('EX: receiving_calendar_events ' + following_filename)
if followingHandles: if following_handles:
try: try:
with open(calendarFilename, 'w+') as fp: with open(calendar_filename, 'w+') as fp_cal:
fp.write(followingHandles) fp_cal.write(following_handles)
except OSError: except OSError:
print('EX: receiving_calendar_events 2 ' + calendarFilename) print('EX: receiving_calendar_events 2 ' + calendar_filename)
return handle + '\n' in open(calendarFilename).read() return handle + '\n' in open(calendar_filename).read()
def _receive_calendar_events(base_dir: str, nickname: str, domain: str, def _receive_calendar_events(base_dir: str, nickname: str, domain: str,
followingNickname: str, following_nickname: str,
followingDomain: str, following_domain: str,
add: bool) -> None: add: bool) -> None:
"""Adds or removes a handle from the following.txt list into a list """Adds or removes a handle from the following.txt list into a list
indicating whether to receive calendar events from that account indicating whether to receive calendar events from that account
""" """
# check that a following file exists # check that a following file exists
domain = _port_domain_remove(domain) domain = _port_domain_remove(domain)
followingFilename = \ following_filename = \
_dir_acct(base_dir, nickname, domain) + '/following.txt' _dir_acct(base_dir, nickname, domain) + '/following.txt'
if not os.path.isfile(followingFilename): if not os.path.isfile(following_filename):
print("WARN: following.txt doesn't exist for " + print("WARN: following.txt doesn't exist for " +
nickname + '@' + domain) nickname + '@' + domain)
return return
handle = followingNickname + '@' + followingDomain handle = following_nickname + '@' + following_domain
# check that you are following this handle # check that you are following this handle
if handle + '\n' not in open(followingFilename).read(): if handle + '\n' not in open(following_filename).read():
print('WARN: ' + handle + ' is not in ' + followingFilename) print('WARN: ' + handle + ' is not in ' + following_filename)
return return
calendarFilename = \ calendar_filename = \
_dir_acct(base_dir, nickname, domain) + '/followingCalendar.txt' _dir_acct(base_dir, nickname, domain) + '/followingCalendar.txt'
# get the contents of the calendar file, which is # get the contents of the calendar file, which is
# a set of handles # a set of handles
followingHandles = '' following_handles = ''
if os.path.isfile(calendarFilename): if os.path.isfile(calendar_filename):
print('Calendar file exists') print('Calendar file exists')
try: try:
with open(calendarFilename, 'r') as calendarFile: with open(calendar_filename, 'r') as calendar_file:
followingHandles = calendarFile.read() following_handles = calendar_file.read()
except OSError: except OSError:
print('EX: _receive_calendar_events ' + calendarFilename) print('EX: _receive_calendar_events ' + calendar_filename)
else: else:
# create a new calendar file from the following file # create a new calendar file from the following file
print('Creating calendar file ' + calendarFilename) print('Creating calendar file ' + calendar_filename)
followingHandles = '' following_handles = ''
try: try:
with open(followingFilename, 'r') as followingFile: with open(following_filename, 'r') as following_file:
followingHandles = followingFile.read() following_handles = following_file.read()
except OSError: except OSError:
print('EX: _receive_calendar_events 2 ' + calendarFilename) print('EX: _receive_calendar_events 2 ' + calendar_filename)
if add: if add:
try: try:
with open(calendarFilename, 'w+') as fp: with open(calendar_filename, 'w+') as fp_cal:
fp.write(followingHandles + handle + '\n') fp_cal.write(following_handles + handle + '\n')
except OSError: except OSError:
print('EX: unable to write ' + calendarFilename) print('EX: unable to write ' + calendar_filename)
# already in the calendar file? # already in the calendar file?
if handle + '\n' in followingHandles: if handle + '\n' in following_handles:
print(handle + ' exists in followingCalendar.txt') print(handle + ' exists in followingCalendar.txt')
if add: if add:
# already added # already added
return return
# remove from calendar file # remove from calendar file
followingHandles = followingHandles.replace(handle + '\n', '') following_handles = following_handles.replace(handle + '\n', '')
try: try:
with open(calendarFilename, 'w+') as fp: with open(calendar_filename, 'w+') as fp_cal:
fp.write(followingHandles) fp_cal.write(following_handles)
except OSError: except OSError:
print('EX: _receive_calendar_events 3 ' + calendarFilename) print('EX: _receive_calendar_events 3 ' + calendar_filename)
else: else:
print(handle + ' not in followingCalendar.txt') print(handle + ' not in followingCalendar.txt')
# not already in the calendar file # not already in the calendar file
if add: if add:
# append to the list of handles # append to the list of handles
followingHandles += handle + '\n' following_handles += handle + '\n'
try: try:
with open(calendarFilename, 'w+') as fp: with open(calendar_filename, 'w+') as fp_cal:
fp.write(followingHandles) fp_cal.write(following_handles)
except OSError: except OSError:
print('EX: _receive_calendar_events 4 ' + calendarFilename) print('EX: _receive_calendar_events 4 ' + calendar_filename)
def add_person_to_calendar(base_dir: str, nickname: str, domain: str, def add_person_to_calendar(base_dir: str, nickname: str, domain: str,
followingNickname: str, following_nickname: str,
followingDomain: str) -> None: following_domain: str) -> None:
"""Add a person to the calendar
"""
_receive_calendar_events(base_dir, nickname, domain, _receive_calendar_events(base_dir, nickname, domain,
followingNickname, followingDomain, True) following_nickname, following_domain, True)
def remove_person_from_calendar(base_dir: str, nickname: str, domain: str, def remove_person_from_calendar(base_dir: str, nickname: str, domain: str,
followingNickname: str, following_nickname: str,
followingDomain: str) -> None: following_domain: str) -> None:
"""Remove a person from the calendar
"""
_receive_calendar_events(base_dir, nickname, domain, _receive_calendar_events(base_dir, nickname, domain,
followingNickname, followingDomain, False) following_nickname, following_domain, False)