add dashboard uploader for uploading results to community
[bottlenecks.git] / utils / dashboard / uploader.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import sys
11 import json
12 import requests
13 import yaml
14
15
16 class Uploader(object):
17
18     def __init__(self, conf):
19         self.headers = {'Content-type': 'application/json'}
20         self.timeout = 5
21         self.result = {
22             "project_name": "bottlenecks",
23             "description": "bottlenecks test cases result"}
24
25         with open(conf) as stream:
26             dashboard_conf = yaml.load(stream)
27         self.result['pod_name'] = dashboard_conf['pod_name']
28         self.result['installer'] = dashboard_conf['installer']
29         self.result['version'] = dashboard_conf['version']
30         self.target = dashboard_conf['target']
31
32
33     def upload_result(self, case_name, raw_data):
34         if self.target == '':
35             print('No target was set, so no data will be posted.')
36             return
37         self.result["case_name"] = case_name
38         self.result["details"] = raw_data
39
40         try:
41             print('Result to be uploaded:\n %s' % json.dumps(self.result))
42             res = requests.post(self.target,
43                                 data=json.dumps(self.result),
44                                 headers=self.headers,
45                                 timeout=self.timeout)
46             print('Test result posting finished with status code %d.' % res.status_code)
47         except Exception as err:
48             print ('Failed to record result data: %s', err)
49
50
51 def _test():
52
53     #data = '{"details": [{"client": 200, "throughput": 20}, {"client": 300, "throughput": 20}], "case_name": "rubbos"}'
54     if len(sys.argv) < 2:
55         print ("no argumens input!!")
56         exit(1)
57
58     with open(sys.argv[1],'r') as stream:
59         data = json.load(stream)
60         Uploader().upload_result(data)
61
62 if __name__ == "__main__":
63     _test()
64