<commit_before>module Devices
  class UnattendedUpgrade < Mutations::Command
    def execute
      all_eligible_devices.map(&:send_upgrade_request)
    end

    def all_eligible_devices
      Release
        .distinct
        .pluck(:channel)
        .compact
        .sort
        .map { |chan| eligible_devices(chan) }
        .reduce(:or)
    end

    def eligible_devices(chan)
      Device
        .includes(:fbos_config)
        .where("last_saw_api > ?", 3.days.ago)
        .where("fbos_configs.update_channel" => chan)
        .where.not(fbos_version: latest_version(chan))
        .where("fbos_configs.os_auto_update" => true)
        .where(ota_hour_utc: [nil, Time.now.utc.hour])
        .order("RANDOM()")
        .limit(200)
    end

    def latest_version(chan)
      Release.maybe_find_latest(channel: chan)&.version
    end
  end
end
<commit_msg>Normalize FBOS version in Devices::UnattendedUpgrade<commit_after>module Devices
  class UnattendedUpgrade < Mutations::Command
    def execute
      all_eligible_devices.map(&:send_upgrade_request)
    end

    def all_eligible_devices
      Release
        .distinct
        .pluck(:channel)
        .compact
        .sort
        .map { |chan| eligible_devices(chan) }
        .reduce(:or)
    end

    def eligible_devices(chan)
      Device
        .includes(:fbos_config)
        .where("last_saw_api > ?", 3.days.ago)
        .where("fbos_configs.update_channel" => chan)
        .where.not(fbos_version: latest_version(chan))
        .where("fbos_configs.os_auto_update" => true)
        .where(ota_hour_utc: [nil, Time.now.utc.hour])
        .order("RANDOM()")
        .limit(200)
    end

    def latest_version(chan)
      Release
        .maybe_find_latest(channel: chan)
        .version
        .downcase
        .gsub(".pre.", "-")
    end
  end
end

{'fraction_non_alphanumeric': 0.11214442013129103, 'fraction_numerical': 0.00437636761487965, 'mean_word_length': 2.3074141048824592, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '2547433', 'n_tokens_mistral': 618, 'n_tokens_neox': 589, 'n_words': 100}