Class: SupplejackApi::InteractionUpdaters::SetMetrics

Inherits:
Object
  • Object
show all
Defined in:
app/models/supplejack_api/interaction_updaters/set_metrics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSetMetrics

Returns a new instance of SetMetrics.



7
8
9
# File 'app/models/supplejack_api/interaction_updaters/set_metrics.rb', line 7

def initialize
  @model = SupplejackApi::InteractionModels::Set
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'app/models/supplejack_api/interaction_updaters/set_metrics.rb', line 5

def model
  @model
end

Instance Method Details

#process(set_interactions) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/supplejack_api/interaction_updaters/set_metrics.rb', line 11

def process(set_interactions)
  unique_facets = set_interactions.map(&:facet).uniq

  unique_facets.each do |facet|
    metric = SupplejackApi::UsageMetrics.find_or_create_by(
      date: Date.current,
      record_field_value: facet
    ) do |um|
      um.date = Date.current
      um.record_field_value = facet
    end

    records_added = set_interactions.count { |x| x.facet == facet }
    metric.records_added_to_user_sets += records_added
    metric.save!
  end

  true
end