Skip to content

Commit

Permalink
Fix Bushfire Recovery Projects Stats panel #385
Browse files Browse the repository at this point in the history
 - Finish refactor of projectTypeName for generateStatus
 - Move individual user details calls to grouped user details calls
 - Pre sort and sublist leaderboard score map to cut down db / userdetails loading
  • Loading branch information
sbearcsiro committed Apr 20, 2020
1 parent 0d5e1a3 commit 24386fb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class IndexController {
[:]
}

def stats(long institutionId, long projectId, String projectType, List<String> tags) {
def stats(long institutionId, long projectId, String projectType) {
List<String> tags = params.list('tags') ?: []
def maxContributors = (params.maxContributors as Integer) ?: 5
def disableStats = params.getBoolean('disableStats', false)
def disableHonourBoard = params.getBoolean('disableHonourBoard', false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,22 +171,25 @@ class LeaderBoardService {
scoreMap[kvp.key] += kvp.value
}

scoreMap = scoreMap.sort { it.value }
if (scoreMap.size() > count) {
scoreMap = scoreMap.take(count)
}

// Flatten the map into a list for easy sorting, so we can slice off the top N
def list = []
def userDetails = userService.detailsForUserIds(scoreMap.keySet() as List<String>).collectEntries { [(it.userId): it] }
scoreMap.each { kvp ->
def user = User.findByUserId(kvp.key)
def details = userService.detailsForUserId(kvp.key)
def details = userDetails[kvp.key]
if (user) {
list << [name: details?.displayName, email: details?.email, score: kvp?.value ?: 0, userId: user?.id]
} else {
println "Failed to find user with key: ${kvp.key}"
}
}

// Sort in descending order...
list?.sort { a, b -> b.score <=> a.score }
// and just return the top N items
return list.subList(0, Math.min(list.size(), count))
return list
}

Map getUserMapForPeriod(Date startDate, Date endDate, ActivityType activityType, Institution institution, List<String> ineligibleUserIds, def projectsInLabels = null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class VolunteerStatsService {

LinkGenerator grailsLinkGenerator

@Cacheable(value = 'MainVolunteerContribution', key = "(#institutionId?.toString()?:'-1') + (#projectId?.toString()?:'-1') + (#projectType?:'') + (#tags?.toString()?:'[]') + (#disableStats.toString()) + (#disableHonourBoard.toString())")
@Cacheable(value = 'MainVolunteerContribution', key = "(#institutionId?.toString()?:'-1') + (#projectId?.toString()?:'-1') + (#projectTypeName?:'') + (#tags?.toString()?:'[]') + (#maxContributors.toString()) + (#disableStats.toString()) + (#disableHonourBoard.toString())")
def generateStats(long institutionId, long projectId, String projectTypeName, List<String> tags, int maxContributors, boolean disableStats, boolean disableHonourBoard) {
Institution institution = (institutionId == -1l) ? null : Institution.get(institutionId)
Project projectInstance = (projectId == -1l) ? null : Project.get(projectId)
Expand All @@ -43,7 +43,7 @@ class VolunteerStatsService {
}
}

log.debug("Generating stats for inst id $institutionId, proj id: $projectId, maxContrib: $maxContributors, disableStats: $disableStats, disableHB: $disableHonourBoard, projectType: $projectType, projectsInLabels: $projectsInLabels")
log.debug("Generating stats for inst id $institutionId, proj id: $projectId, maxContrib: $maxContributors, disableStats: $disableStats, disableHB: $disableHonourBoard, projectType: $projectTypeName, projectsInLabels: $projectsInLabels")

def sw = Stopwatch.createStarted()

Expand Down Expand Up @@ -105,17 +105,17 @@ class VolunteerStatsService {

}

def generateContributors(Institution institution, Project projectInstance, def pt = null, maxContributors) {
def generateContributors(Institution institution, Project projectInstance, List<Long> projectIds, Integer maxContributors) {

def latestTranscribers = LatestTranscribers.withCriteria {
if (institution) {
project {
eq('institution', institution)
ne('inactive', true)
}
} else if (pt) {
} else if (projectIds) {
project {
'in' 'id', pt
'in' 'id', projectIds
ne('inactive', true)
}
} else if (projectInstance) {
Expand All @@ -134,10 +134,10 @@ class VolunteerStatsService {
if (institution) {
latestMessages = ForumMessage.findAll('FROM ForumMessage fm WHERE fm.topic.project.institution = :institution ORDER BY date desc', [institution: institution], [max: maxContributors])
latestMessages += ForumMessage.findAll('FROM ForumMessage fm WHERE fm.topic.task.project.institution = :institution ORDER BY date desc', [institution: institution], [max: maxContributors])
} else if (pt) {
} else if (projectIds) {
// latestMessages = ForumMessage.findAll('FROM ForumMessage fm WHERE fm.topic.project.projectType = :pt ORDER BY date desc', [pt: pt], [max: maxContributors])
// latestMessages += ForumMessage.findAll('FROM ForumMessage fm WHERE fm.topic.task.project.projectType = :pt ORDER BY date desc', [pt: pt], [max: maxContributors])
def projects = Project.findAllByIdInList(pt)
def projects = Project.findAllByIdInList(projectIds)
/*Project.createCriteria().list {
and {
if (pt) {
Expand Down Expand Up @@ -198,10 +198,11 @@ class VolunteerStatsService {
}
}

def userDetails = userService.detailsForUserIds(latestMessages*.user*.userId).collectEntries { [(it.userId): it] }
def messages = latestMessages.collect {
def topic = it.topic
def topicId = topic.id
def details = userService.detailsForUserId(it.user.userId)
def details = userDetails[it.user.userId]
def timestamp = it.date.time / 1000
def topicUrl = grailsLinkGenerator.link(controller: 'forum', action: 'viewForumTopic', id: topic.id)

Expand Down Expand Up @@ -229,10 +230,11 @@ class VolunteerStatsService {
thumbnailUrl: thumbnail, timestamp: timestamp]
}

userDetails = userService.detailsForUserIds(latestTranscribers*.fullyTranscribedBy).collectEntries { [(it.userId): it] }
def transcribers = latestTranscribers.collect {
def proj = it.project
def userId = it.fullyTranscribedBy
def details = userService.detailsForUserId(userId)
def details = userDetails[userId]

def tasks = LatestTranscribersTask.withCriteria() {
eq('project', proj)
Expand Down

0 comments on commit 24386fb

Please sign in to comment.