Skip to content

Commit

Permalink
SW-6251 Add history IDs to observation plots (#2614)
Browse files Browse the repository at this point in the history
To prepare for the ability to redefine subzones that already contain monitoring
plots, start recording which versions of plots were included in observations.
This will allow us to tell the user which subzone a plot was in at the time of
the observation, even if the site map has subsequently changed.
  • Loading branch information
sgrimm authored Nov 19, 2024
1 parent d9f8a56 commit 0e6f4b2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.terraformation.backend.db.tracking.tables.pojos.ObservationsRow
import com.terraformation.backend.db.tracking.tables.pojos.RecordedPlantsRow
import com.terraformation.backend.db.tracking.tables.records.ObservedPlotSpeciesTotalsRecord
import com.terraformation.backend.db.tracking.tables.references.MONITORING_PLOTS
import com.terraformation.backend.db.tracking.tables.references.MONITORING_PLOT_HISTORIES
import com.terraformation.backend.db.tracking.tables.references.OBSERVATIONS
import com.terraformation.backend.db.tracking.tables.references.OBSERVATION_PLOTS
import com.terraformation.backend.db.tracking.tables.references.OBSERVATION_REQUESTED_SUBZONES
Expand Down Expand Up @@ -530,17 +531,24 @@ class ObservationStore(
val createdTime = clock.instant()

plotIds.forEach { plotId ->
observationPlotsDao.insert(
ObservationPlotsRow(
observationId = observationId,
monitoringPlotId = plotId,
createdBy = createdBy,
createdTime = createdTime,
isPermanent = isPermanent,
modifiedBy = createdBy,
modifiedTime = createdTime,
statusId = ObservationPlotStatus.Unclaimed,
))
with(OBSERVATION_PLOTS) {
dslContext
.insertInto(OBSERVATION_PLOTS)
.set(CREATED_BY, createdBy)
.set(CREATED_TIME, createdTime)
.set(IS_PERMANENT, isPermanent)
.set(MODIFIED_BY, createdBy)
.set(MODIFIED_TIME, createdTime)
.set(
MONITORING_PLOT_HISTORY_ID,
DSL.select(DSL.max(MONITORING_PLOT_HISTORIES.ID))
.from(MONITORING_PLOT_HISTORIES)
.where(MONITORING_PLOT_HISTORIES.MONITORING_PLOT_ID.eq(plotId)))
.set(MONITORING_PLOT_ID, plotId)
.set(OBSERVATION_ID, observationId)
.set(STATUS_ID, ObservationPlotStatus.Unclaimed)
.execute()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Record which version of a plot was observed so we can link it to its correct subzone at the
-- time of the observation.
ALTER TABLE tracking.observation_plots
ADD COLUMN monitoring_plot_history_id BIGINT
REFERENCES tracking.monitoring_plot_histories ON DELETE CASCADE;
CREATE INDEX ON tracking.observation_plots (monitoring_plot_history_id);

UPDATE tracking.observation_plots op
SET monitoring_plot_history_id = (
SELECT id
FROM tracking.monitoring_plot_histories mph
WHERE mph.monitoring_plot_id = op.monitoring_plot_id
);

ALTER TABLE tracking.observation_plots
ALTER COLUMN monitoring_plot_history_id SET NOT NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,8 @@ abstract class DatabaseBackedTest {
createdBy: UserId = row.createdBy ?: currentUser().userId,
createdTime: Instant = row.createdTime ?: Instant.EPOCH,
isPermanent: Boolean = row.isPermanent ?: false,
monitoringPlotHistoryId: MonitoringPlotHistoryId =
row.monitoringPlotHistoryId ?: inserted.monitoringPlotHistoryId,
monitoringPlotId: MonitoringPlotId = row.monitoringPlotId ?: inserted.monitoringPlotId,
observationId: ObservationId = row.observationId ?: inserted.observationId,
statusId: ObservationPlotStatus =
Expand All @@ -2317,6 +2319,7 @@ abstract class DatabaseBackedTest {
observationId = observationId,
modifiedBy = createdBy,
modifiedTime = createdTime,
monitoringPlotHistoryId = monitoringPlotHistoryId,
monitoringPlotId = monitoringPlotId,
statusId = statusId,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ internal class SpeciesStoreTest : DatabaseTest(), RunsAsUser {
insertSpecies(scientificName = "Unused species")

insertFacility(type = FacilityType.Nursery)
insertPlantingSite()
insertPlantingSite(x = 0)
insertPlantingZone()
val subzoneId = insertPlantingSubzone()
insertMonitoringPlot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class ObservationResultsStoreTest : DatabaseTest(), RunsAsUser {
@BeforeEach
fun setUp() {
organizationId = insertOrganization()
plantingSiteId = insertPlantingSite(areaHa = BigDecimal(2500))
plantingSiteId = insertPlantingSite(x = 0, areaHa = BigDecimal(2500))

every { user.canReadObservation(any()) } returns true
every { user.canReadOrganization(organizationId) } returns true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ObservationStoreTest : DatabaseTest(), RunsAsUser {
@BeforeEach
fun setUp() {
organizationId = insertOrganization()
plantingSiteId = insertPlantingSite()
plantingSiteId = insertPlantingSite(x = 0)

every { user.canCreateObservation(any()) } returns true
every { user.canManageObservation(any()) } returns true
Expand Down Expand Up @@ -1971,7 +1971,7 @@ class ObservationStoreTest : DatabaseTest(), RunsAsUser {

@Test
fun `does not insert anything if this is the first observation of a site`() {
insertPlantingSite()
insertPlantingSite(x = 0)
insertPlantingZone()
insertPlantingSubzone()
insertMonitoringPlot()
Expand All @@ -1993,7 +1993,7 @@ class ObservationStoreTest : DatabaseTest(), RunsAsUser {

val totalsForOtherSite = helper.fetchAllTotals()

insertPlantingSite()
insertPlantingSite(x = 0)
insertPlantingZone()
insertPlantingSubzone()
insertMonitoringPlot()
Expand All @@ -2014,7 +2014,7 @@ class ObservationStoreTest : DatabaseTest(), RunsAsUser {
speciesName = "Species name",
statusId = Dead)

insertPlantingSite()
insertPlantingSite(x = 0)
insertPlantingZone()
insertPlantingSubzone()
val plotId1 = insertMonitoringPlot()
Expand Down Expand Up @@ -2099,7 +2099,7 @@ class ObservationStoreTest : DatabaseTest(), RunsAsUser {
speciesName = "Species name",
statusId = Live)

insertPlantingSite()
insertPlantingSite(x = 0)
insertPlantingZone()
insertPlantingSubzone()
insertMonitoringPlot()
Expand Down Expand Up @@ -2142,7 +2142,7 @@ class ObservationStoreTest : DatabaseTest(), RunsAsUser {

@BeforeEach
fun setUp() {
plantingSiteId = insertPlantingSite()
plantingSiteId = insertPlantingSite(x = 0)
insertPlantingZone()
insertPlantingSubzone()
}
Expand Down

0 comments on commit 0e6f4b2

Please sign in to comment.