Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- improves response time of get profile call
  • Loading branch information
temi committed Jul 29, 2024
1 parent 366a6e2 commit 5fc1b51
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
27 changes: 23 additions & 4 deletions grails-app/controllers/au/org/ala/profile/api/ApiController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@ class ApiController extends BaseController {
type = "boolean",
defaultValue = "false"
)),
@Parameter(name = "onlyContent",
in = ParameterIn.QUERY,
required = false,
description = "if true, only return the species description and not additional information like opus metadata etc.",
schema = @Schema(
name = "onlyContent",
type = "boolean",
defaultValue = "false"
)),
@Parameter(name = "Access-Token",
in = ParameterIn.HEADER,
required = false,
Expand All @@ -354,14 +363,24 @@ class ApiController extends BaseController {
boolean latest = false
final fullClassification = true
boolean includeImages = params.getBoolean('includeImages', false)
Map profileAndOpus = profileService.getProfile(params.opusId as String, params.profileId as String, latest, fullClassification)
boolean onlyContent = params.getBoolean('onlyContent', false)
Map profileAndOpus
if (onlyContent) {
profileAndOpus = apiService.getProfile(params.opusId as String, params.profileId as String, latest, fullClassification)
}
else {
profileAndOpus = profileService.getProfile(params.opusId as String, params.profileId as String, latest, fullClassification)
}

if (!profileAndOpus) {
notFound()
} else {
String fullURL = grailsApplication.config.grails.serverURL + (request.contextPath ? "/${request.contextPath}" : "")
profileAndOpus.profile.mapSnapshot = mapService.getSnapshotImageUrlWithUUIDs(fullURL, profileAndOpus.opus.uuid, profileAndOpus.profile.uuid)
apiService.supplementProfileData(profileAndOpus, 20, includeImages)
if (!onlyContent) {
String fullURL = grailsApplication.config.grails.serverURL + (request.contextPath ? "/${request.contextPath}" : "")
profileAndOpus.profile.mapSnapshot = mapService.getSnapshotImageUrlWithUUIDs(fullURL, profileAndOpus.opus.uuid, profileAndOpus.profile.uuid)
apiService.supplementProfileData(profileAndOpus, 20, includeImages)
}

render profileAndOpus.profile as JSON
}
}
Expand Down
41 changes: 41 additions & 0 deletions grails-app/services/au/org/ala/profile/api/ApiService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ import au.org.ala.profile.hub.ImageService
import au.org.ala.profile.hub.NslService
import au.org.ala.profile.hub.ProfileService
import au.org.ala.profile.hub.Utils
import au.org.ala.profile.hub.WebServiceWrapperService
import grails.web.mapping.LinkGenerator
import org.apache.http.entity.ContentType

import static au.org.ala.profile.hub.Utils.encPath
import static au.org.ala.profile.hub.util.HubConstants.getDEFAULT_OPUS_BANNER_URL
import static au.org.ala.profile.hub.util.HubConstants.getDEFAULT_OPUS_LOGOS
import static au.org.ala.profile.hub.util.HubConstants.getDEFAULT_OPUS_TITLE
import static org.apache.http.HttpStatus.SC_OK
import static au.org.ala.profile.hub.Utils.*

class ApiService {
def grailsApplication
LinkGenerator grailsLinkGenerator
ImageService imageService
ProfileService profileService
NslService nslService
WebServiceWrapperService webServiceWrapperService

def getProfiles(String opusId, String startIndex = "", String pageSize = "", String sort = "", String order = "", String taxonName = "", String taxonRank = "", String rankFilter = "") {
if (taxonName && taxonRank) {
Expand Down Expand Up @@ -135,6 +142,40 @@ class ApiService {
profile?.attributes?.findAll { attributes?.contains(it.uuid) || attributes?.contains(it.title?.toLowerCase()) }
}

/**
* Faster profile lookup.
*
*/
def getProfile(String opusId, String profileId, boolean latest = false, Boolean fullClassification = false) {
log.debug("Loading profile " + profileId)

Map result

try {
String encodedProfileId = encPath(profileId)
def profile = webServiceWrapperService.get("${grailsApplication.config.profile.service.url}/opus/${encPath(opusId)}/profile/${encodedProfileId}?latest=${latest}&fullClassification=${fullClassification}", [:], ContentType.APPLICATION_JSON, true, false, profileService.getCustomHeaderWithUserId())?.resp

if (!profile) {
return null
}

profileService.injectThumbnailUrls(profile)

result = [
profile : profile
]

} catch (FileNotFoundException e) {
log.error("Profile ${profileId} not found")
result = null
} catch (Exception e) {
log.error("Failed to retrieve profile ${profileId}", e)
result = [error: "Failed to retrieve profile ${profileId} due to ${e.getMessage()}"]
}

result
}

/** Returns true for HTTP status codes from 200 to 299 */
protected isSuccessful(int statusCode) {
return statusCode >= SC_OK && statusCode <= 299
Expand Down

0 comments on commit 5fc1b51

Please sign in to comment.