<commit_before>class V1::LegislatorsController < V1::BaseController

  def index
    render json: Legislator.with_includes
      .order(:first_name, :last_name)
      .to_json( methods: [:name, :title, :state_name, :eligible],
                only: [:id, :with_us])
  end

  def show
    legislator = Legislator.with_includes.find_by_bioguide_id(params[:id])
    render json: legislator
      .to_json( methods: [:name, :title, :state_name, :eligible, :image_url, :state_abbrev], only: [:with_us, :party])
  end

  def targeted
    render json: Legislator.with_includes.targeted
  end

  def newest_supporters
    render json: Legislator.with_includes.joins(:sponsorships).order('sponsorships.cosponsored_at desc').first(5)
  end

  def supporters_map
    json = prep_supports_map_json  #add caching later
    render js: "onLegislatorResponse(#{json})"
  end

  private

  def prep_supports_map_json
    coordinates_output = []
    Legislator.with_includes.includes(:bills,:active_campaigns).all.each_with_index do |l,i|
      next unless Legislator::MAP_COORDINATES.include?(l.map_key)
      coordinates_output << {
        map_key: l.map_key,
        coordinates: Legislator::MAP_COORDINATES[l.map_key],
        legislator: {
          name: l.name,
          title: l.title,
          party: l.party[0],
          support_max: l.support_max,
          targeted: l.targeted?,
          image_url: l.image_url,
          bioguide_id: l.bioguide_id,
          sponsorships: l.sponsorship_hash,
          chamber: l.chamber,
          description: l.support_description
        }
      }
    end
    Oj.dump({tile_coordinates: coordinates_output, label_coordinates: Legislator::MAP_LABELS},  mode: :compat)
  end

end
<commit_msg>Cache legislators response<commit_after>class V1::LegislatorsController < V1::BaseController

  SUPPORTER_MAP_REDIS_KEY = 'supports_map_json'

  def index
    render json: Legislator.with_includes
      .order(:first_name, :last_name)
      .to_json( methods: [:name, :title, :state_name, :eligible],
                only: [:id, :with_us])
  end

  def show
    legislator = Legislator.with_includes.find_by_bioguide_id(params[:id])
    render json: legislator
      .to_json( methods: [:name, :title, :state_name, :eligible, :image_url, :state_abbrev], only: [:with_us, :party])
  end

  def targeted
    render json: Legislator.with_includes.targeted
  end

  def newest_supporters
    render json: Legislator.with_includes.joins(:sponsorships).order('sponsorships.cosponsored_at desc').first(5)
  end

  def supporters_map
    output = redis.get(SUPPORTER_MAP_REDIS_KEY) || prep_supports_map_json
    render js: output
  end

  private

  def prep_supports_map_json
    coordinates_output = []
    Legislator.with_includes.includes(:bills,:active_campaigns).all.each_with_index do |l,i|
      next unless Legislator::MAP_COORDINATES.include?(l.map_key)
      coordinates_output << {
        map_key: l.map_key,
        coordinates: Legislator::MAP_COORDINATES[l.map_key],
        legislator: {
          name: l.name,
          title: l.title,
          party: l.party[0],
          support_max: l.support_max,
          targeted: l.targeted?,
          image_url: l.image_url,
          bioguide_id: l.bioguide_id,
          sponsorships: l.sponsorship_hash,
          chamber: l.chamber,
          description: l.support_description
        }
      }
    end
    json = Oj.dump({tile_coordinates: coordinates_output, label_coordinates: Legislator::MAP_LABELS},  mode: :compat)
    output = "onLegislatorResponse(#{json})"

    redis.set(SUPPORTER_MAP_REDIS_KEY, output)
    redis.expire(SUPPORTER_MAP_REDIS_KEY, 12.hours)

    output
  end



  def redis
    @redis ||= Redis.current
  end


end

{'fraction_non_alphanumeric': 0.1032171581769437, 'fraction_numerical': 0.002680965147453083, 'mean_word_length': 3.211060948081264, 'pattern_counts': {'":': 0, '<': 9, '<?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': '123378', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1261, 'n_words': 231}