Get Count of users whose last login time of last Calendar month

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2023

Hi Guys,

Need your help in fixing my script. I am trying to get a Count of the JIRA users last login time of last calendar month.  Like User whose last login time between 1st March to 31st March. 

Getting this error message :- 
groovy.lang.MissingMethodException: No signature of method: java.lang.Long.between() is applicable for argument types: (Date, Date) values: [Wed Mar 01 23:49:00 PST 2023, Fri Mar 31 23:49:00 PDT 2023]
at Script3103$_run_closure1.doCall(Script3103.groovy:27)
at Script3103.run(Script3103.groovy:26)

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import java.text.SimpleDateFormat;
import java.util.Date;
import com.atlassian.jira.user.util.UserUtil

UserUtil userUtil = ComponentAccessor.getUserUtil()
def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class)

def groupManager = ComponentAccessor.getGroupManager()

def adminGroup = groupManager.getUsersInGroup('jira-administrators')
def softwareGroup = groupManager.getUsersInGroup('jira-users')

def users = adminGroup + softwareGroup
users.unique()

// Get the start and end dates of the last calendar month
Calendar cal = Calendar.getInstance()
cal.add(Calendar.MONTH, -1)
cal.set(Calendar.DAY_OF_MONTH, 1)
Date startDate = cal.time
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH))
Date endDate = cal.time

def activeUsersWithLoginLastMonth = users.findAll { user ->
user.isActive() && loginManager.getLoginInfo(user.username)?.getLastLoginTime()?.between(startDate, endDate)
}

def countActiveUsersWithLoginLastMonth = activeUsersWithLoginLastMonth.size()

return countActiveUsersWithLoginLastMonth

2 answers

1 accepted

0 votes
Answer accepted
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2023

Correct Script to get count of User Last login Date is Previous Calendar Month:- 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.atlassian.jira.user.util.UserUtil

UserUtil userUtil = ComponentAccessor.getUserUtil()
def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class)

def groupManager = ComponentAccessor.getGroupManager()

def adminGroup = groupManager.getUsersInGroup('jira-administrators')
def softwareGroup = groupManager.getUsersInGroup('jira-users')

def users = adminGroup + softwareGroup
users.unique()

def activeUsers = users.findAll { user ->
user.isActive()
}

def calendar = Calendar.getInstance()
calendar.set(Calendar.DAY_OF_MONTH, 1)
calendar.add(Calendar.MONTH, -1)
calendar.set(Calendar.HOUR_OF_DAY, 0)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 0)
def startDate = calendar.time
log.warn startDate

calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
calendar.set(Calendar.HOUR_OF_DAY, 23)
calendar.set(Calendar.MINUTE, 59)
calendar.set(Calendar.SECOND, 59)
def endDate = calendar.time
log.warn endDate

def sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
def count = activeUsers.count { user ->
Long lastLoginTime = loginManager.getLoginInfo(user.username)?.getLastLoginTime()
if (lastLoginTime != null && lastLoginTime >= startDate.getTime() && lastLoginTime <= endDate.getTime() ) {
true
} else {
false
}
}

return count
0 votes
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2023

The problem is that in this line:

loginManager.getLoginInfo(user.username)?.getLastLoginTime()?.between(startDate, endDate)

getLastLoginTime() returns a Long. And Long doesn't have a between() method.

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2023

@Aron Gombas _Midori_  Can you please help me with the correct script ? 

Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2023

Hmm, I don't have the time for the bugfix, sorry.

If that's an option for you, you could use our app to export the users to an Excel spreadhsheet like this:

excel-user-export.png

Then filter the "Last Login" column and count the matching rows.

See: https://www.midori-global.com/products/better-excel-exporter-for-jira/data-center/documentation/user-export

Like Vikrant Yadav likes this
Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2023

Thanks @Aron Gombas _Midori_  I already have script for exporting JIRA Users Data in csv. No need of new plugin. :)


Anyway, thanks for your help. I have fixed my script. 

vasanth January 10, 2024

@Vikrant Yadav  can you share the script

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 10, 2024

Hi @vasanth 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.atlassian.jira.user.util.UserUtil

UserUtil userUtil = ComponentAccessor.getUserUtil()
def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class)

def groupManager = ComponentAccessor.getGroupManager()

def adminGroup = groupManager.getUsersInGroup('jira-administrators')
def softwareGroup = groupManager.getUsersInGroup('jira-software-users')

def users = adminGroup + softwareGroup
users.unique()

def activeUsers = users.findAll { user ->
user.isActive()
}

def calendar = Calendar.getInstance()
calendar.set(Calendar.DAY_OF_MONTH, 1)
calendar.add(Calendar.MONTH, -1)
calendar.set(Calendar.HOUR_OF_DAY, 0)
calendar.set(Calendar.MINUTE, 0)
calendar.set(Calendar.SECOND, 0)
def startDate = calendar.time
log.warn startDate

calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
calendar.set(Calendar.HOUR_OF_DAY, 23)
calendar.set(Calendar.MINUTE, 59)
calendar.set(Calendar.SECOND, 59)
def endDate = calendar.time
log.warn endDate

def sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
def count = activeUsers.count { user ->
Long lastLoginTime = loginManager.getLoginInfo(user.username)?.getLastLoginTime()
if (lastLoginTime != null && lastLoginTime >= startDate.getTime() && lastLoginTime <= endDate.getTime() ) {
true
} else {
false
}
}

return count
Like vasanth likes this
vasanth January 10, 2024

Thank you @Vikrant Yadav 

my request is to remove inactive and no longer active users in jira 

I couldn't get a solution for this.

we have the user's login with the Active directory 

can you help me on this

Vikrant Yadav
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 10, 2024

Suggest an answer

Log in or Sign up to answer