<commit_before>require 'ntlm/http'

class Houston::Itsm::IssuesController < ApplicationController
  
  def index
    users_by_email = benchmark("\e[33mEager-load users\e[0m") do
      User.all.each_with_object({}) { |user, map| user.email_addresses.each { |email| map[email.downcase] = user } }
    end
    
    ActiveRecord::Base.clear_active_connections! unless env.key?('rack.test')
    
    benchmark("\e[33mFetch ITSMs\e[0m") do
      http = Net::HTTP.start("ecphhelper", 80)
      req = Net::HTTP::Get.new("/ITSM.asmx/GetOpenCallsEmergingProducts")
      req.ntlm_auth("Houston", "cph.pri", "gKfub6mFy9BHDs6")
      response = http.request(req)
      issues = Hash.from_xml(response.body).fetch("ArrayOfOpenCallData").fetch("OpenCallData", [])
      @issues = Array.wrap(issues).map do |issue|
        url = Nokogiri::HTML::fragment(issue["CallDetailLink"]).children.first[:href]
        user = users_by_email[issue["AssignedToEmailAddress"].try(:downcase)]
        Issue.new(issue["Summary"], url, issue["AssignedToEmailAddress"], user)
      end
    end
    
    render partial: "houston/itsm/issues/fires" if request.xhr?
  end
  
  
  Issue = Struct.new(:summary, :url, :assigned_to_email, :assigned_to_user)
  
end
<commit_msg>[fix] Swallowed exceptions that result from malformed responses from Back Office (10m)<commit_after>require 'ntlm/http'

class Houston::Itsm::IssuesController < ApplicationController
  
  def index
    users_by_email = benchmark("\e[33mEager-load users\e[0m") do
      User.all.each_with_object({}) { |user, map| user.email_addresses.each { |email| map[email.downcase] = user } }
    end
    
    ActiveRecord::Base.clear_active_connections! unless env.key?('rack.test')
    
    benchmark("\e[33mFetch ITSMs\e[0m") do
      http = Net::HTTP.start("ecphhelper", 80)
      req = Net::HTTP::Get.new("/ITSM.asmx/GetOpenCallsEmergingProducts")
      req.ntlm_auth("Houston", "cph.pri", "<PASSWORD>")
      response = http.request(req)
      @issues = parse_issues(response.body).map do |issue|
        url = Nokogiri::HTML::fragment(issue["CallDetailLink"]).children.first[:href]
        user = users_by_email[issue["AssignedToEmailAddress"].try(:downcase)]
        Issue.new(issue["Summary"], url, issue["AssignedToEmailAddress"], user)
      end
    end
    
    render partial: "houston/itsm/issues/fires" if request.xhr?
  end
  
  def parse_issues(xml)
    Array.wrap(
      Hash.from_xml(response.body)
        .fetch("ArrayOfOpenCallData", {})
        .fetch("OpenCallData", []))
  rescue REXML::ParseException # malformed response upstream
    Rails.logger.error "\e[31;1m#{$!.class}\e[0;31m: #{$!.message}"
    []
  end
  
  
  Issue = Struct.new(:summary, :url, :assigned_to_email, :assigned_to_user)
  
end

{'fraction_non_alphanumeric': 0.14934593023255813, 'fraction_numerical': 0.009811046511627907, 'mean_word_length': 3.8898756660746003, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, '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': '128360', 'n_tokens_mistral': 968, 'n_tokens_neox': 931, 'n_words': 150}