Class: SupplejackApi::InteractionUpdaters::AllUsageMetric

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

Constant Summary collapse

BLACKLISTED_FIELDS =
[:_id, :created_at, :updated_at, :date, :record_field_value].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAllUsageMetric

Returns a new instance of AllUsageMetric.


8
9
10
11
12
# File 'app/models/supplejack_api/interaction_updaters/all_usage_metric.rb', line 8

def initialize
  # Slightly nonstandard model, so mock ActiveRecord interactions
  # that the InteractionMetricsWorker performs
  @model = OpenStruct.new(all: OpenStruct.new(delete_all: true))
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model


6
7
8
# File 'app/models/supplejack_api/interaction_updaters/all_usage_metric.rb', line 6

def model
  @model
end

Instance Method Details

#processObject


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/supplejack_api/interaction_updaters/all_usage_metric.rb', line 14

def process(*)
  usage_metrics = SupplejackApi::UsageMetrics
                  .where(:record_field_value.ne => 'all')
                  .created_on(Time.zone.today)
  fields_to_update = SupplejackApi::UsageMetrics
                     .fields
                     .keys
                     .map(&:to_sym)
                     .reject { |field| BLACKLISTED_FIELDS.include? field }
  summed_usage_metrics_fields = usage_metrics.reduce({}) do |acc, el|
    fields_to_update.each_with_object(acc) do |field, accumulator|
      accumulator[field] = accumulator[field].to_i + el[field]
    end
  end
  all_metric = SupplejackApi::UsageMetrics.find_or_create_by(
    record_field_value: 'all',
    date: Time.zone.today
  ) do |m|
    m.record_field_value = 'all'
    m.date = Time.zone.today
  end

  all_metric.update(summed_usage_metrics_fields)
  true
end