From 86754589129b2a141fa867b761d9211c0cabab0f Mon Sep 17 00:00:00 2001 From: CollinBeczak Date: Fri, 19 Jul 2024 12:46:11 -0500 Subject: [PATCH] further simplify markers endpoint --- .../framework/controller/TaskController.scala | 15 ++--- .../repository/TaskClusterRepository.scala | 56 ++++++++----------- .../service/TaskClusterService.scala | 2 +- conf/v2_route/task.api | 2 +- 4 files changed, 30 insertions(+), 45 deletions(-) diff --git a/app/org/maproulette/framework/controller/TaskController.scala b/app/org/maproulette/framework/controller/TaskController.scala index 65c85bf1..f1892071 100644 --- a/app/org/maproulette/framework/controller/TaskController.scala +++ b/app/org/maproulette/framework/controller/TaskController.scala @@ -139,25 +139,22 @@ class TaskController @Inject() ( top: Double, limit: Int, excludeLocked: Boolean, - includeGeometries: Boolean + includeGeometries: Boolean, + includeTags: Boolean ): Action[AnyContent] = Action.async { implicit request => this.sessionManager.userAwareRequest { implicit user => SearchParameters.withSearch { p => val params = p.copy(location = Some(SearchLocation(left, bottom, right, top))) - val (count, result) = this.taskClusterService.getTaskMarkerDataInBoundingBox( + val result = this.taskClusterService.getTaskMarkerDataInBoundingBox( User.userOrMocked(user), params, limit, excludeLocked ) - result match { - case Some(points) => - val resultJson = this.insertExtraTaskJSON(points) - Ok(Json.obj("total" -> count, "tasks" -> resultJson)) - case None => - Ok(Json.obj("total" -> count)) - } + val resultJson = this.insertExtraTaskJSON(result, includeGeometries, includeTags) + + Ok(resultJson) } } } diff --git a/app/org/maproulette/framework/repository/TaskClusterRepository.scala b/app/org/maproulette/framework/repository/TaskClusterRepository.scala index 47940f04..e0318713 100644 --- a/app/org/maproulette/framework/repository/TaskClusterRepository.scala +++ b/app/org/maproulette/framework/repository/TaskClusterRepository.scala @@ -26,7 +26,7 @@ class TaskClusterRepository @Inject() (override val db: Database, challengeDAL: val DEFAULT_NUMBER_OF_POINTS = 100 - val pointParser = this.challengeDAL.pointParser + val pointParser = this.challengeDAL.pointParser private val joinClause = new StringBuilder( @@ -182,40 +182,28 @@ class TaskClusterRepository @Inject() (override val db: Database, challengeDAL: def queryTaskMarkerDataInBoundingBox( query: Query, limit: Int - ): (Int, Option[List[ClusteredPoint]]) = { + ): List[ClusteredPoint] = { this.withMRTransaction { implicit c => - val count = - query.build(s""" - SELECT count(*) FROM tasks - ${joinClause.toString()} - """).as(SqlParser.int("count").single) - - if (count < limit) { - val results = - query - .build( - s""" - SELECT tasks.id, tasks.name, tasks.parent_id, c.name, tasks.instruction, tasks.status, tasks.mapped_on, - tasks.completed_time_spent, tasks.completed_by, - tasks.bundle_id, tasks.is_bundle_primary, tasks.cooperative_work_json::TEXT as cooperative_work, - task_review.review_status, task_review.review_requested_by, task_review.reviewed_by, task_review.reviewed_at, - task_review.review_started_at, task_review.meta_review_status, task_review.meta_reviewed_by, - task_review.meta_reviewed_at, task_review.additional_reviewers, - ST_AsGeoJSON(tasks.location) AS location, priority, - CASE WHEN task_review.review_started_at IS NULL - THEN 0 - ELSE EXTRACT(epoch FROM (task_review.reviewed_at - task_review.review_started_at)) END - AS reviewDuration - FROM tasks - ${joinClause.toString()} - """ - ) - .as(this.pointParser.*) - - (count, Some(results)) - } else { - (count, Option.empty[List[ClusteredPoint]]) - } + val finalQuery = query.copy(finalClause = s"""LIMIT $limit""") + val results = finalQuery + .build(s""" + SELECT tasks.id, tasks.name, tasks.parent_id, c.name, tasks.instruction, tasks.status, tasks.mapped_on, + tasks.completed_time_spent, tasks.completed_by, + tasks.bundle_id, tasks.is_bundle_primary, tasks.cooperative_work_json::TEXT as cooperative_work, + task_review.review_status, task_review.review_requested_by, task_review.reviewed_by, task_review.reviewed_at, + task_review.review_started_at, task_review.meta_review_status, task_review.meta_reviewed_by, + task_review.meta_reviewed_at, task_review.additional_reviewers, + ST_AsGeoJSON(tasks.location) AS location, priority, + CASE WHEN task_review.review_started_at IS NULL + THEN 0 + ELSE EXTRACT(epoch FROM (task_review.reviewed_at - task_review.review_started_at)) END + AS reviewDuration + FROM tasks + ${joinClause.toString()} + """) + .as(this.pointParser.*) + + results } } diff --git a/app/org/maproulette/framework/service/TaskClusterService.scala b/app/org/maproulette/framework/service/TaskClusterService.scala index b73e215a..9ad81263 100644 --- a/app/org/maproulette/framework/service/TaskClusterService.scala +++ b/app/org/maproulette/framework/service/TaskClusterService.scala @@ -151,7 +151,7 @@ class TaskClusterService @Inject() (repository: TaskClusterRepository) params: SearchParameters, limit: Int, ignoreLocked: Boolean = false - ): (Int, Option[List[ClusteredPoint]]) = { + ): (List[ClusteredPoint]) = { params.location match { case Some(sl) => // params has location case None => diff --git a/conf/v2_route/task.api b/conf/v2_route/task.api index 5b6d1e28..23363cd4 100644 --- a/conf/v2_route/task.api +++ b/conf/v2_route/task.api @@ -571,7 +571,7 @@ PUT /tasks/box/:left/:bottom/:right/:top @org.maproulette.framework.c # in: query # description: Optional flag to have geometries data returned for each task. ### -PUT /markers/box/:left/:bottom/:right/:top @org.maproulette.framework.controller.TaskController.getTaskMarkerDataInBoundingBox(left:Double, bottom:Double, right:Double, top:Double, limit:Int ?= 500, excludeLocked:Boolean ?= false, includeGeometries:Boolean ?= false) +PUT /markers/box/:left/:bottom/:right/:top @org.maproulette.framework.controller.TaskController.getTaskMarkerDataInBoundingBox(left:Double, bottom:Double, right:Double, top:Double, limit:Int ?= 500, excludeLocked:Boolean ?= false, includeGeometries:Boolean ?= false, includeTags:Boolean ?= false) ### # tags: [ Task ] # summary: Update Task Changeset