<commit_before>require 'bundler/setup'
require 'json'
require 'resolv'
require 'rest_client'
require 'sinatra'
require 'socket'

require 'yaml'
CONFIG   = YAML.load_file("config.yml")

helpers do
  def get_external_response(url)
    response = RestClient.get(external_service.token_url)

    {}.tap do |external_response|
      external_response[:status] = response.code

      parsed_body = begin
                      JSON.parse(response.body)
                    rescue JSON::ParserError => error
                      { error: response.body }
                    end
      external_response.merge(parsed_body)
    end
  end

  def mark_http_check_critical(url)
    region = url.gsub(/http:\/\/external-/, "").gsub(".xmag.us", "")
    `ruby scripts/update_service_check.rb #{region} critical`
  end

  def resolved_hostname_cname(nameserver, hostname)
    dns = Resolv::DNS.new(nameserver: nameserver)
    dns.getresources(hostname, Resolv::DNS::Resource::IN::CNAME).first.name.to_s
  end
end

get '/' do
  external_hostname = resolved_hostname_cname(CONFIG[:nameserver],
                                              CONFIG[:external_hostname])

  external_response = get_external_response(url)
  remove_url_from_consul if external_response.fetch(:status, 200).to_i != 200
  {
    local_hostname: Socket.gethostname,
    resolved_hostname: external_hostname,
    timestamp: Time.now.strftime('%Y%m%d%H%M%S (%z)'),
    external_response: external_response
  }.to_json
end<commit_msg>URL for service pointed to localhost instead of external<commit_after>require 'bundler/setup'
require 'json'
require 'resolv'
require 'rest_client'
require 'sinatra'
require 'socket'

require 'yaml'
CONFIG   = YAML.load_file("config.yml")

helpers do
  def get_external_response(url)
    response = RestClient.get(url)

    {}.tap do |external_response|
      external_response[:status] = response.code

      parsed_body = begin
                      JSON.parse(response.body)
                    rescue JSON::ParserError => error
                      { error: response.body }
                    end
      external_response.merge!(parsed_body)
    end
  end

  def mark_http_check_critical(url)
    region = url.gsub(/http:\/\/external-/, "").gsub(".xmag.us", "")
    `ruby scripts/update_service_check.rb #{region} critical`
  end

  def resolved_hostname_cname(nameserver, hostname)
    dns = Resolv::DNS.new(nameserver: nameserver)
    dns.getresources(hostname, Resolv::DNS::Resource::IN::CNAME).first.name.to_s
  end
end

get '/' do
  external_hostname = resolved_hostname_cname(CONFIG[:nameserver],
                                              CONFIG[:external_hostname])

  external_response = get_external_response(external_hostname)
  remove_url_from_consul if external_response.fetch(:status, 200).to_i != 200
  {
    local_hostname: Socket.gethostname,
    resolved_hostname: external_hostname,
    timestamp: Time.now.strftime('%Y%m%d%H%M%S (%z)'),
    external_response: external_response
  }.to_json
end
{'fraction_non_alphanumeric': 0.11107416029265048, 'fraction_numerical': 0.0039906883937479215, 'mean_word_length': 3.1777777777777776, 'pattern_counts': {'":': 0, '<': 3, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2515232', 'n_tokens_mistral': 952, 'n_tokens_neox': 933, 'n_words': 171}