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
|
# File 'app/jobs/rails_local_analytics/record_request_job.rb', line 4
def perform(json)
if json.is_a?(String)
json = JSON.parse(json)
end
request_hash = json.fetch("request_hash")
custom_attributes_by_model = json.fetch("custom_attributes")
MODELS.each do |model|
custom_attrs = custom_attributes_by_model && custom_attributes_by_model[model.name]
attrs = build_attrs(model, custom_attrs, request_hash)
attrs["day"] = json.fetch("day")
existing_record = model.find_by(attrs)
if existing_record
existing_record.increment!(:total, 1)
else
model.create!(attrs)
end
end
end
|