Skip to content

Commit

Permalink
#326 add profile base url into config
Browse files Browse the repository at this point in the history
Refactored Http service call, improvement on annotations template
  • Loading branch information
qifeng-bai committed Nov 13, 2024
1 parent 3c54193 commit 3165bb6
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 70 deletions.
4 changes: 3 additions & 1 deletion grails-app/conf/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ regions:
occurrence:
searchURL: "https://biocache.ala.org.au/occurrences/search?q=passer"
searchTitle: occurrence search

userDetails:
web:
url: https://auth.ala.org.au/userdetails
useSpeciesListsAlerts: true
useSpatialAlerts: true
useBlogsAlerts: true
Expand Down
16 changes: 8 additions & 8 deletions grails-app/services/au/org/ala/alerts/AnnotationService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class AnnotationService {
for (JSONObject occurrence : occurrences.occurrences) {
if (occurrence.uuid) {
// all the verified assertions of this occurrence record

String assertionUrl = baseUrl + '/occurrences/' + occurrence.uuid + '/assertions'
def assertionsData = httpService.get(assertionUrl)
JSONArray assertions = JSON.parse(assertionsData) as JSONArray

def sortedAssertions = assertions.sort { a, b ->
sdf.parse(b.created) <=> sdf.parse(a.created)
}
occurrence.put('user_assertions', sortedAssertions)
def assertionResp = httpService.getJson(assertionUrl)
if (assertionResp.status == 200) {
def assertions = assertionResp.json
def sortedAssertions = assertions.sort { a, b ->
sdf.parse(b.created) <=> sdf.parse(a.created)
}
occurrence.put('user_assertions', sortedAssertions)
}
reconstructedOccurrences.push(occurrence)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CollectionService {
def jsonSlurper = new JsonSlurper()
def collectionResp = httpService.post(dataResourceURL, collectionJsonPayload)
if (collectionResp.status == 200) {
def collectionJson = collectionResp.data
def collectionJson = collectionResp.json
collectionMap = collectionJson.inject([:]) { map, item ->
// Collection server is supposed to return a JSON array of collection objects with a uid field
// but it returns a JSON array of String instead
Expand Down
74 changes: 44 additions & 30 deletions grails-app/services/au/org/ala/alerts/HttpService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,53 @@ package au.org.ala.alerts
import grails.converters.JSON
import org.apache.commons.io.IOUtils
import groovy.json.JsonSlurper
import org.grails.web.json.JSONObject


class HttpService {
def grailsApplication

/**
*
* @param url
* @param jsonPayload
* @return [Status, Json:JSONObject | error:String]
*/
def post(String url, String jsonPayload) {
URL apiUrl = new URL(url)
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection()
connection.requestMethod = 'POST'
connection.doOutput = true
connection.setRequestProperty('Content-Type', 'application/json')
connection.setRequestProperty('Accept', 'application/json')
try {
connection.requestMethod = 'POST'
connection.doOutput = true
connection.setRequestProperty('Content-Type', 'application/json')
connection.setRequestProperty('Accept', 'application/json')

// Write data to the request body
connection.outputStream.withWriter('UTF-8') { writer ->
writer.write(jsonPayload)
}
// Write data to the request body
connection.outputStream.withWriter('UTF-8') { writer ->
writer.write(jsonPayload)
}

// Read the response
int responseCode = connection.getResponseCode()
def resp = [:]
if (responseCode == 200) {
def responseText = connection.inputStream.withReader('UTF-8') { it.text }
resp =[status: 200, "data" : new JsonSlurper().parseText(responseText)]
} else {
def responseText = connection.errorStream?.withReader('UTF-8') { it.text }
resp = [status: responseCode, "error" : responseText]
// Read the response
int responseCode = connection.getResponseCode()
def resp = [:]
if (responseCode == 200) {
def responseText = connection.inputStream.withReader('UTF-8') { it.text }
return [status: 200, "json": new JsonSlurper().parseText(responseText)]
} else {
def responseText = connection.errorStream?.withReader('UTF-8') { it.text }
return [status: responseCode, "error": responseText]
}
} catch (Exception e) {
def error = "Failed to get json from web service (${url}). ${e.getClass()} ${e.getMessage()}, ${e}"
log.error(error)
return [status: 500, "error" : error]
} finally {
connection.disconnect()
}

connection.disconnect()
return resp
}

/**
* @param url
* @return [Status, Json:JSONObject | error:String]
* @return [Status, Json:JSONObject/JsonArray | error:String]
*/
def getJson(String url) {
log.debug "(internal) getJson URL = " + url
Expand All @@ -49,25 +60,28 @@ class HttpService {
connection.setRequestProperty('User-Agent', grailsApplication.config.getProperty("customUserAgent", "ALA-alerts"))

int responseCode = connection.responseCode
def responseText = ""
def resp = [:]
if (responseCode == 200) {
responseText = connection.inputStream.withReader('UTF-8') { it.text }
def responseText = connection.inputStream.withReader('UTF-8') { it.text }
def resp =[status: 200, "json" : JSON.parse(responseText) ]
return resp
} else {
responseText = connection.errorStream?.withReader('UTF-8') { it.text }
def responseText = connection.errorStream?.withReader('UTF-8') { it.text }
return [status: responseCode, "error" : responseText]
}
connection.disconnect()

resp =[status: 200, "json" : JSON.parse(responseText) as JSONObject]
return resp
} catch (Exception e) {
def error = "Failed to get json from web service (${url}). ${e.getClass()} ${e.getMessage()}, ${e}"
log.error(error)
return [status: 500, "error" : error]
} finally {
connection.disconnect()
}
}

/**
* Copied from NotifiationService to avoid circular dependency
* @param url
* @return
*/
def get(String url) {
log.debug "(internal) get URL = " + url
def conn = new URL(url).openConnection()
Expand Down
51 changes: 25 additions & 26 deletions grails-app/services/au/org/ala/alerts/MyAnnotationService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,34 @@ class MyAnnotationService{
for (JSONObject occurrence : occurrences.occurrences) {
if (occurrence.uuid) {
// all the verified assertions of this occurrence record

String assertionUrl = baseUrl + '/occurrences/' + occurrence.uuid + '/assertions'
def assertionsData = httpService.get(assertionUrl)
JSONArray assertions = JSON.parse(assertionsData) as JSONArray
occurrence.put('user_assertions', assertions)

def (origUserAssertions, openAssertions, verifiedAssertions, correctedAssertions) = filterMyAssertions(assertions, userId)


// only include record has at least 1 (50001/50002/50003) assertion
// They will be used for diffService (records that will be included in alert email)
if (!openAssertions.isEmpty() || !verifiedAssertions.isEmpty() || !correctedAssertions.isEmpty()) {

// find the open/verfied/corrected annotations which COMMENTed on all the assertions created by the users
def processedAssertionIds = openAssertions.collect { it.uuid } + verifiedAssertions.collect { it.uuid } + correctedAssertions.collect { it.uuid }
def processedAssertions = assertions.findAll{
processedAssertionIds.contains(it.relatedUuid)
def assertionsData = httpService.getJson(assertionUrl)
if (assertionsData.status == 200) {
JSONArray assertions = assertionsData.json as JSONArray
occurrence.put('user_assertions', assertions)

def (origUserAssertions, openAssertions, verifiedAssertions, correctedAssertions) = filterMyAssertions(assertions, userId)
// only include record has at least 1 (50001/50002/50003) assertion
// They will be used for diffService (records that will be included in alert email)
if (!openAssertions.isEmpty() || !verifiedAssertions.isEmpty() || !correctedAssertions.isEmpty()) {

// find the open/verfied/corrected annotations which COMMENTed on all the assertions created by the users
def processedAssertionIds = openAssertions.collect { it.uuid } + verifiedAssertions.collect { it.uuid } + correctedAssertions.collect { it.uuid }
def processedAssertions = assertions.findAll{
processedAssertionIds.contains(it.relatedUuid)
}
occurrence.put('processed_assertions', processedAssertions)

// Those open/verified/corrected assertions will be used to retrieve diff (records that will be included in alert email)
openAssertions.sort { it.uuid }
verifiedAssertions.sort { it.uuid }
correctedAssertions.sort { it.uuid }
occurrence.put('open_assertions', openAssertions.collect { it.uuid }.join(','))
occurrence.put('verified_assertions', verifiedAssertions.collect { it.uuid }.join(','))
occurrence.put('corrected_assertions', correctedAssertions.collect { it.uuid }.join(','))
}
occurrence.put('processed_assertions', processedAssertions)

// Those open/verified/corrected assertions will be used to retrieve diff (records that will be included in alert email)
openAssertions.sort { it.uuid }
verifiedAssertions.sort { it.uuid }
correctedAssertions.sort { it.uuid }
occurrence.put('open_assertions', openAssertions.collect { it.uuid }.join(','))
occurrence.put('verified_assertions', verifiedAssertions.collect { it.uuid }.join(','))
occurrence.put('corrected_assertions', correctedAssertions.collect { it.uuid }.join(','))
reconstructedOccurrences.push(occurrence)
}
reconstructedOccurrences.push(occurrence)
}
}
reconstructedOccurrences.sort { it.uuid }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ class NotificationService {
queryResult = occurrences.toString()
}
} else {
//todo : Link may be broken, shall we stop the query?
log.error("Failed to process query ${query.name}")
log.error("URL: ${url}")
log.error("${resp.error}")
Expand Down
2 changes: 1 addition & 1 deletion grails-app/views/email/annotations.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
def latestAssertion = oc.user_assertions[0] // Get the first (latest) assertion
%>
<b>Comment:</b><br/>
<i> ${StringUtils.abbreviate(latestAssertion.comment, 100)}</i>
<i>${StringUtils.abbreviate(latestAssertion.comment, 100)}</i>
<br>- <b>${latestAssertion.userDisplayName}
<g:if test="${latestAssertion.created}">,&nbsp;
<%
Expand Down
15 changes: 12 additions & 3 deletions grails-app/views/email/myAnnotations.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,18 @@
<%
def latestAssertion = oc.processed_assertions[0] // Get the first (latest) assertion
%>
Date created:<br/>-&nbsp;<i>${latestAssertion.created}</i><br/>
Comments:<br/>-&nbsp;<i>${latestAssertion.comment}</i><br/>
Comment by:<br/>-&nbsp;<i>${latestAssertion.userDisplayName}</i>
Comments:<br/><i>${StringUtils.abbreviate(latestAssertion.comment,100)}</i>
<br/>-&nbsp;${latestAssertion.userDisplayName}
<g:if test="${latestAssertion.created}">,&nbsp;
<%
try {
def parsedDate = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", latestAssertion.created)
out << parsedDate.format('dd/MM/yyyy')
} catch (Exception e) {
out << latestAssertion.created
}
%>
</g:if>
</g:if>
</td>
<td style="width: 33%; text-align: right;" >
Expand Down

0 comments on commit 3165bb6

Please sign in to comment.