Skip to content

Commit

Permalink
[TRELLO-2536] Format
Browse files Browse the repository at this point in the history
  • Loading branch information
charlescd committed Nov 12, 2024
1 parent 821e56f commit b681e9c
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 40 deletions.
4 changes: 3 additions & 1 deletion app/controllers/ReportController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 16 additions & 5 deletions app/models/report/ArborescenceNode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)))
Expand Down
8 changes: 6 additions & 2 deletions app/models/report/reportmetadata/ReportMetadata.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
22 changes: 18 additions & 4 deletions app/repositories/report/ReportRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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

}
Expand Down
7 changes: 6 additions & 1 deletion app/repositories/subcategorylabel/SubcategoryLabel.scala
Original file line number Diff line number Diff line change
@@ -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]
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions app/repositories/subcategorylabel/SubcategoryLabelTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ 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))

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] = {
Expand All @@ -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)
}

Expand Down
35 changes: 21 additions & 14 deletions app/tasks/subcategorylabel/SubcategoryLabelTask.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
20 changes: 16 additions & 4 deletions test/orchestrators/StatsOrchestratorTest.scala
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion test/tasks/report/ReportRemindersTaskUnitSpec.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit b681e9c

Please sign in to comment.