<commit_before>class PourMonitor
  TICKS      = TapMonitor::TICKS
  FIRST_TICK = TapMonitor::FIRST_TICK
  LAST_TICK  = TapMonitor::LAST_TICK

  def self.start(tap, drop)
    ActiveRecord::Base.connection_pool.disconnect!
    fork { new(tap, drop).monitor }
  end

  def initialize(tap, drop)
    @tap     = tap
    @drop    = drop
    @running = true
  end

  def monitor
    trap(:INT)  { @running = false }
    trap(:TERM) { @running = false }
    trap(:QUIT) { @running = false }

    while @running
      sleep(1) while @running && @drop[TICKS] == 0

      # Stop if we have been told to. Unless a pour
      # has begun, then finish the pour first
      break if !@running && @drop[TICKS] == 0

      pour = @tap.active_keg(true).active_pour(true) || @tap.active_keg.pours.new
      pour.sensor_ticks = @drop[TICKS]
      pour.volume       = pour.sensor_ticks * @tap.floz_per_tick
      pour.started_at   = Time.at(@drop[FIRST_TICK])
      pour.save<commit_msg>Just reset if the tap does not have an assigned keg<commit_after>class PourMonitor
  TICKS      = TapMonitor::TICKS
  FIRST_TICK = TapMonitor::FIRST_TICK
  LAST_TICK  = TapMonitor::LAST_TICK

  def self.start(tap, drop)
    ActiveRecord::Base.connection_pool.disconnect!
    fork { new(tap, drop).monitor }
  end

  def initialize(tap, drop)
    @tap     = tap
    @drop    = drop
    @running = true
  end

  def monitor
    trap(:INT)  { @running = false }
    trap(:TERM) { @running = false }
    trap(:QUIT) { @running = false }

    while @running
      sleep(1) while @running && @drop[TICKS] == 0

      # Stop if we have been told to. Unless a pour
      # has begun, then finish the pour first
      break if !@running && @drop[TICKS] == 0

      keg = @tap.active_keg(true)
      if keg.nil?
        @drop[TICKS] = 0
        next
      end

      pour = keg.active_pour(true) || keg.pours.new
      pour.sensor_ticks = @drop[TICKS]
      pour.volume       = pour.sensor_ticks * @tap.floz_per_tick
      pour.started_at   = Time.at(@drop[FIRST_TICK])
      pour.save
{'fraction_non_alphanumeric': 0.11513963743263106, 'fraction_numerical': 0.0034296913277804997, 'mean_word_length': 2.5950704225352115, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2181822', 'n_tokens_mistral': 755, 'n_tokens_neox': 719, 'n_words': 171}