Search code examples
javajirajira-rest-api

Jira - How to calculate count of days in status?


Jira Data Center. How to find the number of days a task was in progress? Will explain tostring - Status of the task it moved to fromString - Status of the task it came from

So, I need to understand how many days the task spent in work No need to take into account hours and minutes, just the number of days And at the same time, only those days in which the task had the In Progress status

This is what I have

import com.atlassian.jira.component.ComponentAccessor

// Status to be counted
final statusName = 'In Progress'

def changeHistoryManager = ComponentAccessor.changeHistoryManager
def datesInStatus = [:]

// Get all status change items
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
def previousDate = null

changeItems.findAll { it.toString == 'In Progress' || it.fromString == 'In Progress' }.each { item ->
     addMessage(item.toString())
}

This is the output from addMessage com.atlassian.jira.issue.history.ChangeItemBean@1e5b1b95[fieldType=jira,field=status,from=10614,fromString=In Progress,to=10614,toString=In Progress,created=2024-02-26 14:29 :31.846], com.atlassian.jira.issue.history.ChangeItemBean@21c854c9[fieldType=jira,field=status,from=14201,fromString=Testing,to=10614,toString=In Progress,created=2024-02-26 14:29:31.47], com.atlassian.jira.issue.history.ChangeItemBean@1b94684b[fieldType=jira,field=status,from=10614,fromString=In Progress,to=10501,toString=CODE REVIEW,created=2023 -12-13 18:02:23.731]


Solution

  • Your example values such as 2024-02-26 14:29:31.47 are faulty in that they lack an indication of time zone or offset-from-UTC. So we cannot know, for example, if that was 2:29 PM in Tokyo Japan or 2:29 PM in Toledo Ohio US, two very different moments several hours apart.

    If you want to pretend/assume those values are for generic 24-hour days, parse as a LocalDateTime.

    LocalDateTime ldt = LocalDateTime.parse( input.replace( " " , "T" ) ) ;
    

    Collect and sort. Feed the first and last into Duration.between if you define “days” as 24-hour chunks of time.

    Duration.between(
        LocalDateTime.parse( "2023 -12-13 18:02:23.731".replace( " " , "T" ) ) , 
        LocalDateTime.parse( "2024-02-26 14:29:31.846".replace( " " , "T" ) ) 
    ).toDays() 
    

    If you define “days” by the calendar, feed the pair of LocalDateTime objects to Period.between.