<commit_before>require 'csv'

module Api
  module V1
    module Data
      class HistoricalEmissionsCsvContent
        def initialize(filter)
          @grouped_query = filter.call
          @headers = filter.column_aliases
          @headers.shift
          @years = filter.years
        end

        def call
          CSV.generate do |csv|
            csv << @headers.map(&:humanize) + @years
            @grouped_query.each do |record|
              ary = @headers.map { |h| record[h] }
              ary += @years.map do |y|
                emission = record.emissions.find { |e| e['year'] == y }
                emission['value']
              end
              csv << ary
            end
          end
        end
      end
    end
  end
end<commit_msg>Fix nil exception when no value present for year<commit_after>require 'csv'

module Api
  module V1
    module Data
      class HistoricalEmissionsCsvContent
        def initialize(filter)
          @grouped_query = filter.call
          @headers = filter.column_aliases
          @headers.shift
          @years = filter.years
        end

        def call
          CSV.generate do |csv|
            csv << @headers.map(&:humanize) + @years
            @grouped_query.each do |record|
              ary = @headers.map { |h| record[h] }
              ary += @years.map do |y|
                emission = record.emissions.find { |e| e['year'] == y }
                emission && emission['value']
              end
              csv << ary
            end
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08934269304403318, 'fraction_numerical': 0.0012763241863433313, 'mean_word_length': 1.4012251148545176, 'pattern_counts': {'":': 0, '<': 11, '<?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': '935209', 'n_tokens_mistral': 449, 'n_tokens_neox': 428, 'n_words': 118}