I have database record which is like this
[#<Event:0x00007fbe7372de48
@timezone="Europe/London">,
@data={:foo=>"bar", :larry=>"lobster"},
@id="first_event",
@name="page_viewed",
@occurred_at=Thu, 18 Feb 2021 21:16:57 UTC +00:00>,
#<Event:0x00007fbe7372de48
@timezone="Europe/London">,
@data={:foo=>"bar", :larry=>"test2"},
@id="second_event",
@name="page_viewed",
@occurred_at=Thu, 18 Feb 2021 21:17:57 UTC +00:00>,
#<Event:0x00007fbe7372f2e8
@timezone="Europe/London">,
@data={:foo=>"bar", :larry=>"lobster"},
@id="second_event",
@name="rfq_created",
@occurred_at=Thu, 18 Feb 2021 21:19:57 UTC +00:00>
#<Event:0x00007fbe7372f2e8
@timezone="Europe/London">,
@data={:foo=>"bar", :larry=>"lobster"},
@id="second_event",
@name="rfq_created",
@occurred_at=Thu, 18 Feb 2021 22:18:57 UTC +00:00>...]
Now i want fetch list of records when event name(@name) is page_viewed
, started
and selected
i want to fetch only first event occurred record and if event name(@name) is rfq_created
and purchased
i want all the records of event name rfq_created
and purchased
Here is what i have done :
EVENTS = { "unique_kpi" => { "page_viewed" => 'UVisit',
"start" => 'PVisit',
"selected" => 'SVisit'
},
"non_unique_kpi"=> {"rfq_created" => 'RfqCreated',
"purchased" => 'Purchased'
}
}
def key_performance_indicators
key_indicators = events.each_with_object({}) do |event, hash|
if EVENTS["non_unique_kpi"].keys.include?(event.name)
hash[event.name] = [] unless hash.has_key?(event.name)
hash[event.name] << event
else
hash[event.name] = event unless hash.has_key?(event.name)
end
key_indicators.map do |key, value|
kpi_class = indicator_class(key)
"{kpi_class}".constantize.new(event: value)
end
end
def indicator_class(kpi_key)
EVENTS.find { |_k, hash| return hash[kpi_key] if hash.keys.include?(kpi_key) }
end
I want to refactor this in good way is there any other approach we can do this ? Please help.
Aucun commentaire:
Enregistrer un commentaire