Skip to content

Commit

Permalink
update late filers to work with paramter
Browse files Browse the repository at this point in the history
  • Loading branch information
BarakStout committed Nov 18, 2020
1 parent a6d25b6 commit 143c95a
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 0 deletions.
5 changes: 5 additions & 0 deletions hmda-dashboard/src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ hmda {
runtime.mode = "kubernetes"
}

dashboard {
sub-history = "submission-history-mview"
sub-history = ${?SUBMISSION_HISTORY_MVIEW}
}

dashboard_db {
profile = "slick.jdbc.PostgresProfile$"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package hmda.dashboard.api

import akka.http.scaladsl.model.StatusCodes.BadRequest
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._
import akka.http.scaladsl.server.directives.RouteDirectives.complete
import hmda.dashboard.Settings
import hmda.dashboard.models._


trait DashboardDirectives extends Settings {

private def extractDatetime: Directive1[String] =
parameters("late_date" ).flatMap {
case "" =>
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
complete((BadRequest, "must provide late_date"))

case x => provide(x)
}

def extractNationwideMandatoryYears(innerRoute: String => Route): Route =
(extractDatetime) { (datetime) =>
if (datetime.nonEmpty)
innerRoute(datetime)
else {
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
complete((BadRequest, ProvideDatetime()))
}
}

}

object DashboardDirectives extends DashboardDirectives
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ch.megard.akka.http.cors.scaladsl.CorsDirectives.{cors, corsRejectionHand
import com.typesafe.config.{Config, ConfigFactory}
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport._
import hmda.auth.OAuth2Authorization
import hmda.dashboard.api.DashboardDirectives._
import hmda.dashboard.models.HealthCheckStatus.Up
import hmda.dashboard.models._
import hmda.dashboard.repositories._
Expand Down Expand Up @@ -338,6 +339,17 @@ private class HmdaDashboardHttpApi(log: Logger, config: Config)(implicit val ec:
.map(aggs => TopInstitutionsCountOpenEndCreditAggregationResponse(aggs))
.runToFuture
)
} ~
path("late_filers" / Segment) { (period) =>
extractNationwideMandatoryYears { mandatoryFields =>
log.info(s"Fetching late filers for period=${period} with mandatoryFields=${mandatoryFields}")
complete {
query
.fetchLateFilers(period, mandatoryFields)
.map(aggs => LateFilersAggregationResponse(aggs))
.runToFuture
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package hmda.dashboard.models

import io.circe.Encoder
import io.circe.generic.semiauto._

// $COVERAGE-OFF$
sealed trait ErrorResponse {
def errorType: String
def message: String
}

object ProvideDatetime {
implicit val encoder: Encoder[ProvideDatetime] = deriveEncoder[ProvideDatetime]
}
final case class ProvideDatetime(errorType: String = "Bad Format", message: String = "Must provide date time in ##/##/#### ##:##:## format") extends ErrorResponse

// $COVERAGE-ON$
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package hmda.dashboard.models

import io.circe.Codec
import slick.jdbc.GetResult

case class LateFilers(
agency: Int,
institution_name: String,
lei: String,
total_lines: String,
sign_date: String,
sub_id: String
)

object LateFilers {
implicit val getResults: GetResult[LateFilers] = GetResult(r => LateFilers(r.<<,r.<<,r.<<,r.<<,r.<<,r.<<))

implicit val codec: Codec[LateFilers] =
Codec.forProduct6("Agency","Institution Name","LEI","Total Lars","Sign Date (EST)","Submission ID")(LateFilers.apply)(f => (f.agency,f.institution_name,f.lei,f.total_lines,f.sign_date,f.sub_id))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package hmda.dashboard.models

import io.circe.{Decoder, Encoder, HCursor}

case class LateFilersAggregationResponse(aggregations: Seq[LateFilers])

object LateFilersAggregationResponse {
private object constants {
val Results = "results"
}

implicit val encoder: Encoder[LateFilersAggregationResponse] =
Encoder.forProduct1(constants.Results)(aggR =>
aggR.aggregations)

implicit val decoder: Decoder[LateFilersAggregationResponse] = (c: HCursor) =>
for {
a <- c.downField(constants.Results).as[Seq[LateFilers]]
} yield LateFilersAggregationResponse(a)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package hmda.dashboard.models

case class QueryField(name: String,
value: Seq[String])

case class QueryFields(name: String, value: String)
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,15 @@ class PostgresRepository (config: DatabaseConfig[JdbcProfile],bankFilterList: Ar
Task.deferFuture(db.run(query)).guarantee(Task.shift)
}

def fetchLateFilers(period: String, lateDate: String) : Task[Seq[LateFilers]] = {
val tsTable = tsTableSelector(period)
val subHistMview = "submission_hist_mview"
val query = sql"""
select * from (select ts.agency, ts.institution_name, sh.lei, ts.total_lines, sh.sign_date_east :: date, sh.submission_id, rank() over( partition by sh.lei order by sh.sign_date_east asc) from #${subHistMview} as sh left join #${tsTable} as ts on upper(sh.lei) = upper(ts.lei) where upper(sh.lei) not in ( select distinct upper(lei) from #${subHistMview} as sh_sub where sh_sub.sign_date_utc < '#${lateDate}' and split_part(sh_sub.submission_id, '-', 2) = '#${period}' ) and split_part(sh.submission_id, '-', 2) = '#${period}' order by sh.lei, rank) sl where upper(sl.lei) not in (#${filterList}) and rank=1 order by sign_date_east desc;
""".as[LateFilers]
Task.deferFuture(db.run(query)).guarantee(Task.shift)
}

def healthCheck: Task[Unit] = {
Task.deferFuture (db.run (sql"select 1".as[Int] ) ).guarantee (Task.shift).void
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,7 @@ class DashboardQueryService (repo: PostgresRepository) extends QueryService{

override def fetchTopInstitutionsCountOpenEndCredit(year: String, x: Int): Task[Seq[TopInstitutionsCountOpenEndCredit]] =
repo.fetchTopInstitutionsCountOpenEndCredit(year, x)

override def fetchLateFilers(period: String, late: String): Task[Seq[LateFilers]] =
repo.fetchLateFilers(period, late)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ trait QueryService {
def fetchFilersCountOpenEndOriginationsByAgency(period: String, x: Int) : Task[Seq[FilersCountOpenEndOriginationsByAgency]]
def fetchFilersCountOpenEndOriginationsByAgencyGraterOrEqual(period: String, x: Int) : Task[Seq[FilersCountOpenEndOriginationsByAgencyGraterOrEqual]]
def fetchTopInstitutionsCountOpenEndCredit(period: String, x: Int) : Task[Seq[TopInstitutionsCountOpenEndCredit]]
def fetchLateFilers(period: String, late: String) : Task[Seq[LateFilers]]
}

0 comments on commit 143c95a

Please sign in to comment.