<commit_before>package almhirt.components

import scala.concurrent.duration._
import org.joda.time.LocalDateTime
import akka.actor._
import almhirt.core.Almhirt
import almhirt.commanding._
import almhirt.common._

object ExecutionStateTracker {

  sealed trait ExecutionStateTrackerMessage
  final case class GetExecutionStateFor(trackId: String) extends ExecutionStateTrackerMessage
  final case class QueriedExecutionState(trackId: String, executionState: Option[ExecutionState]) extends ExecutionStateTrackerMessage
  final case class SubscribeForFinishedState(trackId: String) extends ExecutionStateTrackerMessage
  final case class UnsubscribeForFinishedState(trackId: String) extends ExecutionStateTrackerMessage

  sealed trait ExecutionFinishedResultMessage extends ExecutionStateTrackerMessage
  final case class FinishedExecutionStateResult(result: ExecutionFinishedState) extends ExecutionFinishedResultMessage
  final case class ExecutionTrackingExpired(trackId: String) extends ExecutionFinishedResultMessage

  import scalaz.syntax.validation._
  import akka.pattern.ask
  import akka.util.Timeout
  import almhirt.almfuture.all._

  trait SecondLevelStore {
    def get(trackId: String)(atMost: scala.concurrent.duration.FiniteDuration): AlmFuture[Option[ExecutionStateEntry]]
  }

  import scala.concurrent.ExecutionContext

  def secondLevelStore(actor: ActorRef)(implicit executionContext: ExecutionContext): SecondLevelStore = {
    new SecondLevelStore {
      def get(trackId: String)(atMost: scala.concurrent.duration.FiniteDuration): AlmFuture[Option[ExecutionStateEntry]] =
        (actor ? ExecutionStateStore.GetEntry(trackId))(atMost).successfulAlmFuture[ExecutionStateStore.GetEntryResponse].mapV(res =>
          res match {
            case ExecutionStateStore.GetEntryResult(entry) => entry.success
            case ExecutionStateStore.GetEntryFailure(problem) => problem.failure
          })
    }
  }

  import com.typesafe.config.Config
  import almhirt.configuration._
  def props(configSection: Config, theSecondLevelStore: SecondLevelStore, secondLevelStoreMaxAskDur: FiniteDuration, theAlmhirt: Almhirt): AlmValidation[Props] =
    for {
      theTargetSize <- configSection.v[Int]("target-size")
      theCeanUpThreshold <- configSection.v[Int]("clean-up-threshold")
      theCleanUpInterval <- configSection.v[FiniteDuration]("clean-up-interval")
      checkSubscriptions <- configSection.v[Boolean]("check-subscriptions")
      checkDurations <- if (checkSubscriptions) {
        configSection.v[FiniteDuration]("check-subscriptions-interval").flatMap(interval =>
          configSection.v[FiniteDuration]("check-subscriptions-warn-age-lvl1").flatMap(warnAge1 =>
            configSection.v[FiniteDuration]("check-subscriptions-warn-age-lvl2").map(warnAge2 => Some((interval, warnAge1, warnAge2)))))
      } else {
        None.success
      }
      _ <- checkDurations match {
        case Some((_, a, b)) if (a >= b) =>
          ConstraintViolatedProblem(s""""ExecutionStateTracker: check-subscriptions-warn-age-lvl1" must be less than "check-subscriptions-warn-age-lvl2"(${a.defaultUnitString} >= {b.defaultUnitString}).""").failure
        case _ => ().success
      }
    } yield {
      theAlmhirt.log.info(s"""ExecutionStateTracker: "target-size" is $theTargetSize""")
      theAlmhirt.log.info(s"""ExecutionStateTracker: "clean-up-threshold" is $theCeanUpThreshold""")
      theAlmhirt.log.info(s"""ExecutionStateTracker: "clean-up-interval" is ${theCleanUpInterval.defaultUnitString}""")
      checkDurations match {<commit_msg>missed a $<commit_after>package almhirt.components

import scala.concurrent.duration._
import org.joda.time.LocalDateTime
import akka.actor._
import almhirt.core.Almhirt
import almhirt.commanding._
import almhirt.common._

object ExecutionStateTracker {

  sealed trait ExecutionStateTrackerMessage
  final case class GetExecutionStateFor(trackId: String) extends ExecutionStateTrackerMessage
  final case class QueriedExecutionState(trackId: String, executionState: Option[ExecutionState]) extends ExecutionStateTrackerMessage
  final case class SubscribeForFinishedState(trackId: String) extends ExecutionStateTrackerMessage
  final case class UnsubscribeForFinishedState(trackId: String) extends ExecutionStateTrackerMessage

  sealed trait ExecutionFinishedResultMessage extends ExecutionStateTrackerMessage
  final case class FinishedExecutionStateResult(result: ExecutionFinishedState) extends ExecutionFinishedResultMessage
  final case class ExecutionTrackingExpired(trackId: String) extends ExecutionFinishedResultMessage

  import scalaz.syntax.validation._
  import akka.pattern.ask
  import akka.util.Timeout
  import almhirt.almfuture.all._

  trait SecondLevelStore {
    def get(trackId: String)(atMost: scala.concurrent.duration.FiniteDuration): AlmFuture[Option[ExecutionStateEntry]]
  }

  import scala.concurrent.ExecutionContext

  def secondLevelStore(actor: ActorRef)(implicit executionContext: ExecutionContext): SecondLevelStore = {
    new SecondLevelStore {
      def get(trackId: String)(atMost: scala.concurrent.duration.FiniteDuration): AlmFuture[Option[ExecutionStateEntry]] =
        (actor ? ExecutionStateStore.GetEntry(trackId))(atMost).successfulAlmFuture[ExecutionStateStore.GetEntryResponse].mapV(res =>
          res match {
            case ExecutionStateStore.GetEntryResult(entry) => entry.success
            case ExecutionStateStore.GetEntryFailure(problem) => problem.failure
          })
    }
  }

  import com.typesafe.config.Config
  import almhirt.configuration._
  def props(configSection: Config, theSecondLevelStore: SecondLevelStore, secondLevelStoreMaxAskDur: FiniteDuration, theAlmhirt: Almhirt): AlmValidation[Props] =
    for {
      theTargetSize <- configSection.v[Int]("target-size")
      theCeanUpThreshold <- configSection.v[Int]("clean-up-threshold")
      theCleanUpInterval <- configSection.v[FiniteDuration]("clean-up-interval")
      checkSubscriptions <- configSection.v[Boolean]("check-subscriptions")
      checkDurations <- if (checkSubscriptions) {
        configSection.v[FiniteDuration]("check-subscriptions-interval").flatMap(interval =>
          configSection.v[FiniteDuration]("check-subscriptions-warn-age-lvl1").flatMap(warnAge1 =>
            configSection.v[FiniteDuration]("check-subscriptions-warn-age-lvl2").map(warnAge2 => Some((interval, warnAge1, warnAge2)))))
      } else {
        None.success
      }
      _ <- checkDurations match {
        case Some((_, a, b)) if (a >= b) =>
          ConstraintViolatedProblem(s""""ExecutionStateTracker: check-subscriptions-warn-age-lvl1" must be less than "check-subscriptions-warn-age-lvl2"(${a.defaultUnitString} >= ${b.defaultUnitString}).""").failure
        case _ => ().success
      }
    } yield {
      theAlmhirt.log.info(s"""ExecutionStateTracker: "target-size" is $theTargetSize""")
      theAlmhirt.log.info(s"""ExecutionStateTracker: "clean-up-threshold" is $theCeanUpThreshold""")
      theAlmhirt.log.info(s"""ExecutionStateTracker: "clean-up-interval" is ${theCleanUpInterval.defaultUnitString}""")
      checkDurations match {
{'fraction_non_alphanumeric': 0.10005589714924538, 'fraction_numerical': 0.0022358859698155395, 'mean_word_length': 5.060118543607112, 'pattern_counts': {'":': 0, '<': 15, '<?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': '7323045', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1990, 'n_words': 354}