<commit_before>require 'trinity'

class IncidentService

  attr_reader :incident
  def initialize(incident)
    @incident = incident
  end

  # Create an open incident for given assertion and check result
  # and setup the flow of incident open
  #
  # @param Assertion
  # @param CheckResponse check_response
  # @return Incident|Bool
  #         incident which was created
  #         false not create an incident, an ongoing aleady has
  def self.create_for_assertion(assertion, check_response)
    incident = assertion.ongoing_incident

    if incident
      # TODO Probably do something for stil down/still happen incident notification
      if !incident.locations['open'].any? { |where| where[:ip] == check_response.from_ip }
        incident.locations['open'] << {ip: check_response.from_ip, message: check_response.error_message || check_response.body}
        incident.locations['open'] = incident.locations['open'].uniq { |l| l[:ip] }
        incident.save
      end
    else
      Trinity::Semaphore.run_once [assertion.check.id.to_s, assertion.id.to_s] do
        incident = open_incident(assertion, check_response)
      end
    end

    return unless incident

    if (incident.locations['open'].try(:length) || 0) >= Rails.configuration.incident_confirm_location
      incident.status = Incident::STATUS_OPEN
      incident.save
    end

    if incident.open?
      Trinity::Semaphore.run_once ['open', 'alert', assertion.check.id.to_s], 30.minutes.to_i do
        notify(incident, Incident::STATUS_OPEN)
      end
    end

    incident
  end

  # Close an incident for given assertion and check response
  #
  # When all location is closed, we will send incident close notification
  #
  # @param Assertion assertion
  # @param CheckResponse check_response
  def self.close_for_assertion(assertion, check_response)
    if partial_incidents = assertion.partial_incidents
      # Since this is partial incident, just delete them
      partial_incidents.each(&:destroy)
      return
    end

    # Check doesn't match, and we have an on-going incident, this mean we can close it
    if incident = assertion.ongoing_incident
      if !incident.locations['close'].any? { |where| where[:ip] == check_response.from_ip }
        incident.locations['close'] << {ip: check_response.from_ip, message: check_response.error_message}
      end
    else
      return
    end

    open_locations = incident.locations['open'].map { |l| l[:ip].strip }
    close_locations = incident.locations['close'].map { |l| l[:ip].strip }
    if (open_locations - close_locations).empty?
      close_incident incident
    end

    Trinity::Semaphore.run_once ['close', 'alert', assertion.check.id.to_s], 30.minutes.to_i do
      notify incident, Incident::STATUS_CLOSE
    end

    incident
  end<commit_msg>Fix ...<commit_after>require 'trinity'

class IncidentService

  attr_reader :incident
  def initialize(incident)
    @incident = incident
  end

  # Create an open incident for given assertion and check result
  # and setup the flow of incident open
  #
  # @param Assertion
  # @param CheckResponse check_response
  # @return Incident|Bool
  #         incident which was created
  #         false not create an incident, an ongoing aleady has
  def self.create_for_assertion(assertion, check_response)
    incident = assertion.ongoing_incident

    if incident
      # TODO Probably do something for stil down/still happen incident notification
      if !incident.locations['open'].any? { |where| where[:ip] == check_response.from_ip }
        incident.locations['open'] << {ip: check_response.from_ip, message: check_response.error_message || check_response.body}
        incident.locations['open'] = incident.locations['open'].uniq { |l| l[:ip] }
        incident.save
      end
    else
      Trinity::Semaphore.run_once [assertion.check.id.to_s, assertion.id.to_s] do
        incident = open_incident(assertion, check_response)
      end
    end

    return unless incident

    if (incident.locations['open'].try(:length) || 0) >= Rails.configuration.incident_confirm_location
      incident.status = Incident::STATUS_OPEN
      incident.save
    end

    if incident.open?
      Trinity::Semaphore.run_once ['open', 'alert', assertion.check.id.to_s], 30.minutes.to_i do
        notify(incident, Incident::STATUS_OPEN)
      end
    end

    incident
  end

  # Close an incident for given assertion and check response
  #
  # When all location is closed, we will send incident close notification
  #
  # @param Assertion assertion
  # @param CheckResponse check_response
  def self.close_for_assertion(assertion, check_response)
    if (partial_incidents = assertion.partial_incidents.length) > 0
      # Since this is partial incident, just delete them
      partial_incidents.each(&:destroy)
      return
    end

    # Check doesn't match, and we have an on-going incident, this mean we can close it
    if incident = assertion.ongoing_incident
      if !incident.locations['close'].any? { |where| where[:ip] == check_response.from_ip }
        incident.locations['close'] << {ip: check_response.from_ip, message: check_response.error_message}
      end
    else
      return
    end

    open_locations = incident.locations['open'].map { |l| l[:ip].strip }
    close_locations = incident.locations['close'].map { |l| l[:ip].strip }
    if (open_locations - close_locations).empty?
      close_incident incident
    end

    Trinity::Semaphore.run_once ['close', 'alert', assertion.check.id.to_s], 30.minutes.to_i do
      notify incident, Incident::STATUS_CLOSE
    end

    incident
  end
{'fraction_non_alphanumeric': 0.09526362823949955, 'fraction_numerical': 0.001966041108132261, 'mean_word_length': 3.371875, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 6, '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': '2340832', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1627, 'n_words': 437}