<commit_before># encoding: utf-8
require 'redis'
require 'rollout'

# Rollout requires a "user object"
class FakeUser < Struct.new(:id); end

class Plugin::Rollout < Plugin
  #
  # Config variables:
  #
  # rollout.redis.host - The redis instance we should connect to. Defaults
  # to Redis.new i.e. localhost
  #

  # Add routes
  def add_routes(rp, opts)

    # ░░░░░░░▄▄▄▄█████████████▄▄▄░░░░░░░ #
    # ████▄▀████████▀▀▀▀▀▀████████▀▄████ #
    # ▀████░▀██████▄▄░░░░▄▄██████▀░████▀ #
    # ░███▀▀█▄▄░▀▀██████████▀▀░▄▄█▀▀███░ #
    # ░████▄▄▄▀▀█▄░░░▀▀▀▀░░░▄█▀▀▄▄▄████░ #
    # ░░██▄▄░▀▀████░██▄▄██░████▀▀░▄▄██░░ #
    # ░░░▀████▄▄▄██░██████░██▄▄▄████▀░░░ #
    # ░░██▄▀▀▀▀▀▀▀▀░░████░░▀▀▀▀▀▀▀▀▄██░░ #
    # ░░░██░░░░░░░░░░████░░░░░░░░░░██░░░ #
    # ░░░███▄▄░░░░▄█░████░█▄░░░░▄▄███░░░ #
    # ░░░███████░███░████░███░███████░░░ #
    # ░░░███████░███░████░███░███████░░░ #
    # ░░░███████░███░▀▀▀▀░███░███████░░░ #
    # ░░░███████░████████████░███████░░░ #
    # ░░░░▀█████░███░▄▄▄▄░███░█████▀░░░░ #
    # ░░░░░░░░▀▀░██▀▄████▄░██░▀▀░░░░░░░░ #
    # ░░░░░░░░░░░░▀░██████░▀░░░░░░░░░░░░ #
    # AUTOBOTS - ROLL OUT!               #
    rollout!

    rp.rollout do

      rollouts = {:production => @rollout, :staging => @rollout_staging}

      # Query the current status of a feature
      rp.route /info (?<feature>\w+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end

      # Activate/Deactivate groups
      rp.route /activate_group (?<feature>\w+) (?<group>[\w\.@]+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          rollout_op(act){ro.activate_group(act.feature.to_sym, act.group.to_sym)}
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end

      rp.route /deactivate_group (?<feature>\w+) (?<group>[\w\.@]+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          rollout_op(act){ro.deactivate_group(act.feature.to_sym, act.group.to_sym)}
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end

      # Activate/Deactivate users
      rp.route /activate_user (?<feature>\w+) (?<user_id>\w+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          rollout_op(act){ro.activate_user(act.feature.to_sym, FakeUser.new(act.user_id))}
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end

      rp.route /deactivate_user (?<feature>\w+) (?<user_id>\w+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          rollout_op(act){ro.deactivate_user(act.feature.to_sym, FakeUser.new(act.user_id))}
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end

      rp.route /percentage (?<feature>\w+) (?<percentage>\w+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          rollout_op(act){ ro.activate_percentage(act.feature.to_sym, Integer(act.percentage)) }
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end
    end

  end

private

  def with_rollout(act)<commit_msg>Fixes for rollout when expecting integers for user_id and percentages<commit_after># encoding: utf-8
require 'redis'
require 'rollout'

# Rollout requires a "user object"
class FakeUser < Struct.new(:id); end

class Plugin::Rollout < Plugin
  #
  # Config variables:
  #
  # rollout.redis.host - The redis instance we should connect to. Defaults
  # to Redis.new i.e. localhost
  #

  # Add routes
  def add_routes(rp, opts)

    # ░░░░░░░▄▄▄▄█████████████▄▄▄░░░░░░░ #
    # ████▄▀████████▀▀▀▀▀▀████████▀▄████ #
    # ▀████░▀██████▄▄░░░░▄▄██████▀░████▀ #
    # ░███▀▀█▄▄░▀▀██████████▀▀░▄▄█▀▀███░ #
    # ░████▄▄▄▀▀█▄░░░▀▀▀▀░░░▄█▀▀▄▄▄████░ #
    # ░░██▄▄░▀▀████░██▄▄██░████▀▀░▄▄██░░ #
    # ░░░▀████▄▄▄██░██████░██▄▄▄████▀░░░ #
    # ░░██▄▀▀▀▀▀▀▀▀░░████░░▀▀▀▀▀▀▀▀▄██░░ #
    # ░░░██░░░░░░░░░░████░░░░░░░░░░██░░░ #
    # ░░░███▄▄░░░░▄█░████░█▄░░░░▄▄███░░░ #
    # ░░░███████░███░████░███░███████░░░ #
    # ░░░███████░███░████░███░███████░░░ #
    # ░░░███████░███░▀▀▀▀░███░███████░░░ #
    # ░░░███████░████████████░███████░░░ #
    # ░░░░▀█████░███░▄▄▄▄░███░█████▀░░░░ #
    # ░░░░░░░░▀▀░██▀▄████▄░██░▀▀░░░░░░░░ #
    # ░░░░░░░░░░░░▀░██████░▀░░░░░░░░░░░░ #
    # AUTOBOTS - ROLL OUT!               #
    rollout!

    rp.rollout do

      rollouts = {:production => @rollout, :staging => @rollout_staging}

      # Query the current status of a feature
      rp.route /info (?<feature>\w+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end

      # Activate/Deactivate groups
      rp.route /activate_group (?<feature>\w+) (?<group>[\w\.@]+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          rollout_op(act){ro.activate_group(act.feature.to_sym, act.group.to_sym)}
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end

      rp.route /deactivate_group (?<feature>\w+) (?<group>[\w\.@]+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          rollout_op(act){ro.deactivate_group(act.feature.to_sym, act.group.to_sym)}
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end

      # Activate/Deactivate users
      rp.route /activate_user (?<feature>\w+) (?<user_id>\d+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          rollout_op(act){ro.activate_user(act.feature.to_sym, FakeUser.new(act.user_id))}
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end

      rp.route /deactivate_user (?<feature>\w+) (?<user_id>\d+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          rollout_op(act){ro.deactivate_user(act.feature.to_sym, FakeUser.new(act.user_id))}
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end

      rp.route /percentage (?<feature>\w+) (?<percentage>\d+)\s*(?<env>\w+)?$/ do |act|
        with_rollout(act) do |ro|
          pct = Integer(act.percentage)
          if pct < 0 or pct > 100
            act.say "#{pct} is an invalid percentage"
            return
          end
          rollout_op(act){ ro.activate_percentage(act.feature.to_sym, pct) }
          act.paste ro.get(act.feature.to_sym).to_hash.to_s
        end
      end
    end

  end

private

  def with_rollout(act)
{'fraction_non_alphanumeric': 0.3151848937844217, 'fraction_numerical': 0.0009441384736428009, 'mean_word_length': 2.875609756097561, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, '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': '763540', 'n_tokens_mistral': 3229, 'n_tokens_neox': 3479, 'n_words': 366}