flipper gem return 可以统计启用用户数吗?

Can the flipper gem return a count of enabled users?

我们正在探索使用脚蹼 gem (https://github.com/jnunemaker/flipper) 来控制哪些人可以看到新功能。在我们的第一个测试中,我们希望仅向看到横幅广告的第一批 X 用户展示特定功能。

我们考虑使用百分比,但业务对数字非常具体,并且还希望立即达到该数字,然后为所有其他用户禁用该功能,而不是为首先看到它的用户禁用该功能.使用百分比,我们无法找到一种方法来确保正确的数字会看到它,并且前 x 个中的每个人都会看到它。

在 gates/actor.rb 里面,有这个:

enabled_actor_ids = value

这意味着我们可以获取已启用 ID 的列表,并对其进行计数,但我们找不到该列表是否或在何处可以公开。

由于我们将 AR 适配器用作试用版,因此我们在加入 flipper_gates table 的 actor 对象上创建了一个作用域,但这感觉非常脆弱并且非常容易进入gem 的内部工作原理。

非常感谢任何建议。

您应该能够以编程方式完成此操作 turning the feature on for Individual Actors,直到达到上限。

重要提示: according to the documentation

The individual actor gate is typically not designed for hundreds or thousands of actors to be enabled. This is an explicit choice to make it easier to batch load data from the adapters instead of performing individual checks for actors over and over. If you need to enable something for more than 20 individual people, I would recommend using a group.

既然我们已经同意,我们无论如何都想继续推进。让我们谈谈实施。

为演员启用该功能

您需要做的第一件事是确保参与者(可能是用户)响应 flipper_id 并且 flipper_id 对于每个参与者都是唯一的。设置完成后,您应该能够在用户看到这样的横幅时简单地为他们启用该功能:

flipper[:stats].enable_actor user

计算参与某项功能的演员

现在,为了确定我们是否应该为用户启用该功能,我们需要确定有多少用户已注册该功能。

为此,我们可以直接查询门:

Flipper::Adapters::ActiveRecord::Gate.where(
  feature_key: "stats",
  key: "actors"
).count

这将 return 计算一个功能中注册的演员数量。

我们怎么知道它有效?

好吧,让我们来看看gem。

flipper[:stats].enable_actor 实际上调用 Feature#enable_actor,我们之前传入的 user(响应 flipper_id)作为演员传入。

接下来,Feature#enable_actor passes the actor into Types::Actor.wrap 创建一个 Types::Actor 的新实例,它检查以确保 actor 不是 nil 并且它有一个 flipper_id,然后设置两个实例变量, thing 设置为演员, value 设置为演员的 flipper_id.

现在我们有了 Types::Actor 的实例,我们将它传递到适配器上的 Feature#enable which looks up the gate which in our case would be a Gates::Actor instance. Finally we call enable(在您的情况下是 ActiveRecord)。

Adapters::ActiveRecord.enable 中,我们首先查看 gate.data_type,在我们的例子中是 :set。从那里我们做:

@gate_class.create! do |g|
  g.feature_key = feature.key
  g.key = gate.key
  g.value = thing.value.to_s
end

其中,如前所述,thing.value就是flipper_id。答对了! @gate_class 是负责门 table 的活动记录 class 而 the default table name 是 "flipper_gates".

现在我们确切地知道要查询哪些内容才能获得在该功能中注册的演员的数量!

number_of_actors_enrolled_in_stats_feature = Flipper::Adapters::ActiveRecord::Gate.where(
  feature_key: "stats",
  key: "actors"
).count

现在您可以 Flipper[:some_feature].actors_value.size,假设您已经使用 Flipper.configure 配置了默认的 flipper 实例。

https://github.com/jnunemaker/flipper/blob/196946c63aee1eaa09fa25e945cdbff896fe71e5/lib/flipper/feature.rb#L258-L260