Skip to content

Commit

Permalink
move isGlobal update to scheduled action
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinBeczak committed Sep 4, 2024
1 parent 8883473 commit 4ce8b96
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 44 deletions.
13 changes: 13 additions & 0 deletions app/org/maproulette/jobs/SchedulerActor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ class SchedulerActor @Inject() (
last_updated = NOW()
WHERE id = ${id};"""
SQL(query).executeUpdate()

// Update is_global based on bounding box
val updateGlobalQuery =
"""UPDATE challenges
SET is_global = (
CASE
WHEN (ST_XMax(bounding)::numeric - ST_XMin(bounding)::numeric) > 180 THEN TRUE
WHEN (ST_YMax(bounding)::numeric - ST_YMin(bounding)::numeric) > 90 THEN TRUE
ELSE FALSE
END
);"""

SQL(updateGlobalQuery).executeUpdate()
}
// The above query will not update the cache, so remove the id from the cache in case it is there
logger.debug(s"Flushing challenge cache of challenge with id $id")
Expand Down
38 changes: 0 additions & 38 deletions app/org/maproulette/models/dal/ChallengeDAL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -524,44 +524,6 @@ class ChallengeDAL @Inject() (
}
}

def updateSpatialFields(user: User)(implicit challengeId: Long, c: Option[Connection] = None) = {
this.withMRConnection { implicit c =>
// Update location, bounding, and last_updated based on tasks
val updateLocationQuery =
s"""
UPDATE challenges SET
location = (
SELECT ST_Centroid(ST_Collect(ST_Makevalid(location)))
FROM tasks
WHERE parent_id = $challengeId
),
bounding = (
SELECT ST_Envelope(ST_Buffer((ST_SetSRID(ST_Extent(location), 4326))::geography, 2)::geometry)
FROM tasks
WHERE parent_id = $challengeId
),
last_updated = NOW()
WHERE id = $challengeId;
""".stripMargin
SQL(updateLocationQuery).executeUpdate()

// Update is_global based on bounding box dimensions
val updateIsGlobalQuery =
s"""
UPDATE challenges
SET is_global = (
CASE
WHEN (ST_XMax(bounding)::numeric - ST_XMin(bounding)::numeric) > 180 THEN TRUE
WHEN (ST_YMax(bounding)::numeric - ST_YMin(bounding)::numeric) > 90 THEN TRUE
ELSE FALSE
END
)
WHERE id = $challengeId;
""".stripMargin
SQL(updateIsGlobalQuery).executeUpdate()
}
}

private def insertPresets(challenge: Challenge, presets: List[String])(
implicit c: Option[Connection] = None
): Challenge = {
Expand Down
6 changes: 0 additions & 6 deletions app/org/maproulette/provider/ChallengeProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ class ChallengeProvider @Inject() (
user
)(challenge.id)
} else {
this.challengeDAL.updateSpatialFields(user)(challenge.id)
this.challengeDAL.update(Json.obj("status" -> Challenge.STATUS_READY), user)(
challenge.id
)
Expand Down Expand Up @@ -243,7 +242,6 @@ class ChallengeProvider @Inject() (
jsonData
)
}
this.challengeDAL.updateSpatialFields(user)(
challenge.id
)
this.challengeDAL.update(Json.obj("status" -> Challenge.STATUS_READY), user)(
Expand All @@ -265,7 +263,6 @@ class ChallengeProvider @Inject() (
if (seqJSON) {
this.buildTasksFromRemoteJson(filePrefix, fileNumber + 1, challenge, user, false)
} else {
this.challengeDAL.updateSpatialFields(user)(challenge.id)
this.challengeDAL.update(Json.obj("status" -> Challenge.STATUS_READY), user)(challenge.id)
this.challengeDAL.markTasksRefreshed()(challenge.id)

Expand All @@ -277,7 +274,6 @@ class ChallengeProvider @Inject() (
case Failure(f) =>
if (fileNumber > 1) {
// todo need to figure out if actual failure or if not finding the next file
this.challengeDAL.updateSpatialFields(user)(challenge.id)
this.challengeDAL.update(Json.obj("status" -> Challenge.STATUS_READY), user)(challenge.id)
} else {
this.challengeDAL.update(
Expand Down Expand Up @@ -425,7 +421,6 @@ class ChallengeProvider @Inject() (
}
}

this.challengeDAL.updateSpatialFields(user)(parent.id)
this.challengeDAL.update(Json.obj("status" -> Challenge.STATUS_READY), user)(parent.id)
this.challengeDAL.markTasksRefreshed()(parent.id)
if (single) {
Expand Down Expand Up @@ -628,7 +623,6 @@ class ChallengeProvider @Inject() (
user
)(challenge.id)
case false =>
this.challengeDAL.updateSpatialFields(user)(challenge.id)
this.challengeDAL.update(Json.obj("status" -> Challenge.STATUS_READY), user)(
challenge.id
)
Expand Down

0 comments on commit 4ce8b96

Please sign in to comment.