forked from bognix/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.py
83 lines (63 loc) · 2.35 KB
/
dashboard.py
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
import json
import datetime
from flask import Flask, jsonify
from flask.templating import render_template
from jenkinsapi.custom_exceptions import NoBuildData
from jenkinsapi.jenkins import Jenkins
from requests.exceptions import ConnectionError
app = Flask(__name__)
def get_jenkins():
return Jenkins(get_config()['sources']['jenkins']['url'])
def get_config():
return json.load(open('config.json'))
@app.route('/')
def index():
config_data = get_config()
screens_count = len(config_data['screens'])
return render_template(
'index.html', config=config_data, json_config=json.dumps(config_data), screens_count=screens_count)
@app.route('/jenkins_results/<build_name>', methods=['GET'])
def get_build_data(build_name):
jenkins_instance = get_jenkins()
if jenkins_instance is not None:
build = jenkins_instance[build_name]
else:
raise ConnectionError("Connection with Jenkins failed")
last_build = build.get_last_build()
last_build_number = build.get_last_buildnumber()
child_runs = last_build.get_matrix_runs()
child_runs_count = 0
failed_runs = []
has_next = True
while has_next:
try:
current_build = child_runs.next()
except StopIteration:
has_next = False
if has_next:
child_runs_count += 1
if current_build.get_number() == last_build_number and current_build.get_status() == 'FAILURE':
failed_runs.append({
'name': current_build.name.split('\xbb')[1].split(',')[0]
})
return_val = {
'name': build_name,
'status': last_build.get_status(),
'hours_ago': get_time_ago(last_build.get_timestamp()),
'failed_runs': failed_runs,
'has_failed_runs': (len(failed_runs) != 0),
'child_runs_count': child_runs_count,
'failure_percentage': len(failed_runs) * 100 / child_runs_count
}
try:
last_success = get_time_ago(build.get_last_stable_build().get_timestamp()),
except NoBuildData:
last_success = '???'
return_val['last_success'] = last_success
return jsonify(return_val)
def get_time_ago(run_date):
return int((datetime.datetime.utcnow().replace(tzinfo=None)
- run_date.replace(tzinfo=None)).total_seconds() / 3600)
if __name__ == '__main__':
app.debug = True
app.run()