AI Impact Analytics Dashboard: Count of Duo Pro & Enterprise users does not work on self-managed instances

This issue came to light while working on #521287 (closed)

From the Rails console:

fields
=>
[:contributors_count,
 :duo_chat_contributors_count,
 :code_contributors_count,
 :code_suggestions_contributors_count,
 :code_suggestions_shown_count,
 :code_suggestions_accepted_count,
 :duo_used_count,
 :duo_assigned_users_count]
irb(main):034:0> params
=> {:start_date=>Sat, 01 Feb 2025 00:00:00.000000000 UTC +00:00, :end_date=>Fri, 28 Feb 2025 23:59:59.999999999 UTC +00:00}
irb(main):035:0> current_user
=> #<User id:1 @root>
irb(main):036:0> namespace
=> #<Group id:35 @health-check>
irb(main):037:1* usage = ::Analytics::AiAnalytics::AiMetricsService.new(
irb(main):038:1* current_user,
irb(main):039:1* namespace: namespace,
irb(main):040:1* from: params[:start_date],
irb(main):041:1* to: params[:end_date],
irb(main):042:1* fields: fields
irb(main):043:0> ).execute
=>
#<ServiceResponse:0x00007318b51adc18
...
irb(main):044:0> usage.payload
=>
{:duo_assigned_users_count=>0,
 :code_contributors_count=>2,
 :code_suggestions_contributors_count=>1,
 :code_suggestions_shown_count=>6,
 :code_suggestions_accepted_count=>3,
 :contributors_count=>2,
 :duo_chat_contributors_count=>2,
 :duo_used_count=>2}
irb(main):045:0>

We noticed that duo_assigned_users_count returned 0, even though Duo Enterprise seats were assigned to users in that instance.

Digging further, we found:

GitlabSubscriptions::AddOnPurchase.by_add_on_name(:duo_enterprise)
=>
[#<GitlabSubscriptions::AddOnPurchase:0x00007318b690f938
  id: 1,
  created_at: Thu, 13 Feb 2025 09:29:54.334680000 UTC +00:00,
  updated_at: Thu, 13 Feb 2025 09:29:54.334680000 UTC +00:00,
  subscription_add_on_id: 1,
  namespace_id: nil,
  quantity: 100,
  expires_on: Tue, 18 Nov 2025,
  purchase_xid: "C-00903696",
  last_assigned_users_refreshed_at: Fri, 28 Feb 2025 04:41:08.290880000 UTC +00:00,
  trial: false,
  started_at: Fri, 17 Jan 2025,
  organization_id: 1>]
irb(main):055:0> GitlabSubscriptions::AddOnPurchase.by_add_on_name(:duo_enterprise).by_namespace(namespace.root_ancestor)
=> []

which is why duo_assigned_users_count is 0.

Code is here and here

In self-managed instances, Duo add-ons are tied to the instance level, not a namespace, so the duo_assigned_users_count count will always be 0 in self-managed instances, which is incorrect.

Screenshot_2025-02-28_at_5.08.21_PM

Proposal:

Correct the logic within ee/app/finders/gitlab_subscriptions/add_on_assigned_users_finder.rb, such that for self-managed instances, Duo Pro + Enterprise count is obtained correctly.

Edited by Manoj M J