Skip to content

Commit

Permalink
Update the data set name after more information is available #942
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed May 14, 2024
1 parent f86a80e commit 8933698
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
36 changes: 28 additions & 8 deletions grails-app/services/au/org/ala/ecodata/ParatooService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,11 @@ class ParatooService {
surveyDataAndObservations = recursivelyTransformData(form.sections[0].template.dataModel, surveyDataAndObservations, form.name, 1, config)
// If we are unable to create a site, null will be returned - assigning a null siteId is valid.

String siteName = null
if (!dataSet.siteId) {
dataSet.siteId = createSiteFromSurveyData(surveyDataAndObservations, collection, surveyId, project.project, config, form)
Map site = createSiteFromSurveyData(surveyDataAndObservations, collection, surveyId, project.project, config, form)
dataSet.siteId = site.siteId
siteName = site.name
}

// plot layout is of type geoMap. Therefore, expects a site id.
Expand All @@ -290,6 +293,9 @@ class ParatooService {
dataSet.endDate = config.getEndDate(surveyDataAndObservations)
dataSet.format = DATASET_DATABASE_TABLE
dataSet.sizeUnknown = true
// Update the data set name as the information supplied during /mint-identifier isn't enough
// to ensure uniqueness
dataSet.name = buildUpdatedDataSetSummaryName(siteName, dataSet.startDate, dataSet.endDate, form.name, surveyId)

// Delete previously created activity so that duplicate species records are not created.
// Updating existing activity will also create duplicates since it relies on outputSpeciesId to determine
Expand All @@ -308,6 +314,23 @@ class ParatooService {
}
}

protected static String buildUpdatedDataSetSummaryName(String siteName, String startDate, String endDate, String protocolName, ParatooCollectionId surveyId) {
String name = protocolName
if (siteName) {
name += " (" + siteName + ")"
}
if (startDate && endDate && startDate != endDate) {
name += " - " + DateUtil.formatAsDisplayDateTime(startDate) + " to " + DateUtil.formatAsDisplayDateTime(endDate)
}
else if (startDate) {
name += " - " +DateUtil.formatAsDisplayDateTime(startDate)
}
else {
name += " - " + DateUtil.formatAsDisplayDateTime(surveyId.eventTime)
}
name
}

/**
* Rearrange survey data to match the data model.
* e.g. [a: [b: [c: 1, d: 2], d: 1], b: [c: 1, d: 2]] => [b: [c: 1, d: 2, a: [d: 1]]]
Expand Down Expand Up @@ -555,12 +578,13 @@ class ParatooService {
output
}

private String createSiteFromSurveyData(Map observation, ParatooCollection collection, ParatooCollectionId surveyId, Project project, ParatooProtocolConfig config, ActivityForm form) {
private Map createSiteFromSurveyData(Map observation, ParatooCollection collection, ParatooCollectionId surveyId, Project project, ParatooProtocolConfig config, ActivityForm form) {
String siteId = null
// Create a site representing the location of the collection
Map siteProps = null
Map geoJson = config.getGeoJson(observation, form)
if (geoJson) {
Map siteProps = siteService.propertiesFromGeoJson(geoJson, 'upload')
siteProps = siteService.propertiesFromGeoJson(geoJson, 'upload')
List features = geoJson?.features ?: []
geoJson.remove('features')
siteProps.features = features
Expand Down Expand Up @@ -592,7 +616,7 @@ class ParatooService {
}
siteId = result.siteId
}
siteId
[siteId:siteId, name:siteProps?.name]
}

private Map syncParatooProtocols(List<Map> protocols) {
Expand Down Expand Up @@ -774,10 +798,6 @@ class ParatooService {
dataSet
}

private static String buildSurveyQueryString(int start, int limit, String createdAt) {
"?populate=deep&sort=updatedAt&pagination[start]=$start&pagination[limit]=$limit&filters[createdAt][\$eq]=$createdAt"
}

Map retrieveSurveyAndObservations(ParatooCollection collection, Map authHeader = null) {
String apiEndpoint = PARATOO_DATA_PATH
Map payload = [
Expand Down
5 changes: 5 additions & 0 deletions src/main/groovy/au/org/ala/ecodata/DateUtil.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class DateUtil {
dateTime.format(DISPLAY_DATE_TIME_FORMATTER)
}

static String formatAsDisplayDateTime(String isoDateString) {
Date date = parse(isoDateString)
formatAsDisplayDateTime(date)
}

/**
* Returns a formatted string representing the financial year a report or activity falls into, based on
* the end date. This method won't necessarily work for start dates as it will subtract a day from the value
Expand Down
11 changes: 10 additions & 1 deletion src/test/groovy/au/org/ala/ecodata/ParatooServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
Map dataSet = [dataSetId:'d1', grantId:'g1', surveyId:paratooCollectionId.toMap(), activityId: "123"]
dataSet.surveyId.survey_metadata.orgMintedUUID = orgMintedId
Map expectedDataSetSync = dataSet + [progress: Activity.STARTED]
Map expectedDataSetAsync = dataSet + [progress: Activity.STARTED, startDate: "2023-09-01T00:00:00Z", endDate: "2023-09-01T00:00:00Z", areSpeciesRecorded: false, activityId: '123', siteId: null, format: "Database Table", sizeUnknown: true]
Map expectedDataSetAsync = dataSet + [progress: Activity.STARTED, startDate: "2023-09-01T00:00:00Z", endDate: "2023-09-01T00:00:00Z", areSpeciesRecorded: false, activityId: '123', siteId: null, format: "Database Table", sizeUnknown: true, name: "aParatooForm 1 - 2023-09-01 10:00 am"]
ParatooProject project = new ParatooProject(id: projectId, project: new Project(projectId: projectId, custom: [dataSets: [dataSet]]))

when:
Expand Down Expand Up @@ -1337,6 +1337,15 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
]
}

def "The data set name will be updated after the callback to Monitor core and be created from available information"() {
expect:
ParatooService.buildUpdatedDataSetSummaryName("site", "2024-05-14T00:00:00Z", "2024-05-14T10:00:00Z", "Protocol 1", null) == "Protocol 1 (site) - 2024-05-14 10:00 am to 2024-05-14 8:00 pm"
ParatooService.buildUpdatedDataSetSummaryName("site", "2024-05-14T00:00:00Z", null, "Protocol 1", null) == "Protocol 1 (site) - 2024-05-14 10:00 am"
ParatooService.buildUpdatedDataSetSummaryName(null, "2024-05-14T00:00:00Z", null, "Protocol 1", null) == "Protocol 1 - 2024-05-14 10:00 am"
ParatooService.buildUpdatedDataSetSummaryName(null, null, null, "Protocol 1", new ParatooCollectionId(eventTime:DateUtil.parse("2024-05-14T00:00:00Z"))) == "Protocol 1 - 2024-05-14 10:00 am"

}

private Map getNormalDefinition() {
def input = """
{
Expand Down

0 comments on commit 8933698

Please sign in to comment.