__filename__ = "fitnessFunctions.py" __author__ = "Bob Mottram" __license__ = "AGPL3+" __version__ = "1.2.0" __maintainer__ = "Bob Mottram" __email__ = "bob@libreserver.org" __status__ = "Production" __module_group__ = "Core" import os import time from webapp_utils import html_header_with_external_style from webapp_utils import html_footer from utils import get_config_param from utils import save_json def fitness_performance(startTime, fitnessState: {}, fitnessId: 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] = { "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) if debug: ctr = fitnessState['performance'][fitnessId][watch_point]['ctr'] total = fitnessState['performance'][fitnessId][watch_point]['total'] print('FITNESS: performance/' + fitnessId + '/' + watch_point + '/' + str(total * 1000 / ctr)) def sorted_watch_points(fitness: {}, fitnessId: str) -> []: """Returns a sorted list of watchpoints times are in mS """ if not fitness.get('performance'): return [] if not fitness['performance'].get(fitnessId): return [] result = [] for watch_point, item in fitness['performance'][fitnessId].items(): if not item.get('total'): continue average_time = item['total'] * 1000 / item['ctr'] average_time_str = str(average_time).zfill(8) result.append(average_time_str + ' ' + watch_point) result.sort(reverse=True) return result def html_watch_points_graph(base_dir: str, fitness: {}, fitnessId: str, maxEntries: int) -> str: """Returns the html for a graph of watchpoints """ watch_points_list = sorted_watch_points(fitness, fitnessId) css_filename = base_dir + '/epicyon-graph.css' if os.path.isfile(base_dir + '/graph.css'): css_filename = base_dir + '/graph.css' instance_title = \ get_config_param(base_dir, 'instanceTitle') html_str = \ html_header_with_external_style(css_filename, instance_title, None) html_str += \ '
Item | \n' + \ 'Percent | \n' + \ '
---|---|
' + name + ' | \n' + \ '' + str(timeMS) + ' | \n' + \ '