Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Get time spent in certain status per issue

Table of Contents

 


Info
titleRequired apps

Power Scripts Scripts™ for Jira (server)

Level: BASIC

...

Writing the code

The following SIL SIL™ code does all the magic:

Code Block
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;
}

...