06bcd4926bf97195f00703489a7d58d671afab1c
[releng.git] / utils / test / reporting / yardstick / reportingUtils.py
1 #!/usr/bin/python
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 from urllib2 import Request, urlopen, URLError
10 import logging
11 import json
12 import reportingConf as conf
13
14
15 def getLogger(module):
16     logFormatter = logging.Formatter("%(asctime)s [" +
17                                      module +
18                                      "] [%(levelname)-5.5s]  %(message)s")
19     logger = logging.getLogger()
20
21     fileHandler = logging.FileHandler("{0}/{1}".format('.', conf.LOG_FILE))
22     fileHandler.setFormatter(logFormatter)
23     logger.addHandler(fileHandler)
24
25     consoleHandler = logging.StreamHandler()
26     consoleHandler.setFormatter(logFormatter)
27     logger.addHandler(consoleHandler)
28     logger.setLevel(conf.LOG_LEVEL)
29     return logger
30
31
32 def getScenarioStatus(installer, version):
33     url = (conf.URL_BASE + "?case=" + "scenario_status" +
34            "&installer=" + installer +
35            "&version=" + version +"&period=" + str(conf.PERIOD))
36     request = Request(url)
37
38     try:
39         response = urlopen(request)
40         k = response.read()
41         response.close()
42         results = json.loads(k)
43         test_results = results['results']
44     except URLError, e:
45         print 'Got an error code:', e
46
47     scenario_results = {}
48     if test_results is not None:
49         for r in test_results:
50             if r['stop_date'] != 'None':
51                 if not r['scenario'] in scenario_results.keys():
52                     scenario_results[r['scenario']] = []
53                 scenario_results[r['scenario']].append(r)
54
55         for k,v in scenario_results.items():
56             scenario_results[k] = v[:conf.LASTEST_TESTS]
57
58     return scenario_results
59
60
61 def _test():
62     status = getScenarioStatus("compass", "master")
63     print "status:++++++++++++++++++++++++"
64     print json.dumps(status,indent=4)
65
66
67 if __name__ == '__main__':    # pragma: no cover
68     _test()