Skip to content

Commit

Permalink
Merge pull request #911 from jmarin/filing-stats
Browse files Browse the repository at this point in the history
Filing stats
  • Loading branch information
schbetsy authored Apr 7, 2017
2 parents 3945568 + e8379c1 commit 15c620e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package hmda.persistence.messages.events.processing

import hmda.model.fi.SubmissionId
import hmda.model.fi.lar.LoanApplicationRegister
import hmda.model.fi.ts.TransmittalSheet
import hmda.persistence.messages.CommonMessages.Event

object CommonHmdaValidatorEvents {
trait HmdaValidatorEvent extends Event
case class TsValidated(ts: TransmittalSheet) extends HmdaValidatorEvent
case class LarValidated(lar: LoanApplicationRegister, institutionId: String) extends HmdaValidatorEvent
case class LarValidated(lar: LoanApplicationRegister, submissionId: SubmissionId) extends HmdaValidatorEvent
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class HmdaFileValidator(submissionId: SubmissionId) extends HmdaPersistentActor
}

case lar: LoanApplicationRegister =>
val validated = LarValidated(lar, submissionId.institutionId)
val validated = LarValidated(lar, submissionId)
persist(validated) { e =>
log.debug(s"Persisted: $e")
updateState(e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package hmda.persistence.processing

import akka.NotUsed
import akka.actor.{ ActorRef, ActorSystem, Props }
import akka.stream.scaladsl.Sink
import hmda.model.fi.SubmissionId
import hmda.model.fi.lar.LoanApplicationRegister
import hmda.persistence.messages.CommonMessages.{ Command, Event, GetState }
import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.LarValidated
import hmda.persistence.model.HmdaPersistentActor
import hmda.persistence.processing.HmdaQuery._

object HmdaFiling {

Expand All @@ -17,9 +13,14 @@ object HmdaFiling {
case class AddLar(lar: LoanApplicationRegister) extends Command
case class LarAdded(lar: LoanApplicationRegister) extends Event

case class HmdaFilingState(size: Long = 0L) {
case class HmdaFilingState(filings: Map[String, Int] = Map.empty[String, Int]) {
def updated(event: Event): HmdaFilingState = {
HmdaFilingState(size + 1)
event match {
case LarValidated(_, submissionId) =>
val count = filings.getOrElse(submissionId.toString, 0)
HmdaFilingState(filings.updated(submissionId.toString, count + 1))
case _ => this
}
}
}

Expand All @@ -44,8 +45,8 @@ class HmdaFiling(filingPeriod: String) extends HmdaPersistentActor {

override def receiveCommand: Receive = super.receiveCommand orElse {

case LarValidated(lar, institutionId) =>
persist(LarValidated(lar, institutionId)) { e =>
case LarValidated(lar, submissionId) =>
persist(LarValidated(lar, submissionId)) { e =>
log.debug(s"Persisted: $e")
updateState(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class HmdaFilingSpec extends ActorSpec with LarGenerators {

val period = "2017"

val lars10 = larListGen.sample.getOrElse(List[LoanApplicationRegister]())
val lars10 = get10SampleLars
val lars20 = get10SampleLars ++ get10SampleLars

val hmdaFiling = createHmdaFiling(system, period)

Expand All @@ -22,19 +23,19 @@ class HmdaFilingSpec extends ActorSpec with LarGenerators {
"HMDA Filing" must {
"Store 10 lars" in {
for (lar <- lars10) {
probe.send(hmdaFiling, LarValidated(lar, ""))
probe.send(hmdaFiling, LarValidated(lar, SubmissionId("12345", period, 0)))
}
probe.send(hmdaFiling, GetState)
probe.expectMsg(HmdaFilingState(10))
probe.expectMsg(HmdaFilingState(Map(s"12345-$period-0" -> 10)))
}
"read validated lars from HmdaFileValidator and save them" in {
val submissionId = SubmissionId("12345", period, 1)
val validator = createHmdaFileValidator(system, submissionId)
for (lar <- lars10) {
probe.send(validator, lar)
"Store additional 20 lars" in {
for (lar <- lars20) {
probe.send(hmdaFiling, LarValidated(lar, SubmissionId("9999", period, 1)))
}
probe.send(hmdaFiling, GetState)
probe.expectMsg(HmdaFilingState(10))
probe.expectMsg(HmdaFilingState(Map(s"12345-$period-0" -> 10, s"9999-$period-1" -> 20)))
}
}

private def get10SampleLars = larListGen.sample.getOrElse(List[LoanApplicationRegister]())
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class HmdaFilingDBProjection(filingPeriod: String) extends HmdaActor {
.map(_ => LarsDeleted(institutionId)) pipeTo sender()

case event: HmdaValidatorEvent => event match {
case LarValidated(lar, institutionId) =>
case LarValidated(lar, submissionId) =>
val larQuery = implicitly[LoanApplicationRegisterQuery](lar)
val larWithPeriod = larQuery.copy(period = filingPeriod, institutionId = institutionId)
val larWithPeriod = larQuery.copy(period = filingPeriod, institutionId = submissionId.institutionId)
log.debug(s"Inserted: ${larWithPeriod.toString}")
larRepository.insertOrUpdate(larWithPeriod)
.map(x => LarInserted(x)) pipeTo sender()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package hmda.query.projections.filing

import akka.testkit.TestProbe
import hmda.model.fi.SubmissionId
import hmda.model.fi.lar.LarGenerators
import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.LarValidated
import hmda.persistence.model.ActorSpec
import hmda.query.DbConfiguration._
import org.scalatest.{ BeforeAndAfterAll, BeforeAndAfterEach }

import scala.concurrent.duration._
import hmda.query.projections.filing.HmdaFilingDBProjection._
Expand All @@ -24,16 +23,16 @@ class HmdaFilingDBProjectionSpec extends ActorSpec with LarGenerators {
}
"Insert records" in {
val lar = sampleLar
probe.send(projection, LarValidated(lar, ""))
probe.send(projection, LarValidated(lar, SubmissionId()))
probe.expectMsg(LarInserted(1))
}
"Delete records by institution id" in {
val lar1 = sampleLar
val lar2 = sampleLar
val testInstId = "test"
probe.send(projection, LarValidated(lar1, testInstId))
probe.send(projection, LarValidated(lar1, SubmissionId().copy(institutionId = testInstId)))
probe.expectMsg(LarInserted(1))
probe.send(projection, LarValidated(lar2, ""))
probe.send(projection, LarValidated(lar2, SubmissionId()))
probe.expectMsg(LarInserted(1))
probe.send(projection, DeleteLars(testInstId))
probe.expectMsg(LarsDeleted(testInstId))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hmda.query.view.filing

import akka.testkit.TestProbe
import hmda.model.fi.SubmissionId
import hmda.model.fi.lar.{ LarGenerators, LoanApplicationRegister }
import hmda.persistence.messages.CommonMessages._
import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.LarValidated
Expand All @@ -22,9 +23,9 @@ class HmdaFilingViewSpec extends ActorSpec with LarGenerators {

override def beforeAll(): Unit = {
super.beforeAll()
hmdaFilingView ! EventWithSeqNr(1, LarValidated(l1, ""))
hmdaFilingView ! EventWithSeqNr(2, LarValidated(l2, ""))
hmdaFilingView ! EventWithSeqNr(3, LarValidated(l3, ""))
hmdaFilingView ! EventWithSeqNr(1, LarValidated(l1, SubmissionId()))
hmdaFilingView ! EventWithSeqNr(2, LarValidated(l2, SubmissionId()))
hmdaFilingView ! EventWithSeqNr(3, LarValidated(l3, SubmissionId()))
}

"HMDA Filing View" must {
Expand Down

0 comments on commit 15c620e

Please sign in to comment.