Skip to content

Commit

Permalink
commit progress organisation metrics #3146
Browse files Browse the repository at this point in the history
  • Loading branch information
salomon-j committed Jun 24, 2024
1 parent 0438b78 commit 4766594
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,44 @@ class OrganisationController {
values
}

def organisationMetrics(String id) {

def organisation = Organisation.findByOrganisationId(id)

boolean approvedOnly = true
boolean targetsOnly = false
boolean includeTargets = true
List scoreIds
Map aggregationConfig = null

Map paramData = request.JSON
if (!paramData) {
approvedOnly = params.getBoolean('approvedOnly')
scoreIds = params.getList('scoreIds')
targetsOnly = params.getBoolean('targetsOnly')
includeTargets = params.getBoolean('includeTargets', true)
}
else {

if (paramData.approvedOnly != null) {
approvedOnly = paramData.approvedOnly
}
if (paramData.targetsOnly != null) {
approvedOnly = paramData.targetsOnly
}
if (paramData.includeTargets != null) {
includeTargets = paramData.includeTargets
}
scoreIds = paramData.scoreIds
aggregationConfig = paramData.aggregationConfig
}

if (organisation) {
render organisationService.organisationMetrics(id, targetsOnly, approvedOnly, scoreIds, aggregationConfig, includeTargets) as JSON

} else {
render (status: 404, text: 'No such id')
}
}

}
3 changes: 3 additions & 0 deletions grails-app/domain/au/org/ala/ecodata/Activity.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Activity {
bulkImportId index: true
version false
externalIds index:true
organisationId index:true
}

static hasMany = [externalIds:ExternalId]
Expand All @@ -58,6 +59,7 @@ class Activity {
Date startDate
Date endDate
List<ExternalId> externalIds
String organisationId

/** The type of activity performed. This field must match the name of an ActivityForm */
String type
Expand Down Expand Up @@ -132,6 +134,7 @@ class Activity {
verificationStatus nullable: true, inList: ['not applicable', 'not approved', 'not verified', 'under review' , 'approved']
bulkImportId nullable: true
externalIds nullable: true
organisationId nullable: true
}

static Activity findByExternalId(String externalId) {
Expand Down
11 changes: 11 additions & 0 deletions grails-app/services/au/org/ala/ecodata/ActivityService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,15 @@ class ActivityService {
}
}

List findAllForOrganisationId(id, levelOfDetail = [], includeDeleted = false) {
List activities
if (includeDeleted) {
activities = Activity.findAllByOrganisationId(id).collect {toMap(it, levelOfDetail)}
}
else {
activities = Activity.findAllByOrganisationIdAndStatus(id, ACTIVE).collect { toMap(it, levelOfDetail) }
}
activities
}

}
63 changes: 63 additions & 0 deletions grails-app/services/au/org/ala/ecodata/OrganisationService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ class OrganisationService {
public static final String PROJECTS = 'projects'

static transactional = 'mongo'
static final FLAT = 'flat'

def commonService, projectService, userService, permissionService, documentService, collectoryService, messageSource, emailService, grailsApplication
ReportingService reportingService
ActivityService activityService
ReportService reportService

def get(String id, levelOfDetail = [], includeDeleted = false) {
Organisation organisation
Expand Down Expand Up @@ -217,4 +219,65 @@ class OrganisationService {
organisationDetails
}


/**
* Returns the reportable metrics for a organisation as determined by the organisation output targets and activities
* that have been undertaken.
* @param id identifies the organisation.
* @return a Map containing the aggregated results.
*
*/
def organisationMetrics(String id, targetsOnly = false, approvedOnly = false, List scoreIds = null, Map aggregationConfig = null, boolean includeTargets = true) {
def org = Organisation.findByOrganisationId(id)
if (org) {
def organisation = toMap(org, OrganisationService.FLAT)

List toAggregate
if (scoreIds && targetsOnly) {
toAggregate = Score.findAllByScoreIdInListAndIsOutputTarget(scoreIds, true)
} else if (scoreIds) {
toAggregate = Score.findAllByScoreIdInList(scoreIds)
} else {
toAggregate = targetsOnly ? Score.findAllByIsOutputTarget(true) : Score.findAll()
}

List outputSummary = reportService.organisationSummary(id, toAggregate, approvedOnly, aggregationConfig) ?: []

// Add project output target information where it exists.
if (includeTargets) {
organisation.outputTargets?.each { target ->
// Outcome targets are text only and not mapped to a score.
if (target.outcomeTarget != null) {
return
}
def result = outputSummary.find { it.scoreId == target.scoreId }
if (result) {
if (!result.target || result.target == "0") {
// Workaround for multiple outputs inputting into the same score. Need to update how scores are defined.
result.target = target.target
}

} else {
// If there are no Outputs recorded containing the score, the results won't be returned, so add
// one in containing the target.
def score = toAggregate.find { it.scoreId == target.scoreId }
if (score) {
outputSummary << [scoreId: score.scoreId, label: score.label, target: target.target, isOutputTarget: score.isOutputTarget, description: score.description, outputType: score.outputType, category: score.category]
} else {
// This can happen if the meta-model is changed after targets have already been defined for a project.
// Once the project output targets are re-edited and saved, the old targets will be deleted.
log.warn "Can't find a score for existing output target: $target.outputLabel $target.scoreLabel, projectId: $project.projectId"
}
}
}
}

return outputSummary
} else {
def error = "Error retrieving metrics for project - no such id ${id}"
log.error error
return [status: 'error', error: error]
}
}

}
17 changes: 17 additions & 0 deletions grails-app/services/au/org/ala/ecodata/ReportService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -526,4 +526,21 @@ class ReportService {
}
return message
}

/**
* Returns aggregated scores for a specified project.
* @param organisationId the organisation of interest.
* @param aggregationSpec defines the scores to be aggregated and if any grouping needs to occur.
* [{score:{name: , units:, aggregationType}, groupBy: {entity: <one of 'activity', 'output', 'organisation', 'site>, property: String <the entity property to group by>}, ...]
*
* @return the results of the aggregation. The results will be a List of Maps, the structure of each Map is
* described in @see au.org.ala.ecodata.reporting.Aggregation.results()
*
*/
List organisationSummary(String organisationId, List aggregationSpec, boolean approvedActivitiesOnly = false, Map topLevelAggregationConfig = null) {

List activities = activityService.findAllForOrganisationId(organisationId, 'FLAT')
aggregate(activities, aggregationSpec, approvedActivitiesOnly, topLevelAggregationConfig)
}

}

0 comments on commit 4766594

Please sign in to comment.