aboutsummaryrefslogtreecommitdiffstats
path: root/qtip/reporter/console.py
blob: 64d677bad54d54c00bb0cab6657ca64a82a88c68 (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
##############################################################################
# Copyright (c) 2017 taseer94@gmail.com and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import glob
import json
from os import path

from jinja2 import Environment
from jinja2 import FileSystemLoader
from qtip.base import BaseActor

ROOT_DIR = path.join(path.dirname(__file__), path.pardir, path.pardir)


class ConsoleReporter(BaseActor):
    """ report benchmark result to console """

    def __init__(self, config, parent=None):
        super(ConsoleReporter, self).__init__(config, parent=parent)

        # TODO (taseer) load template from config
        tpl_path = path.join(path.dirname(__file__), 'templates')
        tpl_loader = FileSystemLoader(tpl_path)
        self._env = Environment(loader=tpl_loader)
        self.result_path = path.join(ROOT_DIR, 'collector')

    def load_result(self):
        # TODO (taseer) change result directory format more suitable to filter out
        result_dirs = glob.glob('{}/20*'.format(self.result_path))
        # select the last (latest) directory for rendering report, result_dirs[-1]
        with open(path.join(self.result_path, result_dirs[-1], 'result.json')) as sample:
            result = json.load(sample)
        return result

    def render(self, metric):
        template = self._env.get_template('base.j2')
        var_dict = self.load_result()
        var_dict['metric_name'] = metric
        out = template.render(var_dict)
        return out