Document toolboxDocument toolbox

(8.9.X) Work log during a period

Description

It gets the total time spent of the work log of last month.

Configuration

1. Create a Projectrak Script field called "Time spent last month"

2. Insert the following Groovy script and modify accordingly to your instance:

Define which range of period you want to track:

  • Modify on the script the number of time (weeks, months) ago → AGO = 1 // E.g. 0 for current, 1 for last (week or month)
  • Modify the period you want to measure -> RANGE = Calendar.MONTH by RANGE or Calendar.WEEK_OF_YEAR

Groovy Script

Time_spent_last_month.groovy
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.project.Project
import com.atlassian.jira.web.bean.PagerFilter


def searchService = ComponentAccessor.getOSGiComponentInstanceOfType(SearchService)
def jiraAuthenticationContext = ComponentAccessor.jiraAuthenticationContext
def worklogManager = ComponentAccessor.worklogManager
def jiraDurationUtils = ComponentAccessor.jiraDurationUtils

/* CONFIGURATION */

AGO = 1 // E.g. 0 for current, 1 for last (week or month)
RANGE = Calendar.MONTH   //  Calendar.WEEK_OF_YEAR or Calendar.MONTH

/* END CONFIGURATION */

def loggedInUser = jiraAuthenticationContext.getLoggedInUser()

def query = JqlQueryBuilder.newBuilder().where().project(((Project) project).id).buildQuery()

def calendar = Calendar.getInstance()
calendar.setTime(new Date())
calendar.add(RANGE, -AGO)
range = calendar.get(RANGE)
year = calendar.get(Calendar.YEAR)

def worklog = searchService.search(loggedInUser, query, PagerFilter.unlimitedFilter).results
        .collect { issue ->

            worklogManager.getByIssue(issue)
                    .findAll { worklog -> worklog != null }
                    .findAll { worklog ->
                        def cal = Calendar.getInstance()
                        cal.setTime(worklog.startDate)
                        cal.get(Calendar.YEAR) == year && cal.get(RANGE) == range
                    }
                    .collect { worklog -> worklog.timeSpent }
                    .sum()

        }
        .findAll { time -> time != null }
        .sum()

jiraDurationUtils.getShortFormattedDuration(!worklog ? 0 : worklog as Long)

3. If you need to see this data on project data page, configure the project's layout including "Time spent last month" field 

4. The result on Project Navigator should be something like this: (please, keep in mind that this is only an example and your results should vary from what it is shown in the screenshot).