<commit_before>#!/usr/bin/env ruby

require 'flapjack/filters/base'

module Flapjack
  module Filters

    # * If the service event’s state is ok and the previous state was ok, don’t alert
    # * If the service event's state is ok and the previous notification was a recovery, don't alert
    # * If the service event's state is ok and the previous state was not ok and for less than 30
    # seconds, don't alert
    # * If the service event's state is ok and there is unscheduled downtime set, end the unscheduled
    #   downtime
    class Ok
      include Base

      def block?(event)
        result = false

        if event.ok?
          if event.previous_state == 'ok'
            @logger.debug("Filter: Ok: block - previous state was ok, so blocking")
            return true
          end

          entity_check = Flapjack::Data::EntityCheck.for_event_id(event.id, :redis => @redis)

          last_notification = entity_check.last_notification
          @logger.debug("Filter: Ok: last notification: #{last_notification.inspect}")

          unless last_notification[:type]
            @logger.debug("Filter: Ok: block - last notification type is nil (never notified)")
            return true
          end

          if [:recovery, :ok].include?(last_notification[:type])
            @logger.debug("Filter: Ok: block - last notification was a recovery")
            return true
          end

          @logger.debug("Filter: Ok: previous_state: #{event.previous_state}, " +
                        "previous_state_duration: #{event.previous_state_duration}")

          # FIXME: change to if last notification was ok ?
          #unless [:warning, :critical, :unknown, :problem, :acknowledgement].include?(last_notification[:type])
          #  @logger.debug("Filter: Ok: block - last notification was not for a problem or acknowledgement")
          #  return true
          #end

          #if event.previous_state != 'ok'
          #  if event.previous_state_duration < 30
          #    @logger.debug("Filter: Ok: previous non ok state was for less than 30 seconds, so blocking")
          #    result = true
          #  end
          #end

          # end any unscheduled downtime
          entity_check.end_unscheduled_maintenance(Time.now.to_i)
        end

        @logger.debug("Filter: Ok: #{result ? "block" : "pass"}")
        result
      end
    end
  end
end<commit_msg>clean up code in ok filter<commit_after>#!/usr/bin/env ruby

require 'flapjack/filters/base'

module Flapjack
  module Filters

    # * If the service event's state is ok and there is unscheduled maintenance set, end the unscheduled
    #   maintenance
    # * If the service event’s state is ok and the previous state was ok, don’t alert
    # * If the service event’s state is ok and there's never been a notification, don't alert
    # * If the service event's state is ok and the previous notification was a recovery or ok, don't alert
    class Ok
      include Base

      def block?(event)

        if event.ok?
          entity_check = Flapjack::Data::EntityCheck.for_event_id(event.id, :redis => @redis)

          entity_check.end_unscheduled_maintenance(Time.now.to_i)

          if event.previous_state == 'ok'
            @logger.debug("Filter: Ok: block - previous state was ok, so blocking")
            return true
          end

          last_notification = entity_check.last_notification
          @logger.debug("Filter: Ok: last notification: #{last_notification.inspect}")

          unless last_notification[:type]
            @logger.debug("Filter: Ok: block - last notification type is nil (never notified)")
            return true
          end

          if [:recovery, :ok].include?(last_notification[:type])
            @logger.debug("Filter: Ok: block - last notification was a recovery")
            return true
          end

          @logger.debug("Filter: Ok: previous_state: #{event.previous_state}, " +
                        "previous_state_duration: #{event.previous_state_duration}")

        end

        @logger.debug("Filter: Ok: pass")
        return false
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09005083514887437, 'fraction_numerical': 0.0014524328249818446, 'mean_word_length': 2.3457489878542512, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '2236167', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1101, 'n_words': 375}