Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the data set name after more information is available #942 #943

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions grails-app/services/au/org/ala/ecodata/ParatooService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class ParatooService {

private static String buildName(String protocolId, String displayDate, Project project) {
ActivityForm protocolForm = ActivityForm.findByExternalId(protocolId)
String dataSetName = protocolForm?.name + " - " + displayDate + " (" + project.name + ")"
String dataSetName = protocolForm?.name + " - " + displayDate
dataSetName
}

Expand Down 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
21 changes: 19 additions & 2 deletions src/test/groovy/au/org/ala/ecodata/ParatooServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import org.codehaus.jackson.map.ObjectMapper
import org.grails.web.converters.marshaller.json.CollectionMarshaller
import org.grails.web.converters.marshaller.json.MapMarshaller

import java.time.format.DateTimeTextProvider
import java.time.temporal.TemporalField

import static grails.async.Promises.waitAll
/**
* Tests for the ParatooService.
Expand All @@ -33,6 +36,10 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
static Map DUMMY_POLYGON = [type: 'Polygon', coordinates: [[[1, 2], [2, 2], [2, 1], [1, 1], [1, 2]]]]
static Map DUMMY_PLOT = ['type':'Point', coordinates: [1,2]]

// The am/pm in the formatted time is local dependent and this appears to be easiest way to determine the value.
String am = DateUtil.formatAsDisplayDateTime("2024-05-14T00:00:00Z")[-2..-1]
String pm = am == "AM" ? "PM" : "pm"

def setup() {

deleteAll()
Expand Down Expand Up @@ -156,7 +163,7 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
assert dataSet.protocol == collectionId.protocolId
assert dataSet.grantId == "g1"
assert dataSet.progress == 'planned'
assert dataSet.name == "aParatooForm 1 - ${DateUtil.formatAsDisplayDateTime(collectionId.eventTime)} (Project 1)"
assert dataSet.name == "aParatooForm 1 - ${DateUtil.formatAsDisplayDateTime(collectionId.eventTime)}"

[status: 'ok']
}
Expand All @@ -182,7 +189,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 +1344,16 @@ 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
Loading