mirror of https://gitlab.com/bashrc2/epicyon
Snake case
parent
b5ef362237
commit
0d4088705f
|
@ -15,46 +15,48 @@ from utils import get_config_param
|
|||
from utils import save_json
|
||||
|
||||
|
||||
def fitness_performance(startTime, fitnessState: {},
|
||||
fitnessId: str, watch_point: str, debug: bool) -> None:
|
||||
def fitness_performance(startTime, fitness_state: {},
|
||||
fitness_id: str, watch_point: str,
|
||||
debug: bool) -> None:
|
||||
"""Log a performance watchpoint
|
||||
"""
|
||||
if 'performance' not in fitnessState:
|
||||
fitnessState['performance'] = {}
|
||||
if fitnessId not in fitnessState['performance']:
|
||||
fitnessState['performance'][fitnessId] = {}
|
||||
if watch_point not in fitnessState['performance'][fitnessId]:
|
||||
fitnessState['performance'][fitnessId][watch_point] = {
|
||||
if 'performance' not in fitness_state:
|
||||
fitness_state['performance'] = {}
|
||||
if fitness_id not in fitness_state['performance']:
|
||||
fitness_state['performance'][fitness_id] = {}
|
||||
if watch_point not in fitness_state['performance'][fitness_id]:
|
||||
fitness_state['performance'][fitness_id][watch_point] = {
|
||||
"total": float(0),
|
||||
"ctr": int(0)
|
||||
}
|
||||
|
||||
time_diff = float(time.time() - startTime)
|
||||
|
||||
fitnessState['performance'][fitnessId][watch_point]['total'] += time_diff
|
||||
fitnessState['performance'][fitnessId][watch_point]['ctr'] += 1
|
||||
if fitnessState['performance'][fitnessId][watch_point]['ctr'] >= 1024:
|
||||
fitnessState['performance'][fitnessId][watch_point]['total'] /= 2
|
||||
fitnessState['performance'][fitnessId][watch_point]['ctr'] = \
|
||||
int(fitnessState['performance'][fitnessId][watch_point]['ctr'] / 2)
|
||||
fitness_state['performance'][fitness_id][watch_point]['total'] += time_diff
|
||||
fitness_state['performance'][fitness_id][watch_point]['ctr'] += 1
|
||||
if fitness_state['performance'][fitness_id][watch_point]['ctr'] >= 1024:
|
||||
fitness_state['performance'][fitness_id][watch_point]['total'] /= 2
|
||||
fitness_state['performance'][fitness_id][watch_point]['ctr'] = \
|
||||
int(fitness_state['performance'][fitness_id][watch_point]['ctr'] /
|
||||
2)
|
||||
|
||||
if debug:
|
||||
ctr = fitnessState['performance'][fitnessId][watch_point]['ctr']
|
||||
total = fitnessState['performance'][fitnessId][watch_point]['total']
|
||||
print('FITNESS: performance/' + fitnessId + '/' +
|
||||
ctr = fitness_state['performance'][fitness_id][watch_point]['ctr']
|
||||
total = fitness_state['performance'][fitness_id][watch_point]['total']
|
||||
print('FITNESS: performance/' + fitness_id + '/' +
|
||||
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
|
||||
times are in mS
|
||||
"""
|
||||
if not fitness.get('performance'):
|
||||
return []
|
||||
if not fitness['performance'].get(fitnessId):
|
||||
if not fitness['performance'].get(fitness_id):
|
||||
return []
|
||||
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'):
|
||||
continue
|
||||
average_time = item['total'] * 1000 / item['ctr']
|
||||
|
@ -64,11 +66,11 @@ def sorted_watch_points(fitness: {}, fitnessId: str) -> []:
|
|||
return result
|
||||
|
||||
|
||||
def html_watch_points_graph(base_dir: str, fitness: {}, fitnessId: str,
|
||||
maxEntries: int) -> str:
|
||||
def html_watch_points_graph(base_dir: str, fitness: {}, fitness_id: str,
|
||||
max_entries: int) -> str:
|
||||
"""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'
|
||||
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_str += \
|
||||
'<table class="graph">\n' + \
|
||||
'<caption>Watchpoints for ' + fitnessId + '</caption>\n' + \
|
||||
'<caption>Watchpoints for ' + fitness_id + '</caption>\n' + \
|
||||
'<thead>\n' + \
|
||||
' <tr>\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:
|
||||
name = watch_point.split(' ', 1)[1]
|
||||
average_time = float(watch_point.split(' ')[0])
|
||||
heightPercent = int(average_time * 100 / max_average_time)
|
||||
timeMS = int(average_time)
|
||||
if heightPercent == 0:
|
||||
height_percent = int(average_time * 100 / max_average_time)
|
||||
time_ms = int(average_time)
|
||||
if height_percent == 0:
|
||||
continue
|
||||
html_str += \
|
||||
'<tr style="height:' + str(heightPercent) + '%">\n' + \
|
||||
'<tr style="height:' + str(height_percent) + '%">\n' + \
|
||||
' <th scope="row">' + name + '</th>\n' + \
|
||||
' <td><span>' + str(timeMS) + '</span></td>\n' + \
|
||||
' <td><span>' + str(time_ms) + '</span></td>\n' + \
|
||||
'</tr>\n'
|
||||
ctr += 1
|
||||
if ctr >= maxEntries:
|
||||
if ctr >= max_entries:
|
||||
break
|
||||
|
||||
html_str += '</tbody></table>\n' + html_footer()
|
||||
|
|
|
@ -11,6 +11,8 @@ import os
|
|||
|
||||
|
||||
def _dir_acct(base_dir: str, nickname: str, domain: str) -> str:
|
||||
"""Returns the directory of an account
|
||||
"""
|
||||
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,
|
||||
followingNickname: str,
|
||||
followingDomain: str) -> bool:
|
||||
following_nickname: str,
|
||||
following_domain: str) -> bool:
|
||||
"""Returns true if receiving calendar events from the given
|
||||
account from following.txt
|
||||
"""
|
||||
if followingNickname == nickname and followingDomain == domain:
|
||||
if following_nickname == nickname and following_domain == domain:
|
||||
# reminder post
|
||||
return True
|
||||
calendarFilename = \
|
||||
calendar_filename = \
|
||||
_dir_acct(base_dir, nickname, domain) + '/followingCalendar.txt'
|
||||
handle = followingNickname + '@' + followingDomain
|
||||
if not os.path.isfile(calendarFilename):
|
||||
followingFilename = \
|
||||
handle = following_nickname + '@' + following_domain
|
||||
if not os.path.isfile(calendar_filename):
|
||||
following_filename = \
|
||||
_dir_acct(base_dir, nickname, domain) + '/following.txt'
|
||||
if not os.path.isfile(followingFilename):
|
||||
if not os.path.isfile(following_filename):
|
||||
return False
|
||||
# create a new calendar file from the following file
|
||||
followingHandles = None
|
||||
following_handles = None
|
||||
try:
|
||||
with open(followingFilename, 'r') as followingFile:
|
||||
followingHandles = followingFile.read()
|
||||
with open(following_filename, 'r') as following_file:
|
||||
following_handles = following_file.read()
|
||||
except OSError:
|
||||
print('EX: receiving_calendar_events ' + followingFilename)
|
||||
if followingHandles:
|
||||
print('EX: receiving_calendar_events ' + following_filename)
|
||||
if following_handles:
|
||||
try:
|
||||
with open(calendarFilename, 'w+') as fp:
|
||||
fp.write(followingHandles)
|
||||
with open(calendar_filename, 'w+') as fp_cal:
|
||||
fp_cal.write(following_handles)
|
||||
except OSError:
|
||||
print('EX: receiving_calendar_events 2 ' + calendarFilename)
|
||||
return handle + '\n' in open(calendarFilename).read()
|
||||
print('EX: receiving_calendar_events 2 ' + calendar_filename)
|
||||
return handle + '\n' in open(calendar_filename).read()
|
||||
|
||||
|
||||
def _receive_calendar_events(base_dir: str, nickname: str, domain: str,
|
||||
followingNickname: str,
|
||||
followingDomain: str,
|
||||
following_nickname: str,
|
||||
following_domain: str,
|
||||
add: bool) -> None:
|
||||
"""Adds or removes a handle from the following.txt list into a list
|
||||
indicating whether to receive calendar events from that account
|
||||
"""
|
||||
# check that a following file exists
|
||||
domain = _port_domain_remove(domain)
|
||||
followingFilename = \
|
||||
following_filename = \
|
||||
_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 " +
|
||||
nickname + '@' + domain)
|
||||
return
|
||||
handle = followingNickname + '@' + followingDomain
|
||||
handle = following_nickname + '@' + following_domain
|
||||
|
||||
# check that you are following this handle
|
||||
if handle + '\n' not in open(followingFilename).read():
|
||||
print('WARN: ' + handle + ' is not in ' + followingFilename)
|
||||
if handle + '\n' not in open(following_filename).read():
|
||||
print('WARN: ' + handle + ' is not in ' + following_filename)
|
||||
return
|
||||
|
||||
calendarFilename = \
|
||||
calendar_filename = \
|
||||
_dir_acct(base_dir, nickname, domain) + '/followingCalendar.txt'
|
||||
|
||||
# get the contents of the calendar file, which is
|
||||
# a set of handles
|
||||
followingHandles = ''
|
||||
if os.path.isfile(calendarFilename):
|
||||
following_handles = ''
|
||||
if os.path.isfile(calendar_filename):
|
||||
print('Calendar file exists')
|
||||
try:
|
||||
with open(calendarFilename, 'r') as calendarFile:
|
||||
followingHandles = calendarFile.read()
|
||||
with open(calendar_filename, 'r') as calendar_file:
|
||||
following_handles = calendar_file.read()
|
||||
except OSError:
|
||||
print('EX: _receive_calendar_events ' + calendarFilename)
|
||||
print('EX: _receive_calendar_events ' + calendar_filename)
|
||||
else:
|
||||
# create a new calendar file from the following file
|
||||
print('Creating calendar file ' + calendarFilename)
|
||||
followingHandles = ''
|
||||
print('Creating calendar file ' + calendar_filename)
|
||||
following_handles = ''
|
||||
try:
|
||||
with open(followingFilename, 'r') as followingFile:
|
||||
followingHandles = followingFile.read()
|
||||
with open(following_filename, 'r') as following_file:
|
||||
following_handles = following_file.read()
|
||||
except OSError:
|
||||
print('EX: _receive_calendar_events 2 ' + calendarFilename)
|
||||
print('EX: _receive_calendar_events 2 ' + calendar_filename)
|
||||
if add:
|
||||
try:
|
||||
with open(calendarFilename, 'w+') as fp:
|
||||
fp.write(followingHandles + handle + '\n')
|
||||
with open(calendar_filename, 'w+') as fp_cal:
|
||||
fp_cal.write(following_handles + handle + '\n')
|
||||
except OSError:
|
||||
print('EX: unable to write ' + calendarFilename)
|
||||
print('EX: unable to write ' + calendar_filename)
|
||||
|
||||
# already in the calendar file?
|
||||
if handle + '\n' in followingHandles:
|
||||
if handle + '\n' in following_handles:
|
||||
print(handle + ' exists in followingCalendar.txt')
|
||||
if add:
|
||||
# already added
|
||||
return
|
||||
# remove from calendar file
|
||||
followingHandles = followingHandles.replace(handle + '\n', '')
|
||||
following_handles = following_handles.replace(handle + '\n', '')
|
||||
try:
|
||||
with open(calendarFilename, 'w+') as fp:
|
||||
fp.write(followingHandles)
|
||||
with open(calendar_filename, 'w+') as fp_cal:
|
||||
fp_cal.write(following_handles)
|
||||
except OSError:
|
||||
print('EX: _receive_calendar_events 3 ' + calendarFilename)
|
||||
print('EX: _receive_calendar_events 3 ' + calendar_filename)
|
||||
else:
|
||||
print(handle + ' not in followingCalendar.txt')
|
||||
# not already in the calendar file
|
||||
if add:
|
||||
# append to the list of handles
|
||||
followingHandles += handle + '\n'
|
||||
following_handles += handle + '\n'
|
||||
try:
|
||||
with open(calendarFilename, 'w+') as fp:
|
||||
fp.write(followingHandles)
|
||||
with open(calendar_filename, 'w+') as fp_cal:
|
||||
fp_cal.write(following_handles)
|
||||
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,
|
||||
followingNickname: str,
|
||||
followingDomain: str) -> None:
|
||||
following_nickname: str,
|
||||
following_domain: str) -> None:
|
||||
"""Add a person to the calendar
|
||||
"""
|
||||
_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,
|
||||
followingNickname: str,
|
||||
followingDomain: str) -> None:
|
||||
following_nickname: str,
|
||||
following_domain: str) -> None:
|
||||
"""Remove a person from the calendar
|
||||
"""
|
||||
_receive_calendar_events(base_dir, nickname, domain,
|
||||
followingNickname, followingDomain, False)
|
||||
following_nickname, following_domain, False)
|
||||
|
|
Loading…
Reference in New Issue