<commit_before>require 'uri'

class Service::VictorOps < Service
  REQUIRED_SETTING_KEYS = [:api_key]

  def receive_validate(errors = {})
    REQUIRED_SETTING_KEYS.inject(true) do |previous_passed, key|
      key_found = !settings[key].to_s.empty?
      errors[:name] = "Not found" unless key_found
      previous_passed && key_found
    end
  end

  def receive_alert
    body = settings.merge(flatten_hash(payload))
    body.delete :api_key

    # Keys that will soon be in the payload
    body[:entity_id] = payload[:incident_key] || payload['alert']['id']
    body[:clear] = payload[:clear] if payload[:clear]

    # Fire
    uri = uri_for_key(settings[:api_key])
    http_post(uri, body, headers)
  end

  def settings
    @settings.merge({
      monitoring_tool: 'librato'
    })
  end

  private

  def flatten_hash(payload)
    {
      state_message: payload['alert']['name'] || 'No Alert Name Provided',
      metric_name: payload['metric']['name'],
      metric_type: payload['metric']['type'],
      measurment_name: payload['measurement']['value'],
      measurment_source: payload['measurement']['source']
    }
  end

  def alert_for_type(type)
    {
      message_type: type,
      entity_id: null,
    }
  end

  # Helpers
  def uri_for_key(key)
    File.join("#{http_scheme}#{host}", integrations_path_for_key(key), (settings[:routing_key] || 'nil')).to_s<commit_msg>Some adjustments<commit_after>require 'uri'

class Service::VictorOps < Service
  REQUIRED_SETTING_KEYS = [:api_key]

  def receive_validate(errors = {})
    REQUIRED_SETTING_KEYS.inject(true) do |previous_passed, key|
      key_found = !settings[key].to_s.empty?
      errors[:name] = "Not found" unless key_found
      previous_passed && key_found
    end
  end

  def receive_alert_clear
    receive_alert
  end

  def receive_alert
    raise_config_error unless receive_validate({})

    # New-style alerts
    if payload[:alert][:version] == 1
      stdout_logger "Only version 2 and greater alerts supported"
      return true
    end

    body = settings.merge(flatten_hash(payload))
    body.delete :api_key

    # Keys that will soon be in the payload
    body[:entity_id] = payload[:incident_key] || payload['alert']['id']
    if payload[:clear]
      body[:clear] = payload[:clear]
      body[:message_type] = "CRITICAL"
    else
      body[:message_type] = "RECOVERY"
    end

    # Fire
    uri = uri_for_key(settings[:api_key])
    http_post(uri, body, headers)
  end

  def settings
    @settings.merge({
      monitoring_tool: 'librato'
    })
  end

  private

  def flatten_hash(payload)
    {
      state_message: payload['alert']['name'] || 'No Alert Name Provided',
      metric_name: payload['metric']['name'],
      metric_type: payload['metric']['type'],
      measurment_name: payload['measurement']['value'],
      measurment_source: payload['measurement']['source']
    }
  end

  # Helpers
  def uri_for_key(key)
    File.join("#{http_scheme}#{host}", integrations_path_for_key(key), (settings[:routing_key] || 'nil')).to_s
{'fraction_non_alphanumeric': 0.1297760210803689, 'fraction_numerical': 0.0006587615283267457, 'mean_word_length': 3.679506933744222, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3268136', 'n_tokens_mistral': 1009, 'n_tokens_neox': 980, 'n_words': 202}