-
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 #1526 from betagouv/master
MEP [TRELLO-2117] Implements probes to detect anomalies (#1525)
- Loading branch information
Showing
10 changed files
with
191 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package repositories.probe | ||
|
||
import repositories.PostgresProfile.api._ | ||
import slick.basic.DatabaseConfig | ||
import slick.jdbc.JdbcProfile | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.duration.FiniteDuration | ||
|
||
class ProbeRepository(dbConfig: DatabaseConfig[JdbcProfile]) { | ||
|
||
import dbConfig._ | ||
|
||
def getReponseConsoRate(interval: FiniteDuration): Future[Option[Double]] = db.run( | ||
sql""" | ||
SELECT (CAST(SUM(CASE | ||
WHEN forward_to_reponseconso = true THEN 1 | ||
ELSE 0 | ||
END) AS FLOAT) / count(*)) * 100 ratio FROM reports WHERE reports.creation_date < (now() - INTERVAL '${interval | ||
.toString()}'); | ||
""".as[Double].headOption | ||
) | ||
|
||
def getLancerDalerteRate(interval: FiniteDuration): Future[Option[Double]] = db.run( | ||
sql""" | ||
SELECT (CAST(SUM(CASE | ||
WHEN status = 'LanceurAlerte' THEN 1 | ||
ELSE 0 | ||
END) AS FLOAT) / count(*)) * 100 ratio FROM reports WHERE reports.creation_date < (now() - INTERVAL '${interval | ||
.toString()}'); | ||
""".as[Double].headOption | ||
) | ||
} |
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,47 @@ | ||
package tasks.probe | ||
|
||
import akka.actor.ActorSystem | ||
import config.TaskConfiguration | ||
import models.UserRole | ||
import play.api.Logger | ||
import repositories.probe.ProbeRepository | ||
import repositories.tasklock.TaskRepositoryInterface | ||
import repositories.user.UserRepositoryInterface | ||
import services.Email.ProbeTriggered | ||
import services.MailService | ||
import tasks.ScheduledTask | ||
import utils.Logs.RichLogger | ||
|
||
import java.time.LocalTime | ||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.Future | ||
import scala.concurrent.duration.DurationInt | ||
import scala.concurrent.duration.FiniteDuration | ||
|
||
class LowRateLanceurDAlerteTask( | ||
actorSystem: ActorSystem, | ||
taskConfiguration: TaskConfiguration, | ||
taskRepository: TaskRepositoryInterface, | ||
probeRepository: ProbeRepository, | ||
userRepository: UserRepositoryInterface, | ||
mailService: MailService | ||
)(implicit executionContext: ExecutionContext) | ||
extends ScheduledTask(101, "low_rate_lanceur_dalerte", taskRepository, actorSystem, taskConfiguration) { | ||
|
||
override val logger: Logger = Logger(this.getClass) | ||
override val startTime: LocalTime = LocalTime.of(2, 0) | ||
override val interval: FiniteDuration = 12.hours | ||
|
||
override def runTask(): Future[Unit] = probeRepository.getLancerDalerteRate(interval).flatMap { | ||
case Some(rate) if rate < 0.1d => | ||
logger.warnWithTitle("probe_triggered", s"Taux de signalements 'Lanceur d'alerte' faible : $rate%") | ||
for { | ||
users <- userRepository.listForRoles(Seq(UserRole.Admin)) | ||
_ <- mailService | ||
.send(ProbeTriggered(users.map(_.email), "Taux de signalements 'Lanceur d'alerte' faible", rate, "bas")) | ||
} yield () | ||
case rate => | ||
logger.debug(s"Taux de signalements correct: $rate%") | ||
Future.unit | ||
} | ||
} |
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,47 @@ | ||
package tasks.probe | ||
|
||
import akka.actor.ActorSystem | ||
import config.TaskConfiguration | ||
import models.UserRole | ||
import play.api.Logger | ||
import repositories.probe.ProbeRepository | ||
import repositories.tasklock.TaskRepositoryInterface | ||
import repositories.user.UserRepositoryInterface | ||
import services.Email.ProbeTriggered | ||
import services.MailService | ||
import tasks.ScheduledTask | ||
import utils.Logs.RichLogger | ||
|
||
import java.time.LocalTime | ||
import scala.concurrent.duration.DurationInt | ||
import scala.concurrent.duration.FiniteDuration | ||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.Future | ||
|
||
class LowRateReponseConsoTask( | ||
actorSystem: ActorSystem, | ||
taskConfiguration: TaskConfiguration, | ||
taskRepository: TaskRepositoryInterface, | ||
probeRepository: ProbeRepository, | ||
userRepository: UserRepositoryInterface, | ||
mailService: MailService | ||
)(implicit executionContext: ExecutionContext) | ||
extends ScheduledTask(100, "low_rate_reponse_conso", taskRepository, actorSystem, taskConfiguration) { | ||
|
||
override val logger: Logger = Logger(this.getClass) | ||
override val startTime: LocalTime = LocalTime.of(2, 0) | ||
override val interval: FiniteDuration = 12.hours | ||
|
||
override def runTask(): Future[Unit] = probeRepository.getReponseConsoRate(interval).flatMap { | ||
case Some(rate) if rate < 1.0d => | ||
logger.warnWithTitle("probe_triggered", s"Taux de signalements 'Réponse conso' faible : $rate%") | ||
for { | ||
users <- userRepository.listForRoles(Seq(UserRole.Admin)) | ||
_ <- mailService | ||
.send(ProbeTriggered(users.map(_.email), "Taux de signalements 'Réponse conso' faible", rate, "bas")) | ||
} yield () | ||
case rate => | ||
logger.debug(s"Taux de signalements correct: $rate%") | ||
Future.unit | ||
} | ||
} |
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,15 @@ | ||
@import java.net.URI | ||
@import utils.FrontRoute | ||
|
||
@(probeName: String, rate: Double, issue: String) | ||
|
||
@views.html.mails.layout("Sonde déclenchée") { | ||
<p> | ||
Cet email a été envoyée car la sonde @probeName a été déclenchée. | ||
</p> | ||
|
||
<p>Le taux détecté (@rate %) est anormalement @issue. | ||
</p> | ||
|
||
<p>Contactez un développeur pour investiguer.</p> | ||
} |
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