(8.7.10) Work log during a period
Title | Time spent last month |
---|---|
Description | Extract the total time spent last month on all the issues of every project. |
Objective | To know how much time it has been work log the previous month in all the issues of every project.. |
Audience |
|
What does a Jira Project Represent? | A Jira Project of any type, where you log work on issues |
Focus | Project tracking and traceability |
Related scripts | Follow up contracted hours by 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:
Description: It gets the total time spent of the work log of last month.
Use in: It should be used as a Projectrak script field
Variables in context: N/A
Define which month to track: modify on the script the number of line months ago → MONTHS_AGO = 1 // E.g. 0 for current, 1 for last month
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).