Skip to content

Commit

Permalink
Remove all temporary code to fill department column
Browse files Browse the repository at this point in the history
  • Loading branch information
eletallbetagouv committed Sep 11, 2024
1 parent de61bf7 commit 3fdff62
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 82 deletions.
16 changes: 0 additions & 16 deletions app/controllers/EtablissementController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,6 @@ class EtablissementController(
res.recover { case err => handleError(request, err) }
}

def fillDepartementWithLimit() = Action.async { request =>
val res = for {
_ <- validateToken(request, token)
_ <- importService.fillDepartmentWithLimit()
} yield NoContent
res.recover { case err => handleError(request, err) }
}

def fillDepartementUntilAllDone() = Action.async { request =>
val res = for {
_ <- validateToken(request, token)
_ <- importService.fillDepartementUntilAllDone()
} yield NoContent
res.recover { case err => handleError(request, err) }
}

}

object EtablissementController {
Expand Down
39 changes: 0 additions & 39 deletions app/orchestrators/EtablissementImportService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,6 @@ class EtablissementImportService(

private[this] val logger = Logger(this.getClass)

// This fill the department column for every etablissement that still doesn't have it
// We should only need to use this once, can be deleted after

// Note : rows for which the department code can't be found (should be really rare)
// are just ignored, but are then queried again by the next iteration.
// It could be a problem if there are too many of them, for now we just try like that.
def fillDepartementUntilAllDone(): Future[Unit] =
for {
rowsProcessed <- fillDepartmentWithLimit()
_ <-
if (rowsProcessed == 0) {
logger.info("All etablissements filled with departments ")
Future.successful(())
} else {
fillDepartementUntilAllDone()
}
} yield ()

def fillDepartmentWithLimit(): Future[Int] =
for {
allCommunes <- geoApiClient.getAllCommunes()
etablissements <- repository.listWithoutMissingDepartment(1000)
_ = logger.info(s"Filling codeDepartement for ${etablissements.length} etablissements")
rowsSuccess <- etablissements.foldLeft(Future.successful(0)) { case (previous, etab) =>
for {
previousCount <- previous
maybeCodeDepartment = Departments.findCodeDepartementOfEtablissement(Right(etab), allCommunes)
rowsUpdatedSuccessfully <- maybeCodeDepartment match {
case Some(codeDepartement) =>
repository.updateDepartment(etab.siret, codeDepartement)
case None =>
logger.warn(s"Can't find departement of ${etab.siret} (CP ${etab.codePostalEtablissement})")
Future.successful(0)
}
} yield previousCount + rowsUpdatedSuccessfully
}
_ = logger.info(s"Filled codeDepartement for $rowsSuccess/${etablissements.length} etablissements")
} yield etablissements.length

// One shot, API
def runImportEtablissementsRequest(importRequest: ImportRequest): Future[Unit] =
for {
Expand Down
18 changes: 0 additions & 18 deletions app/repositories/insee/EtablissementRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ class EtablissementRepository(val dbConfig: DatabaseConfig[JdbcProfile], conf: S
""")
}

override def updateDepartment(siret: SIRET, codeDepartment: String): Future[Int] =
db.run(
table
.filter(_.siret === siret)
.map(a => a.codeDepartement)
.update(Some(codeDepartment))
)

// Be careful when modifying this search, the order is important to use PG Indexes correctly.
// Check the generated query with an EXPLAIN if necessary.
override def search(
Expand Down Expand Up @@ -159,16 +151,6 @@ class EtablissementRepository(val dbConfig: DatabaseConfig[JdbcProfile], conf: S
.result
).map(_.headOption)

override def listWithoutMissingDepartment(limit: Int): Future[Seq[EtablissementData]] =
db.run(
table
.filter(_.codeDepartement.isEmpty)
.filter(_.codePostalEtablissement.isDefined)
.filter(_.codePaysEtrangerEtablissement.isEmpty)
.filter(_.libellePaysEtrangerEtablissement.isEmpty)
.take(limit)
.result
)
}

object EtablissementRepository {
Expand Down
3 changes: 0 additions & 3 deletions app/repositories/insee/EtablissementRepositoryInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ trait EtablissementRepositoryInterface {

def insertOrUpdate(companies: Map[String, Option[String]]): Future[Int]

def updateDepartment(siret: SIRET, codeDepartment: String): Future[Int]

def search(
q: String,
postalCode: Option[String],
Expand All @@ -39,5 +37,4 @@ trait EtablissementRepositoryInterface {
openCompaniesOnly: Boolean
): Future[Option[(EtablissementData, Option[ActivityCode])]]

def listWithoutMissingDepartment(limit: Int): Future[Seq[EtablissementData]]
}
2 changes: 0 additions & 2 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ GET /api/companies/search controllers.EtablissementCont
POST /api/companies/search controllers.EtablissementController.getBySiret(lang: Option[java.util.Locale])
POST /api/companies/siren/search controllers.EtablissementController.getBySiren(lang: Option[java.util.Locale], onlyHeadOffice: Option[Boolean])
POST /api/companies/import controllers.EtablissementController.importEtablissements()
POST /api/companies/fill-department controllers.EtablissementController.fillDepartementWithLimit()
POST /api/companies/fill-department/all controllers.EtablissementController.fillDepartementUntilAllDone()
4 changes: 0 additions & 4 deletions test/orchestrators/EtablissementRepositoryInterfaceMock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class EtablissementRepositoryInterfaceMock(

override def insertOrUpdate(companies: Map[String, Option[String]]): Future[Int] = ???

override def updateDepartment(siret: SIRET, codeDepartment: String): Future[Int] = ???

override def search(
q: String,
postalCode: Option[String],
Expand All @@ -44,6 +42,4 @@ class EtablissementRepositoryInterfaceMock(
openCompaniesOnly: Boolean
): Future[Option[(EtablissementData, Option[ActivityCode])]] = Future.successful(searchHeadOfficeBySirenFunc)

override def listWithoutMissingDepartment(limit: Int): Future[List[EtablissementData]] = ???

}

0 comments on commit 3fdff62

Please sign in to comment.