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 HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::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, nullable_column?, 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 ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.safe_create_for_namespace(target_namespace) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 68

def self.safe_create_for_namespace(target_namespace)
  # Namespaces::ProjectNamespace has no root_ancestor
  # Related: https://gitlab.com/gitlab-org/gitlab/-/issues/386124
  namespace = if target_namespace.is_a?(Group) || target_namespace.is_a?(Namespaces::UserNamespace)
                target_namespace
              else
                target_namespace.parent
              end
  # personal namespace projects and associated ProjectNamespace respond to `namespace`
  # and this is close enough to "root ancestor"
  top_level_namespace =
    target_namespace.respond_to?(:root_ancestor) ? namespace.root_ancestor : namespace.namespace
  aggregation = find_by(group_id: top_level_namespace.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_namespace.id, enabled: true })

  find(top_level_namespace.id)
end

Instance Method Details

#completeObject



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

def complete
  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

#consistency_check_cursor_for(model) ⇒ Object



26
27
28
29
30
31
32
33
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 26

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

  {
    :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



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

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



57
58
59
60
61
62
63
64
65
66
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 57

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



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

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

#set_cursor(mode, model, cursor) ⇒ Object



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

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



51
52
53
54
55
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 51

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