-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1814 from betagouv/master
MEP
- Loading branch information
Showing
35 changed files
with
535 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package models.albert | ||
|
||
import play.api.libs.json.Json | ||
import play.api.libs.json.OFormat | ||
|
||
case class AlbertProblemsResult( | ||
nbReportsUsed: Int, | ||
problemsFound: Seq[AlbertProblem] | ||
) | ||
|
||
object AlbertProblemsResult { | ||
implicit val format: OFormat[AlbertProblemsResult] = Json.format[AlbertProblemsResult] | ||
|
||
} | ||
|
||
case class AlbertProblem( | ||
probleme: String, | ||
signalements: Int | ||
) | ||
object AlbertProblem { | ||
implicit val format: OFormat[AlbertProblem] = Json.format[AlbertProblem] | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package models.report | ||
|
||
import play.api.libs.json.Format | ||
import play.api.libs.json.Json | ||
import repositories.PostgresProfile.api._ | ||
import slick.ast.BaseTypedType | ||
import slick.jdbc.H2Profile.MappedColumnType | ||
import slick.jdbc.JdbcType | ||
|
||
case class ConsumerIp(value: String) extends AnyVal | ||
|
||
object ConsumerIp { | ||
implicit val ConsumerIpFormat: Format[ConsumerIp] = Json.valueFormat[ConsumerIp] | ||
|
||
implicit val ConsumerIpColumnType: JdbcType[ConsumerIp] with BaseTypedType[ConsumerIp] = | ||
MappedColumnType.base[ConsumerIp, String]( | ||
_.value, | ||
ConsumerIp(_) | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package orchestrators | ||
|
||
import models.albert.AlbertProblemsResult | ||
import models.company.Company | ||
import models.report.Report | ||
import play.api.Logger | ||
import repositories.report.ReportRepositoryInterface | ||
import services.AlbertService | ||
|
||
import java.util.UUID | ||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.Future | ||
|
||
class AlbertOrchestrator( | ||
val reportRepository: ReportRepositoryInterface, | ||
val albertService: AlbertService | ||
)(implicit ec: ExecutionContext) { | ||
final val logger = Logger(getClass) | ||
|
||
def genActivityLabelForCompany(company: Company): Future[Option[String]] = { | ||
val maxReportsUsed = 5 | ||
for { | ||
descriptions <- getLatestMeaningfulReportsDescsForCompany( | ||
company.id, | ||
maxReportsUsed | ||
) | ||
maybeLabel <- descriptions match { | ||
case Nil => | ||
logger.info(s"Couldn't find enough usable reports for Albert for ${company.siret}") | ||
Future.successful(None) | ||
case _ => | ||
albertService | ||
.labelCompanyActivity(company.id, descriptions) | ||
.recover { err => | ||
logger.error(s"Didn't get a result from Albert for ${company.siret}", err) | ||
None | ||
} | ||
} | ||
} yield maybeLabel | ||
} | ||
|
||
def genProblemsForCompany(companyId: UUID): Future[Option[AlbertProblemsResult]] = { | ||
val maxReportsUsed = 30 | ||
for { | ||
descriptions <- getLatestMeaningfulReportsDescsForCompany( | ||
companyId, | ||
maxReportsUsed | ||
) | ||
maybeResult <- descriptions match { | ||
case Nil => | ||
logger.info(s"Not enough usable reports for Albert for company ${companyId}") | ||
Future.successful(None) | ||
case _ => | ||
albertService | ||
.findProblems(companyId, descriptions) | ||
.recover { err => | ||
logger.error(s"Didn't get a result from Albert for company $companyId", err) | ||
None | ||
} | ||
|
||
} | ||
} yield maybeResult | ||
} | ||
|
||
private def getLatestMeaningfulReportsDescsForCompany(companyId: UUID, maxReports: Int): Future[Seq[String]] = | ||
for { | ||
allLatestReports <- reportRepository.getLatestReportsOfCompany( | ||
companyId, | ||
// we need a little big of margin: | ||
// - not all reports will have a description | ||
// - we deduplicate later by email | ||
limit = maxReports * 2 | ||
) | ||
meaningfulDescriptions = | ||
// One user could submit several reports in a row on the company, | ||
// thus distorting our data a bit | ||
deduplicateByEmail(allLatestReports) | ||
.flatMap(_.getDescription) | ||
.take(maxReports) | ||
} yield meaningfulDescriptions | ||
|
||
private def deduplicateByEmail(reports: Seq[Report]) = | ||
reports | ||
.groupBy(_.email) | ||
.map(_._2.head) | ||
.toSeq | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.