Class: Analytics::CycleAnalytics::Aggregation

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

Constant Summary

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

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_group(group) ⇒ Object


62
63
64
65
66
67
68
69
# File 'app/models/analytics/cycle_analytics/aggregation.rb', line 62

def self.safe_create_for_group(group)
  top_level_group = group.root_ancestor
  aggregation = find_by(group_id: top_level_group.id)
  return aggregation if aggregation.present?

  insert({ group_id: top_level_group.id }, unique_by: :group_id)
  find_by(group_id: top_level_group.id)
end

Instance Method Details

#cursor_for(mode, model) ⇒ Object


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

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