Find Inactive Users within a Date Range

Diana
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.
December 15, 2022

I'm trying to get the number of users who became Inactive within the past year.

I followed this script https://library.adaptavist.com/entity/retrieve-list-of-inactive-users and added a table to print the list for me, but the problem is the script still pulls up all Inactive users. Meaning I'm seeing users last login since 2015 when I just want to see inactive users whose last login was within 2022.

import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.crowd.embedded.api.CrowdService
import groovy.xml.MarkupBuilder

import java.time.LocalDateTime
import java.time.Instant
import java.time.ZoneId

def crowdService = ComponentAccessor.getComponent(CrowdService)

def result ="<table class='aui'"

// review in the last year
def numOfDays = 365
def dateLimit = LocalDateTime.now().minusDays(numOfDays)

// Search all inactive users
UserSearchParams.Builder paramBuilder = UserSearchParams.builder()
.allowEmptyQuery(true)
.includeActive(false)
.includeInactive(true)

def jiraServiceContext = new JiraServiceContextImpl(ComponentAccessor.jiraAuthenticationContext.loggedInUser)
def allInactiveUsers = ComponentAccessor.getComponent(UserSearchService).findUsers(jiraServiceContext, '', paramBuilder.build())

// Users which last activity is after limit date
def inactivePastYear = allInactiveUsers.findAll { user ->
def userWithAtributes = crowdService.getUserWithAttributes(user.username)
def lastLoginMillis = userWithAtributes.getValue('login.lastLoginMillis')

if (lastLoginMillis?.number) {
def lastLogin = Instant.ofEpochMilli(Long.parseLong(lastLoginMillis)).atZone(ZoneId.systemDefault()).toLocalDateTime()
if (lastLogin.isAfter(dateLimit)) {
def sb = new StringBuffer()

result+="<tr><td>${user.displayName}</td><td>${lastLogin.toString()</tr><td>"}
}
}
result +="</table>"
return result

Is there a way, maybe, I can have the script check between 2 dates? Between start of Year to end of Year?

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 22, 2022

Hi @Diana Gorv

There is another example code for this in the Adaptavist Library. This example uses the Escalation Service.

Since you only want the list of inactive users, you will need to filter the code a bit.

Give it a try and see how it goes.

Thank you and Kind regards,
Ram

TAGS
AUG Leaders

Atlassian Community Events