<commit_before>package jsky.app.ot.ags

import java.beans.{PropertyChangeEvent, PropertyChangeListener}
import java.util.concurrent.{LinkedBlockingQueue, Executors}
import java.util.logging.Logger

import edu.gemini.ags.api.{AgsRegistrar, AgsStrategy}
import edu.gemini.pot.sp._
import edu.gemini.spModel.obs.context.ObsContext
import edu.gemini.spModel.rich.shared.immutable._
import edu.gemini.spModel.target.env.TargetEnvironment
import edu.gemini.spModel.target.obsComp.TargetObsComp
import jsky.app.ot.OT
import jsky.app.ot.tpe._
import scala.collection.JavaConverters._
import scala.collection.immutable.HashMap
import scala.swing.Swing
import scala.util.{Try, Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global

import scalaz._, Scalaz._


object BagsManager {
  private val LOG: Logger = Logger.getLogger(getClass.getName)

  private case class BagsTask(observation: ISPObservation, curCtx: Option[ObsContext], nextCtx: Option[ObsContext]) {
    def obsKey: SPNodeKey =
      observation.getNodeKey

    // The state to move to when processing the task.
    def nextTask: Option[BagsTask] =
      nextCtx.map(_ => BagsTask(observation, nextCtx, None))

    def isDone: Boolean =
      nextCtx.isEmpty

    def newContext(ctxOpt: Option[ObsContext]): BagsTask =
      BagsTask(observation, curCtx, ctxOpt)

    def performLookup(): Unit = {
      LOG.info(s"Performing BAGS lookup for observation=${observation.getObservationID}")

      def resultString[A](r: Option[A]): String =
        s"Results=${r ? "Yes" | "No"}."

      // We must handle GeMS separately from other strategies, and delegate the work to the GemsGuideStarWorker.
      def gemsLookup(obsCtx: ObsContext): Unit = {
        LOG.info(s"BAGS GeMS lookup for observation=${observation.getObservationID} starting...")
        Try { GemsGuideStarWorker.findGuideStars(obsCtx) } match {
          case Success(ggs) =>
            LOG.info(s"BAGS GeMS lookup for observation=${observation.getObservationID} successful. ${resultString(ggs.asScalaOpt)}")
            Swing.onEDT {
              muteObservation(observation)
              GemsGuideStarWorker.applyResults(TpeContext(observation), ggs, true)
              unmuteObservation(observation)
            }
            taskComplete(this, success = true)
          case Failure(ex) =>
            LOG.warning(s"BAGS GeMS lookup for observation=${observation.getObservationID} failed. Exception=$ex")
            taskComplete(this, success = false)
        }
      }

      // The mechanism to perform lookups for all other guide probes.
      def otherLookup(obsCtx: ObsContext, strategy: AgsStrategy): Unit = {
        val fut = strategy.select(obsCtx, OT.getMagnitudeTable)
        fut.onComplete {
          case Success(selOpt) =>
            LOG.info(s"BAGS lookup for observation=${observation.getObservationID} successful. ${resultString(selOpt)}")
            Swing.onEDT {
              muteObservation(observation)
              GuideStarWorker.applyResults(TpeContext(observation), selOpt)
              unmuteObservation(observation)
            }
            taskComplete(this, success = true)
          case Failure(ex) =>
            LOG.warning(s"BAGS lookup for observation=${observation.getObservationID} failed. Exception=$ex")
            taskComplete(this, success = false)
        }
      }


      curCtx.foreach { obsCtx =>
        AgsRegistrar.currentStrategy(obsCtx).foreach { strategy =>
          if (GuideStarSupport.hasGemsComponent(observation)) {
            gemsLookup(obsCtx)
          } else {
            otherLookup(obsCtx, strategy)
          }
        }
      }
    }
  }

  private class BagsThread(id: Int) extends Runnable {
    override def run() {
      // Get the next task to run. This blocks on the queue.
      val task = nextTask()

      // Execute the task.
      task.foreach(_.performLookup())

      // Resubmit self for execution.
      executor.submit(this)
    }
  }

  private val NumThreads = math.min(1, Runtime.getRuntime.availableProcessors())
  private val RequeueDelay = 3000
  private val executor = Executors.newFixedThreadPool(NumThreads)<commit_msg>OCSADV-435: Fixing PR changes overwritten by this PR.<commit_after>package jsky.app.ot.ags

import java.beans.{PropertyChangeEvent, PropertyChangeListener}
import java.util.concurrent.{LinkedBlockingQueue, Executors}
import java.util.logging.Logger

import edu.gemini.ags.api.{AgsRegistrar, AgsStrategy}
import edu.gemini.pot.sp._
import edu.gemini.spModel.obs.context.ObsContext
import edu.gemini.spModel.rich.shared.immutable._
import edu.gemini.spModel.target.env.TargetEnvironment
import jsky.app.ot.OT
import jsky.app.ot.tpe._
import scala.collection.JavaConverters._
import scala.collection.immutable.HashMap
import scala.swing.Swing
import scala.util.{Try, Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global

import scalaz._, Scalaz._


object BagsManager {
  private val LOG: Logger = Logger.getLogger(getClass.getName)

  private case class BagsTask(observation: ISPObservation, curCtx: Option[ObsContext], nextCtx: Option[ObsContext]) {
    def obsKey: SPNodeKey =
      observation.getNodeKey

    // The state to move to when processing the task.
    def nextTask: Option[BagsTask] =
      nextCtx.map(_ => BagsTask(observation, nextCtx, None))

    def isDone: Boolean =
      nextCtx.isEmpty

    def newContext(ctxOpt: Option[ObsContext]): BagsTask =
      BagsTask(observation, curCtx, ctxOpt)

    def performLookup(): Unit = {
      LOG.info(s"Performing BAGS lookup for observation=${observation.getObservationID}")

      def resultString[A](r: Option[A]): String =
        s"Results=${r ? "Yes" | "No"}."

      // We must handle GeMS separately from other strategies, and delegate the work to the GemsGuideStarWorker.
      def gemsLookup(obsCtx: ObsContext): Unit = {
        LOG.info(s"BAGS GeMS lookup for observation=${observation.getObservationID} starting...")
        Try { GemsGuideStarWorker.findGuideStars(obsCtx) } match {
          case Success(ggs) =>
            LOG.info(s"BAGS GeMS lookup for observation=${observation.getObservationID} successful. ${resultString(ggs.asScalaOpt)}")
            Swing.onEDT {
              muteObservation(observation)
              GemsGuideStarWorker.applyResults(TpeContext(observation), ggs, true)
              unmuteObservation(observation)
            }
            taskComplete(this, success = true)
          case Failure(ex) =>
            LOG.warning(s"BAGS GeMS lookup for observation=${observation.getObservationID} failed. Exception=$ex")
            taskComplete(this, success = false)
        }
      }

      // The mechanism to perform lookups for all other guide probes.
      def otherLookup(obsCtx: ObsContext, strategy: AgsStrategy): Unit = {
        val fut = strategy.select(obsCtx, OT.getMagnitudeTable)
        fut.onComplete {
          case Success(selOpt) =>
            LOG.info(s"BAGS lookup for observation=${observation.getObservationID} successful. ${resultString(selOpt)}")
            Swing.onEDT {
              muteObservation(observation)
              GuideStarWorker.applyResults(TpeContext(observation), selOpt)
              unmuteObservation(observation)
            }
            taskComplete(this, success = true)
          case Failure(ex) =>
            LOG.warning(s"BAGS lookup for observation=${observation.getObservationID} failed. Exception=$ex")
            taskComplete(this, success = false)
        }
      }


      curCtx.foreach { obsCtx =>
        AgsRegistrar.currentStrategy(obsCtx).foreach { strategy =>
          if (GuideStarSupport.hasGemsComponent(observation)) {
            gemsLookup(obsCtx)
          } else {
            otherLookup(obsCtx, strategy)
          }
        }
      }
    }
  }

  private class BagsThread(id: Int) extends Thread {
    setPriority(Thread.NORM_PRIORITY - 1)

    override def run() {
      // Get the next task to run. This blocks on the queue.
      val task = nextTask()

      // Execute the task.
      task.foreach(_.performLookup())

      // Resubmit self for execution.
      executor.submit(this)
    }
  }

  private val NumThreads = math.max(1, Runtime.getRuntime.availableProcessors()-1)
  private val RequeueDelay = 3000
  private val executor = Executors.newFixedThreadPool(NumThreads)
{'fraction_non_alphanumeric': 0.09909156108056419, 'fraction_numerical': 0.0017929715515180493, 'mean_word_length': 2.995702005730659, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '6961244', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2456, 'n_words': 536}