Class: ModelScope::Stats
- Inherits:
-
Object
- Object
- ModelScope::Stats
- Defined in:
- lib/modelscope/stats.rb
Constant Summary collapse
- COUNTERS =
%i[ total own inherited rails gems conditional association_generated attribute_generated ].freeze
- SORT =
%i[size name].freeze
- SORT_ORDER =
%i[desc asc].freeze
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
Instance Method Summary collapse
- #by_model ⇒ Object
-
#initialize(callbacks, sort_by: :size, sort_order: :desc) ⇒ Stats
constructor
A new instance of Stats.
- #stats_for(model_name) ⇒ Object
- #stats_for_group(model_name, group) ⇒ Object
Constructor Details
#initialize(callbacks, sort_by: :size, sort_order: :desc) ⇒ Stats
Returns a new instance of Stats.
16 17 18 19 20 21 22 23 |
# File 'lib/modelscope/stats.rb', line 16 def initialize(callbacks, sort_by: :size, sort_order: :desc) @callbacks = callbacks @stats_cache = {} @sort_by = sort_by raise ArgumentError, "Invalid sort_by: #{@sort_by}. Available: #{SORT.join(", ")}" unless SORT.include?(@sort_by) @sort_order = sort_order raise ArgumentError, "Invalid sort_order: #{@sort_order}. Available: #{SORT_ORDER.join(", ")}" unless SORT_ORDER.include?(@sort_order) end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
13 14 15 |
# File 'lib/modelscope/stats.rb', line 13 def callbacks @callbacks end |
Instance Method Details
#by_model ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/modelscope/stats.rb', line 25 def by_model @by_model ||= callbacks.group_by { |cb| cb.model.name }.sort_by do |name, callbacks| if sort_by == :size callbacks.size elsif sort_by == :name name end end.tap do |sorted| sorted.reverse! if sort_order == :desc end.to_h end |
#stats_for(model_name) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/modelscope/stats.rb', line 37 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
46 47 48 49 50 51 52 53 54 |
# File 'lib/modelscope/stats.rb', line 46 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 |