From b681e9c091d8ab2c1a74962ad5f484c33593c6fd Mon Sep 17 00:00:00 2001 From: Charles Dufour Date: Tue, 12 Nov 2024 15:50:21 +0100 Subject: [PATCH] [TRELLO-2536] Format --- app/controllers/ReportController.scala | 4 ++- app/models/report/ArborescenceNode.scala | 21 ++++++++--- .../reportmetadata/ReportMetadata.scala | 8 +++-- .../report/ReportRepository.scala | 22 +++++++++--- .../subcategorylabel/SubcategoryLabel.scala | 7 +++- .../SubcategoryLabelRepository.scala | 3 +- .../SubcategoryLabelTable.scala | 18 ++++++---- .../SubcategoryLabelTask.scala | 35 +++++++++++-------- .../orchestrators/StatsOrchestratorTest.scala | 20 ++++++++--- .../report/ReportRemindersTaskUnitSpec.scala | 7 +++- 10 files changed, 105 insertions(+), 40 deletions(-) diff --git a/app/controllers/ReportController.scala b/app/controllers/ReportController.scala index 60992499..b16aee3c 100644 --- a/app/controllers/ReportController.scala +++ b/app/controllers/ReportController.scala @@ -164,7 +164,9 @@ class ReportController( Ok( Json.toJson( ReportWithFilesAndAssignedUser( - r.subcategoryLabel.map(_.subcategoryLabels).fold(r.report)(labels => r.report.copy(subcategories = labels)), + r.subcategoryLabel + .map(_.subcategoryLabels) + .fold(r.report)(labels => r.report.copy(subcategories = labels)), r.metadata, r.bookmark.isDefined, maybeAssignedMinimalUser, diff --git a/app/models/report/ArborescenceNode.scala b/app/models/report/ArborescenceNode.scala index b6b8d009..c666533f 100644 --- a/app/models/report/ArborescenceNode.scala +++ b/app/models/report/ArborescenceNode.scala @@ -14,9 +14,9 @@ object ArborescenceNode { json.value.flatMap(from(_)).toList def from( - json: JsValue, - currentCategory: Option[CategoryInfo] = None, - currentPath: Vector[(CategoryInfo, NodeInfo)] = Vector.empty + json: JsValue, + currentCategory: Option[CategoryInfo] = None, + currentPath: Vector[(CategoryInfo, NodeInfo)] = Vector.empty ): List[ArborescenceNode] = { val id = (json \ "id").as[String] val title = (json \ "title").as[String] @@ -27,11 +27,22 @@ object ArborescenceNode { subcategories match { case Nil => - List(ArborescenceNode(currentCategory, currentPath :+ (CategoryInfo(subcategory.orElse(category).get, title), NodeInfo(id, tags)))) + List( + ArborescenceNode( + currentCategory, + currentPath :+ (CategoryInfo(subcategory.orElse(category).get, title), NodeInfo(id, tags)) + ) + ) case _ => category match { case Some(cat) => - subcategories.flatMap(jsValue => from(jsValue, Some(CategoryInfo(cat, title)), currentPath :+ (CategoryInfo(cat, title), NodeInfo(id, tags)))) + subcategories.flatMap(jsValue => + from( + jsValue, + Some(CategoryInfo(cat, title)), + currentPath :+ (CategoryInfo(cat, title), NodeInfo(id, tags)) + ) + ) case None => subcategories.flatMap(jsValue => from(jsValue, currentCategory, currentPath :+ (CategoryInfo(subcategory.get, title), NodeInfo(id, tags))) diff --git a/app/models/report/reportmetadata/ReportMetadata.scala b/app/models/report/reportmetadata/ReportMetadata.scala index 269ebf5b..fa92fdc9 100644 --- a/app/models/report/reportmetadata/ReportMetadata.scala +++ b/app/models/report/reportmetadata/ReportMetadata.scala @@ -33,8 +33,12 @@ case class ReportWithMetadataAndBookmark( ) } object ReportWithMetadataAndBookmark { - def from(report: Report, metadata: Option[ReportMetadata], bookmark: Option[Bookmark], subcategoryLabel: Option[SubcategoryLabel] = None): ReportWithMetadataAndBookmark = { + def from( + report: Report, + metadata: Option[ReportMetadata], + bookmark: Option[Bookmark], + subcategoryLabel: Option[SubcategoryLabel] = None + ): ReportWithMetadataAndBookmark = ReportWithMetadataAndBookmark(report, metadata, bookmark, subcategoryLabel) - } } diff --git a/app/repositories/report/ReportRepository.scala b/app/repositories/report/ReportRepository.scala index 1ec0a518..db83af2c 100644 --- a/app/repositories/report/ReportRepository.scala +++ b/app/repositories/report/ReportRepository.scala @@ -27,7 +27,8 @@ import repositories.reportmetadata.ReportMetadataTable import repositories.reportresponse.ReportResponseTable import repositories.CRUDRepository import repositories.PaginateOps -import repositories.subcategorylabel.{SubcategoryLabel, SubcategoryLabelTable} +import repositories.subcategorylabel.SubcategoryLabel +import repositories.subcategorylabel.SubcategoryLabelTable import slick.basic.DatabaseConfig import slick.basic.DatabasePublisher import slick.jdbc.JdbcProfile @@ -382,7 +383,10 @@ class ReportRepository(override val dbConfig: DatabaseConfig[JdbcProfile])(impli reportsAndMetadatas <- queryFilter(ReportTable.table(user), filter, user) .sortBy { case (report, _, _) => report.creationDate.desc } .withPagination(db)(offset, limit) - reportsWithMetadata = reportsAndMetadatas.mapEntities { case (report: Report, metadata: Option[ReportMetadata], bookmark: Option[Bookmark]) => ReportWithMetadataAndBookmark.from(report, metadata, bookmark)} + reportsWithMetadata = reportsAndMetadatas.mapEntities { + case (report: Report, metadata: Option[ReportMetadata], bookmark: Option[Bookmark]) => + ReportWithMetadataAndBookmark.from(report, metadata, bookmark) + } } yield reportsWithMetadata def getReportsByIds(ids: List[UUID]): Future[List[Report]] = db.run( @@ -510,11 +514,21 @@ class ReportRepository(override val dbConfig: DatabaseConfig[JdbcProfile])(impli .on { case (((report, _), _), subcategoryLabel) => report.category === subcategoryLabel.category && report.subcategories === subcategoryLabel.subcategories } - .map { case (((report, metadata), bookmark), subcategoryLabel) => (report, metadata, bookmark, subcategoryLabel) } + .map { case (((report, metadata), bookmark), subcategoryLabel) => + (report, metadata, bookmark, subcategoryLabel) + } .result .headOption ) - maybeReportWithMetadata = maybeTuple.map {case (report: Report, metadata: Option[ReportMetadata], bookmark: Option[Bookmark], subcategoryLabel: Option[SubcategoryLabel]) => ReportWithMetadataAndBookmark.from(report, metadata, bookmark, subcategoryLabel)} + maybeReportWithMetadata = maybeTuple.map { + case ( + report: Report, + metadata: Option[ReportMetadata], + bookmark: Option[Bookmark], + subcategoryLabel: Option[SubcategoryLabel] + ) => + ReportWithMetadataAndBookmark.from(report, metadata, bookmark, subcategoryLabel) + } } yield maybeReportWithMetadata } diff --git a/app/repositories/subcategorylabel/SubcategoryLabel.scala b/app/repositories/subcategorylabel/SubcategoryLabel.scala index 61b17159..9645a087 100644 --- a/app/repositories/subcategorylabel/SubcategoryLabel.scala +++ b/app/repositories/subcategorylabel/SubcategoryLabel.scala @@ -1,3 +1,8 @@ package repositories.subcategorylabel -case class SubcategoryLabel(category: String, subcategories: List[String], categoryLabel: String, subcategoryLabels: List[String]) +case class SubcategoryLabel( + category: String, + subcategories: List[String], + categoryLabel: String, + subcategoryLabels: List[String] +) diff --git a/app/repositories/subcategorylabel/SubcategoryLabelRepository.scala b/app/repositories/subcategorylabel/SubcategoryLabelRepository.scala index a740747a..7d895328 100644 --- a/app/repositories/subcategorylabel/SubcategoryLabelRepository.scala +++ b/app/repositories/subcategorylabel/SubcategoryLabelRepository.scala @@ -4,7 +4,8 @@ import repositories.PostgresProfile.api._ import slick.basic.DatabaseConfig import slick.jdbc.JdbcProfile -import scala.concurrent.{ExecutionContext, Future} +import scala.concurrent.ExecutionContext +import scala.concurrent.Future class SubcategoryLabelRepository(val dbConfig: DatabaseConfig[JdbcProfile])(implicit val ec: ExecutionContext diff --git a/app/repositories/subcategorylabel/SubcategoryLabelTable.scala b/app/repositories/subcategorylabel/SubcategoryLabelTable.scala index d20a11f8..5b9efc8d 100644 --- a/app/repositories/subcategorylabel/SubcategoryLabelTable.scala +++ b/app/repositories/subcategorylabel/SubcategoryLabelTable.scala @@ -6,9 +6,9 @@ import slick.lifted.Tag class SubcategoryLabelTable(tag: Tag) extends Table[SubcategoryLabel](tag, "subcategory_labels") { - def category = column[String]("category") - def subcategories = column[List[String]]("subcategories") - def categoryLabel = column[String]("category_label") + def category = column[String]("category") + def subcategories = column[List[String]]("subcategories") + def categoryLabel = column[String]("category_label") def subcategoryLabels = column[List[String]]("subcategory_labels") def pk = primaryKey("subcategory_labels_pk_category_subcategories", (category, subcategories)) @@ -16,12 +16,13 @@ class SubcategoryLabelTable(tag: Tag) extends Table[SubcategoryLabel](tag, "subc type BookmarkData = ( String, List[String], - String, + String, List[String] ) - def constructSubcategoryLabels: BookmarkData => SubcategoryLabel = { case (category, subcategories, categoryLabel, subcategoryLabels) => - SubcategoryLabel(category, subcategories, categoryLabel, subcategoryLabels) + def constructSubcategoryLabels: BookmarkData => SubcategoryLabel = { + case (category, subcategories, categoryLabel, subcategoryLabels) => + SubcategoryLabel(category, subcategories, categoryLabel, subcategoryLabels) } def extractSubcategoryLabels: PartialFunction[SubcategoryLabel, BookmarkData] = { @@ -31,7 +32,10 @@ class SubcategoryLabelTable(tag: Tag) extends Table[SubcategoryLabel](tag, "subc override def * : ProvenShape[SubcategoryLabel] = ( - category, subcategories, categoryLabel, subcategoryLabels + category, + subcategories, + categoryLabel, + subcategoryLabels ) <> (constructSubcategoryLabels, extractSubcategoryLabels.lift) } diff --git a/app/tasks/subcategorylabel/SubcategoryLabelTask.scala b/app/tasks/subcategorylabel/SubcategoryLabelTask.scala index f3e2c592..0ebcce7f 100644 --- a/app/tasks/subcategorylabel/SubcategoryLabelTask.scala +++ b/app/tasks/subcategorylabel/SubcategoryLabelTask.scala @@ -1,11 +1,13 @@ package tasks.subcategorylabel -import cats.implicits.{catsSyntaxOption, toTraverseOps} +import cats.implicits.catsSyntaxOption +import cats.implicits.toTraverseOps import config.TaskConfiguration import controllers.error.AppError.WebsiteApiError import models.report.ArborescenceNode import org.apache.pekko.actor.ActorSystem -import repositories.subcategorylabel.{SubcategoryLabel, SubcategoryLabelRepositoryInterface} +import repositories.subcategorylabel.SubcategoryLabel +import repositories.subcategorylabel.SubcategoryLabelRepositoryInterface import repositories.tasklock.TaskRepositoryInterface import services.WebsiteApiServiceInterface import tasks.ScheduledTask @@ -35,17 +37,22 @@ class SubcategoryLabelTask( } } yield () - @tailrec - private def toSet(nodes: List[ArborescenceNode], res: Set[SubcategoryLabel] = Set.empty): Set[SubcategoryLabel] = nodes match { - case Nil => res - case h :: t => - val cats = h.path.map(_._1) - val a = cats.indices.map { i => - val tmp = cats.take(i + 1) - - SubcategoryLabel(category = tmp.head.key, subcategories = tmp.tail.map(_.key).toList, categoryLabel = tmp.head.label, subcategoryLabels = tmp.tail.map(_.label).toList) - } - toSet(t, res ++ a) - } + private def toSet(nodes: List[ArborescenceNode], res: Set[SubcategoryLabel] = Set.empty): Set[SubcategoryLabel] = + nodes match { + case Nil => res + case h :: t => + val cats = h.path.map(_._1) + val a = cats.indices.map { i => + val tmp = cats.take(i + 1) + + SubcategoryLabel( + category = tmp.head.key, + subcategories = tmp.tail.map(_.key).toList, + categoryLabel = tmp.head.label, + subcategoryLabels = tmp.tail.map(_.label).toList + ) + } + toSet(t, res ++ a) + } } diff --git a/test/orchestrators/StatsOrchestratorTest.scala b/test/orchestrators/StatsOrchestratorTest.scala index 09588e62..2df6cc12 100644 --- a/test/orchestrators/StatsOrchestratorTest.scala +++ b/test/orchestrators/StatsOrchestratorTest.scala @@ -1,7 +1,10 @@ package orchestrators import models.CountByDate -import models.report.{ArborescenceNode, CategoryInfo, NodeInfo, ReportNode} +import models.report.ArborescenceNode +import models.report.CategoryInfo +import models.report.NodeInfo +import models.report.ReportNode import orchestrators.StatsOrchestrator.formatStatData import orchestrators.StatsOrchestrator.buildReportNodes import org.specs2.mutable.Specification @@ -16,15 +19,24 @@ class StatsOrchestratorTest extends Specification { val arborescence = List( ArborescenceNode( None, - Vector(CategoryInfo("cat1", "Cat 1") -> NodeInfo("1", List("tag1")), CategoryInfo("subcat11", "Subcat 11") -> NodeInfo("1.1", List.empty)) + Vector( + CategoryInfo("cat1", "Cat 1") -> NodeInfo("1", List("tag1")), + CategoryInfo("subcat11", "Subcat 11") -> NodeInfo("1.1", List.empty) + ) ), ArborescenceNode( None, - Vector(CategoryInfo("cat2", "Cat 2") -> NodeInfo("2", List.empty), CategoryInfo("subcat21", "Subcat 1") -> NodeInfo("2.1", List("tag2"))) + Vector( + CategoryInfo("cat2", "Cat 2") -> NodeInfo("2", List.empty), + CategoryInfo("subcat21", "Subcat 1") -> NodeInfo("2.1", List("tag2")) + ) ), ArborescenceNode( None, - Vector(CategoryInfo("cat2", "Cat 2") -> NodeInfo("2", List.empty), CategoryInfo("subcat22", "Subcat 22") -> NodeInfo("2.2", List.empty)) + Vector( + CategoryInfo("cat2", "Cat 2") -> NodeInfo("2", List.empty), + CategoryInfo("subcat22", "Subcat 22") -> NodeInfo("2.2", List.empty) + ) ), ArborescenceNode( None, diff --git a/test/tasks/report/ReportRemindersTaskUnitSpec.scala b/test/tasks/report/ReportRemindersTaskUnitSpec.scala index 652d7939..27d67ea8 100644 --- a/test/tasks/report/ReportRemindersTaskUnitSpec.scala +++ b/test/tasks/report/ReportRemindersTaskUnitSpec.scala @@ -1,7 +1,12 @@ package tasks.report import org.apache.pekko.actor.testkit.typed.scaladsl.ActorTestKit -import config.{ExportReportsToSFTPConfiguration, ProbeConfiguration, ReportRemindersTaskConfiguration, SampleDataConfiguration, SubcategoryLabelsTaskConfiguration, TaskConfiguration} +import config.ExportReportsToSFTPConfiguration +import config.ProbeConfiguration +import config.ReportRemindersTaskConfiguration +import config.SampleDataConfiguration +import config.SubcategoryLabelsTaskConfiguration +import config.TaskConfiguration import models.company.AccessLevel import models.event.Event import models.report.ReportStatus