-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
106 lines (88 loc) · 3.46 KB
/
functions.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import datetime
import math
def get_job_view(execution, prev_execution, stackstorm_url):
"""
Gets a job view from the specified execution and previous execution
:param execution: dict
:param prev_execution: dict
:param stackstorm_url: string
:return: dict
"""
current_time = datetime.datetime.utcnow()
hash_code = abs(hash(execution['action']['name'])) % (10 ** 8)
estimated_duration = ''
prev_time_elapsed_since = ''
if execution and 'start_timestamp' in execution:
start_time = datetime.datetime.strptime(execution['start_timestamp'], '%Y-%m-%dT%H:%M:%S.%fZ')
elapsed_seconds = int((current_time - start_time).total_seconds())
else:
elapsed_seconds = 0
if prev_execution and 'elapsed_seconds' in prev_execution:
prev_elapsed_seconds = int(math.ceil(prev_execution['elapsed_seconds']))
else:
prev_elapsed_seconds = 0
if prev_execution:
prev_execution_id = prev_execution['id']
prev_build_name = prev_execution['id']
if 'end_timestamp' in prev_execution:
prev_end_time = datetime.datetime.strptime(prev_execution['end_timestamp'], '%Y-%m-%dT%H:%M:%S.%fZ')
prev_time_elapsed_since = int((current_time - prev_end_time).total_seconds())
if 'elapsed_seconds' in prev_execution:
estimated_duration = '{}s'.format(int(math.ceil(prev_execution['elapsed_seconds'])))
else:
prev_execution_id = ''
prev_build_name = ''
prev_build_duration = estimated_duration
progress = 0
if execution['status'] == 'succeeded':
status = 'successful'
elif execution['status'] == 'failed':
status = 'failing'
elif execution['status'] == 'running':
if prev_execution and prev_execution['status'] == 'failed':
status = 'failing running'
elif prev_execution and prev_execution['status'] == 'succeeded':
status = 'successful running'
else:
status = 'unknown running'
if prev_execution and (prev_execution['status'] == 'failed' or prev_execution['status'] == 'succeeded'):
if prev_elapsed_seconds > 0:
progress = int(math.floor((float(elapsed_seconds) / float(prev_elapsed_seconds)) * 100))
if progress > 100:
progress = 100
else:
progress = 100
else:
progress = 100
else:
status = 'unknown'
job_view = {
'name': execution['action']['name'],
'url': '{}/#/history/{}/general'.format(stackstorm_url, execution['id']),
'status': status,
'hashCode': hash_code,
'progress': progress,
'estimatedDuration': estimated_duration,
'headline': '',
'lastBuild': {
"timeElapsedSince": str(prev_time_elapsed_since),
"duration": prev_build_duration,
"description": '',
"name": prev_build_name,
"url": '{}/#/history/{}/general'.format(stackstorm_url, prev_execution_id),
},
'debug': {
'elapsed_seconds': elapsed_seconds,
'prev_elapsed_seconds': prev_elapsed_seconds,
}
}
return job_view
def grequests_exception_handler(async_request, exception):
# print "Request failed"
# print async_request.url
pass
def grequests_response_handler(response, *args, **kwargs):
# print response.url
# print response.status_code
# print response.text
pass