Class: ModelScope::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/modelscope/stats.rb

Constant Summary collapse

COUNTERS =
i[
  total own inherited rails gems conditional
  association_generated attribute_generated
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callbacks) ⇒ Stats

Returns a new instance of Stats.



12
13
14
15
# File 'lib/modelscope/stats.rb', line 12

def initialize(callbacks)
  @callbacks = callbacks
  @stats_cache = {}
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



10
11
12
# File 'lib/modelscope/stats.rb', line 10

def callbacks
  @callbacks
end

Instance Method Details

#by_modelObject



17
18
19
# File 'lib/modelscope/stats.rb', line 17

def by_model
  @by_model ||= callbacks.group_by { |cb| cb.model.name }
end

#stats_for(model_name) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/modelscope/stats.rb', line 21

def stats_for(model_name)
  @stats_cache[model_name] ||= begin
    model_callbacks = by_model[model_name]
    return {} unless model_callbacks

    collect_stats(model_callbacks)
  end
end

#stats_for_group(model_name, group) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/modelscope/stats.rb', line 30

def stats_for_group(model_name, group)
  key = "#{model_name}_#{group}"
  @stats_cache[key] ||= begin
    model_callbacks = by_model[model_name]&.select { |cb| cb.callback_group == group }
    return {} unless model_callbacks

    collect_stats(model_callbacks)
  end
end