Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner: Iterating through an issue filter to calculate number of issue created by person

Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2021

I was asked to provide stats for all users submitted issue in the last year.

My first thought was export issues to Excel which seemed to disappear.

So I created a script to run in the console to do the math which worked but only up to 100 maxResult

def query = 'project = "Project name" AND created > "-365d" ORDER BY created ASC'

def searchReq = get("/rest/api/2/search")
.queryString("jql", query)
.queryString("maxResults", "1000") //only returns 100!
.asObject(Map)

Map searchResult = searchReq.body

def issuesByUser = [:]
def user = ''
def count = 0
searchResult.issues.each { Map issue ->
user = issue.fields.reporter.displayName
created = issue.fields.created
count += 1
//logger.info("${count} ${user} ${created}")
issuesByUser.putIfAbsent(user, 0)
issuesByUser.find{it.key == user}.value += 1

}
logger.info("Total ${count} issue")

def total = 0
issuesByUser.each {i ->
logger.info("${i.key} ${i.value}")
total += i.value
}
logger.info("Total ${total} issue")

I could use some help with that! 

Thank you

2 answers

1 accepted

2 votes
Answer accepted
Kristian Walker _Adaptavist_
Community Champion
January 22, 2021

Hi Alexy,

When using the Search for issues API if you want to return more than 100 issues you will need to use pagination by having multiple search queries which the extra issues.

This means for the second query and onwards you would an extra queryString paramater of startAt as documented in the API documentation page above as this would return the next 100 issues if you started from 100 and then 200 etc.

I hope this helps.

Regards,

Kristian

Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2021

Thanks.

Just did that and got "Execution timed out after 60s."

Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2021

def query = 'project = "Project name" AND created > "-365d" ORDER BY created ASC'

def issuesByUser = [:]
def user = ''
def count = 0
def pageSize = 100
def issuesGot = 0
Map searchResult = [:]
def totalIssues = 0

while (true) {
def searchReq = get("/rest/api/2/search")
.queryString("jql", query)
.queryString("maxResults", issuesGot+pageSize <= totalIssues ? pageSize : totalIssues - issuesGot - 1)
.queryString("startAt", issuesGot+1 )
// .queryString("fields", "Flagged")
.asObject(Map)
issuesGot += pageSize

totalIssues = searchReq.body.total
logger.info("Total issues ${totalIssues}")
//logger.info(searchReq.inspect())

searchResult = searchReq.body


searchResult.issues.each { Map issue ->
//print (issue.fields.reporter.displayName)
user = issue.fields.reporter.displayName
created = issue.fields.created
count += 1
//logger.info("${count} ${user} ${created}")
issuesByUser.putIfAbsent(user, 0)
issuesByUser.find{it.key == user}.value += 1

}
logger.info("Total ${count} issue")
if (count >= totalIssues) break
}
def total = 0
issuesByUser.each {i ->
logger.info("${i.key} ${i.value}")
total += i.value
}
logger.info("Total ${total} issue")

Kristian Walker _Adaptavist_
Community Champion
January 23, 2021

Hi Alexy,

I can confirm that we impose a timeout limit on scripts of 60 seconds in order to prevent long-running code as described in the documentation page here.

This means you will need to split your logic up into multiple scripts if your script is exceeding the 60-second timeout limit.

However, I notice from your script you have the line of // .queryString("fields", "Flagged") commented out which means as you are not restricting the fields you need on each issue that you are returning every field on the issue which would cause the search API to take a long time to return.

I would advise uncommenting this line and only returning the fields you need on each issue and if you are just counting the issues then just to return the summary field as doing this will cause the API to return faster and may help with the timeout limits that you are getting.

Regards,

Kristian

Like # people like this
Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2021

Thanks Kristian!

 

The flagged part was from the script example provided 

Can you please share a snippet where only few relevant fields returned? 

Thanks a ton 

Like Anders N likes this
Kristian Walker _Adaptavist_
Community Champion
January 24, 2021

Hi Alexy,

To return only the relevant fields you would need to change that line to be the fields you require, an example is below which shows how to return the summary and a custom field by its ID.

In this query string, you would need to specify the names of all system fields and any custom fields you wish to return.

queryString("fields", "summary,customfield_12345")

An example of using this is in the documentation page here.

However, in your code, it looks like just get the reporter field off the issues so you could try returning just this field off of the issues.

I hope this helps.

Regards,

Kristian

Like Anders N likes this
Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 27, 2021

Thank you Kristian!

That worked so much faster this way with only few select fields requested!

1 vote
Hana Kučerová
Community Champion
January 22, 2021

Hi @Alexey Paveliev ,

I will try to help you with the code, but please, did you try Pie chart gadget? It should give you the same results without groovy scripting...

Hana Kučerová
Community Champion
January 22, 2021

Oh, I mostly forget on Issue statistics gadget - it can be used, when you want only simple table :-)

Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2021

Thanks!

there are around 1200 issues submitted by too many people to fit in a chart

Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 22, 2021

See code below that times out at issues over 1200 but luckily works up to 1000 issues 

Hana Kučerová
Community Champion
January 22, 2021

Hi @Alexey Paveliev ,

did you also check the Issue statictics gadget? I think it is exactly what you need - list of users with number of created issue

  • Statistic Type: Reporter
  • Show Resolved Issue Statistics: Yes

I got results for ~ 20 000 issues and 300 users without any problem.

I wasn't able to find it in cloud documentation, but is looks the same in the server :-).

Please, don't get me wrong, I don't want to force you and I love ScriptRunner, but I always prefer OOB functionality and I use SR only if really necessary :-).

Alexey Paveliev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 23, 2021

Thanks for the suggestion. I was not able to find the report in my cloud instance though

Hana Kučerová
Community Champion
January 23, 2021

@Alexey Paveliev 

Please see here. In general, you need to navigate to one of your dashboards, which you are able to edit, open dialog for adding gadget using button in the right top corner, click on Load all gadgets to get all available gadgets, search for Issue Statistics, add this gadget, configure it and save.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events