Get the time spent by an issue in certain statuses
Problem
You need to track the time an issue spends in specific statuses.
Solution
Use the SIL™ code below to achieve this:
function getStatusesTimeSpent(string issueKey) {
// function that returns a map with key status and value total time spent
interval[] result;
string[] statusHistory = fieldHistory(issueKey, "status");
date oldDate = %issueKey%.created;
if(!size(statusHistory) > 0) {
//status never changed
result[%issueKey%.status] = currentDate() - oldDate;
return result;
}
string oldStatus = statusHistory[1];
interval timeSpent = 0;
for(number i = 3; i < size(statusHistory); i += 2) {
string newStatus = statusHistory[i];
date newDate = statusHistory[i-1];
timeSpent = newDate - oldDate;
if(isNotNull(result[oldStatus])) {
// if issue already was in this status add the time interval to the time spent
result[oldStatus] = result[oldStatus] + timeSpent;
} else {
// otherwise set the time spent for this status
result[oldStatus] = timeSpent;
}
oldStatus = newStatus;
oldDate = newDate;
}
//add also last status time interval
interval lastTime = currentDate() - oldDate;
if(isNotNull(result[oldStatus])) {
result[oldStatus] = result[oldStatus] + lastTime;
} else {
result[oldStatus] = lastTime;
}
return result;
}
Call the getStatusesTimeSpent function and enter the issue key as a parameter. This function returns an array that includes each status the issue went through, along with the total time spent in each status.
string[] timeSpentInStatuses = getStatusesTimeSpent("DEMO-1");
return timeSpentInStatuses[1]; // will return a time interval spent in status "Open" for issue "DEMO-1"To retrieve the time spent on each status, use its unique ID.
To find the ID for each status, you can either:
directly check the issuestatus table in the Jira database
or
go to the Status administration section in Jira, hover over the edit link for each status, and view the ID parameter in the corresponding URL.
Need support? Create a request with our support team.
Copyright © 2005 - 2026 Appfire | All rights reserved.
