aboutsummaryrefslogtreecommitdiffstats
path: root/testcases/Dashboard/vPing2Dashboard.py
blob: d32187afd0a8145bf09dc92c1be58ac0bea1d7c7 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python
#
# Copyright (c) 2015 Orange
# morgan.richomme@orange.com
#
# 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
#
# This script is used to get data from test DB
# and format them into a json format adapted for a dashboard
#
# v0.1: basic example
#
import logging
import argparse
import pprint
import json
import requests

pp = pprint.PrettyPrinter(indent=4)

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", help="Debug mode",  action="store_true")
args = parser.parse_args()

""" logging configuration """
logger = logging.getLogger('vPing2DashBoard')
logger.setLevel(logging.DEBUG)

ch = logging.StreamHandler()
if args.debug:
    ch.setLevel(logging.DEBUG)
else:
    ch.setLevel(logging.INFO)

formatter = logging.Formatter  # retrieve info from DB
('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)


def get_data(test_project, testcase, pod_id, days, version, installerType):
    # ugly hard coding => TODO use yaml config file
    url = "http://213.77.62.197:80/results"

    # use param to filter request to result DB
    # if not precised => no filter
    # filter criteria:
    # - POD
    # - versions
    # - installers
    # - testcase
    # - test projects
    # - timeframe (last 30 days, 365 days, since beginning of the project)
    # e.g.
    # - vPing tests since 2 months
    # - Tempest tests on LF POD2 fuel based / Arno stable since the beginning
    # - yardstick tests on any POD since 30 days
    # - Qtip tests on dell-test1 POD
    #
    # params = {"pod_id":pod_id, "testcase":testcase}
    # filter_date = days # data from now - days

    # TODO complete params (installer type, testcase, version )
    params = {"pod_id": 1}

    # Build headers
    headers = {'Content-Type': 'application/json'}

    # Send Request to Test DB
    myData = requests.get(url, data=json.dumps(params), headers=headers)
    # Get result as a json object
    myNewData = json.loads(myData.text)

    # Get results
    myDataResults = myNewData['test_results']

    # Depending on the use case, json for dashboarding is customized
    # depending on the graph you want to show

    test_data = [{'description': 'vPing results for Dashboard'}]

    # Graph 1: Duration = f(time)
    new_element = []
    for data in myDataResults:
        new_element.append({'x': data['creation_date'],
                            'y': data['details']['duration']})

    test_data.append({'name': "vPing duration",
                      'info': {'type': "graph",
                               'xlabel': 'time',
                               'ylabel': 'duration (s)'},
                      'data_set': new_element})

    # Graph 2: bar
    nbTest = 0
    nbTestOk = 0

    for data in myDataResults:
        nbTest += 1
        if data['details']['status'] == "OK":
            nbTestOk += 1

    # Generate json file
    # TODO complete with other criteria
    fileName = "result-" + test_project + "-" + testcase + "-" + str(days) + "-" + str(pod_id) + "-status.json"

    test_data.append({'name': "vPing status",
                      'info': {"type": "bar"},
                      'data_set': [{'Nb tests': nbTest,
                                    'Nb Success': nbTestOk}]})

    fileName = "result-" + test_project + "-" + testcase + ".json"

    with open(fileName, "w") as outfile:
        json.dump(test_data, outfile, indent=4)


def main():
    get_data('functest', 'vPing', 1, 30, 'Arno master', 'fuel')


if __name__ == '__main__':
    main()