<commit_before>package hmda.api.http

import akka.NotUsed
import akka.stream.scaladsl.{ Sink, Source }
import hmda.api._
import hmda.api.model._
import hmda.census.model.CbsaLookup
import hmda.model.edits.EditMetaDataLookup
import hmda.model.fi.lar.LoanApplicationRegister
import hmda.model.fi.ts.TransmittalSheet
import hmda.model.fi.HmdaFileRow
import hmda.model.validation.{ EmptyValidationError, ValidationError }
import hmda.persistence.messages.CommonMessages.Event
import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.LarValidated
import hmda.persistence.messages.events.processing.HmdaFileValidatorEvents._
import hmda.util.SourceUtils
import spray.json.{ JsNumber, JsObject, JsString }

import scala.concurrent.Future

trait ValidationErrorConverter extends SourceUtils {

  ///// Edit Collection
  def editInfos[ec: EC, mat: MAT, as: AS](editNames: Set[String]): List[EditInfo] = {
    editNames.toList.sorted.map(name => EditInfo(name, editDescription(name)))
  }

  private def editDescription(editName: String): String = {
    EditMetaDataLookup.forEdit(editName).editDescription
  }

  ///// Edits Report CSV
  private val csvHeaderSource = Source.fromIterator(() => Iterator("editType, editId, loanId"))

  def csvResultStream[ec: EC, mat: MAT, as: AS](eventSource: Source[Event, NotUsed]): Source[String, Any] = {
    val edits = allEdits(eventSource)
    val csvSource = edits.map(_.toCsv)
    csvHeaderSource.concat(csvSource)
  }

  def allEdits(eventSource: Source[Event, NotUsed]) = {
    val edits: Source[ValidationError, NotUsed] =
      eventSource.map {
        case LarSyntacticalError(err) => err
        case TsSyntacticalError(err) => err
        case LarValidityError(err) => err
        case TsValidityError(err) => err
        case LarQualityError(err) => err
        case TsQualityError(err) => err
        case LarMacroError(err) => err
        case _ => EmptyValidationError
      }

    edits.filter(_ != EmptyValidationError)<commit_msg>bugfix: Add newline after CSV header<commit_after>package hmda.api.http

import akka.NotUsed
import akka.stream.scaladsl.{ Sink, Source }
import hmda.api._
import hmda.api.model._
import hmda.census.model.CbsaLookup
import hmda.model.edits.EditMetaDataLookup
import hmda.model.fi.lar.LoanApplicationRegister
import hmda.model.fi.ts.TransmittalSheet
import hmda.model.fi.HmdaFileRow
import hmda.model.validation.{ EmptyValidationError, ValidationError }
import hmda.persistence.messages.CommonMessages.Event
import hmda.persistence.messages.events.processing.CommonHmdaValidatorEvents.LarValidated
import hmda.persistence.messages.events.processing.HmdaFileValidatorEvents._
import hmda.util.SourceUtils
import spray.json.{ JsNumber, JsObject, JsString }

import scala.concurrent.Future

trait ValidationErrorConverter extends SourceUtils {

  ///// Edit Collection
  def editInfos[ec: EC, mat: MAT, as: AS](editNames: Set[String]): List[EditInfo] = {
    editNames.toList.sorted.map(name => EditInfo(name, editDescription(name)))
  }

  private def editDescription(editName: String): String = {
    EditMetaDataLookup.forEdit(editName).editDescription
  }

  ///// Edits Report CSV
  private val csvHeaderSource = Source.fromIterator(() => Iterator("editType, editId, loanId\n"))

  def csvResultStream[ec: EC, mat: MAT, as: AS](eventSource: Source[Event, NotUsed]): Source[String, Any] = {
    val edits = allEdits(eventSource)
    val csvSource = edits.map(_.toCsv)
    csvHeaderSource.concat(csvSource)
  }

  def allEdits(eventSource: Source[Event, NotUsed]) = {
    val edits: Source[ValidationError, NotUsed] =
      eventSource.map {
        case LarSyntacticalError(err) => err
        case TsSyntacticalError(err) => err
        case LarValidityError(err) => err
        case TsValidityError(err) => err
        case LarQualityError(err) => err
        case TsQualityError(err) => err
        case LarMacroError(err) => err
        case _ => EmptyValidationError
      }

    edits.filter(_ != EmptyValidationError)
{'fraction_non_alphanumeric': 0.10688540889883172, 'fraction_numerical': 0.0, 'mean_word_length': 4.415881561238224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2142047', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1267, 'n_words': 258}