Class: RailsLocalAnalytics::RecordRequestJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/rails_local_analytics/record_request_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(json) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/jobs/rails_local_analytics/record_request_job.rb', line 3

def perform(json)
  if json.is_a?(String)
    json = JSON.parse(json)
  end

  request_hash = json.fetch("request_hash")

  custom_attributes_by_type = json.fetch("custom_attributes")

  ["site", "page"].each do |type|
    case type
    when "site"
      klass = TrackedRequestsByDaySite
    when "page"
      klass = TrackedRequestsByDayPage
    end

    custom_attrs = custom_attributes_by_type && custom_attributes_by_type[type]

    attrs = build_attrs(klass, custom_attrs, request_hash)

    attrs["day"] = json.fetch("day")

    existing_record = klass.find_by(attrs)

    if existing_record
      existing_record.increment!(:total, 1)
    else
      klass.create!(attrs)
    end
  end
end