Class: Analytics::CycleAnalytics::Aggregation

Inherits:
ApplicationRecord show all
Includes:
Parentable, FromUnion
Defined in:
app/models/analytics/cycle_analytics/aggregation.rb

Constant Summary

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.safe_create_for_namespace(group_or_project_namespace) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 62

def self.safe_create_for_namespace(group_or_project_namespace)
  # Namespaces::ProjectNamespace has no root_ancestor
  # Related: https://gitlab.com/gitlab-org/gitlab/-/issues/386124
  group = group_or_project_namespace.is_a?(Group) ? group_or_project_namespace : group_or_project_namespace.parent
  top_level_group = group.root_ancestor
  aggregation = find_by(group_id: top_level_group.id)
  return aggregation if aggregation&.enabled?

  # At this point we're sure that the group is licensed, we can always enable the aggregation.
  # This re-enables the aggregation in case the group downgraded and later upgraded the license.
  upsert({ group_id: top_level_group.id, enabled: true })

  find(top_level_group.id)
end

Instance Method Details

#consistency_check_cursor_for(model) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 19

def consistency_check_cursor_for(model)
  return {} if self["last_consistency_check_#{model.issuable_model.table_name}_issuable_id"].nil?

  {
    :start_event_timestamp => self["last_consistency_check_#{model.issuable_model.table_name}_start_event_timestamp"],
    :end_event_timestamp => self["last_consistency_check_#{model.issuable_model.table_name}_end_event_timestamp"],
    model.issuable_id_column => self["last_consistency_check_#{model.issuable_model.table_name}_issuable_id"]
  }
end

#cursor_for(mode, model) ⇒ Object



12
13
14
15
16
17
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 12

def cursor_for(mode, model)
  {
    updated_at: self["last_#{mode}_#{model.table_name}_updated_at"],
    id: self["last_#{mode}_#{model.table_name}_id"]
  }.compact
end

#estimated_next_run_atObject



51
52
53
54
55
56
57
58
59
60
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 51

def estimated_next_run_at
  return unless enabled
  return if last_incremental_run_at.nil?

  estimation = duration_until_the_next_aggregation_job +
    average_aggregation_duration +
    (last_incremental_run_at - earliest_last_run_at)

  estimation < 1 ? nil : estimation.from_now
end

#refresh_last_run(mode) ⇒ Object



29
30
31
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 29

def refresh_last_run(mode)
  self["last_#{mode}_run_at"] = Time.current
end

#reset_full_run_cursorsObject



33
34
35
36
37
38
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 33

def reset_full_run_cursors
  self.last_full_issues_id = nil
  self.last_full_issues_updated_at = nil
  self.last_full_merge_requests_id = nil
  self.last_full_merge_requests_updated_at = nil
end

#set_cursor(mode, model, cursor) ⇒ Object



40
41
42
43
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 40

def set_cursor(mode, model, cursor)
  self["last_#{mode}_#{model.table_name}_id"] = cursor[:id]
  self["last_#{mode}_#{model.table_name}_updated_at"] = cursor[:updated_at]
end

#set_stats(mode, runtime, processed_records) ⇒ Object



45
46
47
48
49
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 45

def set_stats(mode, runtime, processed_records)
  # We only store the last 10 data points
  self["#{mode}_runtimes_in_seconds"] = (self["#{mode}_runtimes_in_seconds"] + [runtime]).last(10)
  self["#{mode}_processed_records"] = (self["#{mode}_processed_records"] + [processed_records]).last(10)
end