-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
103 additions
and
61 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 was deleted.
Oops, something went wrong.
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,54 @@ | ||
package processor | ||
|
||
import anorm.SqlParser | ||
import cats.data.ValidatedNec | ||
import cats.implicits._ | ||
import invariants.{Invariant, Invariants} | ||
import io.apibuilder.task.v0.models._ | ||
import lib.{AppConfig, EmailUtil, Person} | ||
import play.api.db.Database | ||
|
||
import javax.inject.Inject | ||
|
||
case class InvariantResult(invariant: Invariant, count: Long) | ||
|
||
class CheckInvariantsProcessor @Inject()( | ||
args: TaskProcessorArgs, | ||
appConfig: AppConfig, | ||
invariants: Invariants, | ||
database: Database, | ||
email: EmailUtil, | ||
) extends TaskProcessor(args, TaskType.CheckInvariants) { | ||
|
||
override def processRecord(id: String): ValidatedNec[String, Unit] = { | ||
val results = invariants.all.map { i => | ||
val count = database.withConnection { c => | ||
i.query.as(SqlParser.long(1).*)(c).headOption.getOrElse(0L) | ||
} | ||
InvariantResult(i, count) | ||
} | ||
sendResults(results) | ||
().validNec | ||
} | ||
|
||
private[this] def sendResults(results: Seq[InvariantResult]): Unit = { | ||
val (noErrors, withErrors) = results.partition(_.count == 0) | ||
|
||
println(s"# Invariants checked with no errors: ${noErrors.length}") | ||
if (withErrors.nonEmpty) { | ||
lazy val subject = if (withErrors.length == 1) { | ||
"1 Error" | ||
} else { | ||
s"${withErrors.length} Errors" | ||
} | ||
lazy val body = views.html.emails.invariants(appConfig, noErrors.map(_.invariant.name), withErrors).toString | ||
appConfig.sendErrorsTo.foreach { recipientEmail => | ||
email.sendHtml( | ||
to = Person(email = recipientEmail), | ||
subject = s"[API Builder Invariants] $subject", | ||
body = body | ||
) | ||
} | ||
} | ||
} | ||
} |
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,27 @@ | ||
@( | ||
appConfig: lib.AppConfig, | ||
noErrors: Seq[String], | ||
withErrors: Seq[processor.InvariantResult] | ||
) | ||
|
||
<h2> | ||
The following invariants resulted in a non zero value | ||
</h2> | ||
|
||
<ul> | ||
@for(i <- withErrors) { | ||
<li> @{i.invariant.name}: @{i.count} | ||
<blockquote>@{i.invariant.query.interpolate()}</blockquote> | ||
</li> | ||
} | ||
</ul> | ||
|
||
<h2> | ||
The following invariants were all valid | ||
</h2> | ||
|
||
<ul> | ||
@for(i <- noErrors) { | ||
<li> @i </li> | ||
} | ||
</ul> |
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
package processor | ||
|
||
import org.scalatestplus.play.PlaySpec | ||
import org.scalatestplus.play.guice.GuiceOneAppPerSuite | ||
|
||
class CheckInvariantsProcessorSpec extends PlaySpec with GuiceOneAppPerSuite with db.Helpers { | ||
|
||
private def processor: CheckInvariantsProcessor = injector.instanceOf[CheckInvariantsProcessor] | ||
|
||
"process" in { | ||
processor.processRecord("periodic") | ||
} | ||
} |
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