Module: Workarea::Metrics::ByDay::ClassMethods

Defined in:
app/models/workarea/metrics/by_day.rb

Instance Method Summary collapse

Instance Method Details

#inc(key: {}, at: Time.current, set: {}, **values) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/workarea/metrics/by_day.rb', line 23

def inc(key: {}, at: Time.current, set: {}, **values)
  key = key.transform_values(&:to_s)
  current_id = "#{at.strftime('%Y%m%d')}-#{key.values.join('-')}".remove(/-$/)
  values = values.transform_values do |value|
    if value.is_a?(Money)
      value.exchange_to(Money.default_currency).to_f
    else
      value
    end
  end

  updates = {
    '$inc' => values,
    '$setOnInsert' => { reporting_on: at.beginning_of_day.utc }
  }

  updates.merge!('$set' => set) if set.present?
  collection.update_one(key.merge(_id: current_id), updates, upsert: true)
end

#todayObject



43
44
45
46
47
48
# File 'app/models/workarea/metrics/by_day.rb', line 43

def today
  by_date_range(
    starts_at: Time.current.beginning_of_day,
    ends_at: Time.current.end_of_day
  ).find_or_initialize_by
end

#yesterdayObject



50
51
52
53
54
55
# File 'app/models/workarea/metrics/by_day.rb', line 50

def yesterday
  by_date_range(
    starts_at: 1.day.ago.beginning_of_day,
    ends_at: 1.day.ago.end_of_day
  ).find_or_initialize_by
end