<commit_before>class Aichi11Target < ActiveRecord::Base
  validates_inclusion_of :singleton_guard, :in => [0]

  def self.instance
    first || import
  end

  STATS = {
    coverage: {
      name: 'Coverage',
      terrestrial: {
        value: CountryStatistic.global_percentage_pa_land_cover,
        target: instance.coverage_terrestrial
      },
      marine: {
        value: CountryStatistic.global_percentage_pa_marine_cover,
        target: instance.coverage_marine
      }
    },
    effectively_managed: {
      name: 'Effectively managed',
      terrestrial: {
        value: PameStatistic.global_pame_percentage_pa_land_cover,
        target: instance.effectively_managed_terrestrial
      },
      marine: {
        value: PameStatistic.global_pame_percentage_pa_marine_cover,
        target: instance.effectively_managed_marine
      }
    }
  }.freeze
  TERRESTRIAL = {
    title: 'Terrestrial',
    colour: 'terrestrial'
  }.freeze
  MARINE = {
    title: 'Marine',
    colour: 'marine'
  }.freeze
  # Global by default for both national and global stats
  # as the API stats are not currently split between marine and terrestrial
  DEFAULT_CHART_JSON = {
    title: 'Global',
    colour: 'global',
    value: nil,
    target: nil
  }.freeze
  def self.get_global_stats
    global_stats = Stats::CountryStatisticsApi.get_global_stats
    STATS.each do |name, attributes|
      json = { title: attributes[:name], charts: [] }
      terrestrial_chart = DEFAULT_CHART_JSON.merge(**TERRESTRIAL, **attributes[:terrestrial])
      marine_chart = DEFAULT_CHART_JSON.merge(**MARINE, **attributes[:marine])
      json[:charts] = [terrestrial_chart, marine_chart]
      global_stats << json.dup
    end
    global_stats
  end

  private

  def self.import
    CSV.foreach(aichi11_target_csv_path, headers: true) do |row|
      return create({}.merge(row))
    end
  end

  def self.aichi11_target_csv_path
    Rails.root.join('lib/data/seeds/aichi11_targets.csv')
  end
end
<commit_msg>Make stats constant a method to overcome issues with instance being called in constant<commit_after>class Aichi11Target < ActiveRecord::Base
  validates_inclusion_of :singleton_guard, :in => [0]

  def self.instance
    first || import
  end

  TERRESTRIAL = {
    title: 'Terrestrial',
    colour: 'terrestrial'
  }.freeze
  MARINE = {
    title: 'Marine',
    colour: 'marine'
  }.freeze
  # Global by default for both national and global stats
  # as the API stats are not currently split between marine and terrestrial
  DEFAULT_CHART_JSON = {
    title: 'Global',
    colour: 'global',
    value: nil,
    target: nil
  }.freeze
  def self.get_global_stats
    global_stats = Stats::CountryStatisticsApi.get_global_stats
    stats.each do |name, attributes|
      json = { title: attributes[:name], charts: [] }
      terrestrial_chart = DEFAULT_CHART_JSON.merge(**TERRESTRIAL, **attributes[:terrestrial])
      marine_chart = DEFAULT_CHART_JSON.merge(**MARINE, **attributes[:marine])
      json[:charts] = [terrestrial_chart, marine_chart]
      global_stats << json.dup
    end
    global_stats
  end

  private

  def self.import
    CSV.foreach(aichi11_target_csv_path, headers: true) do |row|
      return create({}.merge(row))
    end
  end

  def self.aichi11_target_csv_path
    Rails.root.join('lib/data/seeds/aichi11_targets.csv')
  end

  def self.stats
    {
      coverage: {
        name: 'Coverage',
        terrestrial: {
          value: CountryStatistic.global_percentage_pa_land_cover,
          target: instance.coverage_terrestrial
        },
        marine: {
          value: CountryStatistic.global_percentage_pa_marine_cover,
          target: instance.coverage_marine
        }
      },
      effectively_managed: {
        name: 'Effectively managed',
        terrestrial: {
          value: PameStatistic.global_pame_percentage_pa_land_cover,
          target: instance.effectively_managed_terrestrial
        },
        marine: {
          value: PameStatistic.global_pame_percentage_pa_marine_cover,
          target: instance.effectively_managed_marine
        }
      }
    }
  end
end

{'fraction_non_alphanumeric': 0.09110787172011661, 'fraction_numerical': 0.004373177842565598, 'mean_word_length': 3.0442043222003927, 'pattern_counts': {'":': 0, '<': 9, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7030429', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1278, 'n_words': 268}