Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Jun 6, 2024
1 parent 5bc2774 commit 0b37b2b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
19 changes: 12 additions & 7 deletions api/app/processor/CheckInvariantsProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ 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] = {
Expand All @@ -27,22 +31,23 @@ class CheckInvariantsProcessor @Inject()(
().validNec
}

private[this] case class InvariantResult(invariant: Invariant, count: Long)
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) {
val subject = if (withErrors.length == 1) {
lazy val subject = if (withErrors.length == 1) {
"1 Error"
} else {
s"${withErrors.length} Errors"
}
println(subject)
withErrors.foreach { e =>
println(s"${e.invariant.name}: ${e.count}")
println(s"${e.invariant.query.interpolate()}")
println("")
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
)
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions api/app/views/emails/invariants.scala.html
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>

0 comments on commit 0b37b2b

Please sign in to comment.