-
Notifications
You must be signed in to change notification settings - Fork 3
/
resolution_times.py
executable file
·126 lines (108 loc) · 3.85 KB
/
resolution_times.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
import time
import datetime
import requests
import sys
import json
from urllib import urlencode
#Your PagerDuty API key. A read-only key will work for this.
AUTH_TOKEN = 'YQstXoCv5Jsib56A6zeu'
#The API base url, make sure to include the subdomain
BASE_URL = 'https://pdt-ryan.pagerduty.com/api/v1'
#The service that you would like to get data on
service_id = "PFZ3ZG7"
HEADERS = {
'Authorization': 'Token token={0}'.format(AUTH_TOKEN),
'Content-type': 'application/json',
}
total_time = 0
total_ack_time = 0
incident_count = 0
escalation_count = 0
acked_count = 0
def get_service_name(service_id):
params = {
'service':service_id
}
services = requests.get(
'{0}/services/{1}'.format(BASE_URL,service_id),
headers=HEADERS,
data=json.dumps(params)
)
return services.json()['service']['name']
def get_incident_count(service_id):
global incident_count
params = {
'service':service_id,
'date_range':'all'
}
count = requests.get(
'{0}/incidents/count'.format(BASE_URL),
headers=HEADERS,
data=json.dumps(params)
)
incident_count = count.json()['total']
def get_incidents(service_id, offset):
global total_time
params = {
'offset':offset,
'limit':100,
'service':service_id,
'date_range':'all'
}
all_incidents = requests.get(
'{0}/incidents'.format(BASE_URL),
headers=HEADERS,
data=json.dumps(params)
)
print "Listing all incidents:"
for incident in all_incidents.json()['incidents']:
print "{0}:{1}".format(incident["incident_number"],incident["id"])
get_incident_times(incident["id"])
def get_incident_times(incident_id):
global total_time
global total_ack_time
global escalation_count
global acked_count
start_time = ""
end_time = ""
ack_time = ""
params = {
'is_overview': True
}
log_entries = requests.get(
'{0}/incidents/{1}/log_entries'.format(BASE_URL,incident_id),
headers=HEADERS
)
for log_entry in log_entries.json()['log_entries']:
if log_entry["type"] == "trigger":
if log_entry["created_at"] > start_time:
start_time = time.mktime(datetime.datetime.strptime(log_entry["created_at"],"%Y-%m-%dT%H:%M:%SZ").timetuple())
elif log_entry["type"] == "resolve":
end_time = time.mktime(datetime.datetime.strptime(log_entry["created_at"],"%Y-%m-%dT%H:%M:%SZ").timetuple())
elif log_entry["type"] == "acknowledge":
ack_time = time.mktime(datetime.datetime.strptime(log_entry["created_at"],"%Y-%m-%dT%H:%M:%SZ").timetuple())
elif log_entry["type"] in ("escalate", "assign"):
escalation_count = escalation_count + 1
if end_time:
elapsed_time = (end_time - start_time)/60
total_time = total_time + elapsed_time
if ack_time:
ack_time = (ack_time - start_time)/60
total_ack_time = total_ack_time + ack_time
acked_count = acked_count + 1
print "Statistics for service: ", get_service_name(service_id)
get_incident_count(service_id)
print "Number of incidents: ", incident_count
for offset in xrange(0,incident_count):
if offset % 100 == 0:
get_incidents(service_id, offset)
print "Total unresolved incident time: %.2f minutes" % float(total_time)
if incident_count > 0:
print "Average resolution time: %.2f minutes" % (float(total_time)/float(incident_count))
print "Total escalations: ", escalation_count - incident_count
if escalation_count - incident_count > 0:
print "Average number of escalations: %.2f" % (float(escalation_count - incident_count)/float(incident_count))
print "Total acknowledgments: {0}".format(acked_count)
if acked_count > 0:
print "Average acknowledgement time: {0} minutes".format(float(total_ack_time)/float(acked_count))