summaryrefslogtreecommitdiffstats
path: root/python/mozperftest/mozperftest/metrics/notebook/perftestnotebook.py
blob: 99c3766b42cb3bf8d9badad8bd22d33df372daec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import json
import webbrowser
from http.server import BaseHTTPRequestHandler, HTTPServer

from .constant import Constant


class PerftestNotebook(object):
    """Controller class for PerftestNotebook."""

    def __init__(self, data, logger, prefix):
        """Initialize the PerftestNotebook.

        :param dict data: Standardized data, post-transformation.
        """
        self.data = data
        self.logger = logger
        self.prefix = prefix
        self.const = Constant()

    def get_notebook_section(self, func):
        """Fetch notebook content based on analysis name.

        :param str func: analysis or notebook section name
        """
        template_path = self.const.here / "notebook-sections" / func
        if not template_path.exists():
            self.logger.warning(
                f"Could not find the notebook-section called {func}", self.prefix
            )
            return ""
        with template_path.open() as f:
            return f.read()

    def post_to_iodide(self, analysis=None, start_local_server=True):
        """Build notebook and post it to iodide.

        :param list analysis: notebook section names, analysis to perform in iodide
        """
        data = self.data
        notebook_sections = ""

        template_header_path = self.const.here / "notebook-sections" / "header"
        with template_header_path.open() as f:
            notebook_sections += f.read()

        if analysis:
            for func in analysis:
                notebook_sections += self.get_notebook_section(func)

        template_upload_file_path = self.const.here / "template_upload_file.html"
        with template_upload_file_path.open() as f:
            html = f.read().replace("replace_me", repr(notebook_sections))

        upload_file_path = self.const.here / "upload_file.html"
        with upload_file_path.open("w") as f:
            f.write(html)

        # set up local server. Iodide will fetch data from localhost:5000/data
        class DataRequestHandler(BaseHTTPRequestHandler):
            def do_GET(self):
                if self.path == "/data":
                    self.send_response(200)
                    self.send_header("Content-type", "application/json")
                    self.send_header("Access-Control-Allow-Origin", "*")
                    self.end_headers()
                    self.wfile.write(bytes(json.dumps(data).encode("utf-8")))

        PORT_NUMBER = 5000
        server = HTTPServer(("", PORT_NUMBER), DataRequestHandler)
        if start_local_server:
            webbrowser.open_new_tab(str(upload_file_path))
            try:
                server.serve_forever()
            finally:
                server.server_close()