Skip to content

Commit

Permalink
Merge pull request #1453 from betagouv/master
Browse files Browse the repository at this point in the history
MEP [TRELLO-1997] Fix stats by subcategories & add foreign anomalies (#1452)
  • Loading branch information
charlescd authored Sep 20, 2023
2 parents 5aec031 + 0a6891b commit 28974aa
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 17 deletions.
17 changes: 14 additions & 3 deletions app/loader/SignalConsoApplicationLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,18 @@ class SignalConsoComponents(
// This file can be generated in the website using 'yarn minimized-anomalies'.
// This is the first iteration of the story, using an copied generated file from the website
// The second version will be to expose the file in the website and fetch it in the API at runtime.
val arborescenceAsJson = context.environment
.resourceAsStream("minimized-anomalies.json")
val arborescenceFrAsJson = context.environment
.resourceAsStream("minimized-anomalies_fr.json")
.map(json =>
try Json.parse(json)
finally json.close()
)
.map(_.as[JsArray])
.map(ArborescenceNode.fromJson)
.get

val arborescenceEnAsJson = context.environment
.resourceAsStream("minimized-anomalies_en.json")
.map(json =>
try Json.parse(json)
finally json.close()
Expand All @@ -426,7 +436,8 @@ class SignalConsoComponents(
eventRepository,
responseConsumerReviewRepository,
accessTokenRepository,
arborescenceAsJson
arborescenceFrAsJson,
arborescenceEnAsJson
)

val websitesOrchestrator =
Expand Down
7 changes: 7 additions & 0 deletions app/models/report/ReportNode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models.report

import play.api.libs.json.Json
import play.api.libs.json.OFormat
import play.api.libs.json.OWrites

case class ReportNode(
name: String,
Expand All @@ -15,3 +16,9 @@ case class ReportNode(
object ReportNode {
implicit val format: OFormat[ReportNode] = Json.format[ReportNode]
}

case class ReportNodes(fr: List[ReportNode], en: List[ReportNode])

object ReportNodes {
implicit val writes: OWrites[ReportNodes] = Json.writes[ReportNodes]
}
23 changes: 14 additions & 9 deletions app/orchestrators/StatsOrchestrator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import utils.Constants.Departments

import java.sql.Timestamp
import java.time._
import java.util.Locale
import java.util.UUID
import scala.annotation.tailrec
import scala.concurrent.ExecutionContext
Expand All @@ -31,14 +32,18 @@ class StatsOrchestrator(
eventRepository: EventRepositoryInterface,
reportConsumerReviewRepository: ResponseConsumerReviewRepositoryInterface,
accessTokenRepository: AccessTokenRepositoryInterface,
arborescence: List[ArborescenceNode]
arborescenceFr: List[ArborescenceNode],
arborescenceEn: List[ArborescenceNode]
)(implicit val executionContext: ExecutionContext) {

def reportsCountBySubcategories(filters: ReportsCountBySubcategoriesFilter): Future[List[ReportNode]] = for {
reportNodes <- reportRepository
.reportsCountBySubcategories(filters)
.map(StatsOrchestrator.buildReportNodes(arborescence, _))
} yield reportNodes
def reportsCountBySubcategories(filters: ReportsCountBySubcategoriesFilter): Future[ReportNodes] = for {
reportNodesFr <- reportRepository
.reportsCountBySubcategories(filters, Locale.FRENCH)
.map(StatsOrchestrator.buildReportNodes(arborescenceFr, _))
reportNodesEn <- reportRepository
.reportsCountBySubcategories(filters, Locale.ENGLISH)
.map(StatsOrchestrator.buildReportNodes(arborescenceEn, _))
} yield ReportNodes(reportNodesFr, reportNodesEn)

def countByDepartments(start: Option[LocalDate], end: Option[LocalDate]): Future[Seq[(String, Int)]] =
for {
Expand Down Expand Up @@ -179,9 +184,9 @@ object StatsOrchestrator {
val tree = ReportNode("", 0, 0, List.empty, List.empty, None)

arbo.foreach { arborescenceNode =>
val test = merged.find(_._1 == arborescenceNode.path.map(_._1).toList)
val count = test.map(_._2).getOrElse(0)
val reclamations = test.map(_._3).getOrElse(0)
val res = merged.find(_._1 == arborescenceNode.path.map(_._1).toList)
val count = res.map(_._2).getOrElse(0)
val reclamations = res.map(_._3).getOrElse(0)
createOrUpdateReportNode(arborescenceNode.path, count, reclamations, tree)
}

Expand Down
14 changes: 12 additions & 2 deletions app/repositories/report/ReportRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ class ReportRepository(override val dbConfig: DatabaseConfig[JdbcProfile])(impli
}

def reportsCountBySubcategories(
filters: ReportsCountBySubcategoriesFilter
): Future[Seq[(String, List[String], Int, Int)]] =
filters: ReportsCountBySubcategoriesFilter,
lang: Locale
): Future[Seq[(String, List[String], Int, Int)]] = {
implicit val localeColumnType = MappedColumnType.base[Locale, String](_.toLanguageTag, Locale.forLanguageTag)

db.run(
table
.filterOpt(filters.start) { case (table, s) =>
Expand All @@ -65,6 +68,12 @@ class ReportRepository(override val dbConfig: DatabaseConfig[JdbcProfile])(impli
.filterOpt(filters.end) { case (table, e) =>
table.creationDate < ZonedDateTime.of(e, LocalTime.MAX, ZoneOffset.UTC.normalized()).toOffsetDateTime
}
.filter { table =>
lang match {
case Locale.FRENCH => table.lang === Locale.FRENCH || table.lang.isEmpty
case _ => table.lang =!= Locale.FRENCH
}
}
.filterIf(filters.departments.nonEmpty) { case (table) =>
filters.departments
.flatMap(toPostalCode)
Expand All @@ -89,6 +98,7 @@ class ReportRepository(override val dbConfig: DatabaseConfig[JdbcProfile])(impli
}
.result
)
}

def findByEmail(email: EmailAddress): Future[Seq[Report]] =
db.run(table.filter(_.email === email).result)
Expand Down
4 changes: 3 additions & 1 deletion app/repositories/report/ReportRepositoryInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import utils.EmailAddress

import java.time.LocalDate
import java.time.OffsetDateTime
import java.util.Locale
import java.util.UUID
import scala.collection.SortedMap
import scala.concurrent.Future
Expand Down Expand Up @@ -67,7 +68,8 @@ trait ReportRepositoryInterface extends CRUDRepositoryInterface[Report] {
def getPhoneReports(start: Option[LocalDate], end: Option[LocalDate]): Future[List[Report]]

def reportsCountBySubcategories(
filters: ReportsCountBySubcategoriesFilter
filters: ReportsCountBySubcategoriesFilter,
lang: Locale
): Future[Seq[(String, List[String], Int, Int)]]

}
1 change: 0 additions & 1 deletion conf/minimized-anomalies.json

This file was deleted.

1 change: 1 addition & 0 deletions conf/minimized-anomalies_en.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions conf/minimized-anomalies_fr.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion test/controllers/report/ReportRepositoryMock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import utils.EmailAddress

import java.time.LocalDate
import java.time.OffsetDateTime
import java.util.Locale
import java.util.UUID
import scala.collection.SortedMap
import scala.collection.mutable
Expand Down Expand Up @@ -68,7 +69,8 @@ class ReportRepositoryMock(database: mutable.Map[UUID, Report] = mutable.Map.emp
override def cloudWord(companyId: UUID): Future[List[ReportWordOccurrence]] = ???

override def reportsCountBySubcategories(
filters: ReportsCountBySubcategoriesFilter
filters: ReportsCountBySubcategoriesFilter,
lang: Locale
): Future[Seq[(String, List[String], Int, Int)]] = ???

override def getForWebsiteWithoutCompany(websiteHost: String): Future[List[UUID]] = ???
Expand Down

0 comments on commit 28974aa

Please sign in to comment.