Skip to content

Commit

Permalink
Add virtual challenge SearchParameters to getChallengeSummary (challe…
Browse files Browse the repository at this point in the history
…nge metrics) to filter for task actions only within the virtual challenge.
  • Loading branch information
CollinBeczak committed May 7, 2024
1 parent 49bffd1 commit a79da2a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
9 changes: 7 additions & 2 deletions app/org/maproulette/controllers/api/DataController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import org.maproulette.data._
import org.maproulette.framework.model.Challenge
import org.maproulette.models.dal.ChallengeDAL
import org.maproulette.session.{SearchParameters, SessionManager}
import anorm.{SQL, SqlParser}
import anorm.SqlParser.scalar
import org.maproulette.permissions.Permission
import org.maproulette.utils.Utils
import play.api.libs.json.Json.JsValueWrapper
import play.api.libs.json.Json.{JsValueWrapper, toJsObject}
import play.api.libs.json._
import play.api.mvc._

Expand Down Expand Up @@ -126,12 +128,14 @@ class DataController @Inject() (
def getChallengeSummary(
id: Long,
priority: String,
includeByPriority: Boolean = false
includeByPriority: Boolean = false,
virtualChallengeId: Long
): Action[AnyContent] = Action.async { implicit request =>
this.sessionManager.authenticatedRequest { _ =>
SearchParameters.withSearch { implicit params =>
val response = this.dataManager.getChallengeSummary(
challengeId = Some(id),
virtualChallengeId = Some(virtualChallengeId),
priority = Utils.toIntList(priority),
params = Some(params)
)
Expand Down Expand Up @@ -274,6 +278,7 @@ class DataController @Inject() (
this.dataManager.getChallengeSummary(
projectList,
None,
Some(0),
length,
start,
orderColumnName,
Expand Down
30 changes: 26 additions & 4 deletions app/org/maproulette/data/DataManager.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package org.maproulette.data

import java.sql.Connection

import anorm.SqlParser._
import anorm._

import javax.inject.{Inject, Singleton}
import org.joda.time.DateTime
import org.maproulette.Config
Expand All @@ -19,6 +19,7 @@ import org.maproulette.session.SearchParameters
import org.maproulette.utils.BoundingBoxFinder
import play.api.Application
import play.api.db.Database
import play.api.libs.json.{JsObject, Json}

case class ActionSummary(
total: Int,
Expand Down Expand Up @@ -317,6 +318,7 @@ class DataManager @Inject() (
def getChallengeSummary(
projectList: Option[List[Long]] = None,
challengeId: Option[Long] = None,
virtualChallengeId: Option[Long] = None,
limit: Int = (-1),
offset: Int = 0,
orderColumn: Option[String] = None,
Expand All @@ -327,6 +329,18 @@ class DataManager @Inject() (
params: Option[SearchParameters] = None
): List[ChallengeSummary] = {
this.db.withConnection { implicit c =>
val virtualParams = virtualChallengeId.flatMap { id =>
if (id != 0) {
val result =
SQL("SELECT search_parameters FROM virtual_challenges WHERE id = {virtualChallengeId}")
.on("virtualChallengeId" -> id)
.as(SqlParser.str("search_parameters").singleOpt)
result.map(Json.parse).flatMap(_.validate[SearchParameters].asOpt)
} else {
None
}
}

val parser = for {
id <- int("tasks.parent_id")
name <- str("challenges.name")
Expand Down Expand Up @@ -363,9 +377,16 @@ class DataManager @Inject() (
)
)
val searchParams = SearchParameters.withDefaultAllTaskStatuses(
params match {
case Some(p) => p
case None => new SearchParameters()
if (virtualParams.isDefined) {
virtualParams match {
case Some(p) => p
case None => new SearchParameters()
}
} else {
params match {
case Some(p) => p
case None => new SearchParameters()
}
}
)

Expand All @@ -392,6 +413,7 @@ class DataManager @Inject() (
this.paramsOwner(searchParams, searchFilters)
this.paramsReviewer(searchParams, searchFilters)
this.paramsMapper(searchParams, searchFilters)
this.paramsBoundingGeometries(searchParams, searchFilters)

// The percentage columns are a bit of a hack simply so that we can order by the percentages.
// It won't decrease performance as this is simple basic math calculations, but it certainly
Expand Down
2 changes: 1 addition & 1 deletion conf/v2_route/data.api
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ GET /data/status/summary @org.maproulette.controllers
### NoDocs ###
GET /data/challenge/:challengeId/propertyKeys @org.maproulette.controllers.api.DataController.getPropertyKeys(challengeId:Long)
### NoDocs ###
GET /data/challenge/:challengeId @org.maproulette.controllers.api.DataController.getChallengeSummary(challengeId:Long, priority:String ?= "", includeByPriority:Boolean ?= false)
GET /data/challenge/:challengeId @org.maproulette.controllers.api.DataController.getChallengeSummary(challengeId:Long, priority:String ?= "", includeByPriority:Boolean ?= false, virtualChallengeId:Long ?= 0)
### NoDocs ###
GET /data/tag/metrics @org.maproulette.framework.controller.DataController.getTagMetrics()

0 comments on commit a79da2a

Please sign in to comment.