Skip to content

Commit

Permalink
report: add option for continuous report serving (#421)
Browse files Browse the repository at this point in the history
When running experiments locally the workflow is to continuously rebuild
the static pages while having a http server launched. This is a bit
inconvenient. This commit adds an option to the web module such that
both a http server is launched and the static generator continues to
generate files as long as the http server is launched.

---------

Signed-off-by: David Korczynski <[email protected]>
  • Loading branch information
DavidKorczynski authored Jul 5, 2024
1 parent dd0337f commit f838757
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions report/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@
import argparse
import logging
import os
import threading
import time
import urllib.parse
from functools import partial
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from typing import Any, Dict, List, Optional

import jinja2

from report.common import Benchmark, FileSystem, Results, Sample, Target

LOCAL_HOST = '127.0.0.1'
LOCAL_PORT = 8012


class JinjaEnv:
"""JinjaEnv wraps the set up of a jinja2 environment."""
Expand Down Expand Up @@ -168,6 +174,27 @@ def _write_benchmark_sample(self, benchmark: Benchmark, sample: Sample,
sample.id, e)


def generate_report(args: argparse.Namespace) -> None:
"""Generates static web server files."""
logging.info('Generating web page files in %s', args.output_dir)
results = Results(results_dir=args.results_dir,
benchmark_set=args.benchmark_set)
jinja_env = JinjaEnv(template_globals={'model': args.model})
gr = GenerateReport(results=results,
jinja_env=jinja_env,
output_dir=args.output_dir)
gr.generate()


def launch_webserver(args):
"""Launches a local web server to browse results."""
logging.info('Launching webserver at %s:%d', LOCAL_HOST, LOCAL_PORT)
server = ThreadingHTTPServer((LOCAL_HOST, LOCAL_PORT),
partial(SimpleHTTPRequestHandler,
directory=args.output_dir))
server.serve_forever()


def _parse_arguments() -> argparse.Namespace:
"""Parses command line args."""
parser = argparse.ArgumentParser(description=(
Expand All @@ -191,20 +218,33 @@ def _parse_arguments() -> argparse.Namespace:
'-m',
help='Model used for the experiment.',
default='')
parser.add_argument('--serve',
'-s',
help='Will launch a web server if set.',
action='store_true')

return parser.parse_args()


def main():
args = _parse_arguments()

results = Results(results_dir=args.results_dir,
benchmark_set=args.benchmark_set)
jinja_env = JinjaEnv(template_globals={'model': args.model})
gr = GenerateReport(results=results,
jinja_env=jinja_env,
output_dir=args.output_dir)
gr.generate()
if not args.serve:
generate_report(args)
else:
logging.getLogger().setLevel(os.environ.get('LOGLEVEL', 'INFO').upper())
# Launch web server
thread = threading.Thread(target=launch_webserver, args=(args,))
thread.start()

# Generate results continuously while the process runs.
while True:
generate_report(args)
try:
time.sleep(90)
except KeyboardInterrupt:
logging.info('Exiting.')
os._exit(0)


if __name__ == '__main__':
Expand Down

0 comments on commit f838757

Please sign in to comment.