Skip to content

Instantly share code, notes, and snippets.

@1328
Created November 16, 2014 01:38
Show Gist options
  • Select an option

  • Save 1328/6d6990cfd08257469dbe to your computer and use it in GitHub Desktop.

Select an option

Save 1328/6d6990cfd08257469dbe to your computer and use it in GitHub Desktop.
comments
from jira.client import JIRA
from datetime import datetime, date, timedelta
from BusinessHours import BusinessHours
import sys
global jira
global jiraURL
global jiraUser
global jiraPass
global project
global jiraCreated
global jiraResolved
# Specify JIRA authentication here
# it's fine to use globals for configuration details like below
# but consider whether you really want to store user/pass in the script itself
jiraUser = ''
jiraPass = ''
project = ''
jiraURL = ''
def sla(severity,timeToRespond):
# maybe better:
limits = {
'Severity 1':4,
'Severity 2':8,
'Severity 3':12, # look how easy to expand
'other': 24,
}
if severity not in limits:
severity = 'other'
if time_to_respond < limits[severity]:
return 'yes'
else:
return 'no'
def getResults(firstDay, lastDay):
jira = JIRA(options={'server': jiraURL}, basic_auth=(jiraUser, jiraPass))
# Only created and resolved dates are required for the queries
jqlCreated=('project = %s AND created >= %s AND created <= %s ORDER BY created DESC, key DESC') % (project, firstDay, lastDay)
jqlResolved=('project = %s AND resolved >= %s AND resolved <= %s ORDER BY created DESC, key DESC') % (project, firstDay, lastDay)
countCreate = 0
countResolved = 0
countOpen = 0
print "%s - %s" % (firstDay, lastDay)
print "Tickets created"
print "Key,Summary,Severity,Hours until first response,SLA Met"
for issue in jira.search_issues(jqlCreated):
jira_key = issue.key
summary = issue.fields.summary
severity = issue.fields.customfield_10030.value
if "," in jira_key or summary:
# this is always running where there is a summary, but it probably is
# not worth the added complexity to test for it anyway, but if you
# did, do:
if ',' in jira_key or ',' in summary:
jira_key = jira_key.replace(',', '')
summary = summary.replace(',', '')
if issue.fields.created:
created = issue.fields.created.split('.')[0]
created = datetime.strptime(created, '%Y-%m-%dT%H:%M:%S')
else:
created = "None"
if issue.fields.customfield_11971:
response = issue.fields.customfield_11971.split('.')[0]
response = datetime.strptime(response, '%Y-%m-%d %H:%M:%S')
else:
response = "None"
if response is not "None":
timeToRespond = BusinessHours(created,response)
timeToRespond = timeToRespond.gethours()
slaMet = sla(severity,timeToRespond)
timeToRespond = str(timeToRespond)
timeToRespond = timeToRespond.split('.')[0]
else:
timeToRespond = "No Response"
slaMet = "Unknown"
countCreate+=1
print jira_key + "," + summary + "," + severity + "," + timeToRespond + "," + slaMet
# use join or format for the previout line
print ', '.join([jira_key, summary, severity ...
print "Created ticket count: [ %s ]\n" % countCreate
print "Tickets resolved"
print "Key,Summary,Time until resolution"
# break this and previous one into separe functions
# in fact, break them into functions with shared sub-functions, since you are
# doing a bunch of duplicative things
for issue in jira.search_issues(jqlResolved):
jira_key = issue.key
summary = issue.fields.summary
if "," in jira_key or summary:
jira_key = jira_key.replace(',', '')
summary = summary.replace(',', '')
if issue.fields.created:
created = issue.fields.created.split('.')[0]
created = datetime.strptime(created, '%Y-%m-%dT%H:%M:%S')
else:
created = "None"
if issue.fields.customfield_10474:
resolved = issue.fields.customfield_10474.split('.')[0]
resolved = datetime.strptime(resolved, '%Y-%m-%dT%H:%M:%S')
else:
resolved = "None"
if resolved is not "None":
timeToResolve = BusinessHours(created,resolved)
timeToResolve = timeToResolve.gethours()
timeToResolve = str(timeToResolve)
timeToResolve = timeToResolve.split('.')[0]
else:
timeToResolve = "None"
print jira_key + "," + summary + "," + timeToResolve
countResolved+=1
print "Resolved ticket count: [ %s ]" % countResolved
def customerList():
# I might break up even more and move inputting the range into its own
# function. This would be useful esp. if you want to add error checking
# later, e.g. making sure the input fits the format expected.
firstDay = raw_input('Please enter first day yyyy-mm-dd: >')
lastDay = raw_input('Please enter last day yyyy-mm-dd: >')
firstDay = str(firstDay)
lastDay = str(lastDay)
getResults(firstDay, lastDay)
if __name__ == "__main__":
customerList()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment