Skip to content

aiMetrics: Remove legacy contributors-based filtering code after namespace_path migration

Summary

Clean up legacy contributors-based filtering code after the use_ai_events_namespace_path_filter feature flag has been enabled globally and proven stable.

Background

Multiple services currently maintain both legacy contributors-based filtering and new namespace_path filtering behind the feature flag. Once the migration is complete, we should remove the legacy code to reduce complexity and maintenance burden.

Code to Remove

Services with legacy QUERY constants:

  1. CodeSuggestionUsageService (ee/app/services/analytics/ai_analytics/code_suggestion_usage_service.rb)

    • Remove QUERY constant (lines with contributors CTE)
    • Remove NEW_QUERY constant
    • Remove deprecated field queries: CODE_CONTRIBUTORS_COUNT_QUERY, CODE_SUGGESTIONS_CONTRIBUTORS_COUNT_QUERY, CODE_SUGGESTIONS_SHOWN_COUNT_QUERY, CODE_SUGGESTIONS_ACCEPTED_COUNT_QUERY
  2. DuoChatUsageService (ee/app/services/analytics/ai_analytics/duo_chat_usage_service.rb)

    • Remove QUERY constant
    • Remove NEW_QUERY constant
    • Remove CONTRIBUTORS_COUNT_QUERY
  3. DuoUsageService (ee/app/services/analytics/ai_analytics/duo_usage_service.rb)

    • Remove QUERY constant
    • Remove NEW_QUERY constant
  4. CommonUsageService (ee/app/services/analytics/ai_analytics/common_usage_service.rb)

    • Remove CONTRIBUTORS_FILTER_REGEX_PATTERN
    • Remove replace_contributors_filter method
    • Remove base_query method
    • Remove fetch_contributions_from_new_table? method
    • Simplify raw_query to not use format with base_query
  5. AiUserMetricsService (ee/app/services/analytics/ai_analytics/ai_user_metrics_service.rb)

    • Remove filter_by_namespace_path_enabled? method
    • Simplify placeholders method to always use namespace.traversal_path (remove conditional logic)
  6. Ai::CodeSuggestionEventsFinder (ee/app/finders/ai/code_suggestion_events_finder.rb)

    • Remove CONTRIBUTORS_IDS_QUERY
    • Remove CONTRIBUTORS_IDS_NEW_QUERY
    • Remove contributors_ids method
    • Remove contributors_ids_from_postgresql method
    • Remove contributors_ids_from_ch method
    • Remove ch_contributors_ids_query method
    • Remove fetch_contributions_from_new_table? method
    • Simplify initializer to not set @users or clear @namespace

GraphQL Resolvers:

  1. CodeSuggestionEventsResolver (ee/app/graphql/resolvers/analytics/ai_usage/code_suggestion_events_resolver.rb)
    • Remove should_raise_error? method (lines 31-36)
    • Remove the ready? method override that calls should_raise_error?
    • Remove related comments about feature flag

Feature Flag References:

  • Remove feature flag definition: ee/config/feature_flags/wip/use_ai_events_namespace_path_filter.yml
  • Remove all Feature.enabled?(:use_ai_events_namespace_path_filter) checks

Tests to Update:

  • ee/spec/finders/ai/code_suggestion_events_finder_spec.rb
    • Remove contexts testing with feature flag disabled
    • Remove stub_feature_flags(use_ai_events_namespace_path_filter: false) calls
  • ee/spec/services/analytics/ai_analytics/duo_usage_service_spec.rb
    • Remove feature flag contexts and stubs
    • Update expected results to only test namespace_path filtering
  • ee/spec/services/analytics/ai_analytics/duo_chat_usage_service_spec.rb
    • Remove feature flag contexts and stubs
    • Update expected results to only test namespace_path filtering
  • ee/spec/services/analytics/ai_analytics/ai_user_metrics_service_spec.rb
    • Remove feature flag contexts and stubs
  • ee/spec/graphql/resolvers/analytics/ai_usage/code_suggestion_events_resolver_spec.rb
    • Remove tests for should_raise_error? method
    • Update tests to work without feature flag checks

Services to Review (likely no changes needed):

  • TroubleshootUsageService (ee/app/services/analytics/ai_analytics/troubleshoot_usage_service.rb)
    • Already uses namespace_path filtering directly in its QUERY
    • Overrides fetch_contributions_from_new_table? to return false
    • Review to confirm no changes needed

Prerequisites

  • use_ai_events_namespace_path_filter enabled globally for at least 1 milestone
  • No incidents or data accuracy issues reported
  • Metrics show namespace_path filtering is working correctly

Implementation Steps

  1. Create MR to remove legacy code
  2. Update all related tests
  3. Update documentation if needed
  4. Get review from team
  5. Merge and monitor

Related Issues

Edited by Amr Taha