Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Scripted Field - Total Tasks Completed

Murphy
Contributor
May 20, 2019

Hello, 

I've created a script that counts the number of "Tasks" inside of a issue and cross checks that against the status to return a "Completed / Total Tasks (+ % Complete). Even though it works properly, it does take longer to load for issues in the range of (500 - 700 tasks).

My question is, is there a better way to go about this for performance reasons? I'm fairly certain this is optimized, but I could be wrong

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink

int tasks = 0
int completeTasks = 0

for(IssueLink linkedIssue: ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())){
if(linkedIssue.getDestinationObject().getIssueType().getName().equals("Task"))
++tasks
}

for(IssueLink linkedIssue: ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())){
if(linkedIssue.getDestinationObject().getIssueType().getName().equals("Task") && linkedIssue.getDestinationObject().status?.name=='Closed' || linkedIssue.getDestinationObject().getIssueType().getName().equals("Task") && linkedIssue.getDestinationObject().status?.name=='QA Review' || linkedIssue.getDestinationObject().getIssueType().getName().equals("Task") && linkedIssue.getDestinationObject().status?.name=='Done' )
++completeTasks
}
if (tasks>0) {
return completeTasks + "/" + tasks + " (" + new java.text.DecimalFormat("#.##").format((double)(completeTasks/tasks)*100) + "%)"
} else {
return 0 as int
}

 

1 answer

1 accepted

0 votes
Answer accepted
Murphy
Contributor
May 20, 2019

Just to follow up, I've shaved off 6-7 seconds by re-writing it to the following and avoiding adding up (++ tasks and ++ completeTasks) each time. 

I think my code below is as optimized as I can get, though I'll still leave the question open in case someone else has any ideas. 

 


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink

def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issueLinks = issueLinkManager.getOutwardLinks(issue.id)

//Search for issuetype of "Task"
def total = issueLinks.findAll {
it.getDestinationObject().getIssueType().getName().equals("Task")
}

//Look for status (Closed, Done, QA Review)
def tasks = issueLinks.findAll {
it.getDestinationObject().getIssueType().getName().equals("Task") && (it.getDestinationObject().status?.name=='Closed'|| it.getDestinationObject().status?.name=='QA Review' || it.getDestinationObject().status?.name=='Done')
}

if (tasks.size()>0){
return tasks.size() + "/" + total.size() + " (" + new java.text.DecimalFormat("#.##").format((double)(tasks.size()/total.size())*100) + "%)"
} else {
return 0 as int
}   

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events