<commit_before>module PredictHarvest
  extend ActiveSupport::Concern

  included do # rubocop:disable Metrics/BlockLength
    # dates
    def first_harvest_date
      harvests_with_dates.minimum(:harvested_at)
    end

    def last_harvest_date
      harvests_with_dates.maximum(:harvested_at)
    end

    def first_harvest_predicted_at
      return unless crop.median_days_to_first_harvest.present? && planted_at.present?

      planted_at + crop.median_days_to_first_harvest.days
    end

    def last_harvest_predicted_at
      return unless crop.median_days_to_last_harvest.present? && planted_at.present?

      planted_at + crop.median_days_to_last_harvest.days
    end

    # actions
    def update_harvest_days!
      days_to_first_harvest = nil
      days_to_last_harvest = nil
      if planted_at.present? && harvests_with_dates.size.positive?
        days_to_first_harvest = (first_harvest_date - planted_at).to_i
        days_to_last_harvest = (last_harvest_date - planted_at).to_i if finished?
      end
      update(days_to_first_harvest: days_to_first_harvest, days_to_last_harvest: days_to_last_harvest)
    end

    # status
    def harvest_time?
      return false if crop.perennial || finished

      # We have harvests but haven't finished
      harvests.size.positive? ||

        # or, we don't have harvests, but we predict we should by now
        (first_harvest_predicted_at.present? &&
          harvests.empty? &&
          first_harvest_predicted_at < Time.zone.today)
    end

    def before_harvest_time?
      first_harvest_predicted_at.present? &&
        harvests.empty? &&
        first_harvest_predicted_at.present? &&
        first_harvest_predicted_at > Time.zone.today
    end

    def harvest_months
      Rails.cache.fetch("#{cache_key_with_version}/harvest_months", expires_in: 5.minutes) do
        neighbours_for_harvest_predictions.where.not(harvested_at: nil)
          .group("extract(MONTH from harvested_at)")
          .count
      end
    end

    def neighbours_for_harvest_predictions
      # use this planting's harvest if any
      return harvests if harvests.size.positive?

      # otherwise use nearby plantings
      if location
        return Harvest.where(planting: nearby_same_crop.has_harvests)
            .where.not(planting_id: nil)
      end

      Harvest.none
    end

    private

    def harvests_with_dates
      harvests.where.not(harvested_at: nil)
    end
  end
end<commit_msg>Make the month an int<commit_after>module PredictHarvest
  extend ActiveSupport::Concern

  included do # rubocop:disable Metrics/BlockLength
    # dates
    def first_harvest_date
      harvests_with_dates.minimum(:harvested_at)
    end

    def last_harvest_date
      harvests_with_dates.maximum(:harvested_at)
    end

    def first_harvest_predicted_at
      return unless crop.median_days_to_first_harvest.present? && planted_at.present?

      planted_at + crop.median_days_to_first_harvest.days
    end

    def last_harvest_predicted_at
      return unless crop.median_days_to_last_harvest.present? && planted_at.present?

      planted_at + crop.median_days_to_last_harvest.days
    end

    # actions
    def update_harvest_days!
      days_to_first_harvest = nil
      days_to_last_harvest = nil
      if planted_at.present? && harvests_with_dates.size.positive?
        days_to_first_harvest = (first_harvest_date - planted_at).to_i
        days_to_last_harvest = (last_harvest_date - planted_at).to_i if finished?
      end
      update(days_to_first_harvest: days_to_first_harvest, days_to_last_harvest: days_to_last_harvest)
    end

    # status
    def harvest_time?
      return false if crop.perennial || finished

      # We have harvests but haven't finished
      harvests.size.positive? ||

        # or, we don't have harvests, but we predict we should by now
        (first_harvest_predicted_at.present? &&
          harvests.empty? &&
          first_harvest_predicted_at < Time.zone.today)
    end

    def before_harvest_time?
      first_harvest_predicted_at.present? &&
        harvests.empty? &&
        first_harvest_predicted_at.present? &&
        first_harvest_predicted_at > Time.zone.today
    end

    def harvest_months
      Rails.cache.fetch("#{cache_key_with_version}/harvest_months", expires_in: 5.minutes) do
        neighbours_for_harvest_predictions.where.not(harvested_at: nil)
          .group("extract(MONTH from harvested_at)::int")
          .count
      end
    end

    def neighbours_for_harvest_predictions
      # use this planting's harvest if any
      return harvests if harvests.size.positive?

      # otherwise use nearby plantings
      if location
        return Harvest.where(planting: nearby_same_crop.has_harvests)
            .where.not(planting_id: nil)
      end

      Harvest.none
    end

    private

    def harvests_with_dates
      harvests.where.not(harvested_at: nil)
    end
  end
end
{'fraction_non_alphanumeric': 0.06426682936750051, 'fraction_numerical': 0.0004067520846044336, 'mean_word_length': 3.0146938775510206, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6872706', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1578, 'n_words': 297}