Completed
Push — master ( a364e5...2cdc45 )
by Dieter
01:07
created

buildtimetrend.get_config_string()   A

Complexity

Conditions 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 17
rs 9.4286
1
# vim: set expandtab sw=4 ts=4:
2
"""
3
Dashboard related functions.
4
5
Copyright (C) 2014-2015 Dieter Adriaenssens <[email protected]>
6
7
This file is part of buildtimetrend/python-lib
8
<https://github.com/buildtimetrend/python-lib/>
9
10
This program is free software: you can redistribute it and/or modify
11
it under the terms of the GNU Affero General Public License as published by
12
the Free Software Foundation, either version 3 of the License, or
13
any later version.
14
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU Affero General Public License for more details.
19
20
You should have received a copy of the GNU Affero General Public License
21
along with this program. If not, see <http://www.gnu.org/licenses/>.
22
"""
23
24
from __future__ import division
25
from builtins import str
26
from buildtimetrend import keenio
27
from buildtimetrend import logger
28
from buildtimetrend.settings import Settings
29
from buildtimetrend.tools import check_file
30
from buildtimetrend.tools import check_dict
31
32
33
def get_config_dict(repo, extra=None):
34
    """
35
    Generate the configuration settings for the dashboard.
36
37
    The dashboard is Javascript powered HTML file that contains the
38
    graphs generated by Keen.io.
39
40
    Parameters:
41
    - repo : repo name (fe. buildtimetrend/service)
42
    - extra : dictionary of extra config settings, format : {"name" : "value"}
43
    """
44
    # initialise config settings dictionaries
45
    config = {}
46
47
    # add repo and project name
48
    if repo is not None and not repo == "":
49
        # merge extra settings into existing config dictionary
50
        config.update({
51
            'projectName': str(repo),
52
            'repoName': str(repo)
53
        })
54
55
    # add extra config parameters
56
    if extra is not None and check_dict(extra, "extra"):
57
        config.update(extra)
58
59
    return config
60
61
62
def get_config_string(repo, extra=None):
63
    """
64
    Generate the configuration settings for the dashboard.
65
66
    The dashboard is Javascript powered HTML file that contains the
67
    graphs generated by Keen.io.
68
69
    Parameters:
70
    - repo : repo name (fe. buildtimetrend/service)
71
    - extra : dictionary of extra config settings, format : {"name" : "value"}
72
    """
73
    # initialise config settings dictionaries
74
    config = get_config_dict(repo, extra)
75
    keen_config = keenio.get_dashboard_keen_config(repo)
76
77
    # create configuration as a string
78
    return "var config = {};\nvar keenConfig = {};".format(config, keen_config)
79
80
81
def generate_config_file(repo):
82
    """
83
    Generate the configuration file for the dashboard.
84
85
    The dashboard is Javascript powered HTML file that contains the
86
    graphs generated by Keen.io.
87
88
    Parameters:
89
    - repo : repo name (fe. buildtimetrend/service)
90
    """
91
    # get config settings
92
    config_string = get_config_string(repo)
93
94
    # write config file
95
    config_file = Settings().get_setting("dashboard_configfile")
96
    with open(config_file, 'w') as outfile:
97
        outfile.write(config_string)
98
99
    if check_file(config_file):
100
        logger.info("Created trends dashboard config file %s", config_file)
101
        return True
102
    else:
103
        logger.warning("The dashboard config file was not created")
104
        return False
105