Class: Ci::Catalog::Resources::AggregateLast30DayUsageService

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/services/ci/catalog/resources/aggregate_last30_day_usage_service.rb

Overview

This service aggregates CI component usage data and updates ‘last_30_day_usage_count` for each catalog resource daily. It utilizes Gitlab::Ci::Components::Usages::Aggregator which implements a “continue later” mechanism to process the data in time-boxed jobs. rubocop: disable CodeReuse/ActiveRecord – Custom queries required

Constant Summary collapse

TARGET_MODEL =
Ci::Catalog::Resource
GROUP_BY_COLUMN =
:catalog_resource_id
WINDOW_LENGTH =
30.days

Instance Method Summary collapse

Instance Method Details

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/ci/catalog/resources/aggregate_last30_day_usage_service.rb', line 17

def execute
  return execute_with_new_table if ::Feature.enabled?(:ci_catalog_ranking_from_new_usage_table, :instance)

  return ServiceResponse.success(message: "Processing complete for #{today}") if done_processing?

  aggregator = Gitlab::Ci::Components::Usages::Aggregator.new(
    target_model: TARGET_MODEL,
    group_by_column: GROUP_BY_COLUMN,
    usage_start_date: today - WINDOW_LENGTH,
    usage_end_date: today - 1.day,
    lease_key: lease_key
  )

  result = aggregator.each_batch do |usage_counts|
    save_usage_counts!(usage_counts)
  end

  if result
    ServiceResponse.success(message: 'Targets processed', payload: result.to_h)
  else
    ServiceResponse.success(message: 'Lease taken', payload: { lease_key: lease_key })
  end
end

#execute_with_new_tableObject



41
42
43
44
45
46
# File 'app/services/ci/catalog/resources/aggregate_last30_day_usage_service.rb', line 41

def execute_with_new_table
  update_component_counts
  update_resource_counts

  ServiceResponse.success(message: 'Usage counts updated for components and resources')
end