<commit_before># frozen_string_literal: true

require_dependency "gobierto_citizens_charters"

module GobiertoCitizensCharters
  class Edition < ApplicationRecord
    acts_as_paranoid column: :archived_at

    include ActsAsParanoidAliases

    PERIOD_INTERVAL_DATA = {
      year: ->(date) { { year: date.year } },
      quarter: ->(date) { { quarter: (date.month / 4) + 1, year: date.year } },
      month: ->(date) { { month: date.month, year: date.year } },
      week: ->(date) { { week: date.strftime("%W").to_i, year: date.year } }
    }.freeze

    belongs_to :commitment, -> { with_archived }

    enum period_interval: PERIOD_INTERVAL_DATA.keys

    scope :recent, -> { order(period: :desc) }
    scope :of_same_period, ->(edition) { where(period: (edition.period_start..edition.period_end), period_interval: edition.period_interval) }
    scope :group_by_period_interval, ->(period_interval) { where(period_interval: period_interval).group(Arel.sql("date_trunc('#{ period_interval }', period)"), :period_interval) }

    def proportion
      return nil if percentage.blank? && [value, max_value].any?(&:blank?)

      percentage || 100 * (value / max_value)
    end

    def self.progress
      p_rel = where.not(percentage: nil)
      p_val = where(percentage: nil).where.not(value: nil, max_value: nil)
      (p_rel.sum(:percentage) + p_val.select("value/max_value as prop").map(&:prop).sum * 100) / (p_rel.count + p_val.count)
    end

    def period_values
      PERIOD_INTERVAL_DATA[period_interval.to_sym].call(period)
    end

    def editions_of_same_period
      self.class.of_same_period(self)
    end

    def period_start
      period.utc.send("at_beginning_of_#{period_interval}")
    end

    def period_end
      period.utc.send("at_end_of_#{period_interval}")
    end

    def previous_period_start
      period_start.send("prev_#{period_interval}")
    end

    def to_s
      period_values.values.join("-")
    end
<commit_msg>Improve query to calculate progress<commit_after># frozen_string_literal: true

require_dependency "gobierto_citizens_charters"

module GobiertoCitizensCharters
  class Edition < ApplicationRecord
    acts_as_paranoid column: :archived_at

    include ActsAsParanoidAliases

    PERIOD_INTERVAL_DATA = {
      year: ->(date) { { year: date.year } },
      quarter: ->(date) { { quarter: (date.month / 4) + 1, year: date.year } },
      month: ->(date) { { month: date.month, year: date.year } },
      week: ->(date) { { week: date.strftime("%W").to_i, year: date.year } }
    }.freeze

    belongs_to :commitment, -> { with_archived }

    enum period_interval: PERIOD_INTERVAL_DATA.keys

    scope :recent, -> { order(period: :desc) }
    scope :of_same_period, ->(edition) { where(period: (edition.period_start..edition.period_end), period_interval: edition.period_interval) }
    scope :group_by_period_interval, ->(period_interval) { where(period_interval: period_interval).group(Arel.sql("date_trunc('#{ period_interval }', period)"), :period_interval) }

    def proportion
      return nil if percentage.blank? && [value, max_value].any?(&:blank?)

      percentage || 100 * (value / max_value)
    end

    def self.progress
      p_rel = where.not(percentage: nil)
      p_val = where(percentage: nil).where.not(value: nil, max_value: nil)
      (p_rel.sum(:percentage) + p_val.sum("value/max_value") * 100) / (p_rel.count + p_val.count)
    end

    def period_values
      PERIOD_INTERVAL_DATA[period_interval.to_sym].call(period)
    end

    def editions_of_same_period
      self.class.of_same_period(self)
    end

    def period_start
      period.utc.send("at_beginning_of_#{period_interval}")
    end

    def period_end
      period.utc.send("at_end_of_#{period_interval}")
    end

    def previous_period_start
      period_start.send("prev_#{period_interval}")
    end

    def to_s
      period_values.values.join("-")
    end

{'fraction_non_alphanumeric': 0.1279754287176862, 'fraction_numerical': 0.004095213718965958, 'mean_word_length': 3.592244418331375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '7582159', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1315, 'n_words': 246}