3 # This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 from urllib2 import Request, urlopen, URLError
12 import reportingConf as conf
15 def getLogger(module):
16 logFormatter = logging.Formatter("%(asctime)s [" +
18 "] [%(levelname)-5.5s] %(message)s")
19 logger = logging.getLogger()
21 fileHandler = logging.FileHandler("{0}/{1}".format('.', conf.LOG_FILE))
22 fileHandler.setFormatter(logFormatter)
23 logger.addHandler(fileHandler)
25 consoleHandler = logging.StreamHandler()
26 consoleHandler.setFormatter(logFormatter)
27 logger.addHandler(consoleHandler)
28 logger.setLevel(conf.LOG_LEVEL)
32 def getApiResults(case, installer, scenario, version):
33 results = json.dumps([])
34 # to remove proxy (to be removed at the end for local test only)
35 # proxy_handler = urllib2.ProxyHandler({})
36 # opener = urllib2.build_opener(proxy_handler)
37 # urllib2.install_opener(opener)
38 # url = "http://127.0.0.1:8000/results?case=" + case + \
39 # "&period=30&installer=" + installer
40 url = ("http://" + conf.URL_BASE + "?case=" + case +
41 "&period=" + str(conf.PERIOD) + "&installer=" + installer +
42 "&scenario=" + scenario + "&version=" + version +
43 "&last=" + str(conf.NB_TESTS))
44 request = Request(url)
47 response = urlopen(request)
49 results = json.loads(k)
51 print 'No kittez. Got an error code:', e
56 def getScenarios(case, installer, version):
59 url = ("http://" + conf.URL_BASE + "?case=" + case +
60 "&period=" + str(conf.PERIOD) + "&installer=" + installer +
61 "&version=" + version)
62 request = Request(url)
65 response = urlopen(request)
67 results = json.loads(k)
68 test_results = results['results']
70 print 'Got an error code:', e
72 if test_results is not None:
73 test_results.reverse()
77 for r in test_results:
78 # Retrieve all the scenarios per installer
79 if not r['scenario'] in scenario_results.keys():
80 scenario_results[r['scenario']] = []
81 scenario_results[r['scenario']].append(r)
83 return scenario_results
86 def getScenarioStats(scenario_results):
88 for k, v in scenario_results.iteritems():
89 scenario_stats[k] = len(v)
94 def getNbtestOk(results):
97 for k, v in r.iteritems():
102 print "Cannot retrieve test status"
106 def getResult(testCase, installer, scenario, version):
108 # retrieve raw results
109 results = getApiResults(testCase, installer, scenario, version)
110 # let's concentrate on test results only
111 test_results = results['results']
113 # if results found, analyze them
114 if test_results is not None:
115 test_results.reverse()
117 scenario_results = []
119 # print " ---------------- "
121 # print " ---------------- "
122 # print "nb of results:" + str(len(test_results))
124 for r in test_results:
125 # print r["start_date"]
126 # print r["criteria"]
127 scenario_results.append({r["start_date"]: r["criteria"]})
129 scenario_results.sort()
130 # 4 levels for the results
131 # 3: 4+ consecutive runs passing the success criteria
132 # 2: <4 successful consecutive runs but passing the criteria
133 # 1: close to pass the success criteria
134 # 0: 0% success, not passing
135 # -1: no run available
136 test_result_indicator = 0
137 nbTestOk = getNbtestOk(scenario_results)
139 # print "Nb test OK (last 10 days):"+ str(nbTestOk)
140 # check that we have at least 4 runs
141 if len(scenario_results) < 1:
142 # No results available
143 test_result_indicator = -1
145 test_result_indicator = 0
147 test_result_indicator = 1
149 # Test the last 4 run
150 if (len(scenario_results) > 3):
151 last4runResults = scenario_results[-4:]
152 nbTestOkLast4 = getNbtestOk(last4runResults)
153 # print "Nb test OK (last 4 run):"+ str(nbTestOkLast4)
154 if nbTestOkLast4 > 3:
155 test_result_indicator = 3
157 test_result_indicator = 2
159 test_result_indicator = 2
160 return test_result_indicator